| Index: test/browser/elemHideEmulation.js |
| diff --git a/test/browser/elemHideEmulation.js b/test/browser/elemHideEmulation.js |
| index a7b6ce8f375f937f95f529584df729ca172d0088..97081c99f393a24656e0ce75f97f3efc5037755f 100644 |
| --- a/test/browser/elemHideEmulation.js |
| +++ b/test/browser/elemHideEmulation.js |
| @@ -17,9 +17,7 @@ |
| "use strict"; |
| -/* globals ElemHideEmulation */ |
| - |
| -let myUrl = document.currentScript.src; |
| +const {ElemHideEmulation} = require("content/elemHideEmulation"); |
|
Wladimir Palant
2017/08/17 10:05:38
require("../../lib/content/elemHideEmulation")? Su
kzar
2017/08/17 12:40:22
Done.
|
| exports.tearDown = function(callback) |
| { |
| @@ -34,12 +32,6 @@ exports.tearDown = function(callback) |
| callback(); |
| }; |
| -function unexpectedError(error) |
| -{ |
| - console.error(error); |
| - this.ok(false, "Unexpected error: " + error); |
| -} |
| - |
| function expectHidden(test, element) |
| { |
| test.equal(window.getComputedStyle(element).display, "none", |
| @@ -74,7 +66,7 @@ function insertStyleRule(rule) |
| styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); |
| } |
| -// insert a <div> with a unique id and a CSS rule |
| +// Insert a <div> with a unique id and a CSS rule |
| // for the the selector matching the id. |
| function createElementWithStyle(styleBlock, parent) |
| { |
| @@ -88,60 +80,35 @@ function createElementWithStyle(styleBlock, parent) |
| return element; |
| } |
| -// Will ensure the class ElemHideEmulation is loaded. |
| -// Pass true when it calls itself. |
| -function loadElemHideEmulation(inside) |
| -{ |
| - if (typeof ElemHideEmulation == "undefined") |
| - { |
| - if (inside) |
| - return Promise.reject("Failed to load ElemHideEmulation."); |
| - |
| - return loadScript(myUrl + "/../../../lib/common.js").then(() => |
| - { |
| - return loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js"); |
| - }).then(() => |
| - { |
| - return loadElemHideEmulation(true); |
| - }); |
| - } |
| - |
| - return Promise.resolve(); |
| -} |
| - |
| // Create a new ElemHideEmulation instance with @selectors. |
| function applyElemHideEmulation(selectors) |
| { |
| - return loadElemHideEmulation().then(() => |
| - { |
| - let elemHideEmulation = new ElemHideEmulation( |
| - window, |
| - callback => |
| - { |
| - let patterns = []; |
| - selectors.forEach(selector => |
| - { |
| - patterns.push({selector}); |
| - }); |
| - callback(patterns); |
| - }, |
| - newSelectors => |
| - { |
| - if (!newSelectors.length) |
| - return; |
| - let selector = newSelectors.join(", "); |
| - insertStyleRule(selector + "{display: none !important;}"); |
| - }, |
| - elems => |
| + let elemHideEmulation = new ElemHideEmulation( |
| + window, |
| + callback => |
| + { |
| + let patterns = []; |
| + selectors.forEach(selector => |
| { |
| - for (let elem of elems) |
| - elem.style.display = "none"; |
| - } |
| - ); |
| - |
| - elemHideEmulation.apply(); |
| - return Promise.resolve(elemHideEmulation); |
| - }); |
| + patterns.push({selector}); |
| + }); |
| + callback(patterns); |
| + }, |
| + newSelectors => |
| + { |
| + if (!newSelectors.length) |
| + return; |
| + let selector = newSelectors.join(", "); |
| + insertStyleRule(selector + "{display: none !important;}"); |
| + }, |
| + elems => |
| + { |
| + for (let elem of elems) |
| + elem.style.display = "none"; |
| + } |
| + ); |
| + |
| + return elemHideEmulation.apply(); |
|
Wladimir Palant
2017/08/17 10:05:39
elemHideEmulation.apply() doesn't return any resul
kzar
2017/08/17 12:40:22
Done.
|
| } |
| exports.testVerbatimPropertySelector = function(test) |
| @@ -149,10 +116,9 @@ exports.testVerbatimPropertySelector = function(test) |
| let toHide = createElementWithStyle("{background-color: #000}"); |
| applyElemHideEmulation( |
| [":-abp-properties(background-color: rgb(0, 0, 0))"] |
| - ).then(() => |
| - { |
| - expectHidden(test, toHide); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectHidden(test, toHide); |
| + test.done(); |
| }; |
| exports.testVerbatimPropertySelectorWithPrefix = function(test) |
| @@ -161,11 +127,10 @@ exports.testVerbatimPropertySelectorWithPrefix = function(test) |
| let toHide = createElementWithStyle("{background-color: #000}", parent); |
| applyElemHideEmulation( |
| ["div > :-abp-properties(background-color: rgb(0, 0, 0))"] |
| - ).then(() => |
| - { |
| - expectVisible(test, parent); |
| - expectHidden(test, toHide); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectVisible(test, parent); |
| + expectHidden(test, toHide); |
| + test.done(); |
| }; |
| exports.testVerbatimPropertySelectorWithPrefixNoMatch = function(test) |
| @@ -174,11 +139,10 @@ exports.testVerbatimPropertySelectorWithPrefixNoMatch = function(test) |
| let toHide = createElementWithStyle("{background-color: #fff}", parent); |
| applyElemHideEmulation( |
| ["div > :-abp-properties(background-color: rgb(0, 0, 0))"] |
| - ).then(() => |
| - { |
| - expectVisible(test, parent); |
| - expectVisible(test, toHide); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectVisible(test, parent); |
| + expectVisible(test, toHide); |
| + test.done(); |
| }; |
| exports.testVerbatimPropertySelectorWithSuffix = function(test) |
| @@ -187,11 +151,10 @@ exports.testVerbatimPropertySelectorWithSuffix = function(test) |
| let toHide = createElementWithStyle("{background-color: #000}", parent); |
| applyElemHideEmulation( |
| [":-abp-properties(background-color: rgb(0, 0, 0)) > div"] |
| - ).then(() => |
| - { |
| - expectVisible(test, parent); |
| - expectHidden(test, toHide); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectVisible(test, parent); |
| + expectHidden(test, toHide); |
| + test.done(); |
| }; |
| exports.testVerbatimPropertyPseudoSelectorWithPrefixAndSuffix = function(test) |
| @@ -201,12 +164,11 @@ exports.testVerbatimPropertyPseudoSelectorWithPrefixAndSuffix = function(test) |
| let toHide = createElementWithStyle("{background-color: #000}", middle); |
| applyElemHideEmulation( |
| ["div > :-abp-properties(background-color: rgb(0, 0, 0)) > div"] |
| - ).then(() => |
| - { |
| - expectVisible(test, parent); |
| - expectVisible(test, middle); |
| - expectHidden(test, toHide); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectVisible(test, parent); |
| + expectVisible(test, middle); |
| + expectHidden(test, toHide); |
| + test.done(); |
| }; |
| exports.testPropertySelectorWithWildcard = function(test) |
| @@ -214,10 +176,9 @@ exports.testPropertySelectorWithWildcard = function(test) |
| let toHide = createElementWithStyle("{background-color: #000}"); |
| applyElemHideEmulation( |
| [":-abp-properties(*color: rgb(0, 0, 0))"] |
| - ).then(() => |
| - { |
| - expectHidden(test, toHide); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectHidden(test, toHide); |
| + test.done(); |
| }; |
| exports.testPropertySelectorWithRegularExpression = function(test) |
| @@ -225,10 +186,9 @@ exports.testPropertySelectorWithRegularExpression = function(test) |
| let toHide = createElementWithStyle("{background-color: #000}"); |
| applyElemHideEmulation( |
| [":-abp-properties(/.*color: rgb\\(0, 0, 0\\)/)"] |
| - ).then(() => |
| - { |
| - expectHidden(test, toHide); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectHidden(test, toHide); |
| + test.done(); |
| }; |
| exports.testPropertySelectorWithEscapedBrace = function(test) |
| @@ -236,10 +196,9 @@ exports.testPropertySelectorWithEscapedBrace = function(test) |
| let toHide = createElementWithStyle("{background-color: #000}"); |
| applyElemHideEmulation( |
| [":-abp-properties(/background.\\x7B 0,6\\x7D : rgb\\(0, 0, 0\\)/)"] |
| - ).then(() => |
| - { |
| - expectHidden(test, toHide); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectHidden(test, toHide); |
| + test.done(); |
| }; |
| exports.testPropertySelectorWithImproperlyEscapedBrace = function(test) |
| @@ -247,10 +206,9 @@ exports.testPropertySelectorWithImproperlyEscapedBrace = function(test) |
| let toHide = createElementWithStyle("{background-color: #000}"); |
| applyElemHideEmulation( |
| [":-abp-properties(/background.\\x7B0,6\\x7D: rgb\\(0, 0, 0\\)/)"] |
| - ).then(() => |
| - { |
| - expectVisible(test, toHide); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectVisible(test, toHide); |
| + test.done(); |
| }; |
| exports.testDynamicallyChangedProperty = function(test) |
| @@ -258,21 +216,18 @@ exports.testDynamicallyChangedProperty = function(test) |
| let toHide = createElementWithStyle("{}"); |
| applyElemHideEmulation( |
| [":-abp-properties(background-color: rgb(0, 0, 0))"] |
| - ).then(() => |
| + ); |
| + |
| + expectVisible(test, toHide); |
| + insertStyleRule("#" + toHide.id + " {background-color: #000}"); |
| + |
| + // Re-evaluation will only happen after a few seconds |
| + expectVisible(test, toHide); |
| + window.setTimeout(() => |
| { |
| - expectVisible(test, toHide); |
| - insertStyleRule("#" + toHide.id + " {background-color: #000}"); |
| - return new Promise((resolve, reject) => |
| - { |
| - // Re-evaluation will only happen after a few seconds |
| - expectVisible(test, toHide); |
| - window.setTimeout(() => |
| - { |
| - expectHidden(test, toHide); |
| - resolve(); |
| - }, 4000); |
| - }); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + expectHidden(test, toHide); |
| + test.done(); |
| + }, 4000); |
| }; |
| exports.testPseudoClassWithPropBeforeSelector = function(test) |
| @@ -283,11 +238,10 @@ exports.testPseudoClassWithPropBeforeSelector = function(test) |
| applyElemHideEmulation( |
| ["div:-abp-properties(content: \"publicite\")"] |
| - ).then(() => |
| - { |
| - expectHidden(test, child); |
| - expectVisible(test, parent); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectHidden(test, child); |
| + expectVisible(test, parent); |
| + test.done(); |
| }; |
| exports.testPseudoClassHasSelector = function(test) |
| @@ -295,10 +249,9 @@ exports.testPseudoClassHasSelector = function(test) |
| let toHide = createElementWithStyle("{}"); |
| applyElemHideEmulation( |
| ["div:-abp-has(div)"] |
| - ).then(() => |
| - { |
| - expectVisible(test, toHide); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectVisible(test, toHide); |
| + test.done(); |
| }; |
| exports.testPseudoClassHasSelectorWithPrefix = function(test) |
| @@ -307,11 +260,10 @@ exports.testPseudoClassHasSelectorWithPrefix = function(test) |
| let child = createElementWithStyle("{}", parent); |
| applyElemHideEmulation( |
| ["div:-abp-has(div)"] |
| - ).then(() => |
| - { |
| - expectHidden(test, parent); |
| - expectVisible(test, child); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectHidden(test, parent); |
| + expectVisible(test, child); |
| + test.done(); |
| }; |
| exports.testPseudoClassHasSelectorWithSuffix = function(test) |
| @@ -321,12 +273,11 @@ exports.testPseudoClassHasSelectorWithSuffix = function(test) |
| let child = createElementWithStyle("{}", middle); |
| applyElemHideEmulation( |
| ["div:-abp-has(div) > div"] |
| - ).then(() => |
| - { |
| - expectVisible(test, parent); |
| - expectHidden(test, middle); |
| - expectHidden(test, child); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectVisible(test, parent); |
| + expectHidden(test, middle); |
| + expectHidden(test, child); |
| + test.done(); |
| }; |
| exports.testPseudoClassHasSelectorWithSuffixSibling = function(test) |
| @@ -336,12 +287,11 @@ exports.testPseudoClassHasSelectorWithSuffixSibling = function(test) |
| let toHide = createElementWithStyle("{}"); |
| applyElemHideEmulation( |
| ["div:-abp-has(div) + div"] |
| - ).then(() => |
| - { |
| - expectVisible(test, parent); |
| - expectVisible(test, middle); |
| - expectHidden(test, toHide); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectVisible(test, parent); |
| + expectVisible(test, middle); |
| + expectHidden(test, toHide); |
| + test.done(); |
| }; |
| exports.testPseudoClassHasSelectorWithSuffixSiblingChild = function(test) |
| @@ -358,13 +308,12 @@ exports.testPseudoClassHasSelectorWithSuffixSiblingChild = function(test) |
| let toHide = createElementWithStyle("{}", sibling); |
| applyElemHideEmulation( |
| ["div:-abp-has(div) + div > div"] |
| - ).then(() => |
| - { |
| - expectVisible(test, parent); |
| - expectVisible(test, middle); |
| - expectVisible(test, sibling); |
| - expectHidden(test, toHide); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectVisible(test, parent); |
| + expectVisible(test, middle); |
| + expectVisible(test, sibling); |
| + expectHidden(test, toHide); |
| + test.done(); |
| }; |
| function runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(test, selector) |
| @@ -391,15 +340,14 @@ function runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(test, selector |
| applyElemHideEmulation( |
| [selector] |
| - ).then(() => |
| - { |
| - expectVisible(test, parent); |
| - expectVisible(test, middle); |
| - expectVisible(test, inside); |
| - expectVisible(test, sibling); |
| - expectVisible(test, sibling2); |
| - expectHidden(test, toHide); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectVisible(test, parent); |
| + expectVisible(test, middle); |
| + expectVisible(test, inside); |
| + expectVisible(test, sibling); |
| + expectVisible(test, sibling2); |
| + expectHidden(test, toHide); |
| + test.done(); |
| } |
| exports.testPseudoClassHasSelectorWithHasAndWithSuffixSibling = function(test) |
| @@ -429,15 +377,14 @@ exports.testPseudoClassContains = function(test) |
| applyElemHideEmulation( |
| ["#parent div:-abp-contains(to hide)"] |
| - ).then(() => |
| - { |
| - expectVisible(test, parent); |
| - expectVisible(test, middle); |
| - expectVisible(test, inside); |
| - expectHidden(test, sibling); |
| - expectVisible(test, sibling2); |
| - expectHidden(test, toHide); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectVisible(test, parent); |
| + expectVisible(test, middle); |
| + expectVisible(test, inside); |
| + expectHidden(test, sibling); |
| + expectVisible(test, sibling2); |
| + expectHidden(test, toHide); |
| + test.done(); |
| }; |
| exports.testPseudoClassHasSelectorWithPropSelector = function(test) |
| @@ -446,11 +393,10 @@ exports.testPseudoClassHasSelectorWithPropSelector = function(test) |
| let child = createElementWithStyle("{background-color: #000}", parent); |
| applyElemHideEmulation( |
| ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"] |
| - ).then(() => |
| - { |
| - expectVisible(test, child); |
| - expectHidden(test, parent); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectVisible(test, child); |
| + expectHidden(test, parent); |
| + test.done(); |
| }; |
| exports.testPseudoClassHasSelectorWithPropSelector2 = function(test) |
| @@ -460,9 +406,8 @@ exports.testPseudoClassHasSelectorWithPropSelector2 = function(test) |
| insertStyleRule("body #" + parent.id + " > div { background-color: #000}"); |
| applyElemHideEmulation( |
| ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"] |
| - ).then(() => |
| - { |
| - expectVisible(test, child); |
| - expectHidden(test, parent); |
| - }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| + ); |
| + expectVisible(test, child); |
| + expectHidden(test, parent); |
| + test.done(); |
| }; |