layout/style/test/chrome/test_additional_sheets.html

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

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

mercurial