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

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: Remove other -expected.txt files Created 3 years, 10 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:
iclelland 2017/02/24 18:38:42 Should check_interfaces also test that the propert
chasej 2017/02/24 20:04:07 I intentionally simplified to just check for inter
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 use_global = false;
42 var result = []; 49 let interface_object;
43 function collect_property_info(object, property_name, output) { 50 if (interface_name === 'global') {
44 var descriptor = 51 use_global = true;
45 Object.getOwnPropertyDescriptor(object, property_name); 52 interface_object = global_object;
46 if ('value' in descriptor) { 53 } else {
47 if (typeof descriptor.value === 'function') { 54 interface_object = global_object[interface_name];
48 output.push(' method ' + property_name); 55 }
49 } else { 56 assert_true(interface_object !== undefined, 'Interface ' + interface_nam e + ' not found on provided object');
iclelland 2017/02/24 18:38:42 This message seems wrong -- shouldn't it just tell
chasej 2017/02/24 20:04:07 The |global_object| could be anything, as far as t
50 output.push(' attribute ' + property_name); 57 if (interface_object === undefined) {
iclelland 2017/02/24 18:38:42 Is this test necessary, if assert_true will throw
chasej 2017/02/24 20:04:07 No, removed.
51 } 58 return;
52 } else { 59 }
53 if (descriptor.get) { 60 let interface_prototype = use_global ? interface_object : interface_obje ct.prototype;
iclelland 2017/02/24 18:38:42 Why not just have the foreach loop above define in
chasej 2017/02/24 20:04:07 I had separate steps to allow the assert on the in
54 output.push(' getter ' + property_name); 61 property_filters[interface_name].forEach(function(property_name) {
55 } 62 if (should_exist) {
56 if (descriptor.set) { 63 assert_own_property(interface_prototype, property_name, interface_na me);
57 output.push(' setter ' + property_name); 64 } else {
58 } 65 assert_false(interface_prototype.hasOwnProperty(property_name),
iclelland 2017/02/24 18:38:42 You can use assert_not_exists for this, I think.
chasej 2017/02/24 20:04:07 The documentation for testharness.js says that ass
iclelland 2017/02/24 20:23:08 I didn't see anything to that effect in the testha
59 } 66 'Property ' + property_name + ' exists on ' + interface_name);
60 } 67 }
61 interface_names.sort(); 68 });
62 interface_names.forEach(function(interface_name) { 69 });
63 var use_global = false; 70 },
64 var interface_object;
65 if (interface_name === 'global') {
66 use_global = true;
67 interface_object = global_object;
68 } else {
69 interface_object = global_object[interface_name];
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 71
101 check_interfaces_in_sw: 72 check_properties: (global_object, property_filters) => {
102 (t, script, scope) => { 73 OriginTrialsHelper.check_properties_impl(global_object, property_filters, true);
103 var worker; 74 },
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 75
127 add_token: (token_string) => { 76 check_properties_missing: (global_object, property_filters) => {
128 var tokenElement = document.createElement('meta'); 77 OriginTrialsHelper.check_properties_impl(global_object, property_filters, false);
129 tokenElement.httpEquiv = 'origin-trial'; 78 },
130 tokenElement.content = token_string; 79
131 document.head.appendChild(tokenElement); 80 check_interfaces_impl: (global_object, interface_names, should_exist) => {
132 } 81 interface_names.sort();
82 interface_names.forEach(function(interface_name) {
83 assert_equals(global_object[interface_name] !== undefined, should_exist,
84 'Interface ' + interface_name + ' exists on provided object');
iclelland 2017/02/24 18:38:42 When should_exist is true (and the interface doesn
chasej 2017/02/24 20:04:07 I think makes sense. The end result in the output
iclelland 2017/02/24 20:23:08 Ahh, I forgot about the "Expected X Got Y" -- that
85 });
86 },
87
88 check_interfaces: (global_object, interface_names) => {
89 OriginTrialsHelper.check_interfaces_impl(global_object, interface_names, t rue);
90 },
91
92 check_interfaces_missing: (global_object, interface_names) => {
93 OriginTrialsHelper.check_interfaces_impl(global_object, interface_names, f alse);
94 },
95
96 add_token: (token_string) => {
97 var tokenElement = document.createElement('meta');
98 tokenElement.httpEquiv = 'origin-trial';
99 tokenElement.content = token_string;
100 document.head.appendChild(tokenElement);
101 }
133 } 102 }
134 })(); 103 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698