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"/>
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>
12 <script type=
"application/javascript">
14 var gIOService = SpecialPowers.Cc[
"@mozilla.org/network/io-service;1"]
15 .getService(SpecialPowers.Ci.nsIIOService)
17 var gSSService = SpecialPowers.Cc[
"@mozilla.org/content/style-sheet-service;1"]
18 .getService(SpecialPowers.Ci.nsIStyleSheetService);
20 function getUri(style)
22 return
"data:text/css," + style;
25 function getStyle(color, swapped)
27 return
"body {color: " + color + (swapped ?
" !important" :
"") +
28 "; background-color: " + color + (swapped ?
"" :
" !important;") +
";}";
31 function loadUserSheet(win, style)
33 loadSheet(win, style,
"USER_SHEET");
36 function loadAgentSheet(win, style)
38 loadSheet(win, style,
"AGENT_SHEET");
41 function loadAuthorSheet(win, style)
43 loadSheet(win, style,
"AUTHOR_SHEET");
46 function removeUserSheet(win, style)
48 removeSheet(win, style,
"USER_SHEET");
51 function removeAgentSheet(win, style)
53 removeSheet(win, style,
"AGENT_SHEET");
56 function removeAuthorSheet(win, style)
58 removeSheet(win, style,
"AUTHOR_SHEET");
61 function loadSheet(win, style, type)
63 var uri = gIOService.newURI(getUri(style));
64 var windowUtils = SpecialPowers.getDOMWindowUtils(win);
65 windowUtils.loadSheet(uri, windowUtils[type]);
68 function removeSheet(win, style, type)
70 var uri = gIOService.newURI(getUri(style));
71 var windowUtils = SpecialPowers.getDOMWindowUtils(win);
72 windowUtils.removeSheet(uri, windowUtils[type]);
75 function loadAndRegisterUserSheet(win, style)
77 loadAndRegisterSheet(win, style,
"USER_SHEET");
80 function loadAndRegisterAgentSheet(win, style)
82 loadAndRegisterSheet(win, style,
"AGENT_SHEET");
85 function loadAndRegisterAuthorSheet(win, style)
87 loadAndRegisterSheet(win, style,
"AUTHOR_SHEET");
90 function unregisterUserSheet(win, style)
92 unregisterSheet(win, style,
"USER_SHEET");
95 function unregisterAgentSheet(win, style)
97 unregisterSheet(win, style,
"AGENT_SHEET");
100 function unregisterAuthorSheet(win, style)
102 unregisterSheet(win, style,
"AUTHOR_SHEET");
105 function loadAndRegisterSheet(win, style, type)
107 uri = gIOService.newURI(getUri(style));
108 gSSService.loadAndRegisterSheet(uri, gSSService[type]);
109 is(gSSService.sheetRegistered(uri, gSSService[type]), true);
112 function unregisterSheet(win, style, type)
114 var uri = gIOService.newURI(getUri(style));
115 gSSService.unregisterSheet(uri, gSSService[type]);
116 is(gSSService.sheetRegistered(uri, gSSService[type]), false);
119 function setDocSheet(win, style)
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);
130 function removeDocSheet(win)
132 var subdoc = win.document;
133 var node = subdoc.getElementById('docsheet');
139 color: 'rgb(
255,
0,
0)',
140 addRules: loadAndRegisterAgentSheet,
141 removeRules: unregisterAgentSheet
146 color: 'rgb(
0,
255,
0)',
147 addRules: loadAndRegisterUserSheet,
148 removeRules: unregisterUserSheet
151 var additionalAgent = {
152 type: 'additionalAgent',
153 color: 'rgb(
0,
0,
255)',
154 addRules: loadAgentSheet,
155 removeRules: removeAgentSheet
158 var additionalUser = {
159 type: 'additionalUser',
160 color: 'rgb(
255,
255,
0)',
161 addRules: loadUserSheet,
162 removeRules: removeUserSheet
165 var additionalAuthor = {
166 type: 'additionalAuthor',
167 color: 'rgb(
255,
255,
0)',
168 addRules: loadAuthorSheet,
169 removeRules: removeAuthorSheet
174 color: 'rgb(
0,
255,
255)',
175 addRules: setDocSheet,
176 removeRules: removeDocSheet
181 color: 'rgb(
255,
0,
255)',
182 addRules: loadAndRegisterAuthorSheet,
183 removeRules: unregisterAuthorSheet
186 function loadAndCheck(win, firstType, secondType, swap, result1, result2)
188 var firstStyle = getStyle(firstType.color, false);
189 var secondStyle = getStyle(secondType.color, swap);
191 firstType.addRules(win, firstStyle);
192 secondType.addRules(win, secondStyle);
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");
200 firstType.removeRules(win, firstStyle);
201 secondType.removeRules(win, secondStyle);
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");
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.
211 function testStyleVsStyle(win, typeA, typeB, results)
215 return res ? typeB.color : typeA.color;
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));
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));
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
234 var iframe = document.getElementById(
"iframe");
235 var win = iframe.contentWindow;
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}});
247 testStyleVsStyle(win, agent, doc,
248 {AB:{rr:
1, ii:
0, ri:
1, ir:
0}, BA:{rr:
1, ii:
0, ri:
1, ir:
0}});
251 testStyleVsStyle(win, additionalUser, agent,
252 {AB:{rr:
0, ii:
1, ri:
1, ir:
0}, BA:{rr:
0, ii:
1, ri:
1, ir:
0}});
254 testStyleVsStyle(win, additionalUser, doc,
255 {AB:{rr:
1, ii:
0, ri:
1, ir:
0}, BA:{rr:
1, ii:
0, ri:
1, ir:
0}});
257 testStyleVsStyle(win, additionalAgent, user,
258 {AB:{rr:
1, ii:
0, ri:
1, ir:
0}, BA:{rr:
1, ii:
0, ri:
1, ir:
0}});
260 testStyleVsStyle(win, additionalAgent, doc,
261 {AB:{rr:
1, ii:
0, ri:
1, ir:
0}, BA:{rr:
1, ii:
0, ri:
1, ir:
0}});
264 testStyleVsStyle(win, additionalAgent, additionalUser,
265 {AB:{rr:
1, ii:
0, ri:
1, ir:
0}, BA:{rr:
1, ii:
0, ri:
1, ir:
0}});
267 testStyleVsStyle(win, author, doc,
268 {AB:{rr:
0, ii:
0, ri:
1, ir:
0}, BA:{rr:
0, ii:
0, ri:
1, ir:
0}});
270 testStyleVsStyle(win, author, user,
271 {AB:{rr:
0, ii:
1, ri:
1, ir:
0}, BA:{rr:
0, ii:
1, ri:
1, ir:
0}});
273 testStyleVsStyle(win, author, agent,
274 {AB:{rr:
0, ii:
1, ri:
1, ir:
0}, BA:{rr:
0, ii:
1, ri:
1, ir:
0}});
276 testStyleVsStyle(win, author, additionalUser,
277 {AB:{rr:
0, ii:
1, ri:
1, ir:
0}, BA:{rr:
0, ii:
1, ri:
1, ir:
0}});
279 testStyleVsStyle(win, additionalAuthor, doc,
280 {AB:{rr:
0, ii:
0, ri:
1, ir:
0}, BA:{rr:
0, ii:
0, ri:
1, ir:
0}});
282 testStyleVsStyle(win, additionalAuthor, author,
283 {AB:{rr:
0, ii:
0, ri:
1, ir:
0}, BA:{rr:
0, ii:
0, ri:
1, ir:
0}});
285 testStyleVsStyle(win, additionalAuthor, user,
286 {AB:{rr:
0, ii:
1, ri:
1, ir:
0}, BA:{rr:
0, ii:
1, ri:
1, ir:
0}});
288 testStyleVsStyle(win, additionalAuthor, agent,
289 {AB:{rr:
0, ii:
1, ri:
1, ir:
0}, BA:{rr:
0, ii:
1, ri:
1, ir:
0}});
291 testStyleVsStyle(win, additionalAuthor, additionalUser,
292 {AB:{rr:
0, ii:
1, ri:
1, ir:
0}, BA:{rr:
0, ii:
1, ri:
1, ir:
0}});
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);
305 SimpleTest.waitForExplicitFinish();