layout/base/tests/chrome/printpreview_helper.xul

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <?xml version="1.0"?>
     2 <?xml-stylesheet type="text/css" href="chrome://global/skin"?>
     3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
     4                  type="text/css"?>
     5 <window onload="runTests()"
     6         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
     7 <iframe height="200" width="600" type="content"></iframe>
     8 <iframe height="200" width="600" type="content"></iframe>
     9 <script type="application/javascript">
    10 <![CDATA[
    11 var is = window.opener.wrappedJSObject.is;
    12 var isnot = window.opener.wrappedJSObject.isnot;
    13 var ok = window.opener.wrappedJSObject.ok;
    14 var todo = window.opener.wrappedJSObject.todo;
    15 var SimpleTest = window.opener.wrappedJSObject.SimpleTest;
    16 var gWbp;
    17 var ctx1;
    18 var ctx2;
    19 var counter = 0;
    21 var file = Components.classes["@mozilla.org/file/directory_service;1"]
    22              .getService(Components.interfaces.nsIProperties)
    23              .get("TmpD", Components.interfaces.nsILocalFile);
    24 filePath = file.path;
    26 function printpreview() {
    27   gWbp = window.frames[1].QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    28              .getInterface(Components.interfaces.nsIWebBrowserPrint);
    29   var listener = {
    30     onLocationChange: function(webProgress, request, location, flags) { },
    31     onProgressChange: function(webProgress, request, curSelfProgress, 
    32                                maxSelfProgress, curTotalProgress,
    33                                maxTotalProgress) { },
    34     onSecurityChange: function(webProgress, request, state) { },
    35     onStateChange: function(webProgress, request, stateFlags, status) { },
    36     onStatusChange: function(webProgress, request, status, message) { },
    37     QueryInterface: function(iid) {
    38       if (iid.equals(Components.interfaces.nsIWebProgressListener) ||
    39           iid.equals(Components.interfaces.nsISupportsWeakReference))
    40             return this;
    41       throw Components.results.NS_NOINTERFACE;
    42     }
    43   }
    44   var prefs = Components.classes["@mozilla.org/preferences-service;1"]
    45                             .getService(Components.interfaces.nsIPrefBranch);
    46   prefs.setBoolPref('print.show_print_progress', false);
    47   //XXX I would have thought this would work, instead I'm forced to use prefs service
    48   gWbp.globalPrintSettings.showPrintProgress = false;
    49   var before = 0;
    50   var after = 0;
    51   function beforeprint() { ++before; }
    52   function afterprint() { ++after; }
    53   window.frames[0].addEventListener("beforeprint", beforeprint, true);
    54   window.frames[0].addEventListener("afterprint", afterprint, true);
    55   gWbp.printPreview(gWbp.globalPrintSettings, window.frames[0], listener);
    56   is(before, 1, "Should have called beforeprint listener!");
    57   is(after, 1, "Should have called afterprint listener!");
    58   window.frames[0].removeEventListener("beforeprint", beforeprint, true);
    59   window.frames[0].removeEventListener("afterprint", afterprint, true);
    60   prefs.clearUserPref('print.show_print_progress');
    61 }
    63 function exitprintpreview() {
    64   window.frames[1].QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    65    .getInterface(Components.interfaces.nsIWebBrowserPrint).exitPrintPreview(); 
    66 }
    68 function finish() {
    69   SimpleTest.finish();
    70   window.close();
    71 }
    73 function runTests()
    74 {
    75   var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
    76                                .getService(Components.interfaces.nsIPrintSettingsService);
    78   try {
    79     Components.classes["@mozilla.org/gfx/printerenumerator;1"]
    80               .getService(Components.interfaces.nsIPrinterEnumerator);
    81   } catch(e) {
    82     todo(false, "Test skipped on MacOSX, as the print preview code doesn't work there");
    83     finish();
    84     return;
    85   }
    87   if (printService.defaultPrinterName != '') {
    88     startTest1();
    89   } else {
    90     todo(false, "No printer seems installed on this machine, that is necessary for this test");
    91     finish();
    92   }
    93 }
    95 function compareCanvases() {
    96   return window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    97                .getInterface(Components.interfaces.nsIDOMWindowUtils)
    98                .compareCanvases(document.getElementsByTagName("canvas")[0],
    99                                 document.getElementsByTagName("canvas")[1],
   100                                 {}) == 0;
   101 }
   103 function addHTMLContent(parent) {
   104   var n = parent.ownerDocument.createElement("div");
   105   parent.appendChild(n);
   106   var s = "<iframe width='500' height='40' src='data:text/plain,ThisIsAnIframeCreatedDuringPrintPreview'></iframe>";
   107   s += "<table>";
   108   for (var i = 1; i < 501; ++i) {
   109     s += "<tr><td>Cell A" + i + "</td><td>Cell B" + i + "</td><td>Cell C" + i + "</td></tr>";
   110   }
   111   s += "</table>";
   112   n.innerHTML = s;
   113 }
   115 function startTest1() {
   116   ctx1 = document.getElementsByTagName("canvas")[0].getContext("2d");
   117   ctx2 = document.getElementsByTagName("canvas")[1].getContext("2d");
   118   window.frames[0].document.body.innerHTML = "<div> </div><div>" + counter + " timers</div><div> </div>";
   120   // Note this timeout is needed so that we can check that timers run
   121   // after print preview, but not during it.
   122   window.frames[0].wrappedJSObject.counter = counter;
   123   window.frames[0].counterTimeout = "document.body.firstChild.nextSibling.innerHTML = ++counter + ' timers';" +
   124                                     "window.setTimeout(counterTimeout, 0);";
   125   window.frames[0].setTimeout(window.frames[0].counterTimeout, 0);
   126   window.frames[0].document.body.firstChild.innerHTML = "Print preview";
   128   printpreview();
   129   ctx1.drawWindow(window.frames[1], 0, 0, 300, 300, "rgb(256,256,256)");
   130   window.frames[0].document.body.firstChild.innerHTML = "Galley presentation";
   132   // Add some elements.
   133   addHTMLContent(window.frames[0].document.body.lastChild);
   134   // Delete them.
   135   window.frames[0].document.body.lastChild.innerHTML = "";
   136   // And readd.
   137   addHTMLContent(window.frames[0].document.body.lastChild);
   139   setTimeout(finalizeTest1, 1000);
   140 }
   142 function finalizeTest1() {
   143   ctx2.drawWindow(window.frames[1], 0, 0, 300, 300, "rgb(256,256,256)");
   144   exitprintpreview();
   145   ok(compareCanvases(), "Canvas should be the same!");
   146   counter = window.frames[0].counter;
   147   // This timeout is needed so that we can check that timers do run after
   148   // print preview.
   149   setTimeout(runTest2, 1000);
   150 }
   152 function runTest2() {
   153   isnot(window.frames[0].document.body.firstChild.nextSibling.textContent, "0 timers", "Timers should have run!");
   154   isnot(window.frames[0].counter, 0, "Timers should have run!");
   155   counter = window.frames[0].counter;
   156   window.frames[0].counterTimeout = "";
   157   setTimeout(runTest3, 0);
   158 }
   160 var elementIndex = 0;
   161 var compareEmptyElement = true;
   162 var emptyFormElements =
   163   ["<input type='text'>",
   164    "<input type='password'>",
   165    "<input type='file'>",
   166    "<input type='button'>",
   167    "<input type='submit'>",
   168    "<input type='reset'>",
   169    "<input type='checkbox'>",
   170    "<input type='radio'>",
   171    "<select></select>",
   172    "<select size='5'></select>",
   173    "<textarea></textarea>"];
   175 var formElements =
   176   ["<input type='text' value='text'>",
   177    "<input type='password' value='password'>",
   178    "<input type='file' value='" + filePath + "'>",
   179    "<input type='button' value='button'>",
   180    "<input type='submit' value='submit button'>",
   181    "<input type='reset' value='reset button'>",
   182    "<input type='checkbox' checked>",
   183    "<input type='radio' checked>",
   184    "<select><option>option1</option></select>",
   185    "<select size='5'><option>1</option><option>2</option><option>3</option></select>",
   186    "<textarea value='textarea'>textarea</textarea>"];
   188 function runTest3() {
   189   if (compareEmptyElement) {
   190     var currentIndex = elementIndex;
   191     ++elementIndex;
   192     if (elementIndex >= emptyFormElements.length) {
   193       elementIndex = 0;
   194       compareEmptyElement = false;
   195     }
   196     compareFormElementPrint(emptyFormElements[currentIndex], emptyFormElements[currentIndex], true);
   197     return;
   198   } else if (elementIndex < emptyFormElements.length) {
   199     var currentIndex = elementIndex;
   200     ++elementIndex;
   201     compareFormElementPrint(emptyFormElements[currentIndex], formElements[currentIndex], false);
   202     return;
   203   }
   205   setTimeout(runTest4, 0)
   206 }
   208 function compareFormElementPrint(el1, el2, equals) {
   209   window.frames[0].document.body.innerHTML = el1;
   210   window.frames[0].document.body.firstChild.value =
   211     window.frames[0].document.body.firstChild.getAttribute('value');
   212   printpreview();
   213   ctx1.drawWindow(window.frames[1], 0, 0, 300, 300, "rgb(256,256,256)");
   214   exitprintpreview();
   215   window.frames[0].document.body.innerHTML = el2;
   216   window.frames[0].document.body.firstChild.value =
   217     window.frames[0].document.body.firstChild.getAttribute('value');
   218   printpreview();
   219   ctx2.drawWindow(window.frames[1], 0, 0, 300, 300, "rgb(256,256,256)");
   220   exitprintpreview();
   221   is(compareCanvases(), equals,
   222      "Comparing print preview didn't succeed [" + el1 + " : " + el2 + "]");
   223   setTimeout(runTest3, 100);
   224 }
   226 // This is a crash test for bug 539060.
   227 function runTest4() {
   228   window.frames[0].document.body.innerHTML =
   229     "<iframe style='display: none;' src='data:text/html,<iframe>'></iframe>";
   230   setTimeout(runTest4end, 500);
   231 }
   233 function runTest4end() {
   234   printpreview();
   235   exitprintpreview();
   237   runTest5();
   238 }
   240 // This is a crash test for bug 595337
   241 function runTest5() {
   242   window.frames[0].document.body.innerHTML =
   243     '<iframe style="position: fixed; visibility: hidden; bottom: 10em;"></iframe>' +
   244     '<input contenteditable="true" style="display: table; page-break-before: left; width: 10000px;">';
   245   printpreview();
   246   exitprintpreview();
   248   setTimeout(runTest6, 0);
   249 }
   251 // Crash test for bug 878037
   252 function runTest6() {
   253   window.frames[0].document.body.innerHTML =
   254     '<style> li { list-style-image: url("animated.gif"); } </style>' +
   255     '<li>Firefox will crash if you try and print this page</li>';
   257   setTimeout(runTest6end, 500);
   258 }
   260 function runTest6end() {
   261   printpreview();
   262   exitprintpreview();
   264   finish();
   265 }
   267 ]]></script>
   268 <table style="border: 1px solid black;" xmlns="http://www.w3.org/1999/xhtml">
   269 <tr><th>Print preview canvas 1</th><th>Print preview canvas 2</th></tr>
   270 <tr>
   271 <td><canvas height="300" width="300"></canvas></td>
   272 <td><canvas height="300" width="300"></canvas></td>
   273 </tr></table>
   274 </window>

mercurial