layout/base/tests/chrome/printpreview_helper.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/base/tests/chrome/printpreview_helper.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,274 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
     1.6 +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
     1.7 +                 type="text/css"?>
     1.8 +<window onload="runTests()"
     1.9 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    1.10 +<iframe height="200" width="600" type="content"></iframe>
    1.11 +<iframe height="200" width="600" type="content"></iframe>
    1.12 +<script type="application/javascript">
    1.13 +<![CDATA[
    1.14 +var is = window.opener.wrappedJSObject.is;
    1.15 +var isnot = window.opener.wrappedJSObject.isnot;
    1.16 +var ok = window.opener.wrappedJSObject.ok;
    1.17 +var todo = window.opener.wrappedJSObject.todo;
    1.18 +var SimpleTest = window.opener.wrappedJSObject.SimpleTest;
    1.19 +var gWbp;
    1.20 +var ctx1;
    1.21 +var ctx2;
    1.22 +var counter = 0;
    1.23 +
    1.24 +var file = Components.classes["@mozilla.org/file/directory_service;1"]
    1.25 +             .getService(Components.interfaces.nsIProperties)
    1.26 +             .get("TmpD", Components.interfaces.nsILocalFile);
    1.27 +filePath = file.path;
    1.28 +
    1.29 +function printpreview() {
    1.30 +  gWbp = window.frames[1].QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    1.31 +             .getInterface(Components.interfaces.nsIWebBrowserPrint);
    1.32 +  var listener = {
    1.33 +    onLocationChange: function(webProgress, request, location, flags) { },
    1.34 +    onProgressChange: function(webProgress, request, curSelfProgress, 
    1.35 +                               maxSelfProgress, curTotalProgress,
    1.36 +                               maxTotalProgress) { },
    1.37 +    onSecurityChange: function(webProgress, request, state) { },
    1.38 +    onStateChange: function(webProgress, request, stateFlags, status) { },
    1.39 +    onStatusChange: function(webProgress, request, status, message) { },
    1.40 +    QueryInterface: function(iid) {
    1.41 +      if (iid.equals(Components.interfaces.nsIWebProgressListener) ||
    1.42 +          iid.equals(Components.interfaces.nsISupportsWeakReference))
    1.43 +            return this;
    1.44 +      throw Components.results.NS_NOINTERFACE;
    1.45 +    }
    1.46 +  }
    1.47 +  var prefs = Components.classes["@mozilla.org/preferences-service;1"]
    1.48 +                            .getService(Components.interfaces.nsIPrefBranch);
    1.49 +  prefs.setBoolPref('print.show_print_progress', false);
    1.50 +  //XXX I would have thought this would work, instead I'm forced to use prefs service
    1.51 +  gWbp.globalPrintSettings.showPrintProgress = false;
    1.52 +  var before = 0;
    1.53 +  var after = 0;
    1.54 +  function beforeprint() { ++before; }
    1.55 +  function afterprint() { ++after; }
    1.56 +  window.frames[0].addEventListener("beforeprint", beforeprint, true);
    1.57 +  window.frames[0].addEventListener("afterprint", afterprint, true);
    1.58 +  gWbp.printPreview(gWbp.globalPrintSettings, window.frames[0], listener);
    1.59 +  is(before, 1, "Should have called beforeprint listener!");
    1.60 +  is(after, 1, "Should have called afterprint listener!");
    1.61 +  window.frames[0].removeEventListener("beforeprint", beforeprint, true);
    1.62 +  window.frames[0].removeEventListener("afterprint", afterprint, true);
    1.63 +  prefs.clearUserPref('print.show_print_progress');
    1.64 +}
    1.65 +
    1.66 +function exitprintpreview() {
    1.67 +  window.frames[1].QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    1.68 +   .getInterface(Components.interfaces.nsIWebBrowserPrint).exitPrintPreview(); 
    1.69 +}
    1.70 +
    1.71 +function finish() {
    1.72 +  SimpleTest.finish();
    1.73 +  window.close();
    1.74 +}
    1.75 +
    1.76 +function runTests()
    1.77 +{
    1.78 +  var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
    1.79 +                               .getService(Components.interfaces.nsIPrintSettingsService);
    1.80 +
    1.81 +  try {
    1.82 +    Components.classes["@mozilla.org/gfx/printerenumerator;1"]
    1.83 +              .getService(Components.interfaces.nsIPrinterEnumerator);
    1.84 +  } catch(e) {
    1.85 +    todo(false, "Test skipped on MacOSX, as the print preview code doesn't work there");
    1.86 +    finish();
    1.87 +    return;
    1.88 +  }
    1.89 +
    1.90 +  if (printService.defaultPrinterName != '') {
    1.91 +    startTest1();
    1.92 +  } else {
    1.93 +    todo(false, "No printer seems installed on this machine, that is necessary for this test");
    1.94 +    finish();
    1.95 +  }
    1.96 +}
    1.97 +
    1.98 +function compareCanvases() {
    1.99 +  return window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
   1.100 +               .getInterface(Components.interfaces.nsIDOMWindowUtils)
   1.101 +               .compareCanvases(document.getElementsByTagName("canvas")[0],
   1.102 +                                document.getElementsByTagName("canvas")[1],
   1.103 +                                {}) == 0;
   1.104 +}
   1.105 +
   1.106 +function addHTMLContent(parent) {
   1.107 +  var n = parent.ownerDocument.createElement("div");
   1.108 +  parent.appendChild(n);
   1.109 +  var s = "<iframe width='500' height='40' src='data:text/plain,ThisIsAnIframeCreatedDuringPrintPreview'></iframe>";
   1.110 +  s += "<table>";
   1.111 +  for (var i = 1; i < 501; ++i) {
   1.112 +    s += "<tr><td>Cell A" + i + "</td><td>Cell B" + i + "</td><td>Cell C" + i + "</td></tr>";
   1.113 +  }
   1.114 +  s += "</table>";
   1.115 +  n.innerHTML = s;
   1.116 +}
   1.117 +
   1.118 +function startTest1() {
   1.119 +  ctx1 = document.getElementsByTagName("canvas")[0].getContext("2d");
   1.120 +  ctx2 = document.getElementsByTagName("canvas")[1].getContext("2d");
   1.121 +  window.frames[0].document.body.innerHTML = "<div> </div><div>" + counter + " timers</div><div> </div>";
   1.122 +
   1.123 +  // Note this timeout is needed so that we can check that timers run
   1.124 +  // after print preview, but not during it.
   1.125 +  window.frames[0].wrappedJSObject.counter = counter;
   1.126 +  window.frames[0].counterTimeout = "document.body.firstChild.nextSibling.innerHTML = ++counter + ' timers';" +
   1.127 +                                    "window.setTimeout(counterTimeout, 0);";
   1.128 +  window.frames[0].setTimeout(window.frames[0].counterTimeout, 0);
   1.129 +  window.frames[0].document.body.firstChild.innerHTML = "Print preview";
   1.130 +
   1.131 +  printpreview();
   1.132 +  ctx1.drawWindow(window.frames[1], 0, 0, 300, 300, "rgb(256,256,256)");
   1.133 +  window.frames[0].document.body.firstChild.innerHTML = "Galley presentation";
   1.134 +
   1.135 +  // Add some elements.
   1.136 +  addHTMLContent(window.frames[0].document.body.lastChild);
   1.137 +  // Delete them.
   1.138 +  window.frames[0].document.body.lastChild.innerHTML = "";
   1.139 +  // And readd.
   1.140 +  addHTMLContent(window.frames[0].document.body.lastChild);
   1.141 +
   1.142 +  setTimeout(finalizeTest1, 1000);
   1.143 +}
   1.144 +
   1.145 +function finalizeTest1() {
   1.146 +  ctx2.drawWindow(window.frames[1], 0, 0, 300, 300, "rgb(256,256,256)");
   1.147 +  exitprintpreview();
   1.148 +  ok(compareCanvases(), "Canvas should be the same!");
   1.149 +  counter = window.frames[0].counter;
   1.150 +  // This timeout is needed so that we can check that timers do run after
   1.151 +  // print preview.
   1.152 +  setTimeout(runTest2, 1000);
   1.153 +}
   1.154 +
   1.155 +function runTest2() {
   1.156 +  isnot(window.frames[0].document.body.firstChild.nextSibling.textContent, "0 timers", "Timers should have run!");
   1.157 +  isnot(window.frames[0].counter, 0, "Timers should have run!");
   1.158 +  counter = window.frames[0].counter;
   1.159 +  window.frames[0].counterTimeout = "";
   1.160 +  setTimeout(runTest3, 0);
   1.161 +}
   1.162 +
   1.163 +var elementIndex = 0;
   1.164 +var compareEmptyElement = true;
   1.165 +var emptyFormElements =
   1.166 +  ["<input type='text'>",
   1.167 +   "<input type='password'>",
   1.168 +   "<input type='file'>",
   1.169 +   "<input type='button'>",
   1.170 +   "<input type='submit'>",
   1.171 +   "<input type='reset'>",
   1.172 +   "<input type='checkbox'>",
   1.173 +   "<input type='radio'>",
   1.174 +   "<select></select>",
   1.175 +   "<select size='5'></select>",
   1.176 +   "<textarea></textarea>"];
   1.177 +
   1.178 +var formElements =
   1.179 +  ["<input type='text' value='text'>",
   1.180 +   "<input type='password' value='password'>",
   1.181 +   "<input type='file' value='" + filePath + "'>",
   1.182 +   "<input type='button' value='button'>",
   1.183 +   "<input type='submit' value='submit button'>",
   1.184 +   "<input type='reset' value='reset button'>",
   1.185 +   "<input type='checkbox' checked>",
   1.186 +   "<input type='radio' checked>",
   1.187 +   "<select><option>option1</option></select>",
   1.188 +   "<select size='5'><option>1</option><option>2</option><option>3</option></select>",
   1.189 +   "<textarea value='textarea'>textarea</textarea>"];
   1.190 +
   1.191 +function runTest3() {
   1.192 +  if (compareEmptyElement) {
   1.193 +    var currentIndex = elementIndex;
   1.194 +    ++elementIndex;
   1.195 +    if (elementIndex >= emptyFormElements.length) {
   1.196 +      elementIndex = 0;
   1.197 +      compareEmptyElement = false;
   1.198 +    }
   1.199 +    compareFormElementPrint(emptyFormElements[currentIndex], emptyFormElements[currentIndex], true);
   1.200 +    return;
   1.201 +  } else if (elementIndex < emptyFormElements.length) {
   1.202 +    var currentIndex = elementIndex;
   1.203 +    ++elementIndex;
   1.204 +    compareFormElementPrint(emptyFormElements[currentIndex], formElements[currentIndex], false);
   1.205 +    return;
   1.206 +  }
   1.207 +
   1.208 +  setTimeout(runTest4, 0)
   1.209 +}
   1.210 +
   1.211 +function compareFormElementPrint(el1, el2, equals) {
   1.212 +  window.frames[0].document.body.innerHTML = el1;
   1.213 +  window.frames[0].document.body.firstChild.value =
   1.214 +    window.frames[0].document.body.firstChild.getAttribute('value');
   1.215 +  printpreview();
   1.216 +  ctx1.drawWindow(window.frames[1], 0, 0, 300, 300, "rgb(256,256,256)");
   1.217 +  exitprintpreview();
   1.218 +  window.frames[0].document.body.innerHTML = el2;
   1.219 +  window.frames[0].document.body.firstChild.value =
   1.220 +    window.frames[0].document.body.firstChild.getAttribute('value');
   1.221 +  printpreview();
   1.222 +  ctx2.drawWindow(window.frames[1], 0, 0, 300, 300, "rgb(256,256,256)");
   1.223 +  exitprintpreview();
   1.224 +  is(compareCanvases(), equals,
   1.225 +     "Comparing print preview didn't succeed [" + el1 + " : " + el2 + "]");
   1.226 +  setTimeout(runTest3, 100);
   1.227 +}
   1.228 +
   1.229 +// This is a crash test for bug 539060.
   1.230 +function runTest4() {
   1.231 +  window.frames[0].document.body.innerHTML =
   1.232 +    "<iframe style='display: none;' src='data:text/html,<iframe>'></iframe>";
   1.233 +  setTimeout(runTest4end, 500);
   1.234 +}
   1.235 +
   1.236 +function runTest4end() {
   1.237 +  printpreview();
   1.238 +  exitprintpreview();
   1.239 +
   1.240 +  runTest5();
   1.241 +}
   1.242 +
   1.243 +// This is a crash test for bug 595337
   1.244 +function runTest5() {
   1.245 +  window.frames[0].document.body.innerHTML =
   1.246 +    '<iframe style="position: fixed; visibility: hidden; bottom: 10em;"></iframe>' +
   1.247 +    '<input contenteditable="true" style="display: table; page-break-before: left; width: 10000px;">';
   1.248 +  printpreview();
   1.249 +  exitprintpreview();
   1.250 +
   1.251 +  setTimeout(runTest6, 0);
   1.252 +}
   1.253 +
   1.254 +// Crash test for bug 878037
   1.255 +function runTest6() {
   1.256 +  window.frames[0].document.body.innerHTML =
   1.257 +    '<style> li { list-style-image: url("animated.gif"); } </style>' +
   1.258 +    '<li>Firefox will crash if you try and print this page</li>';
   1.259 +
   1.260 +  setTimeout(runTest6end, 500);
   1.261 +}
   1.262 +
   1.263 +function runTest6end() {
   1.264 +  printpreview();
   1.265 +  exitprintpreview();
   1.266 +
   1.267 +  finish();
   1.268 +}
   1.269 +
   1.270 +]]></script>
   1.271 +<table style="border: 1px solid black;" xmlns="http://www.w3.org/1999/xhtml">
   1.272 +<tr><th>Print preview canvas 1</th><th>Print preview canvas 2</th></tr>
   1.273 +<tr>
   1.274 +<td><canvas height="300" width="300"></canvas></td>
   1.275 +<td><canvas height="300" width="300"></canvas></td>
   1.276 +</tr></table>
   1.277 +</window>

mercurial