Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 current_path = 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(current_path, TEMPLATES_DIR, 'base_test_template.html')) |
| 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(current_path, TEMPLATES_DIR) |
| 85 for template in available_templates: | 82 print template_path |
|
ortuno
2016/12/02 09:57:50
nit: no need to print.
| |
| 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 | 83 |
| 91 template_name = os.path.splitext(os.path.basename(template))[0] | 84 available_templates = [] |
| 85 for root, _, files in os.walk(template_path): | |
| 86 for template in files: | |
| 87 if template.endswith('.js'): | |
| 88 available_templates.append(os.path.join(root, template)) | |
| 92 | 89 |
| 93 result = re.search(r'CALLS\(\[(.*?)\]\)', template_file_data, re.MULTILINE | 90 # Generate Test Files |
| 94 | re.DOTALL) | 91 for template in available_templates: |
| 92 # Read template | |
| 93 template_file_handle = open(template) | |
| 94 template_file_data = template_file_handle.read().decode('utf-8') | |
| 95 template_file_handle.close() | |
| 95 | 96 |
| 96 if result is None: | 97 template_name = os.path.splitext(os.path.basename(template))[0] |
| 97 raise Exception('Template must contain \'CALLS\' tokens') | |
| 98 | 98 |
| 99 new_test_file_data = base_template_file_data.replace('TEST', | 99 result = re.search(r'CALLS\(\[(.*?)\]\)', template_file_data, re.MULTILI NE | re.DOTALL) |
| 100 template_file_data) | |
| 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', template_fi le_data) |
|
ortuno
2016/12/02 09:57:50
nit: break line.
| |
| 110 calls = ''.join(calls.split()) # Removes whitespace. | 105 # Replace CALLS([...]) with CALLS so that we don't have to replace the |
| 111 calls = calls.split('|') | 106 # CALLS([...]) for every new test file. |
| 107 new_test_file_data = new_test_file_data.replace(result.group(), 'CALLS') | |
| 112 | 108 |
| 113 for call in calls: | 109 # Replace 'PREVIOUS_CALL' with 'CALLS' so that we can replace it while |
| 114 # Parse call | 110 # replacing CALLS. |
| 115 name, args, uuid_suffix = re.search(r'(.*?)\((.*?)\)(\[UUID\])?', | 111 new_test_file_data = new_test_file_data.replace('PREVIOUS_CALL', 'CALLS' ) |
| 116 call).groups() | |
| 117 | 112 |
| 118 # Replace template tokens | 113 calls = result.group(1) |
| 119 call_test_file_data = new_test_file_data | 114 calls = ''.join(calls.split()) # Removes whitespace. |
| 115 calls = calls.split('|') | |
| 120 | 116 |
| 121 call_test_file_data = call_test_file_data.replace( | 117 for call in calls: |
| 122 'CALLS', '{}({})'.format(name, args)) | 118 # Parse call |
| 119 name, args, uuid_suffix = re.search(r'(.*?)\((.*?)\)(\[UUID\])?', ca ll).groups() | |
| 123 | 120 |
| 124 call_test_file_data = call_test_file_data.replace('FUNCTION_NAME', name) | 121 # Replace template tokens |
| 122 call_test_file_data = new_test_file_data | |
| 123 call_test_file_data = call_test_file_data.replace('CALLS', '{}({})'. format(name, args)) | |
| 124 call_test_file_data = call_test_file_data.replace('FUNCTION_NAME', n ame) | |
| 125 | 125 |
| 126 # Get test file name | 126 # Get test file name |
| 127 call_test_file_name = 'gen-{}{}.html'.format(template_name, '-with-uuid' | 127 directory = os.path.basename(os.path.abspath(os.path.join(template, os.pardir))) |
|
ortuno
2016/12/02 09:57:50
hmm current_path, directory, name, call_test_file_
| |
| 128 if uuid_suffix else '') | |
| 129 call_test_file_path = os.path.join(current_path, name, | |
| 130 call_test_file_name) | |
| 131 | 128 |
| 132 yield GeneratedTest(call_test_file_data, call_test_file_path, template) | 129 call_test_file_name = 'gen-{}{}.html'.format(template_name, '-with-u uid' if uuid_suffix else '') |
| 130 call_test_file_path = os.path.join(current_path, directory, name, ca ll_test_file_name) | |
| 131 | |
| 132 yield GeneratedTest(call_test_file_data, call_test_file_path, templa te) | |
| 133 | 133 |
| 134 | 134 |
| 135 def main(): | 135 def main(): |
| 136 | 136 |
| 137 for generated_test in GetGeneratedTests(): | 137 for generated_test in GetGeneratedTests(): |
| 138 # Create or open test file | 138 # Create or open test file |
| 139 test_file_handle = open(generated_test.path, 'wb') | 139 test_file_handle = open(generated_test.path, 'wb') |
| 140 | 140 |
| 141 # Write contents | 141 # Write contents |
| 142 test_file_handle.write(generated_test.data.encode('utf-8')) | 142 test_file_handle.write(generated_test.data.encode('utf-8')) |
| 143 test_file_handle.close() | 143 test_file_handle.close() |
| 144 | 144 |
| 145 | 145 |
| 146 if __name__ == '__main__': | 146 if __name__ == '__main__': |
| 147 sys.exit(main()) | 147 sys.exit(main()) |
| OLD | NEW |