Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html> |
michael@0 | 3 | <head> |
michael@0 | 4 | <title>Test for additional sheets</title> |
michael@0 | 5 | <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 6 | <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script> |
michael@0 | 7 | <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/> |
michael@0 | 8 | </head> |
michael@0 | 9 | <body onload="run()"> |
michael@0 | 10 | <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=737003">Mozilla Bug 737003</a> |
michael@0 | 11 | <iframe id="iframe" src="http://mochi.test:8888/tests/layout/style/test/chrome/additional_sheets_helper.html"></iframe> |
michael@0 | 12 | <pre id="test"> |
michael@0 | 13 | <script type="application/javascript; version=1.8"> |
michael@0 | 14 | |
michael@0 | 15 | |
michael@0 | 16 | |
michael@0 | 17 | var gIOService = Components.classes["@mozilla.org/network/io-service;1"] |
michael@0 | 18 | .getService(Components.interfaces.nsIIOService) |
michael@0 | 19 | |
michael@0 | 20 | function getUri(style) |
michael@0 | 21 | { |
michael@0 | 22 | return "data:text/css," + style; |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | function getStyle(color, swapped) |
michael@0 | 26 | { |
michael@0 | 27 | return "body {color: " + color + (swapped ? " !important" : "") + |
michael@0 | 28 | "; background-color: " + color + (swapped ? "" : " !important;") + ";}"; |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | function loadUserSheet(win, style) |
michael@0 | 32 | { |
michael@0 | 33 | loadSheet(win, style, "USER_SHEET"); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | function loadAgentSheet(win, style) |
michael@0 | 37 | { |
michael@0 | 38 | loadSheet(win, style, "AGENT_SHEET"); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | function loadAuthorSheet(win, style) |
michael@0 | 42 | { |
michael@0 | 43 | loadSheet(win, style, "AUTHOR_SHEET"); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | function removeUserSheet(win, style) |
michael@0 | 47 | { |
michael@0 | 48 | removeSheet(win, style, "USER_SHEET"); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | function removeAgentSheet(win, style) |
michael@0 | 52 | { |
michael@0 | 53 | removeSheet(win, style, "AGENT_SHEET"); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | function removeAuthorSheet(win, style) |
michael@0 | 57 | { |
michael@0 | 58 | removeSheet(win, style, "AUTHOR_SHEET"); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | function loadSheet(win, style, type) |
michael@0 | 62 | { |
michael@0 | 63 | var uri = gIOService.newURI(getUri(style), null, null); |
michael@0 | 64 | var windowUtils = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor) |
michael@0 | 65 | .getInterface(Components.interfaces.nsIDOMWindowUtils); |
michael@0 | 66 | windowUtils.loadSheet(uri, windowUtils[type]); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | function removeSheet(win, style, type) |
michael@0 | 70 | { |
michael@0 | 71 | var uri = gIOService.newURI(getUri(style), null, null); |
michael@0 | 72 | var windowUtils = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor) |
michael@0 | 73 | .getInterface(Components.interfaces.nsIDOMWindowUtils); |
michael@0 | 74 | windowUtils.removeSheet(uri, windowUtils[type]); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | function loadAndRegisterUserSheet(win, style) |
michael@0 | 78 | { |
michael@0 | 79 | loadAndRegisterSheet(win, style, "USER_SHEET"); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | function loadAndRegisterAgentSheet(win, style) |
michael@0 | 83 | { |
michael@0 | 84 | loadAndRegisterSheet(win, style, "AGENT_SHEET"); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | function loadAndRegisterAuthorSheet(win, style) |
michael@0 | 88 | { |
michael@0 | 89 | loadAndRegisterSheet(win, style, "AUTHOR_SHEET"); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | function unregisterUserSheet(win, style) |
michael@0 | 93 | { |
michael@0 | 94 | unregisterSheet(win, style, "USER_SHEET"); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | function unregisterAgentSheet(win, style) |
michael@0 | 98 | { |
michael@0 | 99 | unregisterSheet(win, style, "AGENT_SHEET"); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | function unregisterAuthorSheet(win, style) |
michael@0 | 103 | { |
michael@0 | 104 | unregisterSheet(win, style, "AUTHOR_SHEET"); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | function loadAndRegisterSheet(win, style, type) |
michael@0 | 108 | { |
michael@0 | 109 | var sss = Components.classes["@mozilla.org/content/style-sheet-service;1"] |
michael@0 | 110 | .getService(Components.interfaces.nsIStyleSheetService); |
michael@0 | 111 | var ios = Components.classes["@mozilla.org/network/io-service;1"] |
michael@0 | 112 | .getService(Components.interfaces.nsIIOService); |
michael@0 | 113 | uri = ios.newURI(getUri(style), null, null); |
michael@0 | 114 | sss.loadAndRegisterSheet(uri, sss[type]); |
michael@0 | 115 | is (sss.sheetRegistered(uri, sss[type]), true); |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | function unregisterSheet(win, style, type) |
michael@0 | 119 | { |
michael@0 | 120 | var sss = Components.classes["@mozilla.org/content/style-sheet-service;1"] |
michael@0 | 121 | .getService(Components.interfaces.nsIStyleSheetService); |
michael@0 | 122 | var ios = Components.classes["@mozilla.org/network/io-service;1"] |
michael@0 | 123 | .getService(Components.interfaces.nsIIOService); |
michael@0 | 124 | var uri = ios.newURI(getUri(style), null, null); |
michael@0 | 125 | sss.unregisterSheet(uri, sss[type]); |
michael@0 | 126 | is (sss.sheetRegistered(uri, sss[type]), false); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | function setDocSheet(win, style) |
michael@0 | 130 | { |
michael@0 | 131 | var subdoc = win.document; |
michael@0 | 132 | var headID = subdoc.getElementsByTagName("head")[0]; |
michael@0 | 133 | var cssNode = subdoc.createElement('style'); |
michael@0 | 134 | cssNode.type = 'text/css'; |
michael@0 | 135 | cssNode.innerHTML = style; |
michael@0 | 136 | cssNode.id = 'docsheet'; |
michael@0 | 137 | headID.appendChild(cssNode); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | function removeDocSheet(win) |
michael@0 | 141 | { |
michael@0 | 142 | var subdoc = win.document; |
michael@0 | 143 | var node = subdoc.getElementById('docsheet'); |
michael@0 | 144 | node.parentNode.removeChild(node); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | var agent = { |
michael@0 | 148 | type: 'agent', |
michael@0 | 149 | color: 'rgb(255, 0, 0)', |
michael@0 | 150 | addRules: loadAndRegisterAgentSheet, |
michael@0 | 151 | removeRules: unregisterAgentSheet |
michael@0 | 152 | }; |
michael@0 | 153 | |
michael@0 | 154 | var user = { |
michael@0 | 155 | type: 'user', |
michael@0 | 156 | color: 'rgb(0, 255, 0)', |
michael@0 | 157 | addRules: loadAndRegisterUserSheet, |
michael@0 | 158 | removeRules: unregisterUserSheet |
michael@0 | 159 | }; |
michael@0 | 160 | |
michael@0 | 161 | var additionalAgent = { |
michael@0 | 162 | type: 'additionalAgent', |
michael@0 | 163 | color: 'rgb(0, 0, 255)', |
michael@0 | 164 | addRules: loadAgentSheet, |
michael@0 | 165 | removeRules: removeAgentSheet |
michael@0 | 166 | }; |
michael@0 | 167 | |
michael@0 | 168 | var additionalUser = { |
michael@0 | 169 | type: 'additionalUser', |
michael@0 | 170 | color: 'rgb(255, 255, 0)', |
michael@0 | 171 | addRules: loadUserSheet, |
michael@0 | 172 | removeRules: removeUserSheet |
michael@0 | 173 | }; |
michael@0 | 174 | |
michael@0 | 175 | var additionalAuthor = { |
michael@0 | 176 | type: 'additionalAuthor', |
michael@0 | 177 | color: 'rgb(255, 255, 0)', |
michael@0 | 178 | addRules: loadAuthorSheet, |
michael@0 | 179 | removeRules: removeAuthorSheet |
michael@0 | 180 | }; |
michael@0 | 181 | |
michael@0 | 182 | var doc = { |
michael@0 | 183 | type: 'doc', |
michael@0 | 184 | color: 'rgb(0, 255, 255)', |
michael@0 | 185 | addRules: setDocSheet, |
michael@0 | 186 | removeRules: removeDocSheet |
michael@0 | 187 | }; |
michael@0 | 188 | |
michael@0 | 189 | var author = { |
michael@0 | 190 | type: 'author', |
michael@0 | 191 | color: 'rgb(255, 0, 255)', |
michael@0 | 192 | addRules: loadAndRegisterAuthorSheet, |
michael@0 | 193 | removeRules: unregisterAuthorSheet |
michael@0 | 194 | }; |
michael@0 | 195 | |
michael@0 | 196 | function loadAndCheck(win, firstType, secondType, swap, result1, result2) |
michael@0 | 197 | { |
michael@0 | 198 | var firstStyle = getStyle(firstType.color, false); |
michael@0 | 199 | var secondStyle = getStyle(secondType.color, swap); |
michael@0 | 200 | |
michael@0 | 201 | firstType.addRules(win, firstStyle); |
michael@0 | 202 | secondType.addRules(win, secondStyle); |
michael@0 | 203 | |
michael@0 | 204 | var cs = win.getComputedStyle(win.document.body, null); |
michael@0 | 205 | is(cs.getPropertyValue('color'), result1, |
michael@0 | 206 | firstType.type + "(normal)" + " vs " + secondType.type + (swap ? "(important)" : "(normal)" ) + " 1"); |
michael@0 | 207 | is(cs.getPropertyValue('background-color'), result2, |
michael@0 | 208 | firstType.type + "(important)" + " vs " + secondType.type + (swap ? "(normal)" : "(important)" ) + " 2"); |
michael@0 | 209 | |
michael@0 | 210 | firstType.removeRules(win, firstStyle); |
michael@0 | 211 | secondType.removeRules(win, secondStyle); |
michael@0 | 212 | |
michael@0 | 213 | is(cs.getPropertyValue('color'), 'rgb(0, 0, 0)', firstType.type + " vs " + secondType.type + " 3"); |
michael@0 | 214 | is(cs.getPropertyValue('background-color'), 'transparent', firstType.type + " vs " + secondType.type + " 4"); |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | // There are 8 cases. Regular against regular, regular against important, important |
michael@0 | 218 | // against regular, important against important. We can load style from typeA first |
michael@0 | 219 | // then typeB or the other way around so that's 4*2=8 cases. |
michael@0 | 220 | |
michael@0 | 221 | function testStyleVsStyle(win, typeA, typeB, results) |
michael@0 | 222 | { |
michael@0 | 223 | function color(res) |
michael@0 | 224 | { |
michael@0 | 225 | return res ? typeB.color : typeA.color; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | loadAndCheck(win, typeA, typeB, false, color(results.AB.rr), color(results.AB.ii)); |
michael@0 | 229 | loadAndCheck(win, typeB, typeA, false, color(results.BA.rr), color(results.BA.ii)); |
michael@0 | 230 | |
michael@0 | 231 | loadAndCheck(win, typeA, typeB, true, color(results.AB.ri), color(results.AB.ir)); |
michael@0 | 232 | loadAndCheck(win, typeB, typeA, true, color(results.BA.ir), color(results.BA.ri)); |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | // 5 user agent normal declarations |
michael@0 | 236 | // 4 user normal declarations |
michael@0 | 237 | // 3 author normal declarations |
michael@0 | 238 | // 2 author important declarations |
michael@0 | 239 | // 1 user important declarations |
michael@0 | 240 | // 0 user agent important declarations |
michael@0 | 241 | |
michael@0 | 242 | function run() |
michael@0 | 243 | { |
michael@0 | 244 | var iframe = document.getElementById("iframe"); |
michael@0 | 245 | var win = iframe.contentWindow; |
michael@0 | 246 | |
michael@0 | 247 | // Some explanation how to interpret this result table... |
michael@0 | 248 | // in case of loading the agent style first and the user style later (AB) |
michael@0 | 249 | // if there is an important rule in both for let's say color (ii) |
michael@0 | 250 | // the rule specified in the agent style will lead (AB.ii == 0) |
michael@0 | 251 | // If both rules would be just regular rules the one specified in the user style |
michael@0 | 252 | // would lead. (AB.rr == 1). If we would load/add the rules in reverse order that |
michael@0 | 253 | // would not change that (BA.rr == 1) |
michael@0 | 254 | testStyleVsStyle(win, agent, user, |
michael@0 | 255 | {AB:{rr:1, ii:0, ri:1, ir:0}, BA:{rr:1, ii:0, ri:1, ir:0}}); |
michael@0 | 256 | |
michael@0 | 257 | testStyleVsStyle(win, agent, doc, |
michael@0 | 258 | {AB:{rr:1, ii:0, ri:1, ir:0}, BA:{rr:1, ii:0, ri:1, ir:0}}); |
michael@0 | 259 | |
michael@0 | 260 | |
michael@0 | 261 | testStyleVsStyle(win, additionalUser, agent, |
michael@0 | 262 | {AB:{rr:0, ii:1, ri:1, ir:0}, BA:{rr:0, ii:1, ri:1, ir:0}}); |
michael@0 | 263 | |
michael@0 | 264 | testStyleVsStyle(win, additionalUser, doc, |
michael@0 | 265 | {AB:{rr:1, ii:0, ri:1, ir:0}, BA:{rr:1, ii:0, ri:1, ir:0}}); |
michael@0 | 266 | |
michael@0 | 267 | testStyleVsStyle(win, additionalAgent, user, |
michael@0 | 268 | {AB:{rr:1, ii:0, ri:1, ir:0}, BA:{rr:1, ii:0, ri:1, ir:0}}); |
michael@0 | 269 | |
michael@0 | 270 | testStyleVsStyle(win, additionalAgent, doc, |
michael@0 | 271 | {AB:{rr:1, ii:0, ri:1, ir:0}, BA:{rr:1, ii:0, ri:1, ir:0}}); |
michael@0 | 272 | |
michael@0 | 273 | |
michael@0 | 274 | testStyleVsStyle(win, additionalAgent, additionalUser, |
michael@0 | 275 | {AB:{rr:1, ii:0, ri:1, ir:0}, BA:{rr:1, ii:0, ri:1, ir:0}}); |
michael@0 | 276 | |
michael@0 | 277 | testStyleVsStyle(win, author, doc, |
michael@0 | 278 | {AB:{rr:0, ii:0, ri:1, ir:0}, BA:{rr:0, ii:0, ri:1, ir:0}}); |
michael@0 | 279 | |
michael@0 | 280 | testStyleVsStyle(win, author, user, |
michael@0 | 281 | {AB:{rr:0, ii:1, ri:1, ir:0}, BA:{rr:0, ii:1, ri:1, ir:0}}); |
michael@0 | 282 | |
michael@0 | 283 | testStyleVsStyle(win, author, agent, |
michael@0 | 284 | {AB:{rr:0, ii:1, ri:1, ir:0}, BA:{rr:0, ii:1, ri:1, ir:0}}); |
michael@0 | 285 | |
michael@0 | 286 | testStyleVsStyle(win, author, additionalUser, |
michael@0 | 287 | {AB:{rr:0, ii:1, ri:1, ir:0}, BA:{rr:0, ii:1, ri:1, ir:0}}); |
michael@0 | 288 | |
michael@0 | 289 | testStyleVsStyle(win, additionalAuthor, doc, |
michael@0 | 290 | {AB:{rr:0, ii:0, ri:1, ir:0}, BA:{rr:0, ii:0, ri:1, ir:0}}); |
michael@0 | 291 | |
michael@0 | 292 | testStyleVsStyle(win, additionalAuthor, author, |
michael@0 | 293 | {AB:{rr:0, ii:0, ri:1, ir:0}, BA:{rr:0, ii:0, ri:1, ir:0}}); |
michael@0 | 294 | |
michael@0 | 295 | testStyleVsStyle(win, additionalAuthor, user, |
michael@0 | 296 | {AB:{rr:0, ii:1, ri:1, ir:0}, BA:{rr:0, ii:1, ri:1, ir:0}}); |
michael@0 | 297 | |
michael@0 | 298 | testStyleVsStyle(win, additionalAuthor, agent, |
michael@0 | 299 | {AB:{rr:0, ii:1, ri:1, ir:0}, BA:{rr:0, ii:1, ri:1, ir:0}}); |
michael@0 | 300 | |
michael@0 | 301 | testStyleVsStyle(win, additionalAuthor, additionalUser, |
michael@0 | 302 | {AB:{rr:0, ii:1, ri:1, ir:0}, BA:{rr:0, ii:1, ri:1, ir:0}}); |
michael@0 | 303 | |
michael@0 | 304 | SimpleTest.finish(); |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 308 | |
michael@0 | 309 | </script> |
michael@0 | 310 | </pre> |
michael@0 | 311 | </body> |
michael@0 | 312 | </html> |