| Index: test/browser/snippets.js |
| =================================================================== |
| new file mode 100644 |
| --- /dev/null |
| +++ b/test/browser/snippets.js |
| @@ -0,0 +1,99 @@ |
| +/* |
| + * This file is part of Adblock Plus <https://adblockplus.org/>, |
| + * Copyright (C) 2006-present eyeo GmbH |
| + * |
| + * Adblock Plus is free software: you can redistribute it and/or modify |
| + * it under the terms of the GNU General Public License version 3 as |
| + * published by the Free Software Foundation. |
| + * |
| + * Adblock Plus is distributed in the hope that it will be useful, |
| + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| + * GNU General Public License for more details. |
| + * |
| + * You should have received a copy of the GNU General Public License |
| + * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| + */ |
| + |
| +"use strict"; |
| + |
| +const library = require("../../lib/content/snippets.js"); |
| +const {timeout} = require("./_utils"); |
| + |
| +// We need this stub for the injector. |
| +window.browser = { |
| + runtime: { |
| + getURL: () => "" |
| + } |
| +}; |
| + |
| +async function runSnippet(test, snippetName, ...args) |
| +{ |
| + let snippet = library[snippetName]; |
| + |
| + test.ok(snippet); |
| + |
| + snippet(...args); |
| + await timeout(100); |
|
Manish Jethani
2019/03/13 16:36:14
Do we really need this timeout here?
hub
2019/03/14 10:28:05
Without it, the test fail. With a 0 timeout, it fa
Manish Jethani
2019/03/14 11:32:08
Why is this though? Is it because the `<script>` e
hub
2019/03/14 12:32:54
that's what I think.
Manish Jethani
2019/03/16 10:50:24
OK, I checked. It is indeed because the snippet re
hub
2019/03/18 18:27:26
Done.
|
| +} |
| + |
| +exports.testAbortPropertyReadSnippet = async function(test) |
|
Manish Jethani
2019/03/13 16:36:14
Nit: We should maybe name this `testAbortOnPropert
hub
2019/03/14 10:28:05
Done.
|
| +{ |
| + window.abpTest = "foo"; |
| + |
| + async function testProperty(property, result = true) |
|
Manish Jethani
2019/03/13 16:36:14
This function doesn't have to be async from what I
hub
2019/03/14 10:28:05
Done.
|
| + { |
| + let properties = property.split("."); |
|
Manish Jethani
2019/03/13 16:36:15
Nit: We could call this `path` instead of `propert
hub
2019/03/14 10:28:04
Done.
|
| + test.ok(properties.length >= 1); |
|
Manish Jethani
2019/03/13 16:36:15
This seems redundant. It is not possible for this
hub
2019/03/14 10:28:04
Done.
|
| + |
| + let exceptionCaught = false; |
| + let value = 1; |
| + try |
|
Manish Jethani
2019/03/13 16:36:14
Nit: Would be cleaner to read if a line was left b
hub
2019/03/14 10:28:05
Done.
|
| + { |
| + let obj = window; |
| + while (properties.length > 1) |
| + obj = obj[properties.shift()]; |
| + value = obj[properties.shift()]; |
| + } |
| + catch (e) |
| + { |
| + if (properties.length == 0) |
|
Manish Jethani
2019/03/14 10:58:12
Will properties.length every be non-zero here?
|
| + exceptionCaught = true; |
| + } |
| + test.equal(exceptionCaught, result, `The property "${property}" didn't trigger and exception.`); |
|
Manish Jethani
2019/03/13 16:36:15
The message here doesn't make complete sense. What
hub
2019/03/14 10:28:04
Done.
|
| + test.equal(value, result ? 1 : undefined, `The value for "${property}" shouldn't have been read.`); |
|
Manish Jethani
2019/03/13 16:36:15
Same as above. Both of these cases assume `result`
hub
2019/03/14 10:28:05
Done.
|
| + } |
| + |
| + await runSnippet(test, "abort-on-property-read", "abpTest"); |
| + await testProperty("abpTest"); |
| + |
| + window.abpTest2 = {prop1: "foo"}; |
| + |
| + await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); |
| + await testProperty("abpTest2.prop1"); |
| + |
| + // Test that we try to catch a property that don't exist yet. |
| + await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); |
| + window.abpTest3 = {prop1: "foo"}; |
| + await testProperty("abpTest3.prop1"); |
| + |
| + // Test that other properties don't trigger. |
| + await testProperty("abpTest3.prop2", false); |
| + |
| + // Test overwriting the object with another object |
| + window.foo = {bar: {}}; |
| + await runSnippet(test, "abort-on-property-read", "foo.bar.lambda"); |
| + await testProperty("foo.bar.lambda"); |
| + window.foo.bar = {}; |
| + await testProperty("foo.bar.lambda"); |
| + |
| + // Test if we start with a non-object |
| + window.foo2 = 5; |
| + await runSnippet(test, "abort-on-property-read", "foo2.bar2.lambda"); |
| + await testProperty("foo2.bar2.lambda"); |
| + window.foo2 = {}; |
| + await testProperty("foo2.bar2.lambda"); |
| + |
| + test.done(); |
| +}; |
| + |