test_bug708874.xhtml (9183B)
1 <?xml version="1.0"?> 2 <?xml-stylesheet type="text/css" href="chrome://global/skin"?> 3 <?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?> 4 <?xml-stylesheet type="text/css" href="test_bug708874.css"?> 5 <!-- 6 https://bugzilla.mozilla.org/show_bug.cgi?id=708874 7 --> 8 <window title="Mozilla Bug 708874" 9 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" 10 onload="RunTests();"> 11 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> 12 <script type="application/javascript"> 13 <![CDATA[ 14 15 /** Test for Bug 708874 - API for locking pseudo-class state of an element */ 16 17 var DOMWindowUtils = window.windowUtils; 18 19 var defaultColor = "rgb(0, 0, 0)"; 20 var disabledColor = "rgb(40, 0, 0)"; 21 22 function RunTests() { 23 testLockEnabled(); 24 testLockDisabled(); 25 testVisited(); 26 testMultiple(); 27 testInvalid(); 28 testNotElement(); 29 } 30 31 function testLockEnabled() { 32 var button = document.getElementById("test-button"); 33 34 /* starting state is enabled */ 35 button.removeAttribute("disabled"); 36 37 is(InspectorUtils.hasPseudoClassLock(button, ":disabled"), false, 38 "doesn't have lock at start"); 39 40 is(window.getComputedStyle(button).color, defaultColor, 41 "color is default color before locking on"); 42 43 is(button.matches(":disabled"), false, 44 "doesn't match selector at start"); 45 46 /* lock */ 47 InspectorUtils.addPseudoClassLock(button, ":disabled"); 48 49 is(InspectorUtils.hasPseudoClassLock(button, ":disabled"), true, 50 "hasPseudoClassLock is true after locking"); 51 52 is(window.getComputedStyle(button).color, disabledColor, 53 ":disabled style applied after adding lock"); 54 55 is(button.matches(":disabled"), true, 56 "matches selector after adding lock"); 57 58 /* change state to disabled */ 59 button.setAttribute("disabled", "disabled"); 60 61 is(window.getComputedStyle(button).color, disabledColor, 62 ":disabled style still applied after really disabling"); 63 64 is(button.matches(":disabled"), true, 65 "matches selector after adding lock"); 66 67 /* remove lock */ 68 InspectorUtils.removePseudoClassLock(button, ":disabled"); 69 70 is(InspectorUtils.hasPseudoClassLock(button, ":disabled"), false, 71 "hasPseudoClassLock is false after removing on lock"); 72 73 is(window.getComputedStyle(button).color, disabledColor, 74 ":disabled style still applied after removing lock"); 75 76 is(button.matches(":disabled"), true, 77 "matches selector after removing lock"); 78 79 /* change state to enabled */ 80 button.removeAttribute("disabled"); 81 82 is(window.getComputedStyle(button).color, defaultColor, 83 "back to default style after un-disabling"); 84 85 is(button.matches(":disabled"), false, 86 "doesn't match selector after enabling"); 87 } 88 89 90 function testLockDisabled() { 91 var button = document.getElementById("test-button"); 92 93 /* starting state is disabled */ 94 button.setAttribute("disabled", "disabled"); 95 96 is(InspectorUtils.hasPseudoClassLock(button, ":disabled"), false, 97 "doesn't have lock at start"); 98 99 is(window.getComputedStyle(button).color, disabledColor, 100 "color is :disabled color before locking"); 101 102 is(button.matches(":disabled"), true, 103 "matches selector before locking"); 104 105 /* lock */ 106 InspectorUtils.addPseudoClassLock(button, ":disabled"); 107 108 is(InspectorUtils.hasPseudoClassLock(button, ":disabled"), true, 109 "hasPseudoClassLock is true after locking"); 110 111 is(window.getComputedStyle(button).color, disabledColor, 112 ":disabled style still applied after adding on lock"); 113 114 is(button.matches(":disabled"), true, 115 "matches selector after locking"); 116 117 /* change state to enabled */ 118 button.removeAttribute("disabled"); 119 120 is(window.getComputedStyle(button).color, disabledColor, 121 ":disabled style applied after enabling"); 122 123 is(button.matches(":disabled"), true, 124 "matches selector after enabling with lock on"); 125 126 /* remove lock */ 127 InspectorUtils.removePseudoClassLock(button, ":disabled"); 128 129 is(InspectorUtils.hasPseudoClassLock(button, ":disabled"), false, 130 "hasPseudoClassLock is false after removing on lock"); 131 132 is(window.getComputedStyle(button).color, defaultColor, 133 "default style applied after removing lock"); 134 135 is(button.matches(":disabled"), false, 136 "doesn't match selector after unlocking"); 137 138 /* change state to disabled */ 139 button.setAttribute("disabled", "disabled"); 140 141 is(window.getComputedStyle(button).color, disabledColor, 142 ":disabled style applied after disabling after unlocking"); 143 144 is(button.matches(":disabled"), true, 145 "matches selector again after disabling"); 146 } 147 148 function testVisited() { 149 var link = document.getElementById("test-link"); 150 var visitedColor = "rgb(20, 0, 0)"; 151 var unvisitedColor = "rgb(30, 0, 0)"; 152 153 /* lock visited */ 154 InspectorUtils.addPseudoClassLock(link, ":visited"); 155 156 is(InspectorUtils.hasPseudoClassLock(link, ":visited"), true, 157 "hasPseudoClassLock is true after adding lock"); 158 159 var color = DOMWindowUtils.getVisitedDependentComputedStyle(link, 160 null, "color"); 161 is(color, visitedColor, "color is :visited color after locking"); 162 163 /* lock unvisited */ 164 InspectorUtils.addPseudoClassLock(link, ":link"); 165 166 is(InspectorUtils.hasPseudoClassLock(link, ":link"), true, 167 "hasPseudoClassLock is true after adding :link lock"); 168 169 is(InspectorUtils.hasPseudoClassLock(link, ":visited"), false, 170 "hasPseudoClassLock is false for :visited after adding :link lock"); 171 172 var color = DOMWindowUtils.getVisitedDependentComputedStyle(link, 173 null, "color"); 174 is(color, unvisitedColor, "color is :link color after locking :link"); 175 176 /* lock visited back on */ 177 InspectorUtils.addPseudoClassLock(link, ":visited"); 178 179 is(InspectorUtils.hasPseudoClassLock(link, ":visited"), true, 180 "hasPseudoClassLock is true after adding :visited lock"); 181 182 is(InspectorUtils.hasPseudoClassLock(link, ":link"), false, 183 "hasPseudoClassLock is false for :link after adding :visited lock"); 184 185 var color = DOMWindowUtils.getVisitedDependentComputedStyle(link, 186 null, "color"); 187 is(color, visitedColor, "color is :visited color after locking back on"); 188 } 189 190 function testMultiple() { 191 var div = document.getElementById("test-div"); 192 193 var styles = { 194 ":hover": { 195 property: "color", 196 value: "rgb(10, 0, 0)", 197 defaultValue: "rgb(0, 0, 0)" 198 }, 199 ":active": { 200 property: "font-family", 201 value: "Arial", 202 defaultValue: "serif" 203 }, 204 ":focus": { 205 property: "font-weight", 206 value: "800", 207 defaultValue: "400" 208 } 209 }; 210 211 for (var pseudo in styles) { 212 InspectorUtils.addPseudoClassLock(div, pseudo); 213 } 214 215 for (var pseudo in styles) { 216 is(InspectorUtils.hasPseudoClassLock(div, pseudo), true, 217 "hasPseudoClassLock is true after locking"); 218 219 var style = styles[pseudo]; 220 is(window.getComputedStyle(div).getPropertyValue(style.property), 221 style.value, "style for pseudo-class is applied after locking"); 222 223 is(div.matches(pseudo), true, 224 "matches selector after locking"); 225 } 226 227 InspectorUtils.clearPseudoClassLocks(div); 228 229 for (var pseudo in styles) { 230 is(InspectorUtils.hasPseudoClassLock(div, pseudo), false, 231 "hasPseudoClassLock is false after clearing"); 232 233 is(window.getComputedStyle(div).getPropertyValue(style.property), 234 style.defaultValue, "style is back to default after clearing"); 235 236 is(div.matches(pseudo), false, 237 "doesn't match selector after unlocking"); 238 } 239 } 240 241 function testInvalid() { 242 var div = document.getElementById("test-div"); 243 var pseudos = ["not a valid pseudo-class", ":ny-link", ":first-child"]; 244 245 for (var i = 0; i < pseudos.length; i++) { 246 var pseudo = pseudos[i]; 247 248 // basically make sure these don't crash the browser. 249 InspectorUtils.addPseudoClassLock(div, pseudo); 250 251 is(InspectorUtils.hasPseudoClassLock(div, pseudo), false); 252 253 InspectorUtils.removePseudoClassLock(div, pseudo); 254 } 255 } 256 257 function testNotElement() { 258 for (var value of [null, undefined, {}]) { 259 SimpleTest.doesThrow(() => InspectorUtils.hasPseudoClassLock(value, ":hover"), 260 "hasPseudoClassLock should throw for " + value); 261 SimpleTest.doesThrow(() => InspectorUtils.addPseudoClassLock(value, ":hover"), 262 "addPseudoClassLock should throw for " + value); 263 SimpleTest.doesThrow(() => InspectorUtils.removePseudoClassLock(value, ":hover"), 264 "removePseudoClassLock should throw for " + value); 265 SimpleTest.doesThrow(() => InspectorUtils.clearPseudoClassLocks(value), 266 "clearPseudoClassLocks should throw for " + value); 267 } 268 } 269 ]]> 270 </script> 271 272 <body xmlns="http://www.w3.org/1999/xhtml"> 273 <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=708874" 274 target="_blank">Mozilla Bug 708874</a> 275 276 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=708874"> 277 Mozilla Bug 708874 - API for locking pseudo-class state of an element 278 </a> 279 280 <a id="test-link" href="http://notavisitedwebsite.com"> 281 test link 282 </a> 283 284 <div id="test-div"> 285 test div 286 </div> 287 288 <button id="test-button"> 289 test button 290 </button> 291 </body> 292 293 </window>