Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-present eyeo GmbH | |
4 * | |
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 | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
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/>. | |
16 */ | |
17 | |
18 "use strict"; | |
19 | |
20 const library = require("../../lib/content/snippets.js"); | |
21 | |
22 // We need this stub for the injector. | |
23 window.browser = | |
Manish Jethani
2019/03/12 07:00:09
Nit: The normal style for object literals we use i
hub
2019/03/12 14:29:17
Done.
Manish Jethani
2019/03/12 14:48:49
The same would apply to the nested `runtime` prope
hub
2019/03/12 15:15:11
I wonder why the linter is silent.
Done
| |
24 { | |
25 runtime: | |
26 { | |
27 getURL: () => "" | |
28 } | |
29 }; | |
30 | |
31 let testDocument = null; | |
32 | |
33 exports.setUp = function(callback) | |
34 { | |
35 let iframe = document.createElement("iframe"); | |
36 document.body.appendChild(iframe); | |
37 testDocument = iframe.contentDocument; | |
Manish Jethani
2019/03/12 07:00:09
Where is `testDocument` being used in this file?
hub
2019/03/12 14:29:18
Used Line 44. Declared line 31.
This is the exact
Manish Jethani
2019/03/12 14:48:49
Yes, but over there we have this line:
elemHi
hub
2019/03/12 15:15:11
On second thought, we don't need this iframe eithe
| |
38 | |
39 callback(); | |
40 }; | |
41 | |
42 exports.tearDown = function(callback) | |
43 { | |
44 let iframe = testDocument.defaultView.frameElement; | |
45 iframe.parentNode.removeChild(iframe); | |
46 testDocument = null; | |
47 | |
48 callback(); | |
49 }; | |
50 | |
51 function timeout(delay) | |
52 { | |
53 return new Promise((resolve, reject) => | |
Manish Jethani
2019/03/12 07:00:09
We don't need `reject` here, and we could also los
hub
2019/03/12 14:29:17
tbh this is a cut&paste from `test/browser/element
Manish Jethani
2019/03/12 14:48:49
Acknowledged.
| |
54 { | |
55 window.setTimeout(resolve, delay); | |
Manish Jethani
2019/03/12 07:00:09
`setTimeout()` is a global function also available
hub
2019/03/12 14:29:17
This is always run in the browser context. But see
Manish Jethani
2019/03/12 14:48:49
Acknowledged.
| |
56 }); | |
57 } | |
58 | |
59 async function runSnippet(test, snippetName, ...args) | |
Manish Jethani
2019/03/12 07:00:09
We could rename this to `runScript(test, script)`
hub
2019/03/12 14:29:18
The goal was to individually test the snippet, sim
| |
60 { | |
61 let snippet = library[snippetName]; | |
62 | |
63 test.ok(snippet); | |
64 | |
65 snippet(...args); | |
66 await timeout(100); | |
67 } | |
68 | |
69 exports.testAbortPropertyReadSnippet = async function(test) | |
70 { | |
71 window.abpTest = "foo"; | |
72 | |
73 async function testProperty(property, result = true) | |
74 { | |
75 let properties = property.split("."); | |
76 test.ok(properties.length >= 1); | |
77 | |
78 let exceptionCaught = false; | |
79 let value = 1; | |
80 try | |
81 { | |
82 let obj = window; | |
83 while (properties.length > 1) | |
84 obj = obj[properties.shift()]; | |
85 value = obj[properties.shift()]; | |
86 } | |
87 catch (e) | |
88 { | |
89 if (properties.length == 0) | |
90 exceptionCaught = true; | |
91 } | |
92 test.equal(exceptionCaught, result, `The property "${property}" didn't trigg er and exception.`); | |
93 test.equal(value, result ? 1 : undefined, `The value for "${property}" shoul dn't have been read.`); | |
94 } | |
95 | |
96 await runSnippet(test, "abort-on-property-read", "abpTest"); | |
97 await testProperty("abpTest"); | |
98 | |
99 window.abpTest2 = {prop1: "foo"}; | |
100 | |
101 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); | |
102 await testProperty("abpTest2.prop1"); | |
103 | |
104 // Test that we try to catch a property that don't exist yet. | |
105 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); | |
106 window.abpTest3 = {prop1: "foo"}; | |
107 await testProperty("abpTest3.prop1"); | |
108 | |
109 // Test that other properties don't trigger. | |
110 await testProperty("abpTest3.prop2", false); | |
111 | |
112 // Test overwriting the object with another object | |
113 window.foo = {bar: {}}; | |
114 await runSnippet(test, "abort-on-property-read", "foo.bar.lambda"); | |
115 await testProperty("foo.bar.lambda"); | |
116 window.foo.bar = {}; | |
117 await testProperty("foo.bar.lambda"); | |
118 | |
119 // Test if we start with a non-object | |
120 window.foo2 = 5; | |
121 await runSnippet(test, "abort-on-property-read", "foo2.bar2.lambda"); | |
122 await testProperty("foo2.bar2.lambda"); | |
123 window.foo2 = {}; | |
124 await testProperty("foo2.bar2.lambda"); | |
125 | |
126 test.done(); | |
127 }; | |
128 | |
OLD | NEW |