| OLD | NEW |
| (Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 """Generator script for Web Bluetooth LayoutTests. |
| 5 |
| 6 For each script-tests/X.js creates the following test files depending on the |
| 7 contents of X.js |
| 8 - getPrimaryService/X.html |
| 9 - getPrimaryServices/X.html |
| 10 - getPrimaryServices/X-with-uuid.html |
| 11 |
| 12 script-tests/X.js files should contain "CALLS([variation1 | variation2 | ...])" |
| 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 |
| 15 variation can end in [UUID] to indicate that the generated file's name should |
| 16 have the -with-uuid suffix. |
| 17 |
| 18 The PREVIOUS_CALL token will be replaced with the function that replaced CALLS. |
| 19 |
| 20 The FUNCTION_NAME token will be replaced with the name of the function that |
| 21 replaced CALLS. |
| 22 |
| 23 For example, for the following template file: |
| 24 |
| 25 // script-tests/example.js |
| 26 promise_test(() => { |
| 27 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); |
| 32 }, 'example test for FUNCTION_NAME'); |
| 33 |
| 34 this script will generate: |
| 35 |
| 36 // getPrimaryService/example.html |
| 37 promise_test(() => { |
| 38 return navigator.bluetooth.requestDevice(...) |
| 39 .then(device => device.gatt.getPrimaryService('heart_rate')) |
| 40 .then(device => device.gatt.getPrimaryService('heart_rate')); |
| 41 }, 'example test for getPrimaryService'); |
| 42 |
| 43 // getPrimaryServices/example-with-uuid.html |
| 44 promise_test(() => { |
| 45 return navigator.bluetooth.requestDevice(...) |
| 46 .then(device => device.gatt.getPrimaryServices('heart_rate')) |
| 47 .then(device => device.gatt.getPrimaryServices('heart_rate')); |
| 48 }, 'example test for getPrimaryServices'); |
| 49 |
| 50 Run |
| 51 $ python //third_party/WebKit/LayoutTests/bluetooth/generate.py |
| 52 and commit the generated files. |
| 53 """ |
| 54 import glob |
| 55 import os |
| 56 import re |
| 57 import sys |
| 58 |
| 59 TEMPLATES_DIR = 'script-tests' |
| 60 |
| 61 |
| 62 class GeneratedTest: |
| 63 |
| 64 def __init__(self, data, path, template): |
| 65 self.data = data |
| 66 self.path = path |
| 67 self.template = template |
| 68 |
| 69 |
| 70 def GetGeneratedTests(): |
| 71 """Yields a GeneratedTest for each call in templates in script-tests.""" |
| 72 current_path = os.path.dirname(os.path.realpath(__file__)) |
| 73 |
| 74 # Read Base Test Template. |
| 75 base_template_file_handle = open( |
| 76 os.path.join(current_path, TEMPLATES_DIR, 'base_test_template.html')) |
| 77 base_template_file_data = base_template_file_handle.read().decode('utf-8') |
| 78 base_template_file_handle.close() |
| 79 |
| 80 # Get Templates. |
| 81 available_templates = glob.glob( |
| 82 os.path.join(current_path, TEMPLATES_DIR, '*.js')) |
| 83 |
| 84 # Generate Test Files |
| 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 |
| 91 template_name = os.path.splitext(os.path.basename(template))[0] |
| 92 |
| 93 result = re.search(r'CALLS\(\[(.*?)\]\)', template_file_data, re.MULTILINE |
| 94 | re.DOTALL) |
| 95 |
| 96 if result is None: |
| 97 raise Exception('Template must contain \'CALLS\' tokens') |
| 98 |
| 99 new_test_file_data = base_template_file_data.replace('TEST', |
| 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 |
| 105 # Replace 'PREVIOUS_CALL' with 'CALLS' so that we can replace it while |
| 106 # replacing CALLS. |
| 107 new_test_file_data = new_test_file_data.replace('PREVIOUS_CALL', 'CALLS') |
| 108 |
| 109 calls = result.group(1) |
| 110 calls = ''.join(calls.split()) # Removes whitespace. |
| 111 calls = calls.split('|') |
| 112 |
| 113 for call in calls: |
| 114 # Parse call |
| 115 name, args, uuid_suffix = re.search(r'(.*?)\((.*?)\)(\[UUID\])?', |
| 116 call).groups() |
| 117 |
| 118 # Replace template tokens |
| 119 call_test_file_data = new_test_file_data |
| 120 |
| 121 call_test_file_data = call_test_file_data.replace( |
| 122 'CALLS', '{}({})'.format(name, args)) |
| 123 |
| 124 call_test_file_data = call_test_file_data.replace('FUNCTION_NAME', name) |
| 125 |
| 126 # Get test file name |
| 127 call_test_file_name = 'gen-{}{}.html'.format(template_name, '-with-uuid' |
| 128 if uuid_suffix else '') |
| 129 call_test_file_path = os.path.join(current_path, name, |
| 130 call_test_file_name) |
| 131 |
| 132 yield GeneratedTest(call_test_file_data, call_test_file_path, template) |
| 133 |
| 134 |
| 135 def main(): |
| 136 |
| 137 for generated_test in GetGeneratedTests(): |
| 138 # Create or open test file |
| 139 test_file_handle = open(generated_test.path, 'wb') |
| 140 |
| 141 # Write contents |
| 142 test_file_handle.write(generated_test.data.encode('utf-8')) |
| 143 test_file_handle.close() |
| 144 |
| 145 |
| 146 if __name__ == '__main__': |
| 147 sys.exit(main()) |
| OLD | NEW |