OLD | NEW |
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 | 23 |
22 // We need this stub for the injector. | 24 describe("Snippets", () => |
23 window.browser = | |
24 { | 25 { |
25 runtime: | 26 before(() => |
26 { | 27 { |
27 getURL: () => "" | 28 // We need this stub for the injector. |
| 29 window.browser = |
| 30 { |
| 31 runtime: |
| 32 { |
| 33 getURL: () => "" |
| 34 } |
| 35 }; |
| 36 }); |
| 37 |
| 38 let testDocument = null; |
| 39 |
| 40 beforeEach(() => |
| 41 { |
| 42 let iframe = document.createElement("iframe"); |
| 43 document.body.appendChild(iframe); |
| 44 testDocument = iframe.contentDocument; |
| 45 }); |
| 46 |
| 47 afterEach(() => |
| 48 { |
| 49 let iframe = testDocument.defaultView.frameElement; |
| 50 iframe.parentNode.removeChild(iframe); |
| 51 testDocument = null; |
| 52 }); |
| 53 |
| 54 function timeout(delay) |
| 55 { |
| 56 return new Promise((resolve, reject) => |
| 57 { |
| 58 window.setTimeout(resolve, delay); |
| 59 }); |
28 } | 60 } |
29 }; | |
30 | 61 |
31 let testDocument = null; | 62 async function runSnippet(snippetName, ...args) |
| 63 { |
| 64 let snippet = library[snippetName]; |
32 | 65 |
33 exports.setUp = function(callback) | 66 assert.ok(snippet); |
34 { | |
35 let iframe = document.createElement("iframe"); | |
36 document.body.appendChild(iframe); | |
37 testDocument = iframe.contentDocument; | |
38 | 67 |
39 callback(); | 68 snippet(...args); |
40 }; | 69 await timeout(100); |
41 | 70 } |
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) => | |
54 { | |
55 window.setTimeout(resolve, delay); | |
56 }); | |
57 } | |
58 | |
59 async function runSnippet(test, snippetName, ...args) | |
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 | 71 |
73 async function testProperty(property, result = true) | 72 async function testProperty(property, result = true) |
74 { | 73 { |
75 let properties = property.split("."); | 74 let properties = property.split("."); |
76 test.ok(properties.length >= 1); | 75 assert.ok(properties.length >= 1); |
77 | 76 |
78 let exceptionCaught = false; | 77 let exceptionCaught = false; |
79 let value = 1; | 78 let value = 1; |
80 try | 79 try |
81 { | 80 { |
82 let obj = window; | 81 let obj = window; |
83 while (properties.length > 1) | 82 while (properties.length > 1) |
84 obj = obj[properties.shift()]; | 83 obj = obj[properties.shift()]; |
85 value = obj[properties.shift()]; | 84 value = obj[properties.shift()]; |
86 } | 85 } |
87 catch (e) | 86 catch (e) |
88 { | 87 { |
89 if (properties.length == 0) | 88 if (properties.length == 0) |
90 exceptionCaught = true; | 89 exceptionCaught = true; |
91 } | 90 } |
92 test.equal(exceptionCaught, result, `The property "${property}" didn't trigg
er and exception.`); | 91 assert.equal(exceptionCaught, result, `The property "${property}" didn't tri
gger and exception.`); |
93 test.equal(value, result ? 1 : undefined, `The value for "${property}" shoul
dn't have been read.`); | 92 assert.equal(value, result ? 1 : undefined, `The value for "${property}" sho
uldn't have been read.`); |
94 } | 93 } |
95 | 94 |
96 await runSnippet(test, "abort-on-property-read", "abpTest"); | 95 it("Test abort property read", async() => |
97 await testProperty("abpTest"); | 96 { |
| 97 window.abpTest = "foo"; |
98 | 98 |
99 window.abpTest2 = {prop1: "foo"}; | 99 await runSnippet("abort-on-property-read", "abpTest"); |
| 100 await testProperty("abpTest"); |
100 | 101 |
101 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); | 102 window.abpTest2 = {prop1: "foo"}; |
102 await testProperty("abpTest2.prop1"); | |
103 | 103 |
104 // Test that we try to catch a property that don't exist yet. | 104 await runSnippet("abort-on-property-read", "abpTest2.prop1"); |
105 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); | 105 await testProperty("abpTest2.prop1"); |
106 window.abpTest3 = {prop1: "foo"}; | |
107 await testProperty("abpTest3.prop1"); | |
108 | 106 |
109 // Test that other properties don't trigger. | 107 // Test that we try to catch a property that don't exist yet. |
110 await testProperty("abpTest3.prop2", false); | 108 await runSnippet("abort-on-property-read", "abpTest3.prop1"); |
| 109 window.abpTest3 = {prop1: "foo"}; |
| 110 await testProperty("abpTest3.prop1"); |
111 | 111 |
112 // Test overwriting the object with another object | 112 // Test that other properties don't trigger. |
113 window.foo = {bar: {}}; | 113 await testProperty("abpTest3.prop2", false); |
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 | 114 |
119 // Test if we start with a non-object | 115 // Test overwriting the object with another object |
120 window.foo2 = 5; | 116 window.foo = {bar: {}}; |
121 await runSnippet(test, "abort-on-property-read", "foo2.bar2.lambda"); | 117 await runSnippet("abort-on-property-read", "foo.bar.lambda"); |
122 await testProperty("foo2.bar2.lambda"); | 118 await testProperty("foo.bar.lambda"); |
123 window.foo2 = {}; | 119 window.foo.bar = {}; |
124 await testProperty("foo2.bar2.lambda"); | 120 await testProperty("foo.bar.lambda"); |
125 | 121 |
126 test.done(); | 122 // Test if we start with a non-object |
127 }; | 123 window.foo2 = 5; |
128 | 124 await runSnippet("abort-on-property-read", "foo2.bar2.lambda"); |
| 125 await testProperty("foo2.bar2.lambda"); |
| 126 window.foo2 = {}; |
| 127 await testProperty("foo2.bar2.lambda"); |
| 128 }); |
| 129 }); |
OLD | NEW |