Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(797)

Side by Side Diff: third_party/WebKit/LayoutTests/bluetooth/generate.py

Issue 2544513004: Fix WebBluetooth generator to distinguish between different methods. (Closed)
Patch Set: Remove TL;DR in README Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2016 The Chromium Authors. All rights reserved. 1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 """Generator script for Web Bluetooth LayoutTests. 4 """Generator script for Web Bluetooth LayoutTests.
5 5
6 For each script-tests/X.js creates the following test files depending on the 6 For each script-tests/X.js creates the following test files depending on the
7 contents of X.js 7 contents of X.js
8 - getPrimaryService/X.html 8 - getPrimaryService/X.html
9 - getPrimaryServices/X.html 9 - getPrimaryServices/X.html
10 - getPrimaryServices/X-with-uuid.html 10 - getPrimaryServices/X-with-uuid.html
11 11
12 script-tests/X.js files should contain "CALLS([variation1 | variation2 | ...])" 12 script-tests/X.js files should contain "CALLS([variation1 | variation2 | ...])"
13 tokens that indicate what files to generate. Each variation in CALLS([...]) 13 tokens that indicate what files to generate. Each variation in CALLS([...])
14 should corresponds to a js function call and its arguments. Additionally a 14 should corresponds to a js function call and its arguments. Additionally a
15 variation can end in [UUID] to indicate that the generated file's name should 15 variation can end in [UUID] to indicate that the generated file's name should
16 have the -with-uuid suffix. 16 have the -with-uuid suffix.
17 17
18 The PREVIOUS_CALL token will be replaced with the function that replaced CALLS. 18 The PREVIOUS_CALL token will be replaced with the function that replaced CALLS.
19 19
20 The FUNCTION_NAME token will be replaced with the name of the function that 20 The FUNCTION_NAME token will be replaced with the name of the function that
21 replaced CALLS. 21 replaced CALLS.
22 22
23 For example, for the following template file: 23 For example, for the following template file:
24 24
25 // script-tests/example.js 25 // script-tests/example.js
26 promise_test(() => { 26 promise_test(() => {
27 return navigator.bluetooth.requestDevice(...) 27 return navigator.bluetooth.requestDevice(...)
28 .then(device => device.gatt.CALLS([ 28 .then(device => device.gatt.CALLS([
29 getPrimaryService('heart_rate')| 29 getPrimaryService('heart_rate')|
30 getPrimaryServices('heart_rate')[UUID]])) 30 getPrimaryServices('heart_rate')[UUID]]))
31 .then(device => device.gatt.PREVIOUS_CALL); 31 .then(device => device.gatt.PREVIOUS_CALL);
32 }, 'example test for FUNCTION_NAME'); 32 }, 'example test for FUNCTION_NAME');
33 33
34 this script will generate: 34 this script will generate:
35 35
36 // getPrimaryService/example.html 36 // getPrimaryService/example.html
37 promise_test(() => { 37 promise_test(() => {
38 return navigator.bluetooth.requestDevice(...) 38 return navigator.bluetooth.requestDevice(...)
39 .then(device => device.gatt.getPrimaryService('heart_rate')) 39 .then(device => device.gatt.getPrimaryService('heart_rate'))
40 .then(device => device.gatt.getPrimaryService('heart_rate')); 40 .then(device => device.gatt.getPrimaryService('heart_rate'));
41 }, 'example test for getPrimaryService'); 41 }, 'example test for getPrimaryService');
42 42
43 // getPrimaryServices/example-with-uuid.html 43 // getPrimaryServices/example-with-uuid.html
44 promise_test(() => { 44 promise_test(() => {
45 return navigator.bluetooth.requestDevice(...) 45 return navigator.bluetooth.requestDevice(...)
46 .then(device => device.gatt.getPrimaryServices('heart_rate')) 46 .then(device => device.gatt.getPrimaryServices('heart_rate'))
47 .then(device => device.gatt.getPrimaryServices('heart_rate')); 47 .then(device => device.gatt.getPrimaryServices('heart_rate'));
48 }, 'example test for getPrimaryServices'); 48 }, 'example test for getPrimaryServices');
49 49
50 Run 50 Run
51 $ python //third_party/WebKit/LayoutTests/bluetooth/generate.py 51 $ python //third_party/WebKit/LayoutTests/bluetooth/generate.py
52 and commit the generated files. 52 and commit the generated files.
53 """ 53 """
54 import glob
55 import os 54 import os
56 import re 55 import re
57 import sys 56 import sys
58 57
59 TEMPLATES_DIR = 'script-tests' 58 TEMPLATES_DIR = 'script-tests'
60 59
61 60
62 class GeneratedTest: 61 class GeneratedTest:
63 62
64 def __init__(self, data, path, template): 63 def __init__(self, data, path, template):
65 self.data = data 64 self.data = data
66 self.path = path 65 self.path = path
67 self.template = template 66 self.template = template
68 67
69 68
70 def GetGeneratedTests(): 69 def GetGeneratedTests():
71 """Yields a GeneratedTest for each call in templates in script-tests.""" 70 """Yields a GeneratedTest for each call in templates in script-tests."""
72 current_path = os.path.dirname(os.path.realpath(__file__)) 71 bluetooth_tests_dir = os.path.dirname(os.path.realpath(__file__))
73 72
74 # Read Base Test Template. 73 # Read Base Test Template.
75 base_template_file_handle = open( 74 base_template_file_handle = open(
76 os.path.join(current_path, TEMPLATES_DIR, 'base_test_template.html')) 75 os.path.join(bluetooth_tests_dir, TEMPLATES_DIR, 'base_test_template.htm l'))
77 base_template_file_data = base_template_file_handle.read().decode('utf-8') 76 base_template_file_data = base_template_file_handle.read().decode('utf-8')
78 base_template_file_handle.close() 77 base_template_file_handle.close()
79 78
80 # Get Templates. 79 # Get Templates.
81 available_templates = glob.glob(
82 os.path.join(current_path, TEMPLATES_DIR, '*.js'))
83 80
84 # Generate Test Files 81 template_path = os.path.join(bluetooth_tests_dir, TEMPLATES_DIR)
85 for template in available_templates:
86 # Read template
87 template_file_handle = open(template)
88 template_file_data = template_file_handle.read().decode('utf-8')
89 template_file_handle.close()
90 82
91 template_name = os.path.splitext(os.path.basename(template))[0] 83 available_templates = []
84 for root, _, files in os.walk(template_path):
85 for template in files:
86 if template.endswith('.js'):
87 available_templates.append(os.path.join(root, template))
92 88
93 result = re.search(r'CALLS\(\[(.*?)\]\)', template_file_data, re.MULTILINE 89 # Generate Test Files
94 | re.DOTALL) 90 for template in available_templates:
91 # Read template
92 template_file_handle = open(template)
93 template_file_data = template_file_handle.read().decode('utf-8')
94 template_file_handle.close()
95 95
96 if result is None: 96 template_name = os.path.splitext(os.path.basename(template))[0]
97 raise Exception('Template must contain \'CALLS\' tokens')
98 97
99 new_test_file_data = base_template_file_data.replace('TEST', 98 result = re.search(r'CALLS\(\[(.*?)\]\)', template_file_data, re.MULTILI NE
100 template_file_data) 99 | re.DOTALL)
101 # Replace CALLS([...]) with CALLS so that we don't have to replace the
102 # CALLS([...]) for every new test file.
103 new_test_file_data = new_test_file_data.replace(result.group(), 'CALLS')
104 100
105 # Replace 'PREVIOUS_CALL' with 'CALLS' so that we can replace it while 101 if result is None:
106 # replacing CALLS. 102 raise Exception('Template must contain \'CALLS\' tokens')
107 new_test_file_data = new_test_file_data.replace('PREVIOUS_CALL', 'CALLS')
108 103
109 calls = result.group(1) 104 new_test_file_data = base_template_file_data.replace('TEST',
110 calls = ''.join(calls.split()) # Removes whitespace. 105 template_file_data)
111 calls = calls.split('|') 106 # Replace CALLS([...]) with CALLS so that we don't have to replace the
107 # CALLS([...]) for every new test file.
108 new_test_file_data = new_test_file_data.replace(result.group(), 'CALLS')
112 109
113 for call in calls: 110 # Replace 'PREVIOUS_CALL' with 'CALLS' so that we can replace it while
114 # Parse call 111 # replacing CALLS.
115 name, args, uuid_suffix = re.search(r'(.*?)\((.*?)\)(\[UUID\])?', 112 new_test_file_data = new_test_file_data.replace('PREVIOUS_CALL', 'CALLS' )
116 call).groups()
117 113
118 # Replace template tokens 114 calls = result.group(1)
119 call_test_file_data = new_test_file_data 115 calls = ''.join(calls.split()) # Removes whitespace.
116 calls = calls.split('|')
120 117
121 call_test_file_data = call_test_file_data.replace( 118 for call in calls:
122 'CALLS', '{}({})'.format(name, args)) 119 # Parse call
120 function_name, args, uuid_suffix = re.search(r'(.*?)\((.*?)\)(\[UUID \])?', call).groups()
123 121
124 call_test_file_data = call_test_file_data.replace('FUNCTION_NAME', name) 122 # Replace template tokens
123 call_test_file_data = new_test_file_data
124 call_test_file_data = call_test_file_data.replace('CALLS', '{}({})'. format(function_name, args))
125 call_test_file_data = call_test_file_data.replace('FUNCTION_NAME', f unction_name)
125 126
126 # Get test file name 127 # Get test file name
127 call_test_file_name = 'gen-{}{}.html'.format(template_name, '-with-uuid' 128 group_dir = os.path.basename(os.path.abspath(os.path.join(template, os.pardir)))
128 if uuid_suffix else '')
129 call_test_file_path = os.path.join(current_path, name,
130 call_test_file_name)
131 129
132 yield GeneratedTest(call_test_file_data, call_test_file_path, template) 130 call_test_file_name = 'gen-{}{}.html'.format(template_name, '-with-u uid' if uuid_suffix else '')
131 call_test_file_path = os.path.join(bluetooth_tests_dir, group_dir, f unction_name, call_test_file_name)
132
133 yield GeneratedTest(call_test_file_data, call_test_file_path, templa te)
133 134
134 135
135 def main(): 136 def main():
136 137
137 for generated_test in GetGeneratedTests(): 138 for generated_test in GetGeneratedTests():
138 # Create or open test file 139 # Create or open test file
139 test_file_handle = open(generated_test.path, 'wb') 140 test_file_handle = open(generated_test.path, 'wb')
140 141
141 # Write contents 142 # Write contents
142 test_file_handle.write(generated_test.data.encode('utf-8')) 143 test_file_handle.write(generated_test.data.encode('utf-8'))
143 test_file_handle.close() 144 test_file_handle.close()
144 145
145 146
146 if __name__ == '__main__': 147 if __name__ == '__main__':
147 sys.exit(main()) 148 sys.exit(main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698