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

Side by Side Diff: third_party/WebKit/LayoutTests/http/tests/resources/origin-trials-helper.js

Issue 2712843005: Use asserts in origin trials layout tests (Closed)
Patch Set: Address more comments Created 3 years, 9 months 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 // 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 assert_equals(interface_prototype.hasOwnProperty(property_name),
65 if (interface_name === 'global') { 60 should_exist,
66 use_global = true; 61 'Property ' + property_name + ' exists on ' + interface_name);
67 interface_object = global_object; 62 });
68 } else { 63 });
69 interface_object = global_object[interface_name]; 64 },
70 }
71 if (interface_object === undefined) {
72 return;
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 65
101 check_interfaces_in_sw: 66 check_properties: (global_object, property_filters) => {
102 (t, script, scope) => { 67 OriginTrialsHelper.check_properties_impl(global_object, property_filters, true);
103 var worker; 68 },
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 69
127 add_token: (token_string) => { 70 check_properties_missing: (global_object, property_filters) => {
128 var tokenElement = document.createElement('meta'); 71 OriginTrialsHelper.check_properties_impl(global_object, property_filters, false);
129 tokenElement.httpEquiv = 'origin-trial'; 72 },
130 tokenElement.content = token_string; 73
131 document.head.appendChild(tokenElement); 74 check_interfaces_impl: (global_object, interface_names, should_exist) => {
132 } 75 interface_names.sort();
76 interface_names.forEach(function(interface_name) {
77 assert_equals(global_object.hasOwnProperty(interface_name), should_exist ,
78 'Interface ' + interface_name + ' exists on provided object');
79 });
80 },
81
82 check_interfaces: (global_object, interface_names) => {
83 OriginTrialsHelper.check_interfaces_impl(global_object, interface_names, t rue);
84 },
85
86 check_interfaces_missing: (global_object, interface_names) => {
87 OriginTrialsHelper.check_interfaces_impl(global_object, interface_names, f alse);
88 },
89
90 add_token: (token_string) => {
91 var tokenElement = document.createElement('meta');
92 tokenElement.httpEquiv = 'origin-trial';
93 tokenElement.content = token_string;
94 document.head.appendChild(tokenElement);
95 }
133 } 96 }
134 })(); 97 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698