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

Delta Between Two Patch Sets: test/browser/snippets.js

Issue 29995559: Issue 7236 - Handle sub properties in abort-on-property snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Cleanup test Created March 12, 2019, 3:14 p.m.
Right Patch Set: tweak property names Created March 20, 2019, 4:18 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « test/browser/elemHideEmulation.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 16 matching lines...) Expand all
27 } 27 }
28 }; 28 };
29 29
30 async function runSnippet(test, snippetName, ...args) 30 async function runSnippet(test, snippetName, ...args)
31 { 31 {
32 let snippet = library[snippetName]; 32 let snippet = library[snippetName];
33 33
34 test.ok(snippet); 34 test.ok(snippet);
35 35
36 snippet(...args); 36 snippet(...args);
37
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
40 // execution to be complete.
37 await timeout(100); 41 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.
38 } 42 }
39 43
40 exports.testAbortPropertyReadSnippet = async function(test) 44 exports.testAbortOnPropertyReadSnippet = 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.
41 { 45 {
42 window.abpTest = "foo"; 46 function testProperty(property, result = true, errorName = "ReferenceError")
43
44 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.
45 { 47 {
46 let properties = property.split("."); 48 let path = 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.
47 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.
48 49
49 let exceptionCaught = false; 50 let exceptionCaught = false;
50 let value = 1; 51 let value = 1;
52
51 try 53 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.
52 { 54 {
53 let obj = window; 55 let obj = window;
54 while (properties.length > 1) 56 while (path.length > 1)
55 obj = obj[properties.shift()]; 57 obj = obj[path.shift()];
56 value = obj[properties.shift()]; 58 value = obj[path.shift()];
57 } 59 }
58 catch (e) 60 catch (e)
59 { 61 {
60 if (properties.length == 0) 62 test.equal(e.name, errorName);
Manish Jethani 2019/03/14 10:58:12 Will properties.length every be non-zero here?
61 exceptionCaught = true; 63 exceptionCaught = true;
62 } 64 }
63 test.equal(exceptionCaught, result, `The property "${property}" didn't trigg er and exception.`); 65
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.
64 test.equal(value, result ? 1 : undefined, `The value for "${property}" shoul dn't have been read.`); 66 test.equal(
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.
67 exceptionCaught,
68 result,
69 `The property "${property}" ${result ? "should" : "shouldn't"} trigger an exception.`
70 );
71 test.equal(
72 value,
73 result ? 1 : undefined,
74 `The value for "${property}" ${result ? "shouldn't" : "should"} have been read.`
75 );
65 } 76 }
66 77
78 window.abpTest = "fortytwo";
67 await runSnippet(test, "abort-on-property-read", "abpTest"); 79 await runSnippet(test, "abort-on-property-read", "abpTest");
68 await testProperty("abpTest"); 80 testProperty("abpTest");
69 81
70 window.abpTest2 = {prop1: "foo"}; 82 window.abpTest2 = {prop1: "fortytwo"};
83 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1");
84 testProperty("abpTest2.prop1");
71 85
72 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); 86 // Test that we try to catch a property that doesn't exist yet.
73 await 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"); 87 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1");
77 window.abpTest3 = {prop1: "foo"}; 88 window.abpTest3 = {prop1: "fortytwo"};
78 await testProperty("abpTest3.prop1"); 89 testProperty("abpTest3.prop1");
79 90
80 // Test that other properties don't trigger. 91 // Test that other properties don't trigger.
81 await testProperty("abpTest3.prop2", false); 92 testProperty("abpTest3.prop2", false);
82 93
83 // Test overwriting the object with another object 94 // Test overwriting the object with another object.
84 window.foo = {bar: {}}; 95 window.abpTest4 = {prop3: {}};
85 await runSnippet(test, "abort-on-property-read", "foo.bar.lambda"); 96 await runSnippet(test, "abort-on-property-read", "abpTest4.prop3.foo");
86 await testProperty("foo.bar.lambda"); 97 testProperty("abpTest4.prop3.foo");
87 window.foo.bar = {}; 98 window.abpTest4.prop3 = {};
88 await testProperty("foo.bar.lambda"); 99 testProperty("abpTest4.prop3.foo");
89 100
90 // Test if we start with a non-object 101 // Test if we start with a non-object.
91 window.foo2 = 5; 102 window.abpTest5 = 42;
92 await runSnippet(test, "abort-on-property-read", "foo2.bar2.lambda"); 103 await runSnippet(test, "abort-on-property-read", "abpTest5.prop4.bar");
93 await testProperty("foo2.bar2.lambda"); 104
94 window.foo2 = {}; 105 testProperty("abpTest5.prop4.bar", true, "TypeError");
95 await testProperty("foo2.bar2.lambda"); 106
107 window.abpTest5 = {prop4: 42};
108 testProperty("abpTest5.prop4.bar", false);
109 window.abpTest5 = {prop4: {}};
110 testProperty("abpTest5.prop4.bar");
96 111
97 test.done(); 112 test.done();
98 }; 113 };
99
LEFTRIGHT

Powered by Google App Engine
This is Rietveld