Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 |
(...skipping 16 matching lines...) Expand all Loading... | |
27 } | 27 } |
28 }; | 28 }; |
29 | 29 |
30 async function runSnippet(test, snippetName, ...args) | 30 async function runSnippet(test, snippetName, ...args) |
31 { | 31 { |
32 let snippet = library[snippetName]; | 32 let snippet = library[snippetName]; |
33 | 33 |
34 test.ok(snippet); | 34 test.ok(snippet); |
35 | 35 |
36 snippet(...args); | 36 snippet(...args); |
37 | |
37 // For snippets that run in the context of the document via a <script> | 38 // For snippets that run in the context of the document via a <script> |
Manish Jethani
2019/03/19 07:44:00
Nit: Mind leaving a line before the comment? It's
hub
2019/03/19 17:25:11
Done.
| |
38 // element (i.e. snippets that use makeInjector()), we need to wait for | 39 // element (i.e. snippets that use makeInjector()), we need to wait for |
39 // execution to be complete. | 40 // execution to be complete. |
40 await timeout(100); | 41 await timeout(100); |
41 } | 42 } |
42 | 43 |
43 exports.testAbortOnPropertyReadSnippet = async function(test) | 44 exports.testAbortOnPropertyReadSnippet = async function(test) |
44 { | 45 { |
45 window.abpTest = "foo"; | 46 function testProperty(property, result = true, errorName = "ReferenceError") |
46 | |
47 function testProperty(property, result = true) | |
48 { | 47 { |
49 let path = property.split("."); | 48 let path = property.split("."); |
50 | 49 |
51 let exceptionCaught = false; | 50 let exceptionCaught = false; |
52 let value = 1; | 51 let value = 1; |
53 | 52 |
54 try | 53 try |
55 { | 54 { |
56 let obj = window; | 55 let obj = window; |
57 while (path.length > 1) | 56 while (path.length > 1) |
58 obj = obj[path.shift()]; | 57 obj = obj[path.shift()]; |
59 value = obj[path.shift()]; | 58 value = obj[path.shift()]; |
60 } | 59 } |
61 catch (e) | 60 catch (e) |
62 { | 61 { |
62 test.equal(e.name, errorName); | |
63 exceptionCaught = true; | 63 exceptionCaught = true; |
64 } | 64 } |
65 | 65 |
66 test.equal(exceptionCaught, result, | 66 test.equal( |
67 `The property "${property}" ${result ? "should" : "shouldn't"} trigger an exception.`); | 67 exceptionCaught, |
Manish Jethani
2019/03/19 07:44:00
The opening tick of the template string should be
hub
2019/03/19 17:25:11
Done.
| |
68 test.equal(value, result ? 1 : undefined, | 68 result, |
69 `The value for "${property}" ${result ? "shouldn't" : "should"} have been read.`); | 69 `The property "${property}" ${result ? "should" : "shouldn't"} trigger an exception.` |
70 ); | |
71 test.equal( | |
72 value, | |
73 result ? 1 : undefined, | |
74 `The value for "${property}" ${result ? "shouldn't" : "should"} have been read.` | |
75 ); | |
70 } | 76 } |
71 | 77 |
78 window.abpTest = "fortytwo"; | |
72 await runSnippet(test, "abort-on-property-read", "abpTest"); | 79 await runSnippet(test, "abort-on-property-read", "abpTest"); |
73 testProperty("abpTest"); | 80 testProperty("abpTest"); |
74 | 81 |
75 window.abpTest2 = {prop1: "foo"}; | 82 window.abpTest2 = {prop1: "fortytwo"}; |
76 | |
77 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); | 83 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); |
78 testProperty("abpTest2.prop1"); | 84 testProperty("abpTest2.prop1"); |
79 | 85 |
80 // Test that we try to catch a property that don't exist yet. | 86 // Test that we try to catch a property that doesn't exist yet. |
81 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); | 87 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); |
82 window.abpTest3 = {prop1: "foo"}; | 88 window.abpTest3 = {prop1: "fortytwo"}; |
83 testProperty("abpTest3.prop1"); | 89 testProperty("abpTest3.prop1"); |
84 | 90 |
85 // Test that other properties don't trigger. | 91 // Test that other properties don't trigger. |
86 testProperty("abpTest3.prop2", false); | 92 testProperty("abpTest3.prop2", false); |
87 | 93 |
88 // Test overwriting the object with another object | 94 // Test overwriting the object with another object. |
89 window.foo = {bar: {}}; | 95 window.abpTest4 = {prop3: {}}; |
90 await runSnippet(test, "abort-on-property-read", "foo.bar.lambda"); | 96 await runSnippet(test, "abort-on-property-read", "abpTest4.prop3.foo"); |
91 testProperty("foo.bar.lambda"); | 97 testProperty("abpTest4.prop3.foo"); |
92 window.foo.bar = {}; | 98 window.abpTest4.prop3 = {}; |
93 testProperty("foo.bar.lambda"); | 99 testProperty("abpTest4.prop3.foo"); |
94 | 100 |
95 // Test if we start with a non-object | 101 // Test if we start with a non-object. |
96 window.foo2 = 5; | 102 window.abpTest5 = 42; |
97 await runSnippet(test, "abort-on-property-read", "foo2.bar2.lambda"); | 103 await runSnippet(test, "abort-on-property-read", "abpTest5.prop4.bar"); |
98 testProperty("foo2.bar2.lambda"); | 104 |
99 window.foo2 = {}; | 105 testProperty("abpTest5.prop4.bar", true, "TypeError"); |
100 testProperty("foo2.bar2.lambda"); | 106 |
107 window.abpTest5 = {prop4: 42}; | |
108 testProperty("abpTest5.prop4.bar", false); | |
109 window.abpTest5 = {prop4: {}}; | |
110 testProperty("abpTest5.prop4.bar"); | |
101 | 111 |
102 test.done(); | 112 test.done(); |
103 }; | 113 }; |
LEFT | RIGHT |