Bug 1368492 - Add assert.browsingContext for parent process.
When accessing a property of the current content browser from
the parent process, the current top-level browsing context has
to be valid.
MozReview-Commit-ID: BZHJTk1j5Nj
--- a/testing/marionette/assert.js
+++ b/testing/marionette/assert.js
@@ -110,16 +110,34 @@ assert.window = function (win, msg = "")
// If the window is no longer available a TypeError is thrown.
} catch (e if e.name === "TypeError") {
return null;
}
}, msg, NoSuchWindowError)(win);
};
/**
+ * Asserts that |context| is a valid browsing context.
+ *
+ * @param {browser.Context} context
+ * Browsing context to test.
+ * @param {string=} msg
+ * Custom error message.
+ *
+ * @throws {NoSuchWindowError}
+ * If |context| is invalid.
+ */
+assert.contentBrowser = function (context, msg = "") {
+ msg = msg || "Current window does not have a content browser";
+ assert.that(c => c && c.contentBrowser,
+ msg,
+ NoSuchWindowError)(context);
+};
+
+/**
* Asserts that there is no current user prompt.
*
* @param {modal.Dialog} dialog
* Reference to current dialogue.
* @param {string=} msg
* Custom error message.
*
* @throws {UnexpectedAlertOpenError}
--- a/testing/marionette/driver.js
+++ b/testing/marionette/driver.js
@@ -972,22 +972,18 @@ GeckoDriver.prototype.getCurrentUrl = fu
const win = assert.window(this.getCurrentWindow());
assert.noUserPrompt(this.dialog);
switch (this.context) {
case Context.CHROME:
return win.location.href;
case Context.CONTENT:
- if (this.curBrowser.contentBrowser) {
- return this.curBrowser.contentBrowser.currentURI.spec;
- } else {
- throw new NoSuchWindowError(
- "Not a browser window, or no tab currently selected");
- }
+ assert.contentBrowser(this.curBrowser);
+ return this.curBrowser.contentBrowser.currentURI.spec;
}
};
/**
* Gets the current title of the window.
*
* @return {string}
* Document title of the top-level browsing context.
--- a/testing/marionette/test_assert.js
+++ b/testing/marionette/test_assert.js
@@ -113,16 +113,26 @@ add_test(function test_window() {
for (let typ of [null, undefined, deadWindow]) {
Assert.throws(() => assert.window(typ), NoSuchWindowError);
}
run_next_test();
});
+add_test(function test_contentBrowser() {
+ assert.contentBrowser({contentBrowser: 42});
+
+ for (let typ of [null, undefined, {contentBrowser: null}]) {
+ Assert.throws(() => assert.contentBrowser(typ), NoSuchWindowError);
+ }
+
+ run_next_test();
+});
+
add_test(function test_object() {
assert.object({});
assert.object(new Object());
for (let typ of [42, "foo", true, null, undefined]) {
Assert.throws(() => assert.object(typ), InvalidArgumentError);
}
run_next_test();