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

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

Issue 29423569: Issue 4796 - Use a modern JS engine in the browser tests and convert all files to ECMAScript 6 (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Removed redundant configuration change Created April 27, 2017, 6:04 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
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 "use strict";
19 // used in the browser tests. See https://issues.adblockplus.org/ticket/4796
20 19
21 // TODO: This should be using document.currentScript once supported by 20 /* globals ElemHideEmulation */
22 // PhantomJS.
23 var myUrl = document.head.lastChild.src;
24 21
25 exports.setUp = function(callback) 22 let myUrl = document.currentScript.src;
26 {
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))
30 {
31 var doc = document.implementation.createHTMLDocument();
32 var anchor = doc.createElement("a");
33 doc.body.appendChild(anchor);
34 URL = function(url)
35 {
36 if (!url)
37 throw "Invalid URL";
38 anchor.href = url;
39 this.origin = anchor.origin;
40 };
41 }
42
43 callback();
44 };
45 23
46 exports.tearDown = function(callback) 24 exports.tearDown = function(callback)
47 { 25 {
48 var styleElements = document.head.getElementsByTagName("style"); 26 let styleElements = document.head.getElementsByTagName("style");
49 while (styleElements.length) 27 while (styleElements.length)
50 styleElements[0].parentNode.removeChild(styleElements[0]); 28 styleElements[0].parentNode.removeChild(styleElements[0]);
51 callback(); 29 callback();
52 }; 30 };
53 31
32 function unexpectedError(error)
33 {
34 console.error(error);
35 this.ok(false, "Unexpected error: " + error);
36 }
37
54 function expectHidden(test, element) 38 function expectHidden(test, element)
55 { 39 {
56 test.equal(window.getComputedStyle(element).display, "none", 40 test.equal(window.getComputedStyle(element).display, "none",
57 "The element's display property should be set to 'none'"); 41 "The element's display property should be set to 'none'");
58 } 42 }
59 43
60 function expectVisible(test, element) 44 function expectVisible(test, element)
61 { 45 {
62 test.notEqual(window.getComputedStyle(element).display, "none", 46 test.notEqual(window.getComputedStyle(element).display, "none",
63 "The element's display property should not be set to 'none'"); 47 "The element's display property should not be set to 'none'");
64 } 48 }
65 49
66 function findUniqueId() 50 function findUniqueId()
67 { 51 {
68 var id = "elemHideEmulationTest-" + Math.floor(Math.random() * 10000); 52 let id = "elemHideEmulationTest-" + Math.floor(Math.random() * 10000);
69 if (!document.getElementById(id)) 53 if (!document.getElementById(id))
70 return id; 54 return id;
71 return findUniqueId(); 55 return findUniqueId();
72 } 56 }
73 57
74 function insertStyleRule(rule) 58 function insertStyleRule(rule)
75 { 59 {
76 var styleElement; 60 let styleElement;
77 var styleElements = document.head.getElementsByTagName("style"); 61 let styleElements = document.head.getElementsByTagName("style");
78 if (styleElements.length) 62 if (styleElements.length)
79 styleElement = styleElements[0]; 63 styleElement = styleElements[0];
80 else 64 else
81 { 65 {
82 styleElement = document.createElement("style"); 66 styleElement = document.createElement("style");
83 document.head.appendChild(styleElement); 67 document.head.appendChild(styleElement);
84 } 68 }
85 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); 69 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length);
86 } 70 }
87 71
88 function createElementWithStyle(styleBlock) 72 function createElementWithStyle(styleBlock)
89 { 73 {
90 var element = document.createElement("div"); 74 let element = document.createElement("div");
91 element.id = findUniqueId(); 75 element.id = findUniqueId();
92 document.body.appendChild(element); 76 document.body.appendChild(element);
93 insertStyleRule("#" + element.id + " " + styleBlock); 77 insertStyleRule("#" + element.id + " " + styleBlock);
94 return element; 78 return element;
95 } 79 }
96 80
97 function applyElemHideEmulation(selectors, callback) 81 function applyElemHideEmulation(selectors)
98 { 82 {
99 if (typeof ElemHideEmulation == "undefined") 83 if (typeof ElemHideEmulation == "undefined")
100 { 84 {
101 loadScript(myUrl + "/../../../lib/common.js", function() 85 return loadScript(myUrl + "/../../../lib/common.js").then(() =>
102 { 86 {
103 loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js", 87 return loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js") ;
104 function() 88 }).then(() =>
105 { 89 {
106 applyElemHideEmulation(selectors, callback); 90 return applyElemHideEmulation(selectors);
107 });
108 }); 91 });
109 return;
110 } 92 }
111 93
112 var elemHideEmulation = new ElemHideEmulation( 94 let elemHideEmulation = new ElemHideEmulation(
113 window, 95 window,
114 function(callback) 96 callback =>
115 { 97 {
116 var patterns = []; 98 let patterns = [];
117 selectors.forEach(function(selector) 99 selectors.forEach(selector =>
118 { 100 {
119 patterns.push({selector: selector}); 101 patterns.push({selector});
120 }); 102 });
121 callback(patterns); 103 callback(patterns);
122 }, 104 }, newSelectors =>
123 function(selectors)
124 { 105 {
125 if (!selectors.length) 106 if (!newSelectors.length)
126 return; 107 return;
127 var selector = selectors.join(", "); 108 let selector = newSelectors.join(", ");
128 insertStyleRule(selector + "{display: none !important;}"); 109 insertStyleRule(selector + "{display: none !important;}");
129 } 110 }
130 ); 111 );
131 112
132 elemHideEmulation.apply(); 113 elemHideEmulation.apply();
133 callback(); 114 return Promise.resolve();
134 } 115 }
135 116
136 exports.testVerbatimPropertySelector = function(test) 117 exports.testVerbatimPropertySelector = function(test)
137 { 118 {
138 var toHide = createElementWithStyle("{background-color: #000}"); 119 let toHide = createElementWithStyle("{background-color: #000}");
139 applyElemHideEmulation( 120 applyElemHideEmulation(
140 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], 121 ["[-abp-properties='background-color: rgb(0, 0, 0)']"]
141 function() 122 ).then(() =>
142 { 123 {
143 expectHidden(test, toHide); 124 expectHidden(test, toHide);
144 test.done(); 125 }).catch(unexpectedError.bind(test)).then(() => test.done());
145 }
146 );
147 }; 126 };
148 127
149 exports.testPropertySelectorWithWildcard = function(test) 128 exports.testPropertySelectorWithWildcard = function(test)
150 { 129 {
151 var toHide = createElementWithStyle("{background-color: #000}"); 130 let toHide = createElementWithStyle("{background-color: #000}");
152 applyElemHideEmulation( 131 applyElemHideEmulation(
153 ["[-abp-properties='*color: rgb(0, 0, 0)']"], 132 ["[-abp-properties='*color: rgb(0, 0, 0)']"]
154 function() 133 ).then(() =>
155 { 134 {
156 expectHidden(test, toHide); 135 expectHidden(test, toHide);
157 test.done(); 136 }).catch(unexpectedError.bind(test)).then(() => test.done());
158 }
159 );
160 }; 137 };
161 138
162 exports.testPropertySelectorWithRegularExpression = function(test) 139 exports.testPropertySelectorWithRegularExpression = function(test)
163 { 140 {
164 var toHide = createElementWithStyle("{background-color: #000}"); 141 let toHide = createElementWithStyle("{background-color: #000}");
165 applyElemHideEmulation( 142 applyElemHideEmulation(
166 ["[-abp-properties='/.*color: rgb\\(0, 0, 0\\)/']"], 143 ["[-abp-properties='/.*color: rgb\\(0, 0, 0\\)/']"]
167 function() 144 ).then(() =>
168 { 145 {
169 expectHidden(test, toHide); 146 expectHidden(test, toHide);
170 test.done(); 147 }).catch(unexpectedError.bind(test)).then(() => test.done());
171 }
172 );
173 }; 148 };
174 149
175 exports.testPropertySelectorWithEscapedBrace = function(test) 150 exports.testPropertySelectorWithEscapedBrace = function(test)
176 { 151 {
177 var toHide = createElementWithStyle("{background-color: #000}"); 152 let toHide = createElementWithStyle("{background-color: #000}");
178 applyElemHideEmulation( 153 applyElemHideEmulation(
179 ["[-abp-properties='/background.\\x7B 0,6\\x7D : rgb\\(0, 0, 0\\)/']"], 154 ["[-abp-properties='/background.\\x7B 0,6\\x7D : rgb\\(0, 0, 0\\)/']"]
180 function() 155 ).then(() =>
181 { 156 {
182 expectHidden(test, toHide); 157 expectHidden(test, toHide);
183 test.done(); 158 }).catch(unexpectedError.bind(test)).then(() => test.done());
184 }
185 );
186 }; 159 };
187 160
188 exports.testPropertySelectorWithImproperlyEscapedBrace = function(test) 161 exports.testPropertySelectorWithImproperlyEscapedBrace = function(test)
189 { 162 {
190 var toHide = createElementWithStyle("{background-color: #000}"); 163 let toHide = createElementWithStyle("{background-color: #000}");
191 applyElemHideEmulation( 164 applyElemHideEmulation(
192 ["[-abp-properties='/background.\\x7B0,6\\x7D: rgb\\(0, 0, 0\\)/']"], 165 ["[-abp-properties='/background.\\x7B0,6\\x7D: rgb\\(0, 0, 0\\)/']"]
193 function() 166 ).then(() =>
194 { 167 {
195 expectVisible(test, toHide); 168 expectVisible(test, toHide);
196 test.done(); 169 }).catch(unexpectedError.bind(test)).then(() => test.done());
197 }
198 );
199 }; 170 };
200 171
201 exports.testDynamicallyChangedProperty = function(test) 172 exports.testDynamicallyChangedProperty = function(test)
202 { 173 {
203 var toHide = createElementWithStyle("{}"); 174 let toHide = createElementWithStyle("{}");
204 applyElemHideEmulation( 175 applyElemHideEmulation(
205 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], 176 ["[-abp-properties='background-color: rgb(0, 0, 0)']"]
206 function() 177 ).then(() =>
178 {
179 expectVisible(test, toHide);
180 insertStyleRule("#" + toHide.id + " {background-color: #000}");
181 return new Promise((resolve, reject) =>
207 { 182 {
208 expectVisible(test, toHide); 183 window.setTimeout(() =>
209 insertStyleRule("#" + toHide.id + " {background-color: #000}");
210 window.setTimeout(function()
211 { 184 {
212 expectHidden(test, toHide); 185 expectHidden(test, toHide);
213 test.done(); 186 resolve();
214 }, 0); 187 }, 0);
215 } 188 });
216 ); 189 }).catch(unexpectedError.bind(test)).then(() => test.done());
217 }; 190 };
OLDNEW
« test/browser/_bootstrap.js ('K') | « test/browser/_bootstrap.js ('k') | test_runner.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld