From 630d892dd694aad313dc7a4f5f36e816e8c45814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kan-Ru=20Chen=20=28=E9=99=B3=E4=BE=83=E5=A6=82=29?= Date: Fri, 31 Oct 2014 10:39:14 +0800 Subject: [PATCH] Bug 1090759 - Part 3. Fix tests broken by DOM Promise's asyncness r=smaug --- .../mochitest/browserElementTestHelpers.js | 16 +++++++++---- .../mochitest/priority/test_Audio.html | 19 ++++++++------- .../mochitest/priority/test_Background.html | 13 +++++------ .../priority/test_BackgroundLRU.html | 12 +++++----- .../priority/test_ExpectingSystemMessage.html | 3 +-- .../test_ExpectingSystemMessage2.html | 12 +++++----- .../mochitest/priority/test_HighPriority.html | 16 ++++++++----- .../priority/test_HighPriorityDowngrade.html | 3 +-- .../priority/test_HighPriorityDowngrade2.html | 3 +-- .../mochitest/priority/test_Keyboard.html | 13 +++++------ .../priority/test_MultipleFrames.html | 23 +++++++++++-------- .../mochitest/priority/test_NestedFrames.html | 3 +-- .../mochitest/priority/test_Preallocated.html | 4 +--- .../mochitest/priority/test_Simple.html | 4 +--- .../mochitest/priority/test_Visibility.html | 12 +++++----- .../priority/test_WebGLContextLost.html | 19 ++++++++------- 16 files changed, 89 insertions(+), 86 deletions(-) diff --git a/dom/browser-element/mochitest/browserElementTestHelpers.js b/dom/browser-element/mochitest/browserElementTestHelpers.js index 72a52d8a53a7..f6d9a8671960 100644 --- a/dom/browser-element/mochitest/browserElementTestHelpers.js +++ b/dom/browser-element/mochitest/browserElementTestHelpers.js @@ -111,7 +111,8 @@ const browserElementTestHelpers = { // Returns a promise which is resolved when a subprocess is created. The // argument to resolve() is the childID of the subprocess. -function expectProcessCreated() { +function expectProcessCreated(/* optional */ initialPriority, + /* optional */ initialCPUPriority) { return new Promise(function(resolve, reject) { var observed = false; browserElementTestHelpers.addProcessPriorityObserver( @@ -126,7 +127,13 @@ function expectProcessCreated() { var childID = parseInt(data); ok(true, 'Got new process, id=' + childID); - resolve(childID); + if (initialPriority) { + expectPriorityChange(childID, initialPriority, initialCPUPriority).then(function() { + resolve(childID); + }); + } else { + resolve(childID); + } } ); }); @@ -134,8 +141,9 @@ function expectProcessCreated() { // Just like expectProcessCreated(), except we'll call ok(false) if a second // process is created. -function expectOnlyOneProcessCreated() { - var p = expectProcessCreated(); +function expectOnlyOneProcessCreated(/* optional */ initialPriority, + /* optional */ initialCPUPriority) { + var p = expectProcessCreated(initialPriority, initialCPUPriority); p.then(function() { expectProcessCreated().then(function(childID) { ok(false, 'Got unexpected process creation, childID=' + childID); diff --git a/dom/browser-element/mochitest/priority/test_Audio.html b/dom/browser-element/mochitest/priority/test_Audio.html index c78c136ebc9e..a29e56fcf501 100644 --- a/dom/browser-element/mochitest/priority/test_Audio.html +++ b/dom/browser-element/mochitest/priority/test_Audio.html @@ -24,16 +24,15 @@ function runTest() { iframe.src = 'file_Audio.html'; var childID = null; - expectOnlyOneProcessCreated().then(function(chid) { - childID = chid; - return Promise.all( - [expectPriorityChange(childID, 'FOREGROUND'), - expectMozbrowserEvent(iframe, 'loadend'), - expectMozbrowserEvent(iframe, 'showmodalprompt').then(function(e) { - is(e.detail.message, 'onplay', 'showmodalprompt message'); - })] - ); - }).then(function() { + Promise.all([ + expectOnlyOneProcessCreated("FOREGROUND").then(function(chid) { + childID = chid; + }), + expectMozbrowserEvent(iframe, 'loadend'), + expectMozbrowserEvent(iframe, 'showmodalprompt').then(function(e) { + is(e.detail.message, 'onplay', 'showmodalprompt message'); + }) + ]).then(function() { // Send the child process into the background. Because it's playing audio, // it should get priority BACKGROUND_PERCEIVABLE, not vanilla BACKGROUND. var p = expectPriorityChange(childID, 'BACKGROUND_PERCEIVABLE'); diff --git a/dom/browser-element/mochitest/priority/test_Background.html b/dom/browser-element/mochitest/priority/test_Background.html index cf5c6fac6d0f..a0a7fb32c880 100644 --- a/dom/browser-element/mochitest/priority/test_Background.html +++ b/dom/browser-element/mochitest/priority/test_Background.html @@ -26,13 +26,12 @@ function runTest() { iframe.src = browserElementTestHelpers.emptyPage1; var childID = null; - expectOnlyOneProcessCreated().then(function(chid) { - childID = chid; - }).then(function() { - return expectPriorityChange(childID, 'FOREGROUND'); - }).then(function() { - return expectMozbrowserEvent(iframe, 'loadend'); - }).then(function() { + Promise.all([ + expectOnlyOneProcessCreated('FOREGROUND').then(function(chid) { + childID = chid; + }), + expectMozbrowserEvent(iframe, 'loadend') + ]).then(function() { var p = expectPriorityChange(childID, 'BACKGROUND'); // We wait until mozbrowserloadend before calling setVisible, because diff --git a/dom/browser-element/mochitest/priority/test_BackgroundLRU.html b/dom/browser-element/mochitest/priority/test_BackgroundLRU.html index 281f12662cb5..183c2671ef97 100644 --- a/dom/browser-element/mochitest/priority/test_BackgroundLRU.html +++ b/dom/browser-element/mochitest/priority/test_BackgroundLRU.html @@ -28,12 +28,12 @@ function runTest() { var iframe2 = null; var childID = null; - expectProcessCreated().then(function(chid) { - childID = chid; - return expectPriorityChange(childID, 'FOREGROUND'); - }).then(function() { - return expectMozbrowserEvent(iframe1, 'openwindow'); - }).then(function() { + Promise.all([ + expectProcessCreated('FOREGROUND').then(function(chid) { + childID = chid; + }), + expectMozbrowserEvent(iframe1, 'openwindow') + ]).then(function() { var p = expectPriorityChange(childID, 'BACKGROUND'); iframe1.setVisible(false); return p; diff --git a/dom/browser-element/mochitest/priority/test_ExpectingSystemMessage.html b/dom/browser-element/mochitest/priority/test_ExpectingSystemMessage.html index 916972695799..4ec4b725494c 100644 --- a/dom/browser-element/mochitest/priority/test_ExpectingSystemMessage.html +++ b/dom/browser-element/mochitest/priority/test_ExpectingSystemMessage.html @@ -30,9 +30,8 @@ function runTest() { iframe.src = browserElementTestHelpers.emptyPage1; var childID = null; - expectOnlyOneProcessCreated().then(function(chid) { + expectOnlyOneProcessCreated('FOREGROUND_HIGH').then(function(chid) { childID = chid; - return expectPriorityChange(childID, 'FOREGROUND_HIGH'); }).then(function() { // We go back to foreground when the wake lock taken on behalf of our new // process times out. diff --git a/dom/browser-element/mochitest/priority/test_ExpectingSystemMessage2.html b/dom/browser-element/mochitest/priority/test_ExpectingSystemMessage2.html index 92d99bf751ea..7658cb4d6182 100644 --- a/dom/browser-element/mochitest/priority/test_ExpectingSystemMessage2.html +++ b/dom/browser-element/mochitest/priority/test_ExpectingSystemMessage2.html @@ -42,12 +42,12 @@ function runTest() { iframe.src = browserElementTestHelpers.emptyPage1; var childID = null; - expectOnlyOneProcessCreated().then(function(chid) { - childID = chid; - return Promise.all( - [expectPriorityChange(childID, 'FOREGROUND'), - expectMozbrowserEvent(iframe, 'loadend')]); - }).then(function() { + Promise.all([ + expectOnlyOneProcessCreated('FOREGROUND').then(function(chid) { + childID = chid; + }), + expectMozbrowserEvent(iframe, 'loadend') + ]).then(function() { var p = expectPriorityChange(childID, 'BACKGROUND_PERCEIVABLE'); iframe.setVisible(false); return p; diff --git a/dom/browser-element/mochitest/priority/test_HighPriority.html b/dom/browser-element/mochitest/priority/test_HighPriority.html index a99cf874429c..d3396d65b6d6 100644 --- a/dom/browser-element/mochitest/priority/test_HighPriority.html +++ b/dom/browser-element/mochitest/priority/test_HighPriority.html @@ -89,19 +89,23 @@ function runTest() { } } - return Promise.all( - [expectMozbrowserEvent(iframe, 'showmodalprompt').then(checkAlertInfo), - expectPriorityChange(childID, priority).then(checkGracePeriod)] - ).then(function(results) { + return Promise.all([ + new Promise(function(resolve, reject) { + iframe.addEventListener('mozbrowsershowmodalprompt', function check(e) { + iframe.removeEventListener('mozbrowsershowmodalprompt', check); + resolve(checkAlertInfo(e)); + }); + }), + expectPriorityChange(childID, priority).then(checkGracePeriod) + ]).then(function(results) { // checkAlertInfo returns the function to call to unblock the alert. // It comes to us as the first element of the results array. results[0](); }); } - expectProcessCreated().then(function(chid) { + expectProcessCreated('FOREGROUND').then(function(chid) { childID = chid; - return expectPriorityChange(childID, 'FOREGROUND'); }).then(function() { return expectAlertAndPriorityChange(0, 'FOREGROUND_HIGH'); }).then(function() { diff --git a/dom/browser-element/mochitest/priority/test_HighPriorityDowngrade.html b/dom/browser-element/mochitest/priority/test_HighPriorityDowngrade.html index 0894ece6d7b3..3f2d71b0ffbb 100644 --- a/dom/browser-element/mochitest/priority/test_HighPriorityDowngrade.html +++ b/dom/browser-element/mochitest/priority/test_HighPriorityDowngrade.html @@ -34,9 +34,8 @@ function runTest() { var lock = null; var p = null; - expectProcessCreated().then(function(chid) { + expectProcessCreated('FOREGROUND', 'CPU_NORMAL').then(function(chid) { childID = chid; - return expectPriorityChange(childID, 'FOREGROUND', 'CPU_NORMAL'); }).then(function() { // Create a new, high-priority iframe. highPriorityIframe = document.createElement('iframe'); diff --git a/dom/browser-element/mochitest/priority/test_HighPriorityDowngrade2.html b/dom/browser-element/mochitest/priority/test_HighPriorityDowngrade2.html index 67fe60174cdd..494733410845 100644 --- a/dom/browser-element/mochitest/priority/test_HighPriorityDowngrade2.html +++ b/dom/browser-element/mochitest/priority/test_HighPriorityDowngrade2.html @@ -33,9 +33,8 @@ function runTest() { var highPriorityIframe = null; var childID = null; - expectProcessCreated().then(function(chid) { + expectProcessCreated('FOREGROUND', 'CPU_NORMAL').then(function(chid) { childID = chid; - return expectPriorityChange(childID, 'FOREGROUND', 'CPU_NORMAL'); }).then(function() { // Create a new, high-priority iframe. highPriorityIframe = document.createElement('iframe'); diff --git a/dom/browser-element/mochitest/priority/test_Keyboard.html b/dom/browser-element/mochitest/priority/test_Keyboard.html index 864e22d1ff07..70e0870a79df 100644 --- a/dom/browser-element/mochitest/priority/test_Keyboard.html +++ b/dom/browser-element/mochitest/priority/test_Keyboard.html @@ -26,13 +26,12 @@ function runTest() { iframe.src = browserElementTestHelpers.emptyPage1; var childID = null; - expectOnlyOneProcessCreated().then(function(chid) { - childID = chid; - }).then(function() { - return expectPriorityChange(childID, 'FOREGROUND_KEYBOARD'); - }).then(function() { - return expectMozbrowserEvent(iframe, 'loadend'); - }).then(function() { + Promise.all([ + expectOnlyOneProcessCreated('FOREGROUND_KEYBOARD').then(function(chid) { + childID = chid; + }), + expectMozbrowserEvent(iframe, 'loadend') + ]).then(function() { var p = expectPriorityChange(childID, 'BACKGROUND'); /* We wait until mozbrowserloadend before calling setVisible, because diff --git a/dom/browser-element/mochitest/priority/test_MultipleFrames.html b/dom/browser-element/mochitest/priority/test_MultipleFrames.html index 90efc2d1383f..632a8dd1e977 100644 --- a/dom/browser-element/mochitest/priority/test_MultipleFrames.html +++ b/dom/browser-element/mochitest/priority/test_MultipleFrames.html @@ -26,16 +26,19 @@ function runTest() { var childID = null; var iframe2; - expectProcessCreated().then(function(chid) { - childID = chid; - return expectPriorityChange(childID, 'FOREGROUND'); - }).then(function() { - return expectMozbrowserEvent(iframe, 'openwindow'); - }).then(function(e) { - iframe2 = e.detail.frameElement; - document.body.appendChild(iframe2); - return expectMozbrowserEvent(iframe2, 'loadend'); - }).then(function() { + Promise.all([ + expectProcessCreated('FOREGROUND').then(function(chid) { + childID = chid; + }), + new Promise(function(resolve, reject) { + iframe.addEventListener('mozbrowseropenwindow', function(e) { + iframe2 = e.detail.frameElement; + var p = expectMozbrowserEvent(iframe2, 'loadend'); + document.body.appendChild(iframe2); + resolve(p); + }); + }) + ]).then(function() { // At this point, the child process has been set to FOREGROUND, and the popup // opened by file_MultipleFrames has finished loading. // diff --git a/dom/browser-element/mochitest/priority/test_NestedFrames.html b/dom/browser-element/mochitest/priority/test_NestedFrames.html index 7d9899bdd22d..3696a2c497f2 100644 --- a/dom/browser-element/mochitest/priority/test_NestedFrames.html +++ b/dom/browser-element/mochitest/priority/test_NestedFrames.html @@ -50,9 +50,8 @@ function runTest() { // outer iframe runs in-process (because it has remote=false). var childID = null; Promise.all( - [expectOnlyOneProcessCreated().then(function(child) { + [expectOnlyOneProcessCreated('FOREGROUND').then(function(child) { childID = child; - return expectPriorityChange(childID, 'FOREGROUND'); }), expectMozbrowserEvent(iframe, 'loadend')] ).then(function() { diff --git a/dom/browser-element/mochitest/priority/test_Preallocated.html b/dom/browser-element/mochitest/priority/test_Preallocated.html index 8096df70dfe2..0e05daf73e7b 100644 --- a/dom/browser-element/mochitest/priority/test_Preallocated.html +++ b/dom/browser-element/mochitest/priority/test_Preallocated.html @@ -60,9 +60,7 @@ function runTest() // Ensure that the preallocated process initially gets BACKGROUND priority. // That's it. - expectProcessCreated().then(function(childID) { - return expectPriorityChange(childID, 'PREALLOC', 'CPU_LOW'); - }).then(function() { + expectProcessCreated('PREALLOC', 'CPU_LOW').then(function() { // We need to set the pref asynchoronously or the preallocated process won't // be shut down. SimpleTest.executeSoon(function(){ diff --git a/dom/browser-element/mochitest/priority/test_Simple.html b/dom/browser-element/mochitest/priority/test_Simple.html index 5e0e6f74dbe2..03ca47684bc7 100644 --- a/dom/browser-element/mochitest/priority/test_Simple.html +++ b/dom/browser-element/mochitest/priority/test_Simple.html @@ -36,9 +36,7 @@ function runTest() { iframe.setAttribute('mozbrowser', true); iframe.src = browserElementTestHelpers.emptyPage1; - expectProcessCreated().then(function(childID) { - return expectPriorityChange(childID, 'FOREGROUND'); - }).then(SimpleTest.finish); + expectProcessCreated('FOREGROUND').then(SimpleTest.finish); document.body.appendChild(iframe); } diff --git a/dom/browser-element/mochitest/priority/test_Visibility.html b/dom/browser-element/mochitest/priority/test_Visibility.html index 229c73a35579..01bdef837069 100644 --- a/dom/browser-element/mochitest/priority/test_Visibility.html +++ b/dom/browser-element/mochitest/priority/test_Visibility.html @@ -24,12 +24,12 @@ function runTest() { iframe.src = browserElementTestHelpers.emptyPage1; var childID = null; - expectOnlyOneProcessCreated().then(function(chid) { - childID = chid; - return Promise.all( - [expectPriorityChange(childID, 'FOREGROUND'), - expectMozbrowserEvent(iframe, 'loadend')]); - }).then(function() { + Promise.all([ + expectOnlyOneProcessCreated('FOREGROUND').then(function(chid) { + childID = chid; + }), + expectMozbrowserEvent(iframe, 'loadend') + ]).then(function() { // Mark the frame as not visible. This should cause its priority to drop // to BACKGROUND. var p = expectPriorityChange(childID, 'BACKGROUND'); diff --git a/dom/browser-element/mochitest/priority/test_WebGLContextLost.html b/dom/browser-element/mochitest/priority/test_WebGLContextLost.html index c163c8446228..383506ec6e32 100644 --- a/dom/browser-element/mochitest/priority/test_WebGLContextLost.html +++ b/dom/browser-element/mochitest/priority/test_WebGLContextLost.html @@ -44,16 +44,15 @@ function runTest() { }).then(finishOnce); var childID = null; - expectOnlyOneProcessCreated().then(function(chid) { - childID = chid; - return Promise.all( - [expectPriorityChange(childID, 'FOREGROUND'), - expectMozbrowserEvent(iframe, 'loadend'), - expectMozbrowserEvent(iframe, 'showmodalprompt').then(function(e) { - is(e.detail.message, 'ready'); - }) - ]); - }).then(function() { + Promise.all([ + expectOnlyOneProcessCreated('FOREGROUND').then(function(chid) { + childID = chid; + }), + expectMozbrowserEvent(iframe, 'loadend'), + expectMozbrowserEvent(iframe, 'showmodalprompt').then(function(e) { + is(e.detail.message, 'ready'); + }) + ]).then(function() { // Fire a low-memory notification once the process goes into the background // due to the setVisible(false) call below. expectPriorityChange(childID, 'BACKGROUND').then(function() {