forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1739369 - [marionette] Move wait for initial page load logic into…
… shared navigate module. r=webdriver-reviewers,jdescottes Differential Revision: https://phabricator.services.mozilla.com/D134541
- Loading branch information
Showing
6 changed files
with
191 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
"use strict"; | ||
|
||
const EXPORTED_SYMBOLS = ["waitForInitialNavigationCompleted"]; | ||
|
||
const { XPCOMUtils } = ChromeUtils.import( | ||
"resource://gre/modules/XPCOMUtils.jsm" | ||
); | ||
|
||
XPCOMUtils.defineLazyModuleGetters(this, { | ||
Log: "chrome://remote/content/shared/Log.jsm", | ||
}); | ||
|
||
XPCOMUtils.defineLazyGetter(this, "logger", () => | ||
Log.get(Log.TYPES.REMOTE_AGENT) | ||
); | ||
|
||
// Used to keep weak references of webProgressListeners alive. | ||
const webProgressListeners = new Set(); | ||
|
||
/** | ||
* Wait until the initial load of the given browsing context is done. | ||
* | ||
* @param {BrowsingContext} browsingContext | ||
* The browsing context to check. | ||
*/ | ||
function waitForInitialNavigationCompleted(browsingContext) { | ||
let listener; | ||
|
||
return new Promise(resolve => { | ||
listener = new ProgressListener(resolve); | ||
// Keep a reference to the weak listener so it's not getting gc'ed. | ||
webProgressListeners.add(listener); | ||
|
||
// Monitor the webprogress listener before checking isLoadingDocument to | ||
// avoid race conditions. | ||
browsingContext.webProgress.addProgressListener( | ||
listener, | ||
Ci.nsIWebProgress.NOTIFY_STATE_WINDOW | | ||
Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT | ||
); | ||
|
||
if (!browsingContext.webProgress.isLoadingDocument) { | ||
logger.trace("Initial navigation already completed"); | ||
resolve(); | ||
} | ||
}).finally(() => { | ||
browsingContext.webProgress.removeProgressListener( | ||
listener, | ||
Ci.nsIWebProgress.NOTIFY_STATE_WINDOW | | ||
Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT | ||
); | ||
webProgressListeners.delete(listener); | ||
}); | ||
} | ||
|
||
class ProgressListener { | ||
constructor(resolve) { | ||
this.resolve = resolve; | ||
} | ||
|
||
onStateChange(progress, request, flag, status) { | ||
const isStop = flag & Ci.nsIWebProgressListener.STATE_STOP; | ||
if (isStop) { | ||
this.resolve(); | ||
} | ||
} | ||
|
||
get QueryInterface() { | ||
return ChromeUtils.generateQI([ | ||
"nsIWebProgressListener", | ||
"nsISupportsWeakReference", | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
const { waitForInitialNavigationCompleted } = ChromeUtils.import( | ||
"chrome://remote/content/shared/Navigate.jsm" | ||
); | ||
|
||
const mockWebProgress = { | ||
onStateChangeCalled: false, | ||
isLoadingDocument: false, | ||
addProgressListener(listener) { | ||
if (!this.isLoadingDocument) { | ||
return; | ||
} | ||
|
||
listener.onStateChange( | ||
null, | ||
null, | ||
Ci.nsIWebProgressListener.STATE_STOP, | ||
null | ||
); | ||
this.onStateChangeCalled = true; | ||
}, | ||
removeProgressListener(listener) {}, | ||
}; | ||
|
||
const mockTopContext = { | ||
get children() { | ||
return [mockNestedContext]; | ||
}, | ||
id: 7, | ||
get top() { | ||
return this; | ||
}, | ||
webProgress: mockWebProgress, | ||
}; | ||
|
||
const mockNestedContext = { | ||
id: 8, | ||
parent: mockTopContext, | ||
top: mockTopContext, | ||
webProgress: mockWebProgress, | ||
}; | ||
|
||
add_test(async function test_waitForInitialNavigationCompleted_isNotLoading() { | ||
const browsingContext = Object.assign({}, mockTopContext); | ||
await waitForInitialNavigationCompleted(browsingContext); | ||
ok( | ||
!browsingContext.webProgress.onStateChangeCalled, | ||
"WebProgress' onStateChange hasn't been fired" | ||
); | ||
|
||
run_next_test(); | ||
}); | ||
|
||
add_test(async function test_waitForInitialNavigationCompleted_topIsLoading() { | ||
const browsingContext = Object.assign({}, mockTopContext); | ||
browsingContext.webProgress.isLoadingDocument = true; | ||
await waitForInitialNavigationCompleted(browsingContext); | ||
ok( | ||
browsingContext.webProgress.onStateChangeCalled, | ||
"WebProgress' onStateChange has been fired" | ||
); | ||
|
||
run_next_test(); | ||
}); | ||
|
||
add_test( | ||
async function test_waitForInitialNavigationCompleted_nestedIsLoading() { | ||
const browsingContext = Object.assign({}, mockNestedContext); | ||
const topContext = browsingContext.top; | ||
|
||
browsingContext.webProgress.isLoadingDocument = true; | ||
await waitForInitialNavigationCompleted(topContext); | ||
ok( | ||
browsingContext.webProgress.onStateChangeCalled, | ||
"WebProgress' onStateChange has been fired on the nested browsing context" | ||
); | ||
ok( | ||
topContext.webProgress.onStateChangeCalled, | ||
"WebProgress' onStateChange has been fired on the top browsing context" | ||
); | ||
|
||
run_next_test(); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters