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: A few test adjustements. Created March 12, 2019, 2:28 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
« test/browser/_utils.js ('K') | « 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 {
27 getURL: () => ""
28 }
29 };
30
31 let testDocument = null;
32
33 exports.setUp = function(callback)
34 {
35 let iframe = document.createElement("iframe");
36 document.body.appendChild(iframe);
37 testDocument = iframe.contentDocument;
38
39 callback();
40 };
41
42 exports.tearDown = function(callback)
43 {
44 let iframe = testDocument.defaultView.frameElement;
45 iframe.parentNode.removeChild(iframe);
46 testDocument = null;
47
48 callback();
49 };
50
51 async function runSnippet(test, snippetName, ...args)
52 {
53 let snippet = library[snippetName];
54
55 test.ok(snippet);
56
57 snippet(...args);
58 await timeout(100);
59 }
60
61 exports.testAbortPropertyReadSnippet = async function(test)
62 {
63 window.abpTest = "foo";
64
65 async function testProperty(property, result = true)
66 {
67 let properties = property.split(".");
68 test.ok(properties.length >= 1);
69
70 let exceptionCaught = false;
71 let value = 1;
72 try
73 {
74 let obj = window;
75 while (properties.length > 1)
76 obj = obj[properties.shift()];
77 value = obj[properties.shift()];
78 }
79 catch (e)
80 {
81 if (properties.length == 0)
82 exceptionCaught = true;
83 }
84 test.equal(exceptionCaught, result, `The property "${property}" didn't trigg er and exception.`);
85 test.equal(value, result ? 1 : undefined, `The value for "${property}" shoul dn't have been read.`);
86 }
87
88 await runSnippet(test, "abort-on-property-read", "abpTest");
89 await testProperty("abpTest");
90
91 window.abpTest2 = {prop1: "foo"};
92
93 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1");
94 await testProperty("abpTest2.prop1");
95
96 // Test that we try to catch a property that don't exist yet.
97 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1");
98 window.abpTest3 = {prop1: "foo"};
99 await testProperty("abpTest3.prop1");
100
101 // Test that other properties don't trigger.
102 await testProperty("abpTest3.prop2", false);
103
104 // Test overwriting the object with another object
105 window.foo = {bar: {}};
106 await runSnippet(test, "abort-on-property-read", "foo.bar.lambda");
107 await testProperty("foo.bar.lambda");
108 window.foo.bar = {};
109 await testProperty("foo.bar.lambda");
110
111 // Test if we start with a non-object
112 window.foo2 = 5;
113 await runSnippet(test, "abort-on-property-read", "foo2.bar2.lambda");
114 await testProperty("foo2.bar2.lambda");
115 window.foo2 = {};
116 await testProperty("foo2.bar2.lambda");
117
118 test.done();
119 };
120
OLDNEW
« test/browser/_utils.js ('K') | « test/browser/elemHideEmulation.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld