Bug 1322383 - getCurrentWindow() has to only return the currently selected window. r=ato+446296,maja_zf

Given that this method will be used in each command for checks of a valid window, we have to return
the currently active window. It means the window mediator should only be used during setting up the
session to find the first browser window.

At the same time the code in this method is getting split-up for chrome and content scopes.

MozReview-Commit-ID: KyzxYk63RgA

--HG--
extra : rebase_source : e21785b1495103056a3b68ccf45054a573f3a967
This commit is contained in:
Henrik Skupin 2017-02-28 12:19:29 +01:00
Родитель adf2cd32d2
Коммит e5fb8dc369
4 изменённых файлов: 72 добавлений и 23 удалений

Просмотреть файл

@ -310,33 +310,51 @@ GeckoDriver.prototype.sendTargettedAsyncMessage_ = function (name, payload) {
};
/**
* Gets the current active window.
* Get the session's current top-level browsing context.
*
* It will return the outer {@ChromeWindow} previously selected by window handle
* through {@code #switchToWindow}, or the first window that was registered.
*
* @param {Context=} forcedContext
* Optional name of the context to use for the checks.
* Defaults to the current context.
* Optional name of the context to use for finding the window. It will be required
* if a command always needs a specific context, whether which context is
* currently set. Defaults to the current context.
*
* @return {nsIDOMWindow}
* @return {ChromeWindow}
* The current top-level browsing context.
*/
GeckoDriver.prototype.getCurrentWindow = function (forcedContext = undefined) {
let context = typeof forcedContext == "undefined" ? this.context : forcedContext;
let win = null;
if (this.curFrame === null) {
if (this.curBrowser === null) {
let typ = (context === Context.CONTENT) ? "navigator:browser" : null;
win = Services.wm.getMostRecentWindow(typ);
} else {
if (context === Context.CHROME) {
switch (context) {
case Context.CHROME:
if (this.curFrame !== null) {
win = this.curFrame;
} else if (this.curBrowser !== null) {
win = this.curBrowser.window;
} else {
if (this.curBrowser.tab && browser.getBrowserForTab(this.curBrowser.tab)) {
}
break;
case Context.CONTENT:
if (this.curFrame !== null) {
win = this.curFrame;
} else if (this.curBrowser !== null) {
if (browser.getTabBrowser(this.curBrowser.window)) {
// For browser windows we have to check if the current tab still exists.
if (this.curBrowser.tab && browser.getBrowserForTab(this.curBrowser.tab)) {
win = this.curBrowser.window;
}
} else {
// For non-browser windows just return the window.
win = this.curBrowser.window;
}
}
}
} else {
win = this.curFrame;
break;
}
return win;
@ -605,7 +623,7 @@ GeckoDriver.prototype.newSession = function* (cmd, resp) {
let browserListening = this.listeningPromise();
let waitForWindow = function() {
let win = this.getCurrentWindow();
let win = Services.wm.getMostRecentWindow("navigator:browser");
if (!win) {
// if the window isn't even created, just poll wait for it
let checkTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
@ -625,10 +643,8 @@ GeckoDriver.prototype.newSession = function* (cmd, resp) {
win.addEventListener("load", listener, true);
} else {
let clickToStart = Preferences.get(CLICK_TO_START_PREF);
if (clickToStart && (this.appName != "B2G")) {
let pService = Cc["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Ci.nsIPromptService);
pService.alert(win, "", "Click to start execution of marionette tests");
if (clickToStart) {
Services.prompt.alert(win, "", "Click to start execution of marionette tests");
}
this.startBrowser(win, true);
}

Просмотреть файл

@ -4,7 +4,7 @@
from marionette_driver import By, Wait
from marionette_harness import MarionetteTestCase, WindowManagerMixin
from marionette_harness import MarionetteTestCase, skip_if_mobile, WindowManagerMixin
class TestWindowHandles(WindowManagerMixin, MarionetteTestCase):
@ -47,7 +47,7 @@ class TestWindowHandles(WindowManagerMixin, MarionetteTestCase):
self.marionette.switch_to_window(self.start_tab)
self.assertEqual(self.marionette.current_window_handle, self.start_tab)
def test_window_handles_after_opening_new_window(self):
def test_window_handles_after_opening_new_browser_window(self):
def open_with_link():
link = self.marionette.find_element(By.ID, "new-window")
link.click()
@ -75,6 +75,28 @@ class TestWindowHandles(WindowManagerMixin, MarionetteTestCase):
self.assertEqual(self.marionette.current_window_handle, self.start_tab)
self.assertEqual(self.marionette.get_url(), self.test_page)
@skip_if_mobile("Fennec doesn't support other chrome windows")
def test_window_handles_after_opening_new_non_browser_window(self):
def open_with_link():
self.marionette.navigate(self.marionette.absolute_url("blob_download.html"))
link = self.marionette.find_element(By.ID, "blob-download")
link.click()
# We open a new window but are actually interested in the new tab
new_tab = self.open_tab(trigger=open_with_link)
self.assertEqual(len(self.marionette.window_handles), len(self.start_tabs) + 1)
self.assertEqual(self.marionette.current_window_handle, self.start_tab)
self.marionette.switch_to_window(new_tab)
self.assertEqual(self.marionette.current_window_handle, new_tab)
# Close the opened window and carry on in our original tab.
self.marionette.close()
self.assertEqual(len(self.marionette.window_handles), len(self.start_tabs))
self.marionette.switch_to_window(self.start_tab)
self.assertEqual(self.marionette.current_window_handle, self.start_tab)
def test_window_handles_after_closing_original_tab(self):
def open_with_link():
link = self.marionette.find_element(By.ID, "new-tab")

Просмотреть файл

@ -14,7 +14,7 @@ class TestNoSuchWindowContent(WindowManagerMixin, MarionetteTestCase):
super(TestNoSuchWindowContent, self).setUp()
def tearDown(self):
self.close_all_windows()
self.close_all_tabs()
super(TestNoSuchWindowContent, self).tearDown()
@skip_if_mobile("Fennec doesn't support other chrome windows")

Просмотреть файл

@ -0,0 +1,11 @@
<!DOCTYPE html>
<a id="blob-download" download="foo.html">Download</a>
<script>
const string = "test";
const blob = new Blob([string], { type: "text/html" });
const link = document.getElementById("blob-download");
link.href = URL.createObjectURL(blob);
</script>