OLD | NEW |
---|---|
1 // This file provides an OriginTrialsHelper object which can be used by | 1 // This file provides an OriginTrialsHelper object which can be used by |
2 // LayoutTests that are checking members exposed to script by origin trials. | 2 // LayoutTests that are checking members exposed to script by origin trials. |
3 // | 3 // |
4 // The current available methods are: | 4 // The current available methods are: |
5 // get_interface_names: | 5 // check_properties: |
6 // Report on the existence of the given interface names on the global object, | 6 // Tests for the existence of the given property names, on the given interface |
7 // listing all the properties found for each interface. The properties can be | 7 // names, on the global object. As well, it can test for properties of the |
8 // filtered by providing a list of desired property names. As well, it can | 8 // global object itself, by giving 'global' as the interface name. |
9 // report on properties of the global object itself, by giving 'global' as one | |
10 // of the interface names. | |
11 // Example: | 9 // Example: |
12 // OriginTrialsHelper.get_interface_names( | 10 // OriginTrialsHelper.check_properties( |
13 // this, | 11 // this, |
14 // ['ForeignFetchEvent', 'InstallEvent', 'global'], | 12 // {'InstallEvent':['registerForeignFetch'], 'global':['onforeignfetch']}); |
15 // {'InstallEvent':['registerForeignFetch'], 'global':['onforeignfetch']})); | |
16 // | 13 // |
17 // check_interfaces_in_sw: | 14 // check_properties_missing: |
18 // Collects the results of calling get_interface_names() in a service worker. | 15 // Tests that the given property names do not exist on the global object. That |
19 // Use in a promise test to output the results. | 16 // is, tests for the opposite of check_properties(). |
20 // Example: | 17 // Example: |
21 // promise_test(t => { | 18 // OriginTrialsHelper.check_properties_missing( |
22 // var script = 'path/to/script/calling/get_interface_names()' | 19 // this, |
23 // var scope = 'matching scope' | 20 // {'InstallEvent':['registerForeignFetch'], 'global':['onforeignfetch']}); |
24 // return OriginTrialsHelper.check_interfaces_in_sw(t, script, scope) | 21 // |
25 // .then(message => { | 22 // check_interfaces: |
26 // console.log('Interfaces in Service Worker - origin trial enabled.\n' | 23 // Tests for the existence of the given interface names, on the global object. |
27 // + message); | 24 // Example: |
28 // }); | 25 // OriginTrialsHelper.check_interfaces( |
26 // this, | |
27 // ['USBAlternateInterface', 'USBConfiguration']); | |
28 // | |
29 // check_interfaces_missing: | |
30 // Tests that the given interface names do not exist on the global object. | |
31 // That is, tests for the opposite of check_interfaces(). | |
32 // Example: | |
33 // OriginTrialsHelper.check_interfaces_missing( | |
34 // this, | |
35 // ['USBAlternateInterface', 'USBConfiguration']); | |
29 // | 36 // |
30 // add_token: | 37 // add_token: |
31 // Adds a trial token to the document, to enable a trial via script | 38 // Adds a trial token to the document, to enable a trial via script |
32 // Example: | 39 // Example: |
33 // OriginTrialsHelper.add_token('token produced by generate_token.py'); | 40 // OriginTrialsHelper.add_token('token produced by generate_token.py'); |
34 'use strict'; | 41 'use strict'; |
35 | 42 |
36 var OriginTrialsHelper = (function() { | 43 var OriginTrialsHelper = (function() { |
37 return { | 44 return { |
38 get_interface_names: | 45 check_properties_impl: (global_object, property_filters, should_exist) => { |
39 (global_object, interface_names, | 46 let interface_names = Object.getOwnPropertyNames(property_filters).sort(); |
40 opt_property_filters) => { | 47 interface_names.forEach(function(interface_name) { |
41 var property_filters = opt_property_filters || {}; | 48 let interface_prototype; |
42 var result = []; | 49 if (interface_name === 'global') { |
43 function collect_property_info(object, property_name, output) { | 50 interface_prototype = global_object; |
44 var descriptor = | 51 } else { |
45 Object.getOwnPropertyDescriptor(object, property_name); | 52 let interface_object = global_object[interface_name]; |
46 if ('value' in descriptor) { | 53 if (interface_object) { |
47 if (typeof descriptor.value === 'function') { | 54 interface_prototype = interface_object.prototype; |
48 output.push(' method ' + property_name); | |
49 } else { | |
50 output.push(' attribute ' + property_name); | |
51 } | |
52 } else { | |
53 if (descriptor.get) { | |
54 output.push(' getter ' + property_name); | |
55 } | |
56 if (descriptor.set) { | |
57 output.push(' setter ' + property_name); | |
58 } | |
59 } | |
60 } | 55 } |
61 interface_names.sort(); | 56 } |
62 interface_names.forEach(function(interface_name) { | 57 assert_true(interface_prototype !== undefined, 'Interface ' + interface_ name + ' not found'); |
63 var use_global = false; | 58 property_filters[interface_name].forEach(function(property_name) { |
64 var interface_object; | 59 if (should_exist) { |
65 if (interface_name === 'global') { | 60 assert_own_property(interface_prototype, property_name, interface_na me); |
66 use_global = true; | 61 } else { |
67 interface_object = global_object; | 62 assert_false(interface_prototype.hasOwnProperty(property_name), |
68 } else { | 63 'Property ' + property_name + ' exists on ' + interface_name); |
69 interface_object = global_object[interface_name]; | 64 } |
70 } | 65 }); |
71 if (interface_object === undefined) { | 66 }); |
72 return; | 67 }, |
73 } | |
74 var interface_prototype; | |
75 var display_name; | |
76 if (use_global) { | |
77 interface_prototype = interface_object; | |
78 display_name = 'global object'; | |
79 } else { | |
80 interface_prototype = interface_object.prototype; | |
81 display_name = 'interface ' + interface_name; | |
82 } | |
83 result.push(display_name); | |
84 var property_names = | |
85 Object.getOwnPropertyNames(interface_prototype); | |
86 var match_property_names = property_filters[interface_name]; | |
87 if (match_property_names) { | |
88 property_names = property_names.filter( | |
89 name => { return match_property_names.indexOf(name) >= 0; }); | |
90 } | |
91 var property_strings = []; | |
92 property_names.forEach(function(property_name) { | |
93 collect_property_info( | |
94 interface_prototype, property_name, property_strings); | |
95 }); | |
96 result.push.apply(result, property_strings.sort()); | |
97 }); | |
98 return result.join('\n'); | |
99 }, | |
100 | 68 |
101 check_interfaces_in_sw: | 69 check_properties: (global_object, property_filters) => { |
102 (t, script, scope) => { | 70 OriginTrialsHelper.check_properties_impl(global_object, property_filters, true); |
103 var worker; | 71 }, |
104 var message; | |
105 var registration; | |
106 return service_worker_unregister_and_register(t, script, scope) | |
107 .then(reg => { | |
108 registration = reg; | |
109 worker = registration.installing; | |
110 return wait_for_state(t, worker, 'activated'); | |
111 }) | |
112 .then(_ => { | |
113 var saw_message = new Promise(resolve => { | |
114 navigator.serviceWorker.onmessage = | |
115 e => { resolve(e.data); }; | |
116 }); | |
117 worker.postMessage(''); | |
118 return saw_message; | |
119 }) | |
120 .then(msg => { | |
121 message = msg; | |
122 return registration.unregister(); | |
123 }) | |
124 .then(_ => { return message; }); | |
125 }, | |
126 | 72 |
127 add_token: (token_string) => { | 73 check_properties_missing: (global_object, property_filters) => { |
128 var tokenElement = document.createElement('meta'); | 74 OriginTrialsHelper.check_properties_impl(global_object, property_filters, false); |
129 tokenElement.httpEquiv = 'origin-trial'; | 75 }, |
130 tokenElement.content = token_string; | 76 |
131 document.head.appendChild(tokenElement); | 77 check_interfaces_impl: (global_object, interface_names, should_exist) => { |
132 } | 78 interface_names.sort(); |
79 interface_names.forEach(function(interface_name) { | |
80 assert_equals(global_object[interface_name] !== undefined, should_exist, | |
iclelland
2017/02/24 20:23:08
Is it necessary for this to test obj[name] === und
| |
81 'Interface ' + interface_name + ' exists on provided object'); | |
82 }); | |
83 }, | |
84 | |
85 check_interfaces: (global_object, interface_names) => { | |
86 OriginTrialsHelper.check_interfaces_impl(global_object, interface_names, t rue); | |
87 }, | |
88 | |
89 check_interfaces_missing: (global_object, interface_names) => { | |
90 OriginTrialsHelper.check_interfaces_impl(global_object, interface_names, f alse); | |
91 }, | |
92 | |
93 add_token: (token_string) => { | |
94 var tokenElement = document.createElement('meta'); | |
95 tokenElement.httpEquiv = 'origin-trial'; | |
96 tokenElement.content = token_string; | |
97 document.head.appendChild(tokenElement); | |
98 } | |
133 } | 99 } |
134 })(); | 100 })(); |
OLD | NEW |