blob: 845996795dab42dd05ecf15d114e5d9dd1385c39 [file] [log] [blame]
ortunoe1fe71772016-10-20 01:54:361# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4"""Generator script for Web Bluetooth LayoutTests.
5
6For each script-tests/X.js creates the following test files depending on the
7contents of X.js
8- getPrimaryService/X.html
9- getPrimaryServices/X.html
10- getPrimaryServices/X-with-uuid.html
11
12script-tests/X.js files should contain "CALLS([variation1 | variation2 | ...])"
13tokens that indicate what files to generate. Each variation in CALLS([...])
14should corresponds to a js function call and its arguments. Additionally a
15variation can end in [UUID] to indicate that the generated file's name should
16have the -with-uuid suffix.
17
18The PREVIOUS_CALL token will be replaced with the function that replaced CALLS.
19
20The FUNCTION_NAME token will be replaced with the name of the function that
21replaced CALLS.
22
23For example, for the following template file:
24
25// script-tests/example.js
26promise_test(() => {
dougt36c5c1c2016-12-03 21:09:0127 return navigator.bluetooth.requestDevice(...)
28 .then(device => device.gatt.CALLS([
29 getPrimaryService('heart_rate')|
30 getPrimaryServices('heart_rate')[UUID]]))
31 .then(device => device.gatt.PREVIOUS_CALL);
ortunoe1fe71772016-10-20 01:54:3632}, 'example test for FUNCTION_NAME');
33
34this script will generate:
35
36// getPrimaryService/example.html
37promise_test(() => {
dougt36c5c1c2016-12-03 21:09:0138 return navigator.bluetooth.requestDevice(...)
39 .then(device => device.gatt.getPrimaryService('heart_rate'))
40 .then(device => device.gatt.getPrimaryService('heart_rate'));
ortunoe1fe71772016-10-20 01:54:3641}, 'example test for getPrimaryService');
42
43// getPrimaryServices/example-with-uuid.html
44promise_test(() => {
dougt36c5c1c2016-12-03 21:09:0145 return navigator.bluetooth.requestDevice(...)
46 .then(device => device.gatt.getPrimaryServices('heart_rate'))
47 .then(device => device.gatt.getPrimaryServices('heart_rate'));
ortunoe1fe71772016-10-20 01:54:3648}, 'example test for getPrimaryServices');
49
50Run
51$ python //third_party/WebKit/LayoutTests/bluetooth/generate.py
52and commit the generated files.
53"""
Giovanni Ortuño Urquidi72c95d62017-05-25 18:41:0154
55import fnmatch
ortunoe1fe71772016-10-20 01:54:3656import os
57import re
58import sys
59
60TEMPLATES_DIR = 'script-tests'
61
62
63class GeneratedTest:
64
dougt36c5c1c2016-12-03 21:09:0165 def __init__(self, data, path, template):
66 self.data = data
67 self.path = path
68 self.template = template
ortunoe1fe71772016-10-20 01:54:3669
70
71def GetGeneratedTests():
dougt36c5c1c2016-12-03 21:09:0172 """Yields a GeneratedTest for each call in templates in script-tests."""
73 bluetooth_tests_dir = os.path.dirname(os.path.realpath(__file__))
ortunoe1fe71772016-10-20 01:54:3674
dougt36c5c1c2016-12-03 21:09:0175 # Read Base Test Template.
76 base_template_file_handle = open(
77 os.path.join(bluetooth_tests_dir, TEMPLATES_DIR, 'base_test_template.html'))
78 base_template_file_data = base_template_file_handle.read().decode('utf-8')
79 base_template_file_handle.close()
ortunoe1fe71772016-10-20 01:54:3680
dougt36c5c1c2016-12-03 21:09:0181 # Get Templates.
ortunoe1fe71772016-10-20 01:54:3682
dougt36c5c1c2016-12-03 21:09:0183 template_path = os.path.join(bluetooth_tests_dir, TEMPLATES_DIR)
ortunoe1fe71772016-10-20 01:54:3684
dougt36c5c1c2016-12-03 21:09:0185 available_templates = []
86 for root, _, files in os.walk(template_path):
87 for template in files:
88 if template.endswith('.js'):
89 available_templates.append(os.path.join(root, template))
ortunoe1fe71772016-10-20 01:54:3690
dougt36c5c1c2016-12-03 21:09:0191 # Generate Test Files
92 for template in available_templates:
93 # Read template
94 template_file_handle = open(template)
95 template_file_data = template_file_handle.read().decode('utf-8')
96 template_file_handle.close()
ortunoe1fe71772016-10-20 01:54:3697
dougt36c5c1c2016-12-03 21:09:0198 template_name = os.path.splitext(os.path.basename(template))[0]
ortunoe1fe71772016-10-20 01:54:3699
scheib1e43caf2017-01-28 03:13:57100 # Find function names in multiline pattern: CALLS( [ function_name,function_name2[UUID] ])
101 result = re.search(
102 r'CALLS\(' + # CALLS(
103 r'[^\[]*' + # Any characters not [, allowing for new lines.
104 r'\[' + # [
105 r'(.*?)' + # group matching: function_name(), function_name2[UUID]
106 r'\]\)', # adjacent closing characters: ])
107 template_file_data, re.MULTILINE | re.DOTALL)
ortunoe1fe71772016-10-20 01:54:36108
dougt36c5c1c2016-12-03 21:09:01109 if result is None:
110 raise Exception('Template must contain \'CALLS\' tokens')
ortunoe1fe71772016-10-20 01:54:36111
dougt36c5c1c2016-12-03 21:09:01112 new_test_file_data = base_template_file_data.replace('TEST',
113 template_file_data)
114 # Replace CALLS([...]) with CALLS so that we don't have to replace the
115 # CALLS([...]) for every new test file.
116 new_test_file_data = new_test_file_data.replace(result.group(), 'CALLS')
ortunoe1fe71772016-10-20 01:54:36117
dougt36c5c1c2016-12-03 21:09:01118 # Replace 'PREVIOUS_CALL' with 'CALLS' so that we can replace it while
119 # replacing CALLS.
120 new_test_file_data = new_test_file_data.replace('PREVIOUS_CALL', 'CALLS')
ortunoe1fe71772016-10-20 01:54:36121
Conley Owensab253b02017-08-01 20:26:17122 for call in result.group(1).split('|'):
dougt36c5c1c2016-12-03 21:09:01123 # Parse call
Conley Owensab253b02017-08-01 20:26:17124 call = call.strip()
125 function_name, args, uuid_suffix = re.search(r'(.*?)\((.*)\)(\[UUID\])?', call).groups()
ortunoe1fe71772016-10-20 01:54:36126
dougt36c5c1c2016-12-03 21:09:01127 # Replace template tokens
128 call_test_file_data = new_test_file_data
129 call_test_file_data = call_test_file_data.replace('CALLS', '{}({})'.format(function_name, args))
130 call_test_file_data = call_test_file_data.replace('FUNCTION_NAME', function_name)
ortunoe1fe71772016-10-20 01:54:36131
dougt36c5c1c2016-12-03 21:09:01132 # Get test file name
133 group_dir = os.path.basename(os.path.abspath(os.path.join(template, os.pardir)))
ortunoe1fe71772016-10-20 01:54:36134
dougt36c5c1c2016-12-03 21:09:01135 call_test_file_name = 'gen-{}{}.html'.format(template_name, '-with-uuid' if uuid_suffix else '')
136 call_test_file_path = os.path.join(bluetooth_tests_dir, group_dir, function_name, call_test_file_name)
137
138 yield GeneratedTest(call_test_file_data, call_test_file_path, template)
ortunoe1fe71772016-10-20 01:54:36139
ortunoe1fe71772016-10-20 01:54:36140def main():
Giovanni Ortuño Urquidi72c95d62017-05-25 18:41:01141 previous_generated_files = set()
142 current_path = os.path.dirname(os.path.realpath(__file__))
143 for root, _, filenames in os.walk(current_path):
144 for filename in fnmatch.filter(filenames, 'gen-*.html'):
145 previous_generated_files.add(os.path.join(root, filename))
ortunoe1fe71772016-10-20 01:54:36146
Giovanni Ortuño Urquidi72c95d62017-05-25 18:41:01147 generated_files = set()
dougt36c5c1c2016-12-03 21:09:01148 for generated_test in GetGeneratedTests():
Giovanni Ortuño Urquidi72c95d62017-05-25 18:41:01149 prev_len = len(generated_files)
150 generated_files.add(generated_test.path)
151 if prev_len == len(generated_files):
152 print 'Generated the same test twice for template:\n{}'.format(
153 generated_test.template)
154
dougt36c5c1c2016-12-03 21:09:01155 # Create or open test file
156 test_file_handle = open(generated_test.path, 'wb')
ortunoe1fe71772016-10-20 01:54:36157
dougt36c5c1c2016-12-03 21:09:01158 # Write contents
159 test_file_handle.write(generated_test.data.encode('utf-8'))
160 test_file_handle.close()
ortunoe1fe71772016-10-20 01:54:36161
Giovanni Ortuño Urquidi72c95d62017-05-25 18:41:01162 new_generated_files = generated_files - previous_generated_files
163 if len(new_generated_files) != 0:
164 print 'Newly generated tests:'
165 for generated_file in new_generated_files:
166 print generated_file
167
168 obsolete_files = previous_generated_files - generated_files
169 if len(obsolete_files) != 0:
170 print 'The following files might be obsolete:'
171 for generated_file in obsolete_files:
172 print generated_file
173
174
ortunoe1fe71772016-10-20 01:54:36175
176if __name__ == '__main__':
dougt36c5c1c2016-12-03 21:09:01177 sys.exit(main())