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