Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/browser/snippets.js

Issue 29995559: Issue 7236 - Handle sub properties in abort-on-property snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: More test cleanup Created March 14, 2019, 10:21 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/browser/elemHideEmulation.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 "use strict";
19
20 const library = require("../../lib/content/snippets.js");
21 const {timeout} = require("./_utils");
22
23 // We need this stub for the injector.
24 window.browser = {
25 runtime: {
26 getURL: () => ""
27 }
28 };
29
30 async function runSnippet(test, snippetName, ...args)
31 {
32 let snippet = library[snippetName];
33
34 test.ok(snippet);
35
36 snippet(...args);
37 await timeout(100);
38 }
39
40 exports.testAbortOnPropertyReadSnippet = async function(test)
41 {
42 window.abpTest = "foo";
43
44 function testProperty(property, result = true)
45 {
46 let path = property.split(".");
47
48 let exceptionCaught = false;
49 let value = 1;
50
51 try
52 {
53 let obj = window;
54 while (path.length > 1)
55 obj = obj[path.shift()];
56 value = obj[path.shift()];
57 }
58 catch (e)
59 {
60 exceptionCaught = true;
61 }
62
63 test.equal(exceptionCaught, result, `The property "${property}" ${result ? " should" : "shouldn't"} trigger an exception.`);
Manish Jethani 2019/03/16 10:50:24 Nit: Let's try to reduce the number of columns as
hub 2019/03/18 18:27:26 Done.
64 test.equal(value, result ? 1 : undefined, `The value for "${property}" ${res ult ? "shouldn't" : "should"} have been read.`);
65 }
66
67 await runSnippet(test, "abort-on-property-read", "abpTest");
68 testProperty("abpTest");
69
70 window.abpTest2 = {prop1: "foo"};
71
72 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1");
73 testProperty("abpTest2.prop1");
74
75 // Test that we try to catch a property that don't exist yet.
76 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1");
77 window.abpTest3 = {prop1: "foo"};
78 testProperty("abpTest3.prop1");
79
80 // Test that other properties don't trigger.
81 testProperty("abpTest3.prop2", false);
82
83 // Test overwriting the object with another object
84 window.foo = {bar: {}};
85 await runSnippet(test, "abort-on-property-read", "foo.bar.lambda");
86 testProperty("foo.bar.lambda");
87 window.foo.bar = {};
88 testProperty("foo.bar.lambda");
89
90 // Test if we start with a non-object
91 window.foo2 = 5;
92 await runSnippet(test, "abort-on-property-read", "foo2.bar2.lambda");
93 testProperty("foo2.bar2.lambda");
94 window.foo2 = {};
95 testProperty("foo2.bar2.lambda");
96
97 test.done();
98 };
OLDNEW
« no previous file with comments | « test/browser/elemHideEmulation.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld