Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/browser/snippets.js

Issue 30025555: Issue 6820 - Move tests to mocha (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Rebased. Created April 5, 2019, 4:16 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/browser/elemHideEmulation.js ('k') | test/caching.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 /* global assert */
21
20 const library = require("../../lib/content/snippets.js"); 22 const library = require("../../lib/content/snippets.js");
21 const {timeout} = require("./_utils"); 23 const {timeout} = require("./_utils");
22 24
23 // We need this stub for the injector. 25 describe("Snippets", () =>
24 window.browser = { 26 {
25 runtime: { 27 before(() =>
26 getURL: () => "" 28 {
29 // We need this stub for the injector.
30 window.browser = {
31 runtime: {
32 getURL: () => ""
33 }
34 };
35 });
36
37 async function runSnippet(snippetName, ...args)
38 {
39 let snippet = library[snippetName];
40
41 assert.ok(snippet);
42
43 snippet(...args);
44
45 // For snippets that run in the context of the document via a <script>
46 // element (i.e. snippets that use makeInjector()), we need to wait for
47 // execution to be complete.
48 await timeout(100);
27 } 49 }
28 };
29 50
30 async function runSnippet(test, snippetName, ...args)
31 {
32 let snippet = library[snippetName];
33
34 test.ok(snippet);
35
36 snippet(...args);
37
38 // For snippets that run in the context of the document via a <script>
39 // element (i.e. snippets that use makeInjector()), we need to wait for
40 // execution to be complete.
41 await timeout(100);
42 }
43
44 exports.testAbortOnPropertyReadSnippet = async function(test)
45 {
46 function testProperty(property, result = true, errorName = "ReferenceError") 51 function testProperty(property, result = true, errorName = "ReferenceError")
47 { 52 {
48 let path = property.split("."); 53 let path = property.split(".");
49 54
50 let exceptionCaught = false; 55 let exceptionCaught = false;
51 let value = 1; 56 let value = 1;
52 57
53 try 58 try
54 { 59 {
55 let obj = window; 60 let obj = window;
56 while (path.length > 1) 61 while (path.length > 1)
57 obj = obj[path.shift()]; 62 obj = obj[path.shift()];
58 value = obj[path.shift()]; 63 value = obj[path.shift()];
59 } 64 }
60 catch (e) 65 catch (e)
61 { 66 {
62 test.equal(e.name, errorName); 67 assert.equal(e.name, errorName);
63 exceptionCaught = true; 68 exceptionCaught = true;
64 } 69 }
65 70
66 test.equal( 71 assert.equal(
67 exceptionCaught, 72 exceptionCaught,
68 result, 73 result,
69 `The property "${property}" ${result ? "should" : "shouldn't"} trigger an exception.` 74 `The property "${property}" ${result ? "should" : "shouldn't"} trigger an exception.`
70 ); 75 );
71 test.equal( 76 assert.equal(
72 value, 77 value,
73 result ? 1 : undefined, 78 result ? 1 : undefined,
74 `The value for "${property}" ${result ? "shouldn't" : "should"} have been read.` 79 `The value for "${property}" ${result ? "shouldn't" : "should"} have been read.`
75 ); 80 );
76 } 81 }
77 82
78 window.abpTest = "fortytwo"; 83 it("Test abort property read", async() =>
79 await runSnippet(test, "abort-on-property-read", "abpTest"); 84 {
80 testProperty("abpTest"); 85 window.abpTest = "fortytwo";
86 await runSnippet("abort-on-property-read", "abpTest");
87 testProperty("abpTest");
81 88
82 window.abpTest2 = {prop1: "fortytwo"}; 89 window.abpTest2 = {prop1: "fortytwo"};
83 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); 90 await runSnippet("abort-on-property-read", "abpTest2.prop1");
84 testProperty("abpTest2.prop1"); 91 testProperty("abpTest2.prop1");
85 92
86 // Test that we try to catch a property that doesn't exist yet. 93 // Test that we try to catch a property that doesn't exist yet.
87 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); 94 await runSnippet("abort-on-property-read", "abpTest3.prop1");
88 window.abpTest3 = {prop1: "fortytwo"}; 95 window.abpTest3 = {prop1: "fortytwo"};
89 testProperty("abpTest3.prop1"); 96 testProperty("abpTest3.prop1");
90 97
91 // Test that other properties don't trigger. 98 // Test that other properties don't trigger.
92 testProperty("abpTest3.prop2", false); 99 testProperty("abpTest3.prop2", false);
93 100
94 // Test overwriting the object with another object. 101 // Test overwriting the object with another object.
95 window.abpTest4 = {prop3: {}}; 102 window.abpTest4 = {prop3: {}};
96 await runSnippet(test, "abort-on-property-read", "abpTest4.prop3.foo"); 103 await runSnippet("abort-on-property-read", "abpTest4.prop3.foo");
97 testProperty("abpTest4.prop3.foo"); 104 testProperty("abpTest4.prop3.foo");
98 window.abpTest4.prop3 = {}; 105 window.abpTest4.prop3 = {};
99 testProperty("abpTest4.prop3.foo"); 106 testProperty("abpTest4.prop3.foo");
100 107
101 // Test if we start with a non-object. 108 // Test if we start with a non-object.
102 window.abpTest5 = 42; 109 window.abpTest5 = 42;
103 await runSnippet(test, "abort-on-property-read", "abpTest5.prop4.bar"); 110 await runSnippet("abort-on-property-read", "abpTest5.prop4.bar");
104 111
105 testProperty("abpTest5.prop4.bar", true, "TypeError"); 112 testProperty("abpTest5.prop4.bar", true, "TypeError");
106 113
107 window.abpTest5 = {prop4: 42}; 114 window.abpTest5 = {prop4: 42};
108 testProperty("abpTest5.prop4.bar", false); 115 testProperty("abpTest5.prop4.bar", false);
109 window.abpTest5 = {prop4: {}}; 116 window.abpTest5 = {prop4: {}};
110 testProperty("abpTest5.prop4.bar"); 117 testProperty("abpTest5.prop4.bar");
111 118
112 // Check that it works on properties that are functions. 119 // Check that it works on properties that are functions.
113 // https://issues.adblockplus.org/ticket/7419 120 // https://issues.adblockplus.org/ticket/7419
114 121
115 // Existing function (from the API). 122 // Existing function (from the API).
116 await runSnippet(test, "abort-on-property-read", "Object.keys"); 123 await runSnippet("abort-on-property-read", "Object.keys");
117 testProperty("Object.keys"); 124 testProperty("Object.keys");
118 125
119 // Function properties. 126 // Function properties.
120 window.abpTest6 = function() {}; 127 window.abpTest6 = function() {};
121 window.abpTest6.prop1 = function() {}; 128 window.abpTest6.prop1 = function() {};
122 await runSnippet(test, "abort-on-property-read", "abpTest6.prop1"); 129 await runSnippet("abort-on-property-read", "abpTest6.prop1");
123 testProperty("abpTest6.prop1"); 130 testProperty("abpTest6.prop1");
124 131
125 // Function properties, with sub-property set afterwards. 132 // Function properties, with sub-property set afterwards.
126 window.abpTest7 = function() {}; 133 window.abpTest7 = function() {};
127 await runSnippet(test, "abort-on-property-read", "abpTest7.prop1"); 134 await runSnippet("abort-on-property-read", "abpTest7.prop1");
128 window.abpTest7.prop1 = function() {}; 135 window.abpTest7.prop1 = function() {};
129 testProperty("abpTest7.prop1"); 136 testProperty("abpTest7.prop1");
130 137
131 // Function properties, with base property as function set afterwards. 138 // Function properties, with base property as function set afterwards.
132 await runSnippet(test, "abort-on-property-read", "abpTest8.prop1"); 139 await runSnippet("abort-on-property-read", "abpTest8.prop1");
133 window.abpTest8 = function() {}; 140 window.abpTest8 = function() {};
134 window.abpTest8.prop1 = function() {}; 141 window.abpTest8.prop1 = function() {};
135 testProperty("abpTest8.prop1"); 142 testProperty("abpTest8.prop1");
136 143
137 // Arrow function properties. 144 // Arrow function properties.
138 window.abpTest9 = () => {}; 145 window.abpTest9 = () => {};
139 await runSnippet(test, "abort-on-property-read", "abpTest9"); 146 await runSnippet("abort-on-property-read", "abpTest9");
140 testProperty("abpTest9"); 147 testProperty("abpTest9");
141 148
142 // Class function properties. 149 // Class function properties.
143 window.abpTest10 = class {}; 150 window.abpTest10 = class {};
144 await runSnippet(test, "abort-on-property-read", "abpTest10"); 151 await runSnippet("abort-on-property-read", "abpTest10");
145 testProperty("abpTest10"); 152 testProperty("abpTest10");
146 153
147 // Class function properties with prototype function properties. 154 // Class function properties with prototype function properties.
148 window.abpTest11 = class {}; 155 window.abpTest11 = class {};
149 window.abpTest11.prototype.prop1 = function() {}; 156 window.abpTest11.prototype.prop1 = function() {};
150 await runSnippet(test, "abort-on-property-read", "abpTest11.prototype.prop1"); 157 await runSnippet("abort-on-property-read", "abpTest11.prototype.prop1");
151 testProperty("abpTest11.prototype.prop1"); 158 testProperty("abpTest11.prototype.prop1");
152 159
153 // Class function properties with prototype function properties, with 160 // Class function properties with prototype function properties, with
154 // prototype property set afterwards. 161 // prototype property set afterwards.
155 window.abpTest12 = class {}; 162 window.abpTest12 = class {};
156 await runSnippet(test, "abort-on-property-read", "abpTest12.prototype.prop1"); 163 await runSnippet("abort-on-property-read", "abpTest12.prototype.prop1");
157 window.abpTest12.prototype.prop1 = function() {}; 164 window.abpTest12.prototype.prop1 = function() {};
158 testProperty("abpTest12.prototype.prop1"); 165 testProperty("abpTest12.prototype.prop1");
159 166 });
160 test.done(); 167 });
161 };
OLDNEW
« no previous file with comments | « test/browser/elemHideEmulation.js ('k') | test/caching.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld