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

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

Issue 29367181: Issue 4726 - Add tests for the element hiding emulation content script (Closed) Base URL: https://bitbucket.org/fhd/adblockpluscore
Patch Set: Wrap and indent QUnit.test invocations consistently Created Jan. 19, 2017, 9:55 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
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 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 // 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
20
21 QUnit.module("Element hiding emulation",
22 {
23 before: function()
24 {
25 // The URL object in PhantomJS 2.1.7 does not implement any properties, so
26 // we need a polyfill.
27 if (!URL || !("origin" in URL))
28 {
29 var doc = document.implementation.createHTMLDocument();
30 var anchor = doc.createElement("a");
31 doc.body.appendChild(anchor);
32 URL = function(url)
33 {
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 }
47 });
48
49 QUnit.assert.hidden = function(element)
50 {
51 this.equal(getComputedStyle(element).display, "none",
52 "The element's display property should be set to 'none'");
53 };
54
55 QUnit.assert.visible = function(element)
56 {
57 this.notEqual(getComputedStyle(element).display, "none",
58 "The element's display property should not be set to 'none'");
59 };
60
61 function findUniqueId()
62 {
63 var id = "elemHideEmulationTest-" + Math.floor(Math.random() * 10000);
64 if (!document.getElementById(id))
65 return id;
66 return findUniqueId();
67 }
68
69 function insertStyleRule(rule)
70 {
71 var styleElement;
72 var styleElements = document.head.getElementsByTagName("style");
73 if (styleElements.length)
74 {
75 styleElement = styleElements[0];
76 }
77 else
78 {
79 styleElement = document.createElement("style");
80 document.head.appendChild(styleElement);
81 }
82 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length);
83 }
84
85 function createElementWithStyle(styleBlock)
86 {
87 var element = document.createElement("div");
88 element.id = findUniqueId();
89 document.body.appendChild(element);
90 insertStyleRule("#" + element.id + " " + styleBlock);
91 return element;
92 }
93
94 function applyElemHideEmulation(selectors, callback)
95 {
96 var elemHideEmulation = new ElemHideEmulation(
97 window,
98 function(callback)
99 {
100 var patterns = [];
101 selectors.forEach(function(selector)
102 {
103 patterns.push({selector: selector});
104 });
105 callback(patterns);
106 },
107 function(selectors)
108 {
109 if (!selectors.length)
110 return;
111 var selector = selectors.join(", ");
112 insertStyleRule(selector + "{display: none !important;}");
113 }
114 );
115
116 elemHideEmulation.load(function()
117 {
118 elemHideEmulation.apply();
119 callback();
120 });
121 }
122
123 QUnit.test(
124 "Verbatim property selector",
125 function(assert)
126 {
127 var done = assert.async();
128 var toHide = createElementWithStyle("{background-color: #000}");
129 applyElemHideEmulation(
130 ["[-abp-properties='background-color: rgb(0, 0, 0)']"],
131 function()
132 {
133 assert.hidden(toHide);
134 done();
135 }
136 );
137 }
138 );
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