layout/base/tests/chrome/printpreview_helper.xul

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial