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

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

Issue 29373596: Issue 4838 - Use nodeunit framework for integration tests running in browser (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Patch Set: Addressed comments and added additional minor changes Created Feb. 24, 2017, 9:17 a.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.html ('k') | test_runner.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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]);
46 } 41 }
47 });
48 42
49 QUnit.assert.hidden = function(element) 43 callback();
50 {
51 this.equal(getComputedStyle(element).display, "none",
52 "The element's display property should be set to 'none'");
53 }; 44 };
54 45
55 QUnit.assert.visible = function(element) 46 exports.tearDown = function(callback)
56 { 47 {
57 this.notEqual(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",
57 "The element's display property should be set to 'none'");
58 }
59
60 function expectVisible(test, element)
61 {
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 }
68 73
69 function insertStyleRule(rule) 74 function insertStyleRule(rule)
70 { 75 {
71 var styleElement; 76 var styleElement;
72 var styleElements = document.head.getElementsByTagName("style"); 77 var styleElements = document.head.getElementsByTagName("style");
73 if (styleElements.length) 78 if (styleElements.length)
74 {
75 styleElement = styleElements[0]; 79 styleElement = styleElements[0];
76 }
77 else 80 else
78 { 81 {
79 styleElement = document.createElement("style"); 82 styleElement = document.createElement("style");
80 document.head.appendChild(styleElement); 83 document.head.appendChild(styleElement);
81 } 84 }
82 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); 85 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length);
83 } 86 }
84 87
85 function createElementWithStyle(styleBlock) 88 function createElementWithStyle(styleBlock)
86 { 89 {
87 var element = document.createElement("div"); 90 var element = document.createElement("div");
88 element.id = findUniqueId(); 91 element.id = findUniqueId();
89 document.body.appendChild(element); 92 document.body.appendChild(element);
90 insertStyleRule("#" + element.id + " " + styleBlock); 93 insertStyleRule("#" + element.id + " " + styleBlock);
91 return element; 94 return element;
92 } 95 }
93 96
94 function applyElemHideEmulation(selectors, callback) 97 function applyElemHideEmulation(selectors, callback)
95 { 98 {
99 if (typeof ElemHideEmulation == "undefined")
100 {
101 loadScript(myUrl + "/../../../lib/common.js", function()
102 {
103 loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js",
104 function()
105 {
106 applyElemHideEmulation(selectors, callback);
107 });
108 });
109 return;
110 }
111
96 var elemHideEmulation = new ElemHideEmulation( 112 var elemHideEmulation = new ElemHideEmulation(
97 window, 113 window,
98 function(callback) 114 function(callback)
99 { 115 {
100 var patterns = []; 116 var patterns = [];
101 selectors.forEach(function(selector) 117 selectors.forEach(function(selector)
102 { 118 {
103 patterns.push({selector: selector}); 119 patterns.push({selector: selector});
104 }); 120 });
105 callback(patterns); 121 callback(patterns);
106 }, 122 },
107 function(selectors) 123 function(selectors)
108 { 124 {
109 if (!selectors.length) 125 if (!selectors.length)
110 return; 126 return;
111 var selector = selectors.join(", "); 127 var selector = selectors.join(", ");
112 insertStyleRule(selector + "{display: none !important;}"); 128 insertStyleRule(selector + "{display: none !important;}");
113 } 129 }
114 ); 130 );
115 131
116 elemHideEmulation.load(function() 132 elemHideEmulation.load(function()
117 { 133 {
118 elemHideEmulation.apply(); 134 elemHideEmulation.apply();
119 callback(); 135 callback();
120 }); 136 });
121 } 137 }
122 138
123 QUnit.test( 139 exports.testVerbatimPropertySelector = function(test)
124 "Verbatim property selector", 140 {
125 function(assert) 141 var toHide = createElementWithStyle("{background-color: #000}");
126 { 142 applyElemHideEmulation(
127 var done = assert.async(); 143 ["[-abp-properties='background-color: rgb(0, 0, 0)']"],
128 var toHide = createElementWithStyle("{background-color: #000}"); 144 function()
129 applyElemHideEmulation( 145 {
130 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], 146 expectHidden(test, toHide);
131 function() 147 test.done();
148 }
149 );
150 };
151
152 exports.testPropertySelectorWithWildcard = function(test)
153 {
154 var toHide = createElementWithStyle("{background-color: #000}");
155 applyElemHideEmulation(
156 ["[-abp-properties='*color: rgb(0, 0, 0)']"],
157 function()
158 {
159 expectHidden(test, toHide);
160 test.done();
161 }
162 );
163 };
164
165 exports.testPropertySelectorWithRegularExpression = function(test)
166 {
167 var toHide = createElementWithStyle("{background-color: #000}");
168 applyElemHideEmulation(
169 ["[-abp-properties='/.*color: rgb\\(0, 0, 0\\)/']"],
170 function()
171 {
172 expectHidden(test, toHide);
173 test.done();
174 }
175 );
176 };
177
178 exports.testPropertySelectorWithEscapedBrace = function(test)
179 {
180 var toHide = createElementWithStyle("{background-color: #000}");
181 applyElemHideEmulation(
182 ["[-abp-properties='/background.\\x7B 0,6\\x7D : rgb\\(0, 0, 0\\)/']"],
183 function()
184 {
185 expectHidden(test, toHide);
186 test.done();
187 }
188 );
189 };
190
191 exports.testPropertySelectorWithImproperlyEscapedBrace = function(test)
192 {
193 var toHide = createElementWithStyle("{background-color: #000}");
194 applyElemHideEmulation(
195 ["[-abp-properties='/background.\\x7B0,6\\x7D: rgb\\(0, 0, 0\\)/']"],
196 function()
197 {
198 expectVisible(test, toHide);
199 test.done();
200 }
201 );
202 };
203
204 exports.testDynamicallyChangedProperty = function(test)
205 {
206 var toHide = createElementWithStyle("{}");
207 applyElemHideEmulation(
208 ["[-abp-properties='background-color: rgb(0, 0, 0)']"],
209 function()
210 {
211 expectVisible(test, toHide);
212 insertStyleRule("#" + toHide.id + " {background-color: #000}");
213 window.setTimeout(function()
132 { 214 {
133 assert.hidden(toHide); 215 expectHidden(test, toHide);
134 done(); 216 test.done();
135 } 217 }, 0);
136 ); 218 }
137 } 219 );
138 ); 220 };
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 );
OLDNEW
« no previous file with comments | « test/browser/elemHideEmulation.html ('k') | test_runner.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld