Index: test/browser/snippets.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/test/browser/snippets.js |
@@ -0,0 +1,100 @@ |
+/* |
+ * 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); |
+} |
+ |
+exports.testAbortOnPropertyReadSnippet = async function(test) |
+{ |
+ window.abpTest = "foo"; |
+ |
+ function testProperty(property, result = true) |
+ { |
+ let path = property.split("."); |
+ |
+ let exceptionCaught = false; |
+ let value = 1; |
+ |
+ try |
+ { |
+ let obj = window; |
Manish Jethani
2019/03/14 10:58:13
What about just doing it exactly like in `lib/cont
hub
2019/03/14 12:32:55
I don't think it really matters either way.
Manish Jethani
2019/03/14 16:56:23
Alright.
|
+ while (path.length > 1) |
+ obj = obj[path.shift()]; |
+ value = obj[path.shift()]; |
+ } |
+ catch (e) |
+ { |
+ if (path.length == 0) |
Manish Jethani
2019/03/14 10:58:13
Will `path.length` ever be non-zero here?
hub
2019/03/14 12:32:55
if any other exception, it could.
Manish Jethani
2019/03/14 16:56:22
And why would there be any other exception? Can yo
hub
2019/03/14 22:22:11
it's gone then.
|
+ exceptionCaught = true; |
+ } |
+ |
+ test.equal(exceptionCaught, result, `The property "${property}" ${result ? "didn't" : "did"} trigger an exception.`); |
Manish Jethani
2019/03/14 10:58:13
Nit: We could use "should" here for consistency: "
hub
2019/03/14 12:32:55
Done.
Manish Jethani
2019/03/14 16:56:23
"should" should be first, then "shouldn't".
hub
2019/03/14 22:22:11
my bad, fixed.
|
+ test.equal(value, result ? 1 : undefined, `The value for "${property}" ${result ? "shouldn't" : "should"} have been read.`); |
+ } |
+ |
+ await runSnippet(test, "abort-on-property-read", "abpTest"); |
+ testProperty("abpTest"); |
+ |
+ window.abpTest2 = {prop1: "foo"}; |
+ |
+ await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); |
+ 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"}; |
+ testProperty("abpTest3.prop1"); |
+ |
+ // Test that other properties don't trigger. |
+ testProperty("abpTest3.prop2", false); |
+ |
+ // Test overwriting the object with another object |
+ window.foo = {bar: {}}; |
+ await runSnippet(test, "abort-on-property-read", "foo.bar.lambda"); |
+ testProperty("foo.bar.lambda"); |
+ window.foo.bar = {}; |
+ 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"); |
+ testProperty("foo2.bar2.lambda"); |
+ window.foo2 = {}; |
+ testProperty("foo2.bar2.lambda"); |
+ |
+ test.done(); |
+}; |
+ |
Manish Jethani
2019/03/14 10:58:13
Unnecessary blank line at the end.
hub
2019/03/14 12:32:55
Done.
|