Bug 1465078 - Wait for MozAfterPaint on new windows before requesting fullscreen. r?xidorn draft
authorKartikaya Gupta <kgupta@mozilla.com>
Tue, 29 May 2018 11:33:25 -0400
changeset 800971 478f8a16e729b19faa8fc05ee80eb56a5c31a4e0
parent 800874 f01bb6245db1ea2a87e5360104a4110571265137
push id111538
push userkgupta@mozilla.com
push dateTue, 29 May 2018 15:40:21 +0000
reviewersxidorn
bugs1465078
milestone62.0a1
Bug 1465078 - Wait for MozAfterPaint on new windows before requesting fullscreen. r?xidorn MozReview-Commit-ID: 7cgujvsCdfh
dom/html/test/file_fullscreen-multiple.html
dom/html/test/file_fullscreen-unprefix-disabled.html
dom/html/test/file_fullscreen-utils.js
dom/html/test/test_fullscreen-api.html
--- a/dom/html/test/file_fullscreen-multiple.html
+++ b/dom/html/test/file_fullscreen-multiple.html
@@ -28,17 +28,17 @@ function ok(condition, msg) {
 function is(a, b, msg) {
   opener.is(a, b, "[multiple] " + msg);
 }
 
 var window1, window2;
 
 function openWindow(id) {
   var w = window.open("file_fullscreen-multiple-inner.html", "", "width=500,height=500");
-  w.addEventListener("load", function onload() {
+  waitForLoadAndPaint(w, function() {
     w.focus();
     SimpleTest.waitForFocus(function(){w.begin(id)}, w);
   });
   return w;
 }
 
 function begin() {
   window1 = openWindow("one");
--- a/dom/html/test/file_fullscreen-unprefix-disabled.html
+++ b/dom/html/test/file_fullscreen-unprefix-disabled.html
@@ -1,28 +1,29 @@
 <!DOCTYPE html>
 <html>
 <head>
   <meta charset="UTF-8">
   <title>Test for Bug 1268749</title>
   <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="application/javascript" src="file_fullscreen-utils.js"></script>
 </head>
 <body>
 <script>
 
 var gWindow = null;
 
 function begin() {
   SpecialPowers.pushPrefEnv({
     "set": [["full-screen-api.unprefix.enabled", false]]
   }, () => {
     gWindow = window.open("file_fullscreen-unprefix-disabled-inner.html",
                           "", "width=500,height=500");
-    gWindow.addEventListener("load", () => {
+    waitForLoadAndPaint(gWindow, () => {
       gWindow.focus();
       SimpleTest.waitForFocus(() => gWindow.begin(), gWindow);
     });
   });
 }
 
 function finish() {
   gWindow.close();
--- a/dom/html/test/file_fullscreen-utils.js
+++ b/dom/html/test/file_fullscreen-utils.js
@@ -75,8 +75,22 @@ function addFullscreenErrorContinuation(
   var doc = inDoc || document;
   var listener = function(event) {
     doc.removeEventListener("fullscreenerror", listener);
     setTimeout(function(){callback(event);}, 0);
   };
   doc.addEventListener("fullscreenerror", listener);
 }
 
+// Waits until the window has both the load event and a MozAfterPaint called on
+// it, and then invokes the callback
+function waitForLoadAndPaint(win, callback) {
+  win.addEventListener("MozAfterPaint", function() {
+    // The load event may have fired before the MozAfterPaint, in which case
+    // listening for it now will hang. Instead we check the readyState to see if
+    // it already fired, and if so, invoke the callback right away.
+    if (win.document.readyState == 'complete') {
+      callback();
+    } else {
+      win.addEventListener("load", callback, {once: true});
+    }
+  }, { once: true });
+}
--- a/dom/html/test/test_fullscreen-api.html
+++ b/dom/html/test/test_fullscreen-api.html
@@ -1,15 +1,16 @@
  <!DOCTYPE HTML>
 <html>
 <head>
   <title>Test for Bug 545812</title>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
+  <script type="application/javascript" src="file_fullscreen-utils.js"></script>
   <style>
   body {
     background-color: black;
   }
   </style>
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=545812">Mozilla Bug 545812</a>
@@ -111,32 +112,32 @@ function runNextTest() {
       SimpleTest.executeSoon(runNextTest);
     } else {
       info(`Run test ${test}`);
       testWindow = window.open(test, "", "width=500,height=500,scrollbars=yes");
       // We'll wait for the window to load, then make sure our window is refocused
       // before starting the test, which will get kicked off on "focus".
       // This ensures that we're essentially back on the primary "desktop" on
       // OS X Lion before we run the test.
-      testWindow.addEventListener("load", function() {
+      waitForLoadAndPaint(testWindow, function() {
         SimpleTest.waitForFocus(function() {
           SimpleTest.waitForFocus(function() {
             // For the platforms that support reporting occlusion state (e.g. Mac),
             // we should wait until the docshell has been activated again,
             // otherwise, the fullscreen request might be denied.
             if (testWindow.document.hidden) {
               waitForEvent(testWindow.document, "visibilitychange", (event) => {
                 return !testWindow.document.hidden;
               }, testWindow.begin);
               return;
             }
             testWindow.begin();
           }, testWindow);
         });
-      }, {once: true});
+      });
     }
     gTestIndex++;
   } else {
     SimpleTest.finish();
   }
 }
 
 try {