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

Delta Between Two Patch Sets: test/browser/elemHideEmulation.js

Issue 29714601: Issue 6437 - Skip elements not affected by DOM mutations (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Rebase Created March 20, 2018, 10:04 a.m.
Right Patch Set: Rename debug mode to test mode Created May 18, 2018, 3:41 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
« lib/content/elemHideEmulation.js ('K') | « lib/content/elemHideEmulation.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
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
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 const {ElemHideEmulation} = require("../../lib/content/elemHideEmulation"); 20 const {ElemHideEmulation, setTestMode,
21 getTestInfo} = require("../../lib/content/elemHideEmulation");
21 22
22 const REFRESH_INTERVAL = 200; 23 const REFRESH_INTERVAL = 200;
23 24
24 let testDocument = null; 25 let testDocument = null;
25 26
26 exports.setUp = function(callback) 27 exports.setUp = function(callback)
27 { 28 {
29 setTestMode();
30
28 let iframe = document.createElement("iframe"); 31 let iframe = document.createElement("iframe");
29 document.body.appendChild(iframe); 32 document.body.appendChild(iframe);
30 testDocument = iframe.contentDocument; 33 testDocument = iframe.contentDocument;
31 34
32 callback(); 35 callback();
33 }; 36 };
34 37
35 exports.tearDown = function(callback) 38 exports.tearDown = function(callback)
36 { 39 {
37 let iframe = testDocument.defaultView.frameElement; 40 let iframe = testDocument.defaultView.frameElement;
(...skipping 30 matching lines...) Expand all
68 71
69 function expectVisible(test, element, id) 72 function expectVisible(test, element, id)
70 { 73 {
71 let withId = ""; 74 let withId = "";
72 if (typeof id != "undefined") 75 if (typeof id != "undefined")
73 withId = ` with ID '${id}'`; 76 withId = ` with ID '${id}'`;
74 77
75 test.notEqual( 78 test.notEqual(
76 window.getComputedStyle(element).display, "none", 79 window.getComputedStyle(element).display, "none",
77 `The element${withId}'s display property should not be set to 'none'`); 80 `The element${withId}'s display property should not be set to 'none'`);
81 }
82
83 function expectProcessed(test, element, id = null)
84 {
85 let withId = "";
86 if (id)
87 withId = ` with ID '${id}'`;
88
89 test.ok(
90 getTestInfo().lastProcessedElements.has(element),
91 `The element${withId} should have been processed`);
92 }
93
94 function expectNotProcessed(test, element, id = null)
95 {
96 let withId = "";
97 if (id)
98 withId = ` with ID '${id}'`;
99
100 test.ok(
101 !getTestInfo().lastProcessedElements.has(element),
102 `The element${withId} should not have been processed`);
78 } 103 }
79 104
80 function findUniqueId() 105 function findUniqueId()
81 { 106 {
82 let id = "elemHideEmulationTest-" + Math.floor(Math.random() * 10000); 107 let id = "elemHideEmulationTest-" + Math.floor(Math.random() * 10000);
83 if (!testDocument.getElementById(id)) 108 if (!testDocument.getElementById(id))
84 return id; 109 return id;
85 return findUniqueId(); 110 return findUniqueId();
86 } 111 }
87 112
88 function insertStyleRule(rule) 113 function insertStyleRule(rule)
89 { 114 {
90 let styleElement; 115 let styleElement;
91 let styleElements = testDocument.head.getElementsByTagName("style"); 116 let styleElements = testDocument.head.getElementsByTagName("style");
92 if (styleElements.length) 117 if (styleElements.length)
93 styleElement = styleElements[0]; 118 styleElement = styleElements[0];
94 else 119 else
95 { 120 {
96 styleElement = testDocument.createElement("style"); 121 styleElement = testDocument.createElement("style");
97 testDocument.head.appendChild(styleElement); 122 testDocument.head.appendChild(styleElement);
98 } 123 }
99 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); 124 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length);
100 } 125 }
101 126
102 function createElement(parent) 127 function createElement(parent, type = "div", id = findUniqueId(),
103 { 128 innerText = null)
104 let element = testDocument.createElement("div"); 129 {
105 element.id = findUniqueId(); 130 let element = testDocument.createElement(type);
131 element.id = id;
106 if (!parent) 132 if (!parent)
107 testDocument.body.appendChild(element); 133 testDocument.body.appendChild(element);
108 else 134 else
109 parent.appendChild(element); 135 parent.appendChild(element);
136 if (innerText)
137 element.innerText = innerText;
110 return element; 138 return element;
111 } 139 }
112 140
113 // Insert a <div> with a unique id and a CSS rule 141 // Insert a <div> with a unique id and a CSS rule
114 // for the the selector matching the id. 142 // for the the selector matching the id.
115 function createElementWithStyle(styleBlock, parent) 143 function createElementWithStyle(styleBlock, parent)
116 { 144 {
117 let element = createElement(parent); 145 let element = createElement(parent);
118 insertStyleRule("#" + element.id + " " + styleBlock); 146 insertStyleRule("#" + element.id + " " + styleBlock);
119 return element; 147 return element;
(...skipping 771 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 919
892 return timeout(REFRESH_INTERVAL); 920 return timeout(REFRESH_INTERVAL);
893 }).then(() => 921 }).then(() =>
894 { 922 {
895 // Note: Even though it runs both the :-abp-contains() patterns, it only 923 // Note: Even though it runs both the :-abp-contains() patterns, it only
896 // hides the parent element because of revision d7d51d29aa34. 924 // hides the parent element because of revision d7d51d29aa34.
897 expectHidden(test, parent); 925 expectHidden(test, parent);
898 expectVisible(test, child); 926 expectVisible(test, child);
899 }).catch(unexpectedError.bind(test)).then(() => test.done()); 927 }).catch(unexpectedError.bind(test)).then(() => test.done());
900 }; 928 };
929
930 exports.testOnlyRelevantElementsProcessed = function(test)
931 {
932 // <body>
933 // <div id="n1">
934 // <p id="n1_1"></p>
935 // <p id="n1_2"></p>
936 // <p id="n1_4">Hello</p>
937 // </div>
938 // <div id="n2">
939 // <p id="n2_1"></p>
940 // <p id="n2_2"></p>
941 // <p id="n2_4">Hello</p>
942 // </div>
943 // <div id="n3">
944 // <p id="n3_1"></p>
945 // <p id="n3_2"></p>
946 // <p id="n3_4">Hello</p>
947 // </div>
948 // <div id="n4">
949 // <p id="n4_1"></p>
950 // <p id="n4_2"></p>
951 // <p id="n4_4">Hello</p>
952 // </div>
953 // </body>
954 for (let i of [1, 2, 3, 4])
955 {
956 let n = createElement(null, "div", `n${i}`);
957 for (let [j, text] of [[1], [2], [4, "Hello"]])
958 createElement(n, "p", `n${i}_${j}`, text);
959 }
960
961 applyElemHideEmulation(
962 ["p:-abp-contains(Hello)",
963 "div:-abp-contains(Try me!)",
964 "div:-abp-has(p:-abp-contains(This is good))"]
965 ).then(() => timeout(REFRESH_INTERVAL)
966 ).then(() =>
967 {
968 // This is only a sanity check to make sure everything else is working
969 // before we do the actual test.
970 for (let i of [1, 2, 3, 4])
971 {
972 for (let j of [1, 2, 4])
973 {
974 let id = `n${i}_${j}`;
975 if (j == 4)
976 expectHidden(test, testDocument.getElementById(id), id);
977 else
978 expectVisible(test, testDocument.getElementById(id), id);
979 }
980 }
981
982 // All <div> and <p> elements should be processed initially.
983 for (let element of [...testDocument.getElementsByTagName("div"),
984 ...testDocument.getElementsByTagName("p")])
985 {
986 expectProcessed(test, element, element.id);
987 }
988
989 // Modify the text in <p id="n4_1">
990 testDocument.getElementById("n4_1").innerText = "Try me!";
991
992 return timeout(REFRESH_INTERVAL);
993 }).then(() =>
994 {
995 // When an element's text is modified, only the element or one of its
996 // ancestors matching any selector is processed for :-abp-has() and
997 // :-abp-contains()
998 for (let element of [...testDocument.getElementsByTagName("div"),
999 ...testDocument.getElementsByTagName("p")])
1000 {
1001 if (element.id == "n4" || element.id == "n4_1")
1002 expectProcessed(test, element, element.id);
1003 else
1004 expectNotProcessed(test, element, element.id);
1005 }
1006
1007 // Create a new <p id="n2_3"> element with no text.
1008 createElement(testDocument.getElementById("n2"), "p", "n2_3");
1009
1010 return timeout(REFRESH_INTERVAL);
1011 }).then(() =>
1012 {
1013 // When a new element is added, only the element or one of its ancestors
1014 // matching any selector is processed for :-abp-has() and :-abp-contains()
1015 for (let element of [...testDocument.getElementsByTagName("div"),
1016 ...testDocument.getElementsByTagName("p")])
1017 {
1018 if (element.id == "n2" || element.id == "n2_3")
1019 expectProcessed(test, element, element.id);
1020 else
1021 expectNotProcessed(test, element, element.id);
1022 }
1023 }).catch(unexpectedError.bind(test)).then(() => test.done());
1024 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld