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

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

Issue 29517687: Issue 5079, 5516 - Use webpack for browser tests, modules for content scripts (Closed)
Patch Set: Addressed Wladimir's and Hubert's initial feedback Created Aug. 17, 2017, 12:36 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 "use strict"; 18 "use strict";
19 19
20 /* globals ElemHideEmulation */ 20 const {ElemHideEmulation} = require("../../lib/content/elemHideEmulation");
21
22 let myUrl = document.currentScript.src;
23 21
24 exports.tearDown = function(callback) 22 exports.tearDown = function(callback)
25 { 23 {
26 let styleElements = document.head.getElementsByTagName("style"); 24 let styleElements = document.head.getElementsByTagName("style");
27 while (styleElements.length) 25 while (styleElements.length)
28 styleElements[0].parentNode.removeChild(styleElements[0]); 26 styleElements[0].parentNode.removeChild(styleElements[0]);
29 27
30 let child; 28 let child;
31 while (child = document.body.firstChild) 29 while (child = document.body.firstChild)
32 child.parentNode.removeChild(child); 30 child.parentNode.removeChild(child);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 if (styleElements.length) 65 if (styleElements.length)
68 styleElement = styleElements[0]; 66 styleElement = styleElements[0];
69 else 67 else
70 { 68 {
71 styleElement = document.createElement("style"); 69 styleElement = document.createElement("style");
72 document.head.appendChild(styleElement); 70 document.head.appendChild(styleElement);
73 } 71 }
74 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); 72 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length);
75 } 73 }
76 74
77 // insert a <div> with a unique id and a CSS rule 75 // Insert a <div> with a unique id and a CSS rule
78 // for the the selector matching the id. 76 // for the the selector matching the id.
79 function createElementWithStyle(styleBlock, parent) 77 function createElementWithStyle(styleBlock, parent)
80 { 78 {
81 let element = document.createElement("div"); 79 let element = document.createElement("div");
82 element.id = findUniqueId(); 80 element.id = findUniqueId();
83 if (!parent) 81 if (!parent)
84 document.body.appendChild(element); 82 document.body.appendChild(element);
85 else 83 else
86 parent.appendChild(element); 84 parent.appendChild(element);
87 insertStyleRule("#" + element.id + " " + styleBlock); 85 insertStyleRule("#" + element.id + " " + styleBlock);
88 return element; 86 return element;
89 } 87 }
90 88
91 // Will ensure the class ElemHideEmulation is loaded.
92 // Pass true when it calls itself.
93 function loadElemHideEmulation(inside)
94 {
95 if (typeof ElemHideEmulation == "undefined")
96 {
97 if (inside)
98 return Promise.reject("Failed to load ElemHideEmulation.");
99
100 return loadScript(myUrl + "/../../../lib/common.js").then(() =>
101 {
102 return loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js") ;
103 }).then(() =>
104 {
105 return loadElemHideEmulation(true);
106 });
107 }
108
109 return Promise.resolve();
110 }
111
112 // Create a new ElemHideEmulation instance with @selectors. 89 // Create a new ElemHideEmulation instance with @selectors.
113 function applyElemHideEmulation(selectors) 90 function applyElemHideEmulation(selectors)
114 { 91 {
115 return loadElemHideEmulation().then(() => 92 return Promise.resolve().then(() =>
116 { 93 {
117 let elemHideEmulation = new ElemHideEmulation( 94 let elemHideEmulation = new ElemHideEmulation(
118 window, 95 window,
119 callback => 96 callback =>
120 { 97 {
121 let patterns = []; 98 let patterns = [];
122 selectors.forEach(selector => 99 selectors.forEach(selector =>
123 { 100 {
124 patterns.push({selector}); 101 patterns.push({selector});
125 }); 102 });
126 callback(patterns); 103 callback(patterns);
127 }, 104 },
128 newSelectors => 105 newSelectors =>
129 { 106 {
130 if (!newSelectors.length) 107 if (!newSelectors.length)
131 return; 108 return;
132 let selector = newSelectors.join(", "); 109 let selector = newSelectors.join(", ");
133 insertStyleRule(selector + "{display: none !important;}"); 110 insertStyleRule(selector + "{display: none !important;}");
134 }, 111 },
135 elems => 112 elems =>
136 { 113 {
137 for (let elem of elems) 114 for (let elem of elems)
138 elem.style.display = "none"; 115 elem.style.display = "none";
139 } 116 }
140 ); 117 );
141 118
142 elemHideEmulation.apply(); 119 elemHideEmulation.apply();
143 return Promise.resolve(elemHideEmulation); 120 return elemHideEmulation;
144 }); 121 });
145 } 122 }
146 123
147 exports.testVerbatimPropertySelector = function(test) 124 exports.testVerbatimPropertySelector = function(test)
148 { 125 {
149 let toHide = createElementWithStyle("{background-color: #000}"); 126 let toHide = createElementWithStyle("{background-color: #000}");
150 applyElemHideEmulation( 127 applyElemHideEmulation(
151 [":-abp-properties(background-color: rgb(0, 0, 0))"] 128 [":-abp-properties(background-color: rgb(0, 0, 0))"]
152 ).then(() => 129 ).then(() =>
153 { 130 {
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 let child = createElementWithStyle("{}", parent); 436 let child = createElementWithStyle("{}", parent);
460 insertStyleRule("body #" + parent.id + " > div { background-color: #000}"); 437 insertStyleRule("body #" + parent.id + " > div { background-color: #000}");
461 applyElemHideEmulation( 438 applyElemHideEmulation(
462 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"] 439 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"]
463 ).then(() => 440 ).then(() =>
464 { 441 {
465 expectVisible(test, child); 442 expectVisible(test, child);
466 expectHidden(test, parent); 443 expectHidden(test, parent);
467 }).catch(unexpectedError.bind(test)).then(() => test.done()); 444 }).catch(unexpectedError.bind(test)).then(() => test.done());
468 }; 445 };
OLDNEW
« no previous file with comments | « test/browser/_bootstrap.js ('k') | test_runner.js » ('j') | test_runner.js » ('J')

Powered by Google App Engine
This is Rietveld