OLD | NEW |
(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 |
| 22 // We need this stub for the injector. |
| 23 window.browser = |
| 24 { |
| 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 function timeout(delay) |
| 52 { |
| 53 return new Promise((resolve, reject) => |
| 54 { |
| 55 window.setTimeout(resolve, delay); |
| 56 }); |
| 57 } |
| 58 |
| 59 async function runSnippet(test, snippetName, ...args) |
| 60 { |
| 61 let snippet = library[snippetName]; |
| 62 |
| 63 test.ok(snippet); |
| 64 |
| 65 snippet(...args); |
| 66 await timeout(100); |
| 67 } |
| 68 |
| 69 exports.testAbortPropertyReadSnippet = async function(test) |
| 70 { |
| 71 window.abpTest = "foo"; |
| 72 |
| 73 async function testProperty(property, result = true) |
| 74 { |
| 75 let properties = property.split("."); |
| 76 test.ok(properties.length >= 1); |
| 77 |
| 78 let exceptionCaught = false; |
| 79 let value = 1; |
| 80 try |
| 81 { |
| 82 let obj = window; |
| 83 while (properties.length > 1) |
| 84 obj = obj[properties.shift()]; |
| 85 value = obj[properties.shift()]; |
| 86 } |
| 87 catch (e) |
| 88 { |
| 89 if (properties.length == 0) |
| 90 exceptionCaught = true; |
| 91 } |
| 92 test.equal(exceptionCaught, result, `The property "${property}" didn't trigg
er and exception.`); |
| 93 test.equal(value, result ? 1 : undefined, `The value for "${property}" shoul
dn't have been read.`); |
| 94 } |
| 95 |
| 96 await runSnippet(test, "abort-on-property-read", "abpTest"); |
| 97 await testProperty("abpTest"); |
| 98 |
| 99 window.abpTest2 = {prop1: "foo"}; |
| 100 |
| 101 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); |
| 102 await testProperty("abpTest2.prop1"); |
| 103 |
| 104 // Test that we try to catch a property that don't exist yet. |
| 105 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); |
| 106 window.abpTest3 = {prop1: "foo"}; |
| 107 await testProperty("abpTest3.prop1"); |
| 108 |
| 109 // Test that other properties don't trigger. |
| 110 await testProperty("abpTest3.prop2", false); |
| 111 |
| 112 test.done(); |
| 113 }; |
| 114 |
OLD | NEW |