Bug 1627577 part 1: Change printing reftests to work with remote printing. r=emilio
authorBob Owen <bobowencode@gmail.com>
Fri, 22 May 2020 12:26:56 +0000
changeset 531665 103b671e8cb02427afdd3e7a128588479c981cd9
parent 531664 45f3db139940749ed7bce0b0e40b18846425c5b3
child 531666 763932c16c297127a1b3d4202f808394633bf31f
push id37441
push userapavel@mozilla.com
push dateFri, 22 May 2020 21:38:53 +0000
treeherdermozilla-central@d6abd35b54ad [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersemilio
bugs1627577
milestone78.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1627577 part 1: Change printing reftests to work with remote printing. r=emilio Differential Revision: https://phabricator.services.mozilla.com/D73095
layout/tools/reftest/reftest-content.js
layout/tools/reftest/reftest.jsm
--- a/layout/tools/reftest/reftest-content.js
+++ b/layout/tools/reftest/reftest-content.js
@@ -247,20 +247,21 @@ function printToPdf(callback) {
         if (printRange === 'selection') {
             isPrintSelection = true;
         } else if (!/^[1-9]\d*-[1-9]\d*$/.test(printRange)) {
             SendException("invalid value for reftest-print-range");
             return;
         }
     }
 
-    let fileName = "reftest-print.pdf";
+    let fileName =`reftest-print-${Date.now()}-`;
+    content.crypto.getRandomValues(new Uint8Array(4)).forEach(x => fileName += x.toString(16));
+    fileName += ".pdf"
     let file = Services.dirsvc.get("TmpD", Ci.nsIFile);
     file.append(fileName);
-    file.createUnique(file.NORMAL_FILE_TYPE, 0o644);
 
     let PSSVC = Cc[PRINTSETTINGS_CONTRACTID].getService(Ci.nsIPrintSettingsService);
     let ps = PSSVC.newPrintSettings;
     ps.printSilent = true;
     ps.showPrintProgress = false;
     ps.printBGImages = true;
     ps.printBGColors = true;
     ps.printToFile = true;
--- a/layout/tools/reftest/reftest.jsm
+++ b/layout/tools/reftest/reftest.jsm
@@ -24,24 +24,16 @@ const { E10SUtils } = ChromeUtils.import
   "resource://gre/modules/E10SUtils.jsm"
 );
 
 XPCOMUtils.defineLazyGetter(this, "OS", function() {
     const { OS } = Cu.import("resource://gre/modules/osfile.jsm");
     return OS;
 });
 
-XPCOMUtils.defineLazyGetter(this, "PDFJS", function() {
-    const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm");
-    return {
-        main: require('resource://pdf.js/build/pdf.js'),
-        worker: require('resource://pdf.js/build/pdf.worker.js')
-    };
-});
-
 function HasUnexpectedResult()
 {
     return g.testResults.Exception > 0 ||
            g.testResults.FailedLoad > 0 ||
            g.testResults.UnexpectedFail > 0 ||
            g.testResults.UnexpectedPass > 0 ||
            g.testResults.AssertionUnexpected > 0 ||
            g.testResults.AssertionUnexpectedFixed > 0;
@@ -288,28 +280,24 @@ function InitAndStartRefTests()
     try {
         g.focusFilterMode = prefs.getStringPref("reftest.focusFilterMode");
     } catch(e) {}
 
     try {
         g.compareRetainedDisplayLists = prefs.getBoolPref("reftest.compareRetainedDisplayLists");
     } catch (e) {}
 
-#ifdef MOZ_ENABLE_SKIA_PDF
     try {
-        // We have to disable printing via parent or else silent print operations
-        // (the type that we use here) would be treated as non-silent -- in other
-        // words, a print dialog would appear for each print operation, which
-        // would interrupt the test run.
-        // See http://searchfox.org/mozilla-central/rev/bd39b6170f04afeefc751a23bb04e18bbd10352b/layout/printing/nsPrintEngine.cpp#617
-        prefs.setBoolPref("print.print_via_parent", false);
+        // We have to set print.always_print_silent or a print dialog would
+        // appear for each print operation, which would interrupt the test run.
+        prefs.setBoolPref("print.always_print_silent", true);
     } catch (e) {
         /* uh oh, print reftests may not work... */
+        logger.warning("Failed to set silent printing pref, EXCEPTION: " + e);
     }
-#endif
 
     g.windowUtils = g.containingWindow.windowUtils;
     if (!g.windowUtils || !g.windowUtils.compareCanvases)
         throw "nsIDOMWindowUtils inteface missing";
 
     g.ioService = Cc[IO_SERVICE_CONTRACTID].getService(Ci.nsIIOService);
     g.debug = Cc[DEBUG_CONTRACTID].getService(Ci.nsIDebug2);
 
@@ -1753,55 +1741,67 @@ function SendLoadTest(type, uri, uriTarg
     );
 }
 
 function SendResetRenderingState()
 {
     g.browserMessageManager.sendAsyncMessage("reftest:ResetRenderingState");
 }
 
+var pdfjsHasLoaded;
+
+function pdfjsHasLoadedPromise() {
+  if (pdfjsHasLoaded === undefined) {
+    pdfjsHasLoaded = new Promise((resolve, reject) => {
+      let doc = g.containingWindow.document;
+      const script = doc.createElement("script");
+      script.src = "resource://pdf.js/build/pdf.js";
+      script.onload = resolve;
+      script.onerror = () => reject(new Error("PDF.js script load failed."));
+      doc.documentElement.appendChild(script);
+    });
+  }
+
+  return pdfjsHasLoaded;
+}
+
 function readPdf(path, callback) {
     OS.File.open(path, { read: true }).then(function (file) {
-        file.flush().then(function() {
-            file.read().then(function (data) {
-                let fakePort = new PDFJS.main.LoopbackPort(true);
-                PDFJS.worker.WorkerMessageHandler.initializeFromPort(fakePort);
-                let myWorker = new PDFJS.main.PDFWorker("worker", fakePort);
-                PDFJS.main.GlobalWorkerOptions.workerSrc = "resource://pdf.js/build/pdf.worker.js";
-                PDFJS.main.getDocument({
-                    worker: myWorker,
-                    data: data
-                }).promise.then(function (pdf) {
-                    callback(null, pdf);
-                }, function () {
-                    callback(new Error("Couldn't parse " + path));
-                });
-                return;
-            }, function () {
-                callback(new Error("Couldn't read PDF"));
+        file.read().then(function (data) {
+            pdfjsLib.GlobalWorkerOptions.workerSrc = "resource://pdf.js/build/pdf.worker.js";
+            pdfjsLib.getDocument({
+                data: data
+            }).promise.then(function (pdf) {
+                callback(null, pdf);
+            }, function (e) {
+                callback(new Error(`Couldn't parse ${path}, exception: ${e}`));
             });
+            return;
+        }, function (e) {
+            callback(new Error(`Couldn't read PDF ${path}, exception: ${e}`));
         });
     });
 }
 
 function comparePdfs(pathToTestPdf, pathToRefPdf, callback) {
-    Promise.all([pathToTestPdf, pathToRefPdf].map(function(path) {
+    pdfjsHasLoadedPromise().then(() =>
+      Promise.all([pathToTestPdf, pathToRefPdf].map(function(path) {
         return new Promise(function(resolve, reject) {
             readPdf(path, function(error, pdf) {
                 // Resolve or reject outer promise. reject and resolve are
                 // passed to the callback function given as first arguments
                 // to the Promise constructor.
                 if (error) {
                     reject(error);
                 } else {
                     resolve(pdf);
                 }
             });
         });
-    })).then(function(pdfs) {
+    }))).then(function(pdfs) {
         let numberOfPages = pdfs[1].numPages;
         let sameNumberOfPages = numberOfPages === pdfs[0].numPages;
 
         let resultPromises = [Promise.resolve({
             passed: sameNumberOfPages,
             description: "Expected number of pages: " + numberOfPages +
                                              ", got " + pdfs[0].numPages
         })];