 Issue 29373596:
  Issue 4838 - Use nodeunit framework for integration tests running in browser  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore
    
  
    Issue 29373596:
  Issue 4838 - Use nodeunit framework for integration tests running in browser  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore| Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| 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-2017 Eyeo GmbH | 3 * Copyright (C) 2006-2017 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 | 
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. | 
| 13 * | 13 * | 
| 14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 
| 16 */ | 16 */ | 
| 17 | 17 | 
| 18 // We are currently limited to ECMAScript 5 in this file, because it is being | 18 // We are currently limited to ECMAScript 5 in this file, because it is being | 
| 19 // used in the browser tests. See https://issues.adblockplus.org/ticket/4796 | 19 // used in the browser tests. See https://issues.adblockplus.org/ticket/4796 | 
| 20 | 20 | 
| 21 QUnit.module("Element hiding emulation", | 21 // TODO: This should be using document.currentScript once supported by | 
| 22 // PhantomJS. | |
| 23 var myUrl = document.head.lastChild.src; | |
| 24 | |
| 25 exports.setUp = function(callback) | |
| 22 { | 26 { | 
| 23 before: function() | 27 // The URL object in PhantomJS 2.1.7 does not implement any properties, so | 
| 28 // we need a polyfill. | |
| 29 if (!URL || !("origin" in URL)) | |
| 24 { | 30 { | 
| 25 // The URL object in PhantomJS 2.1.7 does not implement any properties, so | 31 var doc = document.implementation.createHTMLDocument(); | 
| 26 // we need a polyfill. | 32 var anchor = doc.createElement("a"); | 
| 27 if (!URL || !("origin" in URL)) | 33 doc.body.appendChild(anchor); | 
| 34 URL = function(url) | |
| 28 { | 35 { | 
| 29 var doc = document.implementation.createHTMLDocument(); | 36 if (!url) | 
| 30 var anchor = doc.createElement("a"); | 37 throw "Invalid URL"; | 
| 31 doc.body.appendChild(anchor); | 38 anchor.href = url; | 
| 32 URL = function(url) | 39 this.origin = anchor.origin; | 
| 33 { | 40 }; | 
| 34 if (!url) | |
| 35 throw "Invalid URL"; | |
| 36 anchor.href = url; | |
| 37 this.origin = anchor.origin; | |
| 38 }; | |
| 39 } | |
| 40 }, | |
| 41 afterEach: function() | |
| 42 { | |
| 43 var styleElements = document.head.getElementsByTagName("style"); | |
| 44 for (var i = 0; i < styleElements.length; i++) | |
| 45 document.head.removeChild(styleElements[0]); | |
| 
Wladimir Palant
2017/01/24 16:39:24
This piece of code has multiple issues:
* getElem
 
Felix Dahlke
2017/01/30 14:37:58
Oops - all true.
 | |
| 46 } | 41 } | 
| 47 }); | |
| 48 | 42 | 
| 49 QUnit.assert.hidden = function(element) | 43 callback(); | 
| 44 }; | |
| 45 | |
| 46 exports.tearDown = function(callback) | |
| 50 { | 47 { | 
| 51 this.equal(getComputedStyle(element).display, "none", | 48 var styleElements = document.head.getElementsByTagName("style"); | 
| 49 while (styleElements.length) | |
| 50 styleElements[0].parentNode.removeChild(styleElements[0]); | |
| 51 callback(); | |
| 52 } | |
| 53 | |
| 54 function expectHidden(test, element) | |
| 55 { | |
| 56 test.equal(window.getComputedStyle(element).display, "none", | |
| 52 "The element's display property should be set to 'none'"); | 57 "The element's display property should be set to 'none'"); | 
| 53 }; | 58 }; | 
| 54 | 59 | 
| 55 QUnit.assert.visible = function(element) | 60 function expectVisible(test, element) | 
| 56 { | 61 { | 
| 57 this.notEqual(getComputedStyle(element).display, "none", | 62 test.notEqual(window.getComputedStyle(element).display, "none", | 
| 58 "The element's display property should not be set to 'none'"); | 63 "The element's display property should not be set to 'none'"); | 
| 59 }; | 64 }; | 
| 60 | 65 | 
| 61 function findUniqueId() | 66 function findUniqueId() | 
| 62 { | 67 { | 
| 63 var id = "elemHideEmulationTest-" + Math.floor(Math.random() * 10000); | 68 var id = "elemHideEmulationTest-" + Math.floor(Math.random() * 10000); | 
| 64 if (!document.getElementById(id)) | 69 if (!document.getElementById(id)) | 
| 65 return id; | 70 return id; | 
| 66 return findUniqueId(); | 71 return findUniqueId(); | 
| 67 } | 72 } | 
| (...skipping 18 matching lines...) Expand all Loading... | |
| 86 { | 91 { | 
| 87 var element = document.createElement("div"); | 92 var element = document.createElement("div"); | 
| 88 element.id = findUniqueId(); | 93 element.id = findUniqueId(); | 
| 89 document.body.appendChild(element); | 94 document.body.appendChild(element); | 
| 90 insertStyleRule("#" + element.id + " " + styleBlock); | 95 insertStyleRule("#" + element.id + " " + styleBlock); | 
| 91 return element; | 96 return element; | 
| 92 } | 97 } | 
| 93 | 98 | 
| 94 function applyElemHideEmulation(selectors, callback) | 99 function applyElemHideEmulation(selectors, callback) | 
| 95 { | 100 { | 
| 101 if (typeof ElemHideEmulation == "undefined") | |
| 102 { | |
| 103 loadScript(myUrl + "/../../../lib/common.js", function() | |
| 104 { | |
| 105 loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js", functi on() | |
| 106 { | |
| 107 applyElemHideEmulation(selectors, callback); | |
| 108 }); | |
| 109 }); | |
| 110 return; | |
| 111 } | |
| 112 | |
| 96 var elemHideEmulation = new ElemHideEmulation( | 113 var elemHideEmulation = new ElemHideEmulation( | 
| 97 window, | 114 window, | 
| 98 function(callback) | 115 function(callback) | 
| 99 { | 116 { | 
| 100 var patterns = []; | 117 var patterns = []; | 
| 101 selectors.forEach(function(selector) | 118 selectors.forEach(function(selector) | 
| 102 { | 119 { | 
| 103 patterns.push({selector: selector}); | 120 patterns.push({selector: selector}); | 
| 104 }); | 121 }); | 
| 105 callback(patterns); | 122 callback(patterns); | 
| 106 }, | 123 }, | 
| 107 function(selectors) | 124 function(selectors) | 
| 108 { | 125 { | 
| 109 if (!selectors.length) | 126 if (!selectors.length) | 
| 110 return; | 127 return; | 
| 111 var selector = selectors.join(", "); | 128 var selector = selectors.join(", "); | 
| 112 insertStyleRule(selector + "{display: none !important;}"); | 129 insertStyleRule(selector + "{display: none !important;}"); | 
| 113 } | 130 } | 
| 114 ); | 131 ); | 
| 115 | 132 | 
| 116 elemHideEmulation.load(function() | 133 elemHideEmulation.load(function() | 
| 117 { | 134 { | 
| 118 elemHideEmulation.apply(); | 135 elemHideEmulation.apply(); | 
| 119 callback(); | 136 callback(); | 
| 120 }); | 137 }); | 
| 121 } | 138 } | 
| 122 | 139 | 
| 123 QUnit.test( | 140 exports.testVerbatimPropertySelector = function(test) | 
| 124 "Verbatim property selector", | 141 { | 
| 125 function(assert) | 142 var toHide = createElementWithStyle("{background-color: #000}"); | 
| 126 { | 143 applyElemHideEmulation( | 
| 127 var done = assert.async(); | 144 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], | 
| 128 var toHide = createElementWithStyle("{background-color: #000}"); | 145 function() | 
| 129 applyElemHideEmulation( | 146 { | 
| 130 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], | 147 expectHidden(test, toHide); | 
| 131 function() | 148 test.done(); | 
| 149 } | |
| 150 ); | |
| 151 }; | |
| 152 | |
| 153 exports.testPropertySelectorWithWildcard = function(test) | |
| 154 { | |
| 155 var toHide = createElementWithStyle("{background-color: #000}"); | |
| 156 applyElemHideEmulation( | |
| 157 ["[-abp-properties='*color: rgb(0, 0, 0)']"], | |
| 158 function() | |
| 159 { | |
| 160 expectHidden(test, toHide); | |
| 161 test.done(); | |
| 162 } | |
| 163 ); | |
| 164 }; | |
| 165 | |
| 166 exports.testPropertySelectorWithRegularExpression = function(test) | |
| 167 { | |
| 168 var toHide = createElementWithStyle("{background-color: #000}"); | |
| 169 applyElemHideEmulation( | |
| 170 ["[-abp-properties='/.*color: rgb\\(0, 0, 0\\)/']"], | |
| 171 function() | |
| 172 { | |
| 173 expectHidden(test, toHide); | |
| 174 test.done(); | |
| 175 } | |
| 176 ); | |
| 177 }; | |
| 178 | |
| 179 exports.testPropertySelectorWithEscapedBrace = function(test) | |
| 180 { | |
| 181 var toHide = createElementWithStyle("{background-color: #000}"); | |
| 182 applyElemHideEmulation( | |
| 183 ["[-abp-properties='/background.\\x7B 0,6\\x7D : rgb\\(0, 0, 0\\)/']"], | |
| 184 function() | |
| 185 { | |
| 186 expectHidden(test, toHide); | |
| 187 test.done(); | |
| 188 } | |
| 189 ); | |
| 190 }; | |
| 191 | |
| 192 exports.testPropertySelectorWithImproperlyEscapedBrace = function(test) | |
| 193 { | |
| 194 var toHide = createElementWithStyle("{background-color: #000}"); | |
| 195 applyElemHideEmulation( | |
| 196 ["[-abp-properties='/background.\\x7B0,6\\x7D: rgb\\(0, 0, 0\\)/']"], | |
| 197 function() | |
| 198 { | |
| 199 expectVisible(test, toHide); | |
| 200 test.done(); | |
| 201 } | |
| 202 ); | |
| 203 }; | |
| 204 | |
| 205 exports.testPropertySelectorWithDynamicallyChangedProperty = function(test) | |
| 206 { | |
| 207 var toHide = createElementWithStyle("{}"); | |
| 208 applyElemHideEmulation( | |
| 209 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], | |
| 210 function() | |
| 211 { | |
| 212 expectVisible(test, toHide); | |
| 213 insertStyleRule("#" + toHide.id + " {background-color: #000}"); | |
| 214 window.setTimeout(function() | |
| 132 { | 215 { | 
| 133 assert.hidden(toHide); | 216 expectHidden(test, toHide); | 
| 134 done(); | 217 test.done(); | 
| 135 } | 218 }, 0); | 
| 136 ); | 219 } | 
| 137 } | 220 ); | 
| 138 ); | 221 }; | 
| 139 | |
| 140 QUnit.test( | |
| 141 "Property selector with wildcard", | |
| 142 function(assert) | |
| 143 { | |
| 144 var done = assert.async(); | |
| 145 var toHide = createElementWithStyle("{background-color: #000}"); | |
| 146 applyElemHideEmulation( | |
| 147 ["[-abp-properties='*color: rgb(0, 0, 0)']"], | |
| 148 function() | |
| 149 { | |
| 150 assert.hidden(toHide); | |
| 151 done(); | |
| 152 } | |
| 153 ); | |
| 154 } | |
| 155 ); | |
| 156 | |
| 157 QUnit.test( | |
| 158 "Property selector with regular expression", | |
| 159 function(assert) | |
| 160 { | |
| 161 var done = assert.async(); | |
| 162 var toHide = createElementWithStyle("{background-color: #000}"); | |
| 163 applyElemHideEmulation( | |
| 164 ["[-abp-properties='/.*color: rgb\\(0, 0, 0\\)/']"], | |
| 165 function() | |
| 166 { | |
| 167 assert.hidden(toHide); | |
| 168 done(); | |
| 169 } | |
| 170 ); | |
| 171 } | |
| 172 ); | |
| 173 | |
| 174 QUnit.test( | |
| 175 "Property selector with regular expression containing escaped brace", | |
| 176 function(assert) | |
| 177 { | |
| 178 var done = assert.async(); | |
| 179 var toHide = createElementWithStyle("{background-color: #000}"); | |
| 180 applyElemHideEmulation( | |
| 181 ["[-abp-properties='/background.\\x7B 0,6\\x7D : rgb\\(0, 0, 0\\)/']"], | |
| 182 function() | |
| 183 { | |
| 184 assert.hidden(toHide); | |
| 185 done(); | |
| 186 } | |
| 187 ); | |
| 188 } | |
| 189 ); | |
| 190 | |
| 191 QUnit.test( | |
| 192 "Property selector with regular expression containing improperly escaped \ | |
| 193 brace", | |
| 194 function(assert) | |
| 195 { | |
| 196 var done = assert.async(); | |
| 197 var toHide = createElementWithStyle("{background-color: #000}"); | |
| 198 applyElemHideEmulation( | |
| 199 ["[-abp-properties='/background.\\x7B0,6\\x7D: rgb\\(0, 0, 0\\)/']"], | |
| 200 function() | |
| 201 { | |
| 202 assert.visible(toHide); | |
| 203 done(); | |
| 204 } | |
| 205 ); | |
| 206 } | |
| 207 ); | |
| 208 | |
| 209 QUnit.test( | |
| 210 "Property selector works for dynamically changed property", | |
| 211 function(assert) | |
| 212 { | |
| 213 var done = assert.async(); | |
| 214 var toHide = createElementWithStyle("{}"); | |
| 215 applyElemHideEmulation( | |
| 216 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], | |
| 217 function() | |
| 218 { | |
| 219 assert.visible(toHide); | |
| 220 insertStyleRule("#" + toHide.id + " {background-color: #000}"); | |
| 221 setTimeout(function() | |
| 222 { | |
| 223 assert.hidden(toHide); | |
| 224 done(); | |
| 225 }); | |
| 226 } | |
| 227 ); | |
| 228 } | |
| 229 ); | |
| OLD | NEW |