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 25 matching lines...) Expand all Loading... | |
36 snippet(...args); | 36 snippet(...args); |
37 | 37 |
38 // 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> |
39 // 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 |
40 // execution to be complete. | 40 // execution to be complete. |
41 await timeout(100); | 41 await timeout(100); |
42 } | 42 } |
43 | 43 |
44 exports.testAbortOnPropertyReadSnippet = async function(test) | 44 exports.testAbortOnPropertyReadSnippet = async function(test) |
45 { | 45 { |
46 window.abpTest = "foo"; | 46 function testProperty(property, result = true, errorName = "ReferenceError") |
Manish Jethani
2019/03/20 01:42:00
This seems odd standing out on the top. Let's move
hub
2019/03/20 15:10:07
Done.
| |
47 | |
48 function testProperty(property, result = true, errorType = "ReferenceError") | |
49 { | 47 { |
50 let path = property.split("."); | 48 let path = property.split("."); |
51 | 49 |
52 let exceptionCaught = false; | 50 let exceptionCaught = false; |
53 let value = 1; | 51 let value = 1; |
54 | 52 |
55 try | 53 try |
56 { | 54 { |
57 let obj = window; | 55 let obj = window; |
58 while (path.length > 1) | 56 while (path.length > 1) |
59 obj = obj[path.shift()]; | 57 obj = obj[path.shift()]; |
60 value = obj[path.shift()]; | 58 value = obj[path.shift()]; |
61 } | 59 } |
62 catch (e) | 60 catch (e) |
63 { | 61 { |
64 test.equal(e.name, errorType); | 62 test.equal(e.name, errorName); |
Manish Jethani
2019/03/20 01:42:01
Nit: Since we're comparing with the `name` propert
hub
2019/03/20 15:10:05
Done.
| |
65 exceptionCaught = true; | 63 exceptionCaught = true; |
66 } | 64 } |
67 | 65 |
68 test.equal( | 66 test.equal( |
69 exceptionCaught, | 67 exceptionCaught, |
70 result, | 68 result, |
71 `The property "${property}" ${result ? "should" : "shouldn't"} trigger an exception.` | 69 `The property "${property}" ${result ? "should" : "shouldn't"} trigger an exception.` |
72 ); | 70 ); |
73 test.equal( | 71 test.equal( |
74 value, | 72 value, |
75 result ? 1 : undefined, | 73 result ? 1 : undefined, |
76 `The value for "${property}" ${result ? "shouldn't" : "should"} have been read.` | 74 `The value for "${property}" ${result ? "shouldn't" : "should"} have been read.` |
77 ); | 75 ); |
78 } | 76 } |
79 | 77 |
78 window.abpTest = "fortytwo"; | |
80 await runSnippet(test, "abort-on-property-read", "abpTest"); | 79 await runSnippet(test, "abort-on-property-read", "abpTest"); |
81 testProperty("abpTest"); | 80 testProperty("abpTest"); |
82 | 81 |
83 window.abpTest2 = {prop1: "foo"}; | 82 window.abpTest2 = {prop1: "fortytwo"}; |
84 | |
Manish Jethani
2019/03/20 01:41:59
Nit: Blank line here seems unnecessary if we are g
hub
2019/03/20 15:10:06
Done.
| |
85 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); | 83 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); |
86 testProperty("abpTest2.prop1"); | 84 testProperty("abpTest2.prop1"); |
87 | 85 |
88 // 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. |
Manish Jethani
2019/03/20 01:41:59
Nit: s/don't/doesn't/
hub
2019/03/20 15:10:06
Done.
| |
89 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); | 87 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); |
90 window.abpTest3 = {prop1: "foo"}; | 88 window.abpTest3 = {prop1: "fortytwo"}; |
91 testProperty("abpTest3.prop1"); | 89 testProperty("abpTest3.prop1"); |
92 | 90 |
93 // Test that other properties don't trigger. | 91 // Test that other properties don't trigger. |
94 testProperty("abpTest3.prop2", false); | 92 testProperty("abpTest3.prop2", false); |
95 | 93 |
96 // Test overwriting the object with another object | 94 // Test overwriting the object with another object. |
Manish Jethani
2019/03/20 01:42:01
Nit: Period at the end (I don't mind too much but
hub
2019/03/20 15:10:06
Done.
| |
97 window.foo = {bar: {}}; | 95 window.abpTest4 = {prop3: {}}; |
Manish Jethani
2019/03/20 01:41:59
There are some inconsistencies with the naming her
hub
2019/03/20 15:10:05
Done.
| |
98 await runSnippet(test, "abort-on-property-read", "foo.bar.lambda"); | 96 await runSnippet(test, "abort-on-property-read", "abpTest4.prop3.foo"); |
99 testProperty("foo.bar.lambda"); | 97 testProperty("abpTest4.prop3.foo"); |
100 window.foo.bar = {}; | 98 window.abpTest4.prop3 = {}; |
101 testProperty("foo.bar.lambda"); | 99 testProperty("abpTest4.prop3.foo"); |
102 | 100 |
103 // Test if we start with a non-object | 101 // Test if we start with a non-object. |
104 window.foo2 = 5; | 102 window.abpTest5 = 42; |
105 await runSnippet(test, "abort-on-property-read", "foo2.bar2.lambda"); | 103 await runSnippet(test, "abort-on-property-read", "abpTest5.prop4.bar"); |
106 | 104 |
107 // We expect that accessing the property cause a TypeError. | 105 testProperty("abpTest5.prop4.bar", true, "TypeError"); |
Manish Jethani
2019/03/20 01:42:00
Nit: s/cause/causes/
(FWIW I'm not sure this comm
hub
2019/03/20 15:10:06
Deleted.
| |
108 testProperty("foo2.bar2.lambda", true, "TypeError"); | |
109 | 106 |
110 window.foo2 = {bar2: {}}; | 107 window.abpTest5 = {prop4: 42}; |
Manish Jethani
2019/03/20 01:42:00
Before this line, we could throw in another test:
hub
2019/03/20 15:10:07
Done.
| |
111 testProperty("foo2.bar2.lambda"); | 108 testProperty("abpTest5.prop4.bar", false); |
109 window.abpTest5 = {prop4: {}}; | |
110 testProperty("abpTest5.prop4.bar"); | |
112 | 111 |
113 test.done(); | 112 test.done(); |
114 }; | 113 }; |
LEFT | RIGHT |