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

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

Issue 29984555: Issue 6959 - Use async/await in test/browser/elemHideEmulation.js (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Pass test object to unexpectedError() Created Jan. 17, 2019, 7:41 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 | « no previous file | no next file » | 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-present eyeo GmbH 3 * Copyright (C) 2006-present 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
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 }; 45 };
46 46
47 function timeout(delay) 47 function timeout(delay)
48 { 48 {
49 return new Promise((resolve, reject) => 49 return new Promise((resolve, reject) =>
50 { 50 {
51 window.setTimeout(resolve, delay); 51 window.setTimeout(resolve, delay);
52 }); 52 });
53 } 53 }
54 54
55 function unexpectedError(error) 55 function unexpectedError(test, error)
56 { 56 {
57 console.error(error); 57 console.error(error);
58 this.ok(false, "Unexpected error: " + error); 58 test.ok(false, "Unexpected error: " + error);
59 } 59 }
60 60
61 function expectHidden(test, element, id) 61 function expectHidden(test, element, id)
62 { 62 {
63 let withId = ""; 63 let withId = "";
64 if (typeof id != "undefined") 64 if (typeof id != "undefined")
65 withId = ` with ID '${id}'`; 65 withId = ` with ID '${id}'`;
66 66
67 test.equal( 67 test.equal(
68 window.getComputedStyle(element).display, "none", 68 window.getComputedStyle(element).display, "none",
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 // Insert a <div> with a unique id and a CSS rule 141 // Insert a <div> with a unique id and a CSS rule
142 // for the the selector matching the id. 142 // for the the selector matching the id.
143 function createElementWithStyle(styleBlock, parent) 143 function createElementWithStyle(styleBlock, parent)
144 { 144 {
145 let element = createElement(parent); 145 let element = createElement(parent);
146 insertStyleRule("#" + element.id + " " + styleBlock); 146 insertStyleRule("#" + element.id + " " + styleBlock);
147 return element; 147 return element;
148 } 148 }
149 149
150 // Create a new ElemHideEmulation instance with @selectors. 150 // Create a new ElemHideEmulation instance with @selectors.
151 function applyElemHideEmulation(selectors) 151 async function applyElemHideEmulation(test, selectors)
152 { 152 {
153 return Promise.resolve().then(() => 153 await Promise.resolve();
154 { 154
155 let elemHideEmulation = new ElemHideEmulation( 155 let elemHideEmulation = null;
156
157 try
158 {
159 elemHideEmulation = new ElemHideEmulation(
156 elems => 160 elems =>
157 { 161 {
158 for (let elem of elems) 162 for (let elem of elems)
159 elem.style.display = "none"; 163 elem.style.display = "none";
160 } 164 }
161 ); 165 );
162 166
163 elemHideEmulation.document = testDocument; 167 elemHideEmulation.document = testDocument;
164 elemHideEmulation.MIN_INVOCATION_INTERVAL = REFRESH_INTERVAL / 2; 168 elemHideEmulation.MIN_INVOCATION_INTERVAL = REFRESH_INTERVAL / 2;
165 elemHideEmulation.apply(selectors.map( 169 elemHideEmulation.apply(selectors.map(
166 selector => ({selector, text: selector}) 170 selector => ({selector, text: selector})
167 )); 171 ));
172
168 return elemHideEmulation; 173 return elemHideEmulation;
169 }); 174 }
175 catch (error)
176 {
177 unexpectedError(test, error);
178 }
170 } 179 }
171 180
172 exports.testVerbatimPropertySelector = function(test) 181 exports.testVerbatimPropertySelector = async function(test)
173 { 182 {
174 let toHide = createElementWithStyle("{background-color: #000}"); 183 let toHide = createElementWithStyle("{background-color: #000}");
175 applyElemHideEmulation( 184
185 if (await applyElemHideEmulation(
186 test,
176 [":-abp-properties(background-color: rgb(0, 0, 0))"] 187 [":-abp-properties(background-color: rgb(0, 0, 0))"]
177 ).then(() => 188 ))
178 { 189 {
179 expectHidden(test, toHide); 190 expectHidden(test, toHide);
180 }).catch(unexpectedError.bind(test)).then(() => test.done()); 191 }
181 }; 192
182 193 test.done();
183 exports.testVerbatimPropertySelectorWithPrefix = function(test) 194 };
195
196 exports.testVerbatimPropertySelectorWithPrefix = async function(test)
184 { 197 {
185 let parent = createElementWithStyle("{background-color: #000}"); 198 let parent = createElementWithStyle("{background-color: #000}");
186 let toHide = createElementWithStyle("{background-color: #000}", parent); 199 let toHide = createElementWithStyle("{background-color: #000}", parent);
187 applyElemHideEmulation( 200
201 if (await applyElemHideEmulation(
202 test,
188 ["div > :-abp-properties(background-color: rgb(0, 0, 0))"] 203 ["div > :-abp-properties(background-color: rgb(0, 0, 0))"]
189 ).then(() => 204 ))
190 { 205 {
191 expectVisible(test, parent); 206 expectVisible(test, parent);
192 expectHidden(test, toHide); 207 expectHidden(test, toHide);
193 }).catch(unexpectedError.bind(test)).then(() => test.done()); 208 }
194 }; 209
195 210 test.done();
196 exports.testVerbatimPropertySelectorWithPrefixNoMatch = function(test) 211 };
212
213 exports.testVerbatimPropertySelectorWithPrefixNoMatch = async function(test)
197 { 214 {
198 let parent = createElementWithStyle("{background-color: #000}"); 215 let parent = createElementWithStyle("{background-color: #000}");
199 let toHide = createElementWithStyle("{background-color: #fff}", parent); 216 let toHide = createElementWithStyle("{background-color: #fff}", parent);
200 applyElemHideEmulation( 217
218 if (await applyElemHideEmulation(
219 test,
201 ["div > :-abp-properties(background-color: rgb(0, 0, 0))"] 220 ["div > :-abp-properties(background-color: rgb(0, 0, 0))"]
202 ).then(() => 221 ))
203 { 222 {
204 expectVisible(test, parent); 223 expectVisible(test, parent);
205 expectVisible(test, toHide); 224 expectVisible(test, toHide);
206 }).catch(unexpectedError.bind(test)).then(() => test.done()); 225 }
207 }; 226
208 227 test.done();
209 exports.testVerbatimPropertySelectorWithSuffix = function(test) 228 };
229
230 exports.testVerbatimPropertySelectorWithSuffix = async function(test)
210 { 231 {
211 let parent = createElementWithStyle("{background-color: #000}"); 232 let parent = createElementWithStyle("{background-color: #000}");
212 let toHide = createElementWithStyle("{background-color: #000}", parent); 233 let toHide = createElementWithStyle("{background-color: #000}", parent);
213 applyElemHideEmulation( 234
235 if (await applyElemHideEmulation(
236 test,
214 [":-abp-properties(background-color: rgb(0, 0, 0)) > div"] 237 [":-abp-properties(background-color: rgb(0, 0, 0)) > div"]
215 ).then(() => 238 ))
216 { 239 {
217 expectVisible(test, parent); 240 expectVisible(test, parent);
218 expectHidden(test, toHide); 241 expectHidden(test, toHide);
219 }).catch(unexpectedError.bind(test)).then(() => test.done()); 242 }
220 }; 243
221 244 test.done();
222 exports.testVerbatimPropertyPseudoSelectorWithPrefixAndSuffix = function(test) 245 };
246
247 exports.testVerbatimPropertyPseudoSelectorWithPrefixAndSuffix = async function(t est)
223 { 248 {
224 let parent = createElementWithStyle("{background-color: #000}"); 249 let parent = createElementWithStyle("{background-color: #000}");
225 let middle = createElementWithStyle("{background-color: #000}", parent); 250 let middle = createElementWithStyle("{background-color: #000}", parent);
226 let toHide = createElementWithStyle("{background-color: #000}", middle); 251 let toHide = createElementWithStyle("{background-color: #000}", middle);
227 applyElemHideEmulation( 252
253 if (await applyElemHideEmulation(
254 test,
228 ["div > :-abp-properties(background-color: rgb(0, 0, 0)) > div"] 255 ["div > :-abp-properties(background-color: rgb(0, 0, 0)) > div"]
229 ).then(() => 256 ))
230 { 257 {
231 expectVisible(test, parent); 258 expectVisible(test, parent);
232 expectVisible(test, middle); 259 expectVisible(test, middle);
233 expectHidden(test, toHide); 260 expectHidden(test, toHide);
234 }).catch(unexpectedError.bind(test)).then(() => test.done()); 261 }
262
263 test.done();
235 }; 264 };
236 265
237 // Add the style. Then add the element for that style. 266 // Add the style. Then add the element for that style.
238 // This should retrigger the filtering and hide it. 267 // This should retrigger the filtering and hide it.
239 exports.testPropertyPseudoSelectorAddStyleAndElement = function(test) 268 exports.testPropertyPseudoSelectorAddStyleAndElement = async function(test)
240 { 269 {
241 let styleElement; 270 let styleElement;
242 let toHide; 271 let toHide;
243 applyElemHideEmulation( 272
273 if (await applyElemHideEmulation(
274 test,
244 [":-abp-properties(background-color: rgb(0, 0, 0))"] 275 [":-abp-properties(background-color: rgb(0, 0, 0))"]
245 ).then(() => 276 ))
246 { 277 {
247 styleElement = testDocument.createElement("style"); 278 styleElement = testDocument.createElement("style");
248 testDocument.head.appendChild(styleElement); 279 testDocument.head.appendChild(styleElement);
249 styleElement.sheet.insertRule("#toHide {background-color: #000}"); 280 styleElement.sheet.insertRule("#toHide {background-color: #000}");
250 return timeout(REFRESH_INTERVAL); 281 await timeout(REFRESH_INTERVAL);
251 }).then(() => 282
252 {
253 toHide = createElement(); 283 toHide = createElement();
254 toHide.id = "toHide"; 284 toHide.id = "toHide";
255 expectVisible(test, toHide); 285 expectVisible(test, toHide);
256 return timeout(REFRESH_INTERVAL); 286 await timeout(REFRESH_INTERVAL);
257 }).then(() => 287
258 { 288 expectHidden(test, toHide);
259 expectHidden(test, toHide); 289 }
260 }).catch(unexpectedError.bind(test)).then(() => test.done()); 290
261 }; 291 test.done();
262 292 };
263 exports.testPropertySelectorWithWildcard = function(test) 293
264 { 294 exports.testPropertySelectorWithWildcard = async function(test)
265 let toHide = createElementWithStyle("{background-color: #000}"); 295 {
266 applyElemHideEmulation( 296 let toHide = createElementWithStyle("{background-color: #000}");
297
298 if (await applyElemHideEmulation(
299 test,
267 [":-abp-properties(*color: rgb(0, 0, 0))"] 300 [":-abp-properties(*color: rgb(0, 0, 0))"]
268 ).then(() => 301 ))
269 { 302 {
270 expectHidden(test, toHide); 303 expectHidden(test, toHide);
271 }).catch(unexpectedError.bind(test)).then(() => test.done()); 304 }
272 }; 305
273 306 test.done();
274 exports.testPropertySelectorWithRegularExpression = function(test) 307 };
275 { 308
276 let toHide = createElementWithStyle("{background-color: #000}"); 309 exports.testPropertySelectorWithRegularExpression = async function(test)
277 applyElemHideEmulation( 310 {
311 let toHide = createElementWithStyle("{background-color: #000}");
312
313 if (await applyElemHideEmulation(
314 test,
278 [":-abp-properties(/.*color: rgb\\(0, 0, 0\\)/)"] 315 [":-abp-properties(/.*color: rgb\\(0, 0, 0\\)/)"]
279 ).then(() => 316 ))
280 { 317 {
281 expectHidden(test, toHide); 318 expectHidden(test, toHide);
282 }).catch(unexpectedError.bind(test)).then(() => test.done()); 319 }
283 }; 320
284 321 test.done();
285 exports.testPropertySelectorWithEscapedBrace = function(test) 322 };
286 { 323
287 let toHide = createElementWithStyle("{background-color: #000}"); 324 exports.testPropertySelectorWithEscapedBrace = async function(test)
288 applyElemHideEmulation( 325 {
326 let toHide = createElementWithStyle("{background-color: #000}");
327
328 if (await applyElemHideEmulation(
329 test,
289 [":-abp-properties(/background.\\7B 0,6\\7D : rgb\\(0, 0, 0\\)/)"] 330 [":-abp-properties(/background.\\7B 0,6\\7D : rgb\\(0, 0, 0\\)/)"]
290 ).then(() => 331 ))
291 { 332 {
292 expectHidden(test, toHide); 333 expectHidden(test, toHide);
293 }).catch(unexpectedError.bind(test)).then(() => test.done()); 334 }
294 }; 335
295 336 test.done();
296 exports.testPropertySelectorWithImproperlyEscapedBrace = function(test) 337 };
297 { 338
298 let toHide = createElementWithStyle("{background-color: #000}"); 339 exports.testPropertySelectorWithImproperlyEscapedBrace = async function(test)
299 applyElemHideEmulation( 340 {
341 let toHide = createElementWithStyle("{background-color: #000}");
342
343 if (await applyElemHideEmulation(
344 test,
300 [":-abp-properties(/background.\\7B0,6\\7D: rgb\\(0, 0, 0\\)/)"] 345 [":-abp-properties(/background.\\7B0,6\\7D: rgb\\(0, 0, 0\\)/)"]
301 ).then(() => 346 ))
302 { 347 {
303 expectVisible(test, toHide); 348 expectVisible(test, toHide);
304 }).catch(unexpectedError.bind(test)).then(() => test.done()); 349 }
305 }; 350
306 351 test.done();
307 exports.testDynamicallyChangedProperty = function(test) 352 };
353
354 exports.testDynamicallyChangedProperty = async function(test)
308 { 355 {
309 let toHide = createElementWithStyle("{}"); 356 let toHide = createElementWithStyle("{}");
310 applyElemHideEmulation( 357
358 if (await applyElemHideEmulation(
359 test,
311 [":-abp-properties(background-color: rgb(0, 0, 0))"] 360 [":-abp-properties(background-color: rgb(0, 0, 0))"]
312 ).then(() => 361 ))
313 { 362 {
314 expectVisible(test, toHide); 363 expectVisible(test, toHide);
315 insertStyleRule("#" + toHide.id + " {background-color: #000}"); 364 insertStyleRule("#" + toHide.id + " {background-color: #000}");
316 365
317 return timeout(0); 366 await timeout(0);
318 }).then(() => 367
319 {
320 // Re-evaluation will only happen after a delay 368 // Re-evaluation will only happen after a delay
321 expectVisible(test, toHide); 369 expectVisible(test, toHide);
322 return timeout(REFRESH_INTERVAL); 370 await timeout(REFRESH_INTERVAL);
323 }).then(() => 371
324 { 372 expectHidden(test, toHide);
325 expectHidden(test, toHide); 373 }
326 }).catch(unexpectedError.bind(test)).then(() => test.done()); 374
327 }; 375 test.done();
328 376 };
329 exports.testPseudoClassWithPropBeforeSelector = function(test) 377
378 exports.testPseudoClassWithPropBeforeSelector = async function(test)
330 { 379 {
331 let parent = createElementWithStyle("{}"); 380 let parent = createElementWithStyle("{}");
332 let child = createElementWithStyle("{background-color: #000}", parent); 381 let child = createElementWithStyle("{background-color: #000}", parent);
333 insertStyleRule(`#${child.id}::before {content: "publicite"}`); 382 insertStyleRule(`#${child.id}::before {content: "publicite"}`);
334 383
335 applyElemHideEmulation( 384 if (await applyElemHideEmulation(
385 test,
336 ["div:-abp-properties(content: \"publicite\")"] 386 ["div:-abp-properties(content: \"publicite\")"]
337 ).then(() => 387 ))
338 { 388 {
339 expectHidden(test, child); 389 expectHidden(test, child);
340 expectVisible(test, parent); 390 expectVisible(test, parent);
341 }).catch(unexpectedError.bind(test)).then(() => test.done()); 391 }
342 }; 392
343 393 test.done();
344 exports.testPseudoClassHasSelector = function(test) 394 };
395
396 exports.testPseudoClassHasSelector = async function(test)
345 { 397 {
346 let toHide = createElementWithStyle("{}"); 398 let toHide = createElementWithStyle("{}");
347 applyElemHideEmulation( 399
348 ["div:-abp-has(div)"] 400 if (await applyElemHideEmulation(test, ["div:-abp-has(div)"]))
349 ).then(() => 401 expectVisible(test, toHide);
350 { 402
351 expectVisible(test, toHide); 403 test.done();
352 }).catch(unexpectedError.bind(test)).then(() => test.done()); 404 };
353 }; 405
354 406 exports.testPseudoClassHasSelectorWithPrefix = async function(test)
355 exports.testPseudoClassHasSelectorWithPrefix = function(test)
356 { 407 {
357 let parent = createElementWithStyle("{}"); 408 let parent = createElementWithStyle("{}");
358 let child = createElementWithStyle("{}", parent); 409 let child = createElementWithStyle("{}", parent);
359 applyElemHideEmulation( 410
360 ["div:-abp-has(div)"] 411 if (await applyElemHideEmulation(test, ["div:-abp-has(div)"]))
361 ).then(() =>
362 { 412 {
363 expectHidden(test, parent); 413 expectHidden(test, parent);
364 expectVisible(test, child); 414 expectVisible(test, child);
365 }).catch(unexpectedError.bind(test)).then(() => test.done()); 415 }
366 }; 416
367 417 test.done();
368 exports.testPseudoClassHasSelectorWithSuffix = function(test) 418 };
419
420 exports.testPseudoClassHasSelectorWithSuffix = async function(test)
369 { 421 {
370 let parent = createElementWithStyle("{}"); 422 let parent = createElementWithStyle("{}");
371 let middle = createElementWithStyle("{}", parent); 423 let middle = createElementWithStyle("{}", parent);
372 let child = createElementWithStyle("{}", middle); 424 let child = createElementWithStyle("{}", middle);
373 applyElemHideEmulation( 425
374 ["div:-abp-has(div) > div"] 426 if (await applyElemHideEmulation(test, ["div:-abp-has(div) > div"]))
375 ).then(() =>
376 { 427 {
377 expectVisible(test, parent); 428 expectVisible(test, parent);
378 expectHidden(test, middle); 429 expectHidden(test, middle);
379 expectHidden(test, child); 430 expectHidden(test, child);
380 }).catch(unexpectedError.bind(test)).then(() => test.done()); 431 }
381 }; 432
382 433 test.done();
383 exports.testPseudoClassHasSelectorWithSuffixSibling = function(test) 434 };
435
436 exports.testPseudoClassHasSelectorWithSuffixSibling = async function(test)
384 { 437 {
385 let parent = createElementWithStyle("{}"); 438 let parent = createElementWithStyle("{}");
386 let middle = createElementWithStyle("{}", parent); 439 let middle = createElementWithStyle("{}", parent);
387 let toHide = createElementWithStyle("{}"); 440 let toHide = createElementWithStyle("{}");
388 applyElemHideEmulation( 441
389 ["div:-abp-has(div) + div"] 442 if (await applyElemHideEmulation(test, ["div:-abp-has(div) + div"]))
390 ).then(() =>
391 { 443 {
392 expectVisible(test, parent); 444 expectVisible(test, parent);
393 expectVisible(test, middle); 445 expectVisible(test, middle);
394 expectHidden(test, toHide); 446 expectHidden(test, toHide);
395 }).catch(unexpectedError.bind(test)).then(() => test.done()); 447 }
396 }; 448
397 449 test.done();
398 exports.testPseudoClassHasSelectorWithSuffixSiblingChild = function(test) 450 };
451
452 exports.testPseudoClassHasSelectorWithSuffixSiblingChild = async function(test)
399 { 453 {
400 // <div> 454 // <div>
401 // <div></div> 455 // <div></div>
402 // <div> 456 // <div>
403 // <div>to hide</div> 457 // <div>to hide</div>
404 // </div> 458 // </div>
405 // </div> 459 // </div>
406 let parent = createElementWithStyle("{}"); 460 let parent = createElementWithStyle("{}");
407 let middle = createElementWithStyle("{}", parent); 461 let middle = createElementWithStyle("{}", parent);
408 let sibling = createElementWithStyle("{}"); 462 let sibling = createElementWithStyle("{}");
409 let toHide = createElementWithStyle("{}", sibling); 463 let toHide = createElementWithStyle("{}", sibling);
410 applyElemHideEmulation( 464
411 ["div:-abp-has(div) + div > div"] 465 if (await applyElemHideEmulation(test, ["div:-abp-has(div) + div > div"]))
412 ).then(() =>
413 { 466 {
414 expectVisible(test, parent); 467 expectVisible(test, parent);
415 expectVisible(test, middle); 468 expectVisible(test, middle);
416 expectVisible(test, sibling); 469 expectVisible(test, sibling);
417 expectHidden(test, toHide); 470 expectHidden(test, toHide);
418 }).catch(unexpectedError.bind(test)).then(() => test.done()); 471 }
472
473 test.done();
419 }; 474 };
420 475
421 function compareExpectations(test, elems, expectations) 476 function compareExpectations(test, elems, expectations)
422 { 477 {
423 for (let elem in expectations) 478 for (let elem in expectations)
424 { 479 {
425 if (elems[elem]) 480 if (elems[elem])
426 { 481 {
427 if (expectations[elem]) 482 if (expectations[elem])
428 expectVisible(test, elems[elem], elem); 483 expectVisible(test, elems[elem], elem);
429 else 484 else
430 expectHidden(test, elems[elem], elem); 485 expectHidden(test, elems[elem], elem);
431 } 486 }
432 } 487 }
433 } 488 }
434 489
435 function runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(test, selector , expectations) 490 async function runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(test, se lector, expectations)
436 { 491 {
437 testDocument.body.innerHTML = `<div id="parent"> 492 testDocument.body.innerHTML = `<div id="parent">
438 <div id="middle"> 493 <div id="middle">
439 <div id="middle1"><div id="inside" class="inside"></div></div> 494 <div id="middle1"><div id="inside" class="inside"></div></div>
440 </div> 495 </div>
441 <div id="sibling"> 496 <div id="sibling">
442 <div id="tohide"><span>to hide</span></div> 497 <div id="tohide"><span>to hide</span></div>
443 </div> 498 </div>
444 <div id="sibling2"> 499 <div id="sibling2">
445 <div id="sibling21"><div id="sibling211" class="inside"></div></div> 500 <div id="sibling21"><div id="sibling211" class="inside"></div></div>
446 </div> 501 </div>
447 </div>`; 502 </div>`;
448 let elems = { 503 let elems = {
449 parent: testDocument.getElementById("parent"), 504 parent: testDocument.getElementById("parent"),
450 middle: testDocument.getElementById("middle"), 505 middle: testDocument.getElementById("middle"),
451 inside: testDocument.getElementById("inside"), 506 inside: testDocument.getElementById("inside"),
452 sibling: testDocument.getElementById("sibling"), 507 sibling: testDocument.getElementById("sibling"),
453 sibling2: testDocument.getElementById("sibling2"), 508 sibling2: testDocument.getElementById("sibling2"),
454 toHide: testDocument.getElementById("tohide") 509 toHide: testDocument.getElementById("tohide")
455 }; 510 };
456 511
457 insertStyleRule(".inside {}"); 512 insertStyleRule(".inside {}");
458 513
459 applyElemHideEmulation( 514 if (await applyElemHideEmulation(test, [selector]))
460 [selector]
461 ).then(() =>
462 {
463 compareExpectations(test, elems, expectations); 515 compareExpectations(test, elems, expectations);
464 }).catch(unexpectedError.bind(test)).then(() => test.done()); 516
517 test.done();
465 } 518 }
466 519
467 exports.testPseudoClassHasSelectorWithHasAndWithSuffixSibling = function(test) 520 exports.testPseudoClassHasSelectorWithHasAndWithSuffixSibling = async function(t est)
468 { 521 {
469 let expectations = { 522 let expectations = {
470 parent: true, 523 parent: true,
471 middile: true, 524 middile: true,
472 inside: true, 525 inside: true,
473 sibling: true, 526 sibling: true,
474 sibling2: true, 527 sibling2: true,
475 toHide: false 528 toHide: false
476 }; 529 };
477 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling( 530 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(
478 test, "div:-abp-has(:-abp-has(div.inside)) + div > div", expectations); 531 test, "div:-abp-has(:-abp-has(div.inside)) + div > div", expectations);
479 }; 532 };
480 533
481 exports.testPseudoClassHasSelectorWithHasAndWithSuffixSibling2 = function(test) 534 exports.testPseudoClassHasSelectorWithHasAndWithSuffixSibling2 = async function( test)
482 { 535 {
483 let expectations = { 536 let expectations = {
484 parent: true, 537 parent: true,
485 middile: true, 538 middile: true,
486 inside: true, 539 inside: true,
487 sibling: true, 540 sibling: true,
488 sibling2: true, 541 sibling2: true,
489 toHide: false 542 toHide: false
490 }; 543 };
491 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling( 544 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(
492 test, "div:-abp-has(:-abp-has(> div.inside)) + div > div", expectations); 545 test, "div:-abp-has(:-abp-has(> div.inside)) + div > div", expectations);
493 }; 546 };
494 547
495 exports.testPseudoClassHasSelectorWithHasAndWithSuffixSibling3 = function(test) 548 exports.testPseudoClassHasSelectorWithHasAndWithSuffixSibling3 = async function( test)
496 { 549 {
497 let expectations = { 550 let expectations = {
498 parent: true, 551 parent: true,
499 middile: true, 552 middile: true,
500 inside: true, 553 inside: true,
501 sibling: true, 554 sibling: true,
502 sibling2: true, 555 sibling2: true,
503 toHide: false 556 toHide: false
504 }; 557 };
505 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling( 558 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(
506 test, "div:-abp-has(> div:-abp-has(div.inside)) + div > div", expectations); 559 test, "div:-abp-has(> div:-abp-has(div.inside)) + div > div", expectations);
507 }; 560 };
508 561
509 exports.testPseudoClassHasSelectorWithSuffixSiblingNoop = function(test) 562 exports.testPseudoClassHasSelectorWithSuffixSiblingNoop = async function(test)
510 { 563 {
511 let expectations = { 564 let expectations = {
512 parent: true, 565 parent: true,
513 middile: true, 566 middile: true,
514 inside: true, 567 inside: true,
515 sibling: true, 568 sibling: true,
516 sibling2: true, 569 sibling2: true,
517 toHide: true 570 toHide: true
518 }; 571 };
519 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling( 572 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(
520 test, "div:-abp-has(> body div.inside) + div > div", expectations); 573 test, "div:-abp-has(> body div.inside) + div > div", expectations);
521 }; 574 };
522 575
523 exports.testPseudoClassHasSelectorWithSuffixSiblingContains = function(test) 576 exports.testPseudoClassHasSelectorWithSuffixSiblingContains = async function(tes t)
524 { 577 {
525 let expectations = { 578 let expectations = {
526 parent: true, 579 parent: true,
527 middile: true, 580 middile: true,
528 inside: true, 581 inside: true,
529 sibling: true, 582 sibling: true,
530 sibling2: true, 583 sibling2: true,
531 toHide: true 584 toHide: true
532 }; 585 };
533 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling( 586 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(
534 test, "div:-abp-has(> span:-abp-contains(Advertisment))", expectations); 587 test, "div:-abp-has(> span:-abp-contains(Advertisment))", expectations);
535 }; 588 };
536 589
537 function runTestPseudoClassContains(test, selector, expectations) 590 async function runTestPseudoClassContains(test, selector, expectations)
538 { 591 {
539 testDocument.body.innerHTML = `<div id="parent"> 592 testDocument.body.innerHTML = `<div id="parent">
540 <div id="middle"> 593 <div id="middle">
541 <div id="middle1"><div id="inside" class="inside"></div></div> 594 <div id="middle1"><div id="inside" class="inside"></div></div>
542 </div> 595 </div>
543 <div id="sibling"> 596 <div id="sibling">
544 <div id="tohide">to hide \ud83d\ude42!</div> 597 <div id="tohide">to hide \ud83d\ude42!</div>
545 </div> 598 </div>
546 <div id="sibling2"> 599 <div id="sibling2">
547 <div id="sibling21"><div id="sibling211" class="inside">Ad*</div></div> 600 <div id="sibling21"><div id="sibling211" class="inside">Ad*</div></div>
548 </div> 601 </div>
549 </div>`; 602 </div>`;
550 let elems = { 603 let elems = {
551 parent: testDocument.getElementById("parent"), 604 parent: testDocument.getElementById("parent"),
552 middle: testDocument.getElementById("middle"), 605 middle: testDocument.getElementById("middle"),
553 inside: testDocument.getElementById("inside"), 606 inside: testDocument.getElementById("inside"),
554 sibling: testDocument.getElementById("sibling"), 607 sibling: testDocument.getElementById("sibling"),
555 sibling2: testDocument.getElementById("sibling2"), 608 sibling2: testDocument.getElementById("sibling2"),
556 toHide: testDocument.getElementById("tohide") 609 toHide: testDocument.getElementById("tohide")
557 }; 610 };
558 611
559 applyElemHideEmulation( 612 if (await applyElemHideEmulation(test, [selector]))
560 [selector] 613 compareExpectations(test, elems, expectations);
561 ).then( 614
562 () => compareExpectations(test, elems, expectations) 615 test.done();
563 ).catch(unexpectedError.bind(test)).then(() => test.done());
564 } 616 }
565 617
566 exports.testPseudoClassContainsText = function(test) 618 exports.testPseudoClassContainsText = async function(test)
567 { 619 {
568 let expectations = { 620 let expectations = {
569 parent: true, 621 parent: true,
570 middle: true, 622 middle: true,
571 inside: true, 623 inside: true,
572 sibling: false, 624 sibling: false,
573 sibling2: true, 625 sibling2: true,
574 toHide: true 626 toHide: true
575 }; 627 };
576 runTestPseudoClassContains( 628 runTestPseudoClassContains(
577 test, "#parent div:-abp-contains(to hide)", expectations); 629 test, "#parent div:-abp-contains(to hide)", expectations);
578 }; 630 };
579 631
580 exports.testPseudoClassContainsRegexp = function(test) 632 exports.testPseudoClassContainsRegexp = async function(test)
581 { 633 {
582 let expectations = { 634 let expectations = {
583 parent: true, 635 parent: true,
584 middle: true, 636 middle: true,
585 inside: true, 637 inside: true,
586 sibling: false, 638 sibling: false,
587 sibling2: true, 639 sibling2: true,
588 toHide: true 640 toHide: true
589 }; 641 };
590 runTestPseudoClassContains( 642 runTestPseudoClassContains(
591 test, "#parent div:-abp-contains(/to\\shide/)", expectations); 643 test, "#parent div:-abp-contains(/to\\shide/)", expectations);
592 }; 644 };
593 645
594 exports.testPseudoClassContainsRegexpIFlag = function(test) 646 exports.testPseudoClassContainsRegexpIFlag = async function(test)
595 { 647 {
596 let expectations = { 648 let expectations = {
597 parent: true, 649 parent: true,
598 middle: true, 650 middle: true,
599 inside: true, 651 inside: true,
600 sibling: false, 652 sibling: false,
601 sibling2: true, 653 sibling2: true,
602 toHide: true 654 toHide: true
603 }; 655 };
604 runTestPseudoClassContains( 656 runTestPseudoClassContains(
605 test, "#parent div:-abp-contains(/to\\sHide/i)", expectations); 657 test, "#parent div:-abp-contains(/to\\sHide/i)", expectations);
606 }; 658 };
607 659
608 exports.testPseudoClassContainsRegexpUFlag = function(test) 660 exports.testPseudoClassContainsRegexpUFlag = async function(test)
609 { 661 {
610 let expectations = { 662 let expectations = {
611 parent: true, 663 parent: true,
612 middle: true, 664 middle: true,
613 inside: true, 665 inside: true,
614 sibling: false, 666 sibling: false,
615 sibling2: true, 667 sibling2: true,
616 toHide: true 668 toHide: true
617 }; 669 };
618 runTestPseudoClassContains( 670 runTestPseudoClassContains(
619 test, "#parent div:-abp-contains(/to\\shide\\s.!/u)", expectations); 671 test, "#parent div:-abp-contains(/to\\shide\\s.!/u)", expectations);
620 }; 672 };
621 673
622 exports.testPseudoClassContainsWildcardNoMatch = function(test) 674 exports.testPseudoClassContainsWildcardNoMatch = async function(test)
623 { 675 {
624 let expectations = { 676 let expectations = {
625 parent: true, 677 parent: true,
626 middle: true, 678 middle: true,
627 inside: true, 679 inside: true,
628 sibling: true, 680 sibling: true,
629 sibling2: true, 681 sibling2: true,
630 toHide: true 682 toHide: true
631 }; 683 };
632 // this filter shouldn't match anything as "*" has no meaning. 684 // this filter shouldn't match anything as "*" has no meaning.
633 runTestPseudoClassContains( 685 runTestPseudoClassContains(
634 test, "#parent div:-abp-contains(to *hide)", expectations); 686 test, "#parent div:-abp-contains(to *hide)", expectations);
635 }; 687 };
636 688
637 exports.testPseudoClassContainsWildcardMatch = function(test) 689 exports.testPseudoClassContainsWildcardMatch = async function(test)
638 { 690 {
639 let expectations = { 691 let expectations = {
640 parent: true, 692 parent: true,
641 middle: true, 693 middle: true,
642 inside: true, 694 inside: true,
643 sibling: true, 695 sibling: true,
644 sibling2: false, 696 sibling2: false,
645 toHide: true 697 toHide: true
646 }; 698 };
647 runTestPseudoClassContains( 699 runTestPseudoClassContains(
648 test, "#parent div:-abp-contains(Ad*)", expectations); 700 test, "#parent div:-abp-contains(Ad*)", expectations);
649 }; 701 };
650 702
651 exports.testPseudoClassHasSelectorWithPropSelector = function(test) 703 exports.testPseudoClassHasSelectorWithPropSelector = async function(test)
652 { 704 {
653 let parent = createElementWithStyle("{}"); 705 let parent = createElementWithStyle("{}");
654 let child = createElementWithStyle("{background-color: #000}", parent); 706 let child = createElementWithStyle("{background-color: #000}", parent);
655 applyElemHideEmulation( 707
708 if (await applyElemHideEmulation(
709 test,
656 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"] 710 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"]
657 ).then(() => 711 ))
658 { 712 {
659 expectVisible(test, child); 713 expectVisible(test, child);
660 expectHidden(test, parent); 714 expectHidden(test, parent);
661 }).catch(unexpectedError.bind(test)).then(() => test.done()); 715 }
662 }; 716
663 717 test.done();
664 exports.testPseudoClassHasSelectorWithPropSelector2 = function(test) 718 };
719
720 exports.testPseudoClassHasSelectorWithPropSelector2 = async function(test)
665 { 721 {
666 let parent = createElementWithStyle("{}"); 722 let parent = createElementWithStyle("{}");
667 let child = createElementWithStyle("{}", parent); 723 let child = createElementWithStyle("{}", parent);
668 insertStyleRule("body #" + parent.id + " > div { background-color: #000}"); 724 insertStyleRule("body #" + parent.id + " > div { background-color: #000}");
669 applyElemHideEmulation( 725
726 if (await applyElemHideEmulation(
727 test,
670 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"] 728 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"]
671 ).then(() => 729 ))
672 { 730 {
673 expectVisible(test, child); 731 expectVisible(test, child);
674 expectHidden(test, parent); 732 expectHidden(test, parent);
675 }).catch(unexpectedError.bind(test)).then(() => test.done()); 733 }
676 }; 734
677 735 test.done();
678 exports.testDomUpdatesStyle = function(test) 736 };
737
738 exports.testDomUpdatesStyle = async function(test)
679 { 739 {
680 let parent = createElementWithStyle("{}"); 740 let parent = createElementWithStyle("{}");
681 let child = createElementWithStyle("{}", parent); 741 let child = createElementWithStyle("{}", parent);
682 applyElemHideEmulation( 742
743 if (await applyElemHideEmulation(
744 test,
683 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"] 745 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"]
684 ).then(() => 746 ))
685 { 747 {
686 expectVisible(test, child); 748 expectVisible(test, child);
687 expectVisible(test, parent); 749 expectVisible(test, parent);
688 750
689 insertStyleRule("body #" + parent.id + " > div { background-color: #000}"); 751 insertStyleRule("body #" + parent.id + " > div { background-color: #000}");
690 return timeout(0); 752 await timeout(0);
691 }).then(() => 753
692 { 754 expectVisible(test, child);
693 expectVisible(test, child); 755 expectVisible(test, parent);
694 expectVisible(test, parent); 756 await timeout(REFRESH_INTERVAL);
695 return timeout(REFRESH_INTERVAL); 757
696 }).then(() => 758 expectVisible(test, child);
697 { 759 expectHidden(test, parent);
698 expectVisible(test, child); 760 }
699 expectHidden(test, parent); 761
700 }).catch(unexpectedError.bind(test)).then(() => test.done()); 762 test.done();
701 }; 763 };
702 764
703 exports.testDomUpdatesContent = function(test) 765 exports.testDomUpdatesContent = async function(test)
704 { 766 {
705 let parent = createElementWithStyle("{}"); 767 let parent = createElementWithStyle("{}");
706 let child = createElementWithStyle("{}", parent); 768 let child = createElementWithStyle("{}", parent);
707 applyElemHideEmulation( 769
770 if (await applyElemHideEmulation(
771 test,
708 ["div > div:-abp-contains(hide me)"] 772 ["div > div:-abp-contains(hide me)"]
709 ).then(() => 773 ))
710 { 774 {
711 expectVisible(test, parent); 775 expectVisible(test, parent);
712 expectVisible(test, child); 776 expectVisible(test, child);
713 777
714 child.textContent = "hide me"; 778 child.textContent = "hide me";
715 return timeout(0); 779 await timeout(0);
716 }).then(() => 780
717 { 781 expectVisible(test, parent);
718 expectVisible(test, parent); 782 expectVisible(test, child);
719 expectVisible(test, child); 783 await timeout(REFRESH_INTERVAL);
720 return timeout(REFRESH_INTERVAL); 784
721 }).then(() =>
722 {
723 expectVisible(test, parent); 785 expectVisible(test, parent);
724 expectHidden(test, child); 786 expectHidden(test, child);
725 }).catch(unexpectedError.bind(test)).then(() => test.done()); 787 }
726 }; 788
727 789 test.done();
728 exports.testDomUpdatesNewElement = function(test) 790 };
791
792 exports.testDomUpdatesNewElement = async function(test)
729 { 793 {
730 let parent = createElementWithStyle("{}"); 794 let parent = createElementWithStyle("{}");
731 let child = createElementWithStyle("{ background-color: #000}", parent); 795 let child = createElementWithStyle("{ background-color: #000}", parent);
732 let sibling; 796 let sibling;
733 let child2; 797 let child2;
734 applyElemHideEmulation( 798
799 if (await applyElemHideEmulation(
800 test,
735 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"] 801 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"]
736 ).then(() => 802 ))
737 { 803 {
738 expectHidden(test, parent); 804 expectHidden(test, parent);
739 expectVisible(test, child); 805 expectVisible(test, child);
740 806
741 sibling = createElementWithStyle("{}"); 807 sibling = createElementWithStyle("{}");
742 return timeout(0); 808 await timeout(0);
743 }).then(() => 809
744 {
745 expectHidden(test, parent); 810 expectHidden(test, parent);
746 expectVisible(test, child); 811 expectVisible(test, child);
747 expectVisible(test, sibling); 812 expectVisible(test, sibling);
748 813
749 return timeout(REFRESH_INTERVAL); 814 await timeout(REFRESH_INTERVAL);
750 }).then(() => 815
751 {
752 expectHidden(test, parent); 816 expectHidden(test, parent);
753 expectVisible(test, child); 817 expectVisible(test, child);
754 expectVisible(test, sibling); 818 expectVisible(test, sibling);
755 819
756 child2 = createElementWithStyle("{ background-color: #000}", 820 child2 = createElementWithStyle("{ background-color: #000}",
757 sibling); 821 sibling);
758 return timeout(0); 822 await timeout(0);
759 }).then(() => 823
760 {
761 expectVisible(test, child2); 824 expectVisible(test, child2);
762 return timeout(REFRESH_INTERVAL); 825 await timeout(REFRESH_INTERVAL);
763 }).then(() => 826
764 {
765 expectHidden(test, parent); 827 expectHidden(test, parent);
766 expectVisible(test, child); 828 expectVisible(test, child);
767 expectHidden(test, sibling); 829 expectHidden(test, sibling);
768 expectVisible(test, child2); 830 expectVisible(test, child2);
769 }).catch(unexpectedError.bind(test)).then(() => test.done()); 831 }
770 }; 832
771 833 test.done();
772 exports.testPseudoClassPropertiesOnStyleSheetLoad = function(test) 834 };
835
836 exports.testPseudoClassPropertiesOnStyleSheetLoad = async function(test)
773 { 837 {
774 let parent = createElement(); 838 let parent = createElement();
775 let child = createElement(parent); 839 let child = createElement(parent);
776 applyElemHideEmulation( 840
841 if (await applyElemHideEmulation(
842 test,
777 ["div:-abp-properties(background-color: rgb(0, 0, 0))", 843 ["div:-abp-properties(background-color: rgb(0, 0, 0))",
778 "div:-abp-contains(hide me)", 844 "div:-abp-contains(hide me)",
779 "div:-abp-has(> div.hideMe)"] 845 "div:-abp-has(> div.hideMe)"]
780 ).then(() => timeout(REFRESH_INTERVAL) 846 ))
781 ).then(() => 847 {
782 { 848 await timeout(REFRESH_INTERVAL);
849
783 expectVisible(test, parent); 850 expectVisible(test, parent);
784 expectVisible(test, child); 851 expectVisible(test, child);
785 852
786 // Load a style sheet that targets the parent element. This should run only 853 // Load a style sheet that targets the parent element. This should run only
787 // the "div:-abp-properties(background-color: rgb(0, 0, 0))" pattern. 854 // the "div:-abp-properties(background-color: rgb(0, 0, 0))" pattern.
788 insertStyleRule("#" + parent.id + " {background-color: #000}"); 855 insertStyleRule("#" + parent.id + " {background-color: #000}");
789 856
790 return timeout(REFRESH_INTERVAL); 857 await timeout(REFRESH_INTERVAL);
791 }).then(() => 858
792 { 859 expectHidden(test, parent);
793 expectHidden(test, parent); 860 expectVisible(test, child);
794 expectVisible(test, child); 861 }
795 }).catch(unexpectedError.bind(test)).then(() => test.done()); 862
796 }; 863 test.done();
797 864 };
798 exports.testPlainAttributeOnDomMutation = function(test) 865
866 exports.testPlainAttributeOnDomMutation = async function(test)
799 { 867 {
800 let parent = createElement(); 868 let parent = createElement();
801 let child = createElement(parent); 869 let child = createElement(parent);
802 applyElemHideEmulation( 870
871 if (await applyElemHideEmulation(
872 test,
803 ["div:-abp-properties(background-color: rgb(0, 0, 0))", 873 ["div:-abp-properties(background-color: rgb(0, 0, 0))",
804 "div[data-hide-me]", 874 "div[data-hide-me]",
805 "div:-abp-contains(hide me)", 875 "div:-abp-contains(hide me)",
806 "div:-abp-has(> div.hideMe)"] 876 "div:-abp-has(> div.hideMe)"]
807 ).then(() => timeout(REFRESH_INTERVAL) 877 ))
808 ).then(() => 878 {
809 { 879 await timeout(REFRESH_INTERVAL);
880
810 expectVisible(test, parent); 881 expectVisible(test, parent);
811 expectVisible(test, child); 882 expectVisible(test, child);
812 883
813 // Set the "data-hide-me" attribute on the child element. 884 // Set the "data-hide-me" attribute on the child element.
814 // 885 //
815 // Note: Since the "div[data-hide-me]" pattern has already been processed 886 // Note: Since the "div[data-hide-me]" pattern has already been processed
816 // and the selector added to the document's style sheet, this will in fact 887 // and the selector added to the document's style sheet, this will in fact
817 // not do anything at our end, but the browser will just match the selector 888 // not do anything at our end, but the browser will just match the selector
818 // and hide the element. 889 // and hide the element.
819 child.setAttribute("data-hide-me", ""); 890 child.setAttribute("data-hide-me", "");
820 891
821 return timeout(REFRESH_INTERVAL); 892 await timeout(REFRESH_INTERVAL);
822 }).then(() => 893
823 {
824 expectVisible(test, parent); 894 expectVisible(test, parent);
825 expectHidden(test, child); 895 expectHidden(test, child);
826 }).catch(unexpectedError.bind(test)).then(() => test.done()); 896 }
827 }; 897
828 898 test.done();
829 exports.testPseudoClassContainsOnDomMutation = function(test) 899 };
900
901 exports.testPseudoClassContainsOnDomMutation = async function(test)
830 { 902 {
831 let parent = createElement(); 903 let parent = createElement();
832 let child = createElement(parent); 904 let child = createElement(parent);
833 905
834 child.innerText = "do nothing"; 906 child.innerText = "do nothing";
835 907
836 applyElemHideEmulation( 908 if (await applyElemHideEmulation(
909 test,
837 ["div:-abp-properties(background-color: rgb(0, 0, 0))", 910 ["div:-abp-properties(background-color: rgb(0, 0, 0))",
838 "div[data-hide-me]", 911 "div[data-hide-me]",
839 "div:-abp-contains(hide me)", 912 "div:-abp-contains(hide me)",
840 "div:-abp-has(> div.hideMe)"] 913 "div:-abp-has(> div.hideMe)"]
841 ).then(() => timeout(REFRESH_INTERVAL) 914 ))
842 ).then(() => 915 {
843 { 916 await timeout(REFRESH_INTERVAL);
917
844 expectVisible(test, parent); 918 expectVisible(test, parent);
845 expectVisible(test, child); 919 expectVisible(test, child);
846 920
847 // Set the child element's text to "hide me". This should run only the 921 // Set the child element's text to "hide me". This should run only the
848 // "div:-abp-contains(hide me)" pattern. 922 // "div:-abp-contains(hide me)" pattern.
849 // 923 //
850 // Note: We need to set Node.innerText here in order to trigger the 924 // Note: We need to set Node.innerText here in order to trigger the
851 // "characterData" DOM mutation on Chromium. If we set Node.textContent 925 // "characterData" DOM mutation on Chromium. If we set Node.textContent
852 // instead, it triggers the "childList" DOM mutation instead. 926 // instead, it triggers the "childList" DOM mutation instead.
853 child.innerText = "hide me"; 927 child.innerText = "hide me";
854 928
855 return timeout(REFRESH_INTERVAL); 929 await timeout(REFRESH_INTERVAL);
856 }).then(() => 930
857 { 931 expectHidden(test, parent);
858 expectHidden(test, parent); 932 expectVisible(test, child);
859 expectVisible(test, child); 933 }
860 }).catch(unexpectedError.bind(test)).then(() => test.done()); 934
861 }; 935 test.done();
862 936 };
863 exports.testPseudoClassHasOnDomMutation = function(test) 937
938 exports.testPseudoClassHasOnDomMutation = async function(test)
864 { 939 {
865 let parent = createElement(); 940 let parent = createElement();
866 let child = null; 941 let child = null;
867 applyElemHideEmulation( 942
943 if (await applyElemHideEmulation(
944 test,
868 ["div:-abp-properties(background-color: rgb(0, 0, 0))", 945 ["div:-abp-properties(background-color: rgb(0, 0, 0))",
869 "div[data-hide-me]", 946 "div[data-hide-me]",
870 "div:-abp-contains(hide me)", 947 "div:-abp-contains(hide me)",
871 "div:-abp-has(> div)"] 948 "div:-abp-has(> div)"]
872 ).then(() => timeout(REFRESH_INTERVAL) 949 ))
873 ).then(() => 950 {
874 { 951 await timeout(REFRESH_INTERVAL);
952
875 expectVisible(test, parent); 953 expectVisible(test, parent);
876 954
877 // Add the child element. This should run all the DOM-dependent patterns 955 // Add the child element. This should run all the DOM-dependent patterns
878 // ("div:-abp-contains(hide me)" and "div:-abp-has(> div)"). 956 // ("div:-abp-contains(hide me)" and "div:-abp-has(> div)").
879 child = createElement(parent); 957 child = createElement(parent);
880 958
881 return timeout(REFRESH_INTERVAL); 959 await timeout(REFRESH_INTERVAL);
882 }).then(() => 960
883 { 961 expectHidden(test, parent);
884 expectHidden(test, parent); 962 expectVisible(test, child);
885 expectVisible(test, child); 963 }
886 }).catch(unexpectedError.bind(test)).then(() => test.done()); 964
887 }; 965 test.done();
888 966 };
889 exports.testPseudoClassHasWithClassOnDomMutation = function(test) 967
968 exports.testPseudoClassHasWithClassOnDomMutation = async function(test)
890 { 969 {
891 let parent = createElement(); 970 let parent = createElement();
892 let child = createElement(parent); 971 let child = createElement(parent);
893 applyElemHideEmulation( 972
973 if (await applyElemHideEmulation(
974 test,
894 ["div:-abp-properties(background-color: rgb(0, 0, 0))", 975 ["div:-abp-properties(background-color: rgb(0, 0, 0))",
895 "div[data-hide-me]", 976 "div[data-hide-me]",
896 "div:-abp-contains(hide me)", 977 "div:-abp-contains(hide me)",
897 "div:-abp-has(> div.hideMe)"] 978 "div:-abp-has(> div.hideMe)"]
898 ).then(() => timeout(REFRESH_INTERVAL) 979 ))
899 ).then(() => 980 {
900 { 981 await timeout(REFRESH_INTERVAL);
982
901 expectVisible(test, parent); 983 expectVisible(test, parent);
902 expectVisible(test, child); 984 expectVisible(test, child);
903 985
904 // Set the child element's class to "hideMe". This should run only the 986 // Set the child element's class to "hideMe". This should run only the
905 // "div:-abp-has(> div.hideMe)" pattern. 987 // "div:-abp-has(> div.hideMe)" pattern.
906 child.className = "hideMe"; 988 child.className = "hideMe";
907 989
908 return timeout(REFRESH_INTERVAL); 990 await timeout(REFRESH_INTERVAL);
909 }).then(() => 991
910 { 992 expectHidden(test, parent);
911 expectHidden(test, parent); 993 expectVisible(test, child);
912 expectVisible(test, child); 994 }
913 }).catch(unexpectedError.bind(test)).then(() => test.done()); 995
914 }; 996 test.done();
915 997 };
916 exports.testPseudoClassHasWithPseudoClassContainsOnDomMutation = function(test) 998
999 exports.testPseudoClassHasWithPseudoClassContainsOnDomMutation = async function( test)
917 { 1000 {
918 let parent = createElement(); 1001 let parent = createElement();
919 let child = createElement(parent); 1002 let child = createElement(parent);
920 1003
921 child.innerText = "do nothing"; 1004 child.innerText = "do nothing";
922 1005
923 applyElemHideEmulation( 1006 if (await applyElemHideEmulation(
1007 test,
924 ["div:-abp-properties(background-color: rgb(0, 0, 0))", 1008 ["div:-abp-properties(background-color: rgb(0, 0, 0))",
925 "div[data-hide-me]", 1009 "div[data-hide-me]",
926 "div:-abp-contains(hide me)", 1010 "div:-abp-contains(hide me)",
927 "div:-abp-has(> div:-abp-contains(hide me))"] 1011 "div:-abp-has(> div:-abp-contains(hide me))"]
928 ).then(() => timeout(REFRESH_INTERVAL) 1012 ))
929 ).then(() => 1013 {
930 { 1014 await timeout(REFRESH_INTERVAL);
1015
931 expectVisible(test, parent); 1016 expectVisible(test, parent);
932 expectVisible(test, child); 1017 expectVisible(test, child);
933 1018
934 // Set the child element's text to "hide me". This should run only the 1019 // Set the child element's text to "hide me". This should run only the
935 // "div:-abp-contains(hide me)" and 1020 // "div:-abp-contains(hide me)" and
936 // "div:-abp-has(> div:-abp-contains(hide me))" patterns. 1021 // "div:-abp-has(> div:-abp-contains(hide me))" patterns.
937 child.innerText = "hide me"; 1022 child.innerText = "hide me";
938 1023
939 return timeout(REFRESH_INTERVAL); 1024 await timeout(REFRESH_INTERVAL);
940 }).then(() => 1025
941 {
942 // Note: Even though it runs both the :-abp-contains() patterns, it only 1026 // Note: Even though it runs both the :-abp-contains() patterns, it only
943 // hides the parent element because of revision d7d51d29aa34. 1027 // hides the parent element because of revision d7d51d29aa34.
944 expectHidden(test, parent); 1028 expectHidden(test, parent);
945 expectVisible(test, child); 1029 expectVisible(test, child);
946 }).catch(unexpectedError.bind(test)).then(() => test.done()); 1030 }
947 }; 1031
948 1032 test.done();
949 exports.testOnlyRelevantElementsProcessed = function(test) 1033 };
1034
1035 exports.testOnlyRelevantElementsProcessed = async function(test)
950 { 1036 {
951 // <body> 1037 // <body>
952 // <div id="n1"> 1038 // <div id="n1">
953 // <p id="n1_1"></p> 1039 // <p id="n1_1"></p>
954 // <p id="n1_2"></p> 1040 // <p id="n1_2"></p>
955 // <p id="n1_4">Hello</p> 1041 // <p id="n1_4">Hello</p>
956 // </div> 1042 // </div>
957 // <div id="n2"> 1043 // <div id="n2">
958 // <p id="n2_1"></p> 1044 // <p id="n2_1"></p>
959 // <p id="n2_2"></p> 1045 // <p id="n2_2"></p>
(...skipping 10 matching lines...) Expand all
970 // <p id="n4_4">Hello</p> 1056 // <p id="n4_4">Hello</p>
971 // </div> 1057 // </div>
972 // </body> 1058 // </body>
973 for (let i of [1, 2, 3, 4]) 1059 for (let i of [1, 2, 3, 4])
974 { 1060 {
975 let n = createElement(null, "div", `n${i}`); 1061 let n = createElement(null, "div", `n${i}`);
976 for (let [j, text] of [[1], [2], [4, "Hello"]]) 1062 for (let [j, text] of [[1], [2], [4, "Hello"]])
977 createElement(n, "p", `n${i}_${j}`, text); 1063 createElement(n, "p", `n${i}_${j}`, text);
978 } 1064 }
979 1065
980 applyElemHideEmulation( 1066 if (await applyElemHideEmulation(
1067 test,
981 ["p:-abp-contains(Hello)", 1068 ["p:-abp-contains(Hello)",
982 "div:-abp-contains(Try me!)", 1069 "div:-abp-contains(Try me!)",
983 "div:-abp-has(p:-abp-contains(This is good))"] 1070 "div:-abp-has(p:-abp-contains(This is good))"]
984 ).then(() => timeout(REFRESH_INTERVAL) 1071 ))
985 ).then(() =>
986 { 1072 {
1073 await timeout(REFRESH_INTERVAL);
1074
987 // This is only a sanity check to make sure everything else is working 1075 // This is only a sanity check to make sure everything else is working
988 // before we do the actual test. 1076 // before we do the actual test.
989 for (let i of [1, 2, 3, 4]) 1077 for (let i of [1, 2, 3, 4])
990 { 1078 {
991 for (let j of [1, 2, 4]) 1079 for (let j of [1, 2, 4])
992 { 1080 {
993 let id = `n${i}_${j}`; 1081 let id = `n${i}_${j}`;
994 if (j == 4) 1082 if (j == 4)
995 expectHidden(test, testDocument.getElementById(id), id); 1083 expectHidden(test, testDocument.getElementById(id), id);
996 else 1084 else
997 expectVisible(test, testDocument.getElementById(id), id); 1085 expectVisible(test, testDocument.getElementById(id), id);
998 } 1086 }
999 } 1087 }
1000 1088
1001 // All <div> and <p> elements should be processed initially. 1089 // All <div> and <p> elements should be processed initially.
1002 for (let element of [...testDocument.getElementsByTagName("div"), 1090 for (let element of [...testDocument.getElementsByTagName("div"),
1003 ...testDocument.getElementsByTagName("p")]) 1091 ...testDocument.getElementsByTagName("p")])
1004 { 1092 {
1005 expectProcessed(test, element, element.id); 1093 expectProcessed(test, element, element.id);
1006 } 1094 }
1007 1095
1008 // Modify the text in <p id="n4_1"> 1096 // Modify the text in <p id="n4_1">
1009 testDocument.getElementById("n4_1").innerText = "Try me!"; 1097 testDocument.getElementById("n4_1").innerText = "Try me!";
1010 1098
1011 return timeout(REFRESH_INTERVAL); 1099 await timeout(REFRESH_INTERVAL);
1012 }).then(() => 1100
1013 {
1014 // When an element's text is modified, only the element or one of its 1101 // When an element's text is modified, only the element or one of its
1015 // ancestors matching any selector is processed for :-abp-has() and 1102 // ancestors matching any selector is processed for :-abp-has() and
1016 // :-abp-contains() 1103 // :-abp-contains()
1017 for (let element of [...testDocument.getElementsByTagName("div"), 1104 for (let element of [...testDocument.getElementsByTagName("div"),
1018 ...testDocument.getElementsByTagName("p")]) 1105 ...testDocument.getElementsByTagName("p")])
1019 { 1106 {
1020 if (element.id == "n4" || element.id == "n4_1") 1107 if (element.id == "n4" || element.id == "n4_1")
1021 expectProcessed(test, element, element.id); 1108 expectProcessed(test, element, element.id);
1022 else 1109 else
1023 expectNotProcessed(test, element, element.id); 1110 expectNotProcessed(test, element, element.id);
1024 } 1111 }
1025 1112
1026 // Create a new <p id="n2_3"> element with no text. 1113 // Create a new <p id="n2_3"> element with no text.
1027 createElement(testDocument.getElementById("n2"), "p", "n2_3"); 1114 createElement(testDocument.getElementById("n2"), "p", "n2_3");
1028 1115
1029 return timeout(REFRESH_INTERVAL); 1116 await timeout(REFRESH_INTERVAL);
1030 }).then(() => 1117
1031 {
1032 // When a new element is added, only the element or one of its ancestors 1118 // When a new element is added, only the element or one of its ancestors
1033 // matching any selector is processed for :-abp-has() and :-abp-contains() 1119 // matching any selector is processed for :-abp-has() and :-abp-contains()
1034 for (let element of [...testDocument.getElementsByTagName("div"), 1120 for (let element of [...testDocument.getElementsByTagName("div"),
1035 ...testDocument.getElementsByTagName("p")]) 1121 ...testDocument.getElementsByTagName("p")])
1036 { 1122 {
1037 if (element.id == "n2" || element.id == "n2_3") 1123 if (element.id == "n2" || element.id == "n2_3")
1038 expectProcessed(test, element, element.id); 1124 expectProcessed(test, element, element.id);
1039 else 1125 else
1040 expectNotProcessed(test, element, element.id); 1126 expectNotProcessed(test, element, element.id);
1041 } 1127 }
1042 }).catch(unexpectedError.bind(test)).then(() => test.done()); 1128 }
1129
1130 test.done();
1043 }; 1131 };
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld