diff options
Diffstat (limited to 'testing/mochitest/tests/browser')
35 files changed, 831 insertions, 0 deletions
diff --git a/testing/mochitest/tests/browser/browser.ini b/testing/mochitest/tests/browser/browser.ini new file mode 100644 index 0000000000..bcacb18bf3 --- /dev/null +++ b/testing/mochitest/tests/browser/browser.ini @@ -0,0 +1,53 @@ +[DEFAULT] +support-files = + head.js + +[browser_add_task.js] +[browser_async.js] +[browser_browserLoaded_content_loaded.js] +[browser_BrowserTestUtils.js] +skip-if = verify +support-files = + dummy.html +[browser_fail.js] +skip-if = verify +[browser_fail_add_task.js] +skip-if = verify +[browser_fail_add_task_uncaught_rejection.js] +skip-if = verify +[browser_fail_async.js] +skip-if = verify +[browser_fail_if.js] +fail-if = true +[browser_fail_throw.js] +skip-if = verify +[browser_fail_timeout.js] +skip-if = true # Disabled beacuse it takes too long (bug 1178959) +[browser_fail_uncaught_rejection.js] +skip-if = verify +[browser_fail_uncaught_rejection_expected.js] +skip-if = verify +[browser_fail_uncaught_rejection_expected_multi.js] +skip-if = verify +[browser_fail_unexpectedTimeout.js] +skip-if = true # Disabled beacuse it takes too long (bug 1178959) +[browser_getTestFile.js] +support-files = + test-dir/* + waitForFocusPage.html +[browser_head.js] +[browser_pass.js] +[browser_parameters.js] +[browser_popupNode.js] +[browser_popupNode_check.js] +[browser_privileges.js] +[browser_requestLongerTimeout.js] +skip-if = true # Disabled beacuse it takes too long (bug 1178959) +[browser_sanityException.js] +[browser_sanityException2.js] +[browser_tasks_skip.js] +[browser_tasks_skipall.js] +[browser_uncaught_rejection_expected.js] +[browser_waitForFocus.js] +[browser_zz_fail_openwindow.js] +skip-if = true # this catches outside of the main loop to find an extra window diff --git a/testing/mochitest/tests/browser/browser_BrowserTestUtils.js b/testing/mochitest/tests/browser/browser_BrowserTestUtils.js new file mode 100644 index 0000000000..22e5bec37d --- /dev/null +++ b/testing/mochitest/tests/browser/browser_BrowserTestUtils.js @@ -0,0 +1,194 @@ +function getLastEventDetails(browser) { + return SpecialPowers.spawn(browser, [], async function() { + return content.document.getElementById("out").textContent; + }); +} + +add_task(async function() { + let onClickEvt = + 'document.getElementById("out").textContent = event.target.localName + "," + event.clientX + "," + event.clientY;'; + const url = + "<body onclick='" + + onClickEvt + + "' style='margin: 0'>" + + "<button id='one' style='margin: 0; margin-left: 16px; margin-top: 14px; width: 80px; height: 40px;'>Test</button>" + + "<div onmousedown='event.preventDefault()' style='margin: 0; width: 80px; height: 60px;'>Other</div>" + + "<span id='out'></span></body>"; + let tab = await BrowserTestUtils.openNewForegroundTab( + gBrowser, + "data:text/html," + url + ); + + let browser = tab.linkedBrowser; + await BrowserTestUtils.synthesizeMouseAtCenter("#one", {}, browser); + let details = await getLastEventDetails(browser); + + is(details, "button,56,34", "synthesizeMouseAtCenter"); + + await BrowserTestUtils.synthesizeMouse("#one", 4, 9, {}, browser); + details = await getLastEventDetails(browser); + is(details, "button,20,23", "synthesizeMouse"); + + await BrowserTestUtils.synthesizeMouseAtPoint(15, 6, {}, browser); + details = await getLastEventDetails(browser); + is(details, "body,15,6", "synthesizeMouseAtPoint on body"); + + await BrowserTestUtils.synthesizeMouseAtPoint( + 20, + 22, + {}, + browser.browsingContext + ); + details = await getLastEventDetails(browser); + is(details, "button,20,22", "synthesizeMouseAtPoint on button"); + + await BrowserTestUtils.synthesizeMouseAtCenter("body > div", {}, browser); + details = await getLastEventDetails(browser); + is(details, "div,40,84", "synthesizeMouseAtCenter with complex selector"); + + let cancelled = await BrowserTestUtils.synthesizeMouseAtCenter( + "body > div", + { type: "mousedown" }, + browser + ); + details = await getLastEventDetails(browser); + is( + details, + "div,40,84", + "synthesizeMouseAtCenter mousedown with complex selector" + ); + ok( + cancelled, + "synthesizeMouseAtCenter mousedown with complex selector not cancelled" + ); + + cancelled = await BrowserTestUtils.synthesizeMouseAtCenter( + "body > div", + { type: "mouseup" }, + browser + ); + details = await getLastEventDetails(browser); + is( + details, + "div,40,84", + "synthesizeMouseAtCenter mouseup with complex selector" + ); + ok( + !cancelled, + "synthesizeMouseAtCenter mouseup with complex selector cancelled" + ); + + gBrowser.removeTab(tab); +}); + +add_task(async function mouse_in_iframe() { + let onClickEvt = "document.body.lastChild.textContent = event.target.id;"; + const url = `<iframe style='margin: 30px;' src='data:text/html,<body onclick="${onClickEvt}"> + <p><button>One</button></p><p><button id="two">Two</button></p><p id="out"></p></body>'></iframe> + <iframe style='margin: 10px;' src='data:text/html,<body onclick="${onClickEvt}"> + <p><button>Three</button></p><p><button id="four">Four</button></p><p id="out"></p></body>'></iframe>`; + let tab = await BrowserTestUtils.openNewForegroundTab( + gBrowser, + "data:text/html," + url + ); + + let browser = tab.linkedBrowser; + + await BrowserTestUtils.synthesizeMouse( + "#two", + 5, + 10, + {}, + browser.browsingContext.children[0] + ); + + let details = await getLastEventDetails(browser.browsingContext.children[0]); + is(details, "two", "synthesizeMouse"); + + await BrowserTestUtils.synthesizeMouse( + "#four", + 5, + 10, + {}, + browser.browsingContext.children[1] + ); + details = await getLastEventDetails(browser.browsingContext.children[1]); + is(details, "four", "synthesizeMouse"); + + gBrowser.removeTab(tab); +}); + +add_task(async function() { + await BrowserTestUtils.registerAboutPage( + registerCleanupFunction, + "about-pages-are-cool", + getRootDirectory(gTestPath) + "dummy.html", + 0 + ); + let tab = await BrowserTestUtils.openNewForegroundTab( + gBrowser, + "about:about-pages-are-cool", + true + ); + ok(tab, "Successfully created an about: page and loaded it."); + BrowserTestUtils.removeTab(tab); + try { + await BrowserTestUtils.unregisterAboutPage("about-pages-are-cool"); + ok(true, "Successfully unregistered the about page."); + } catch (ex) { + ok(false, "Should not throw unregistering a known about: page"); + } + await BrowserTestUtils.unregisterAboutPage("random-other-about-page").then( + () => { + ok( + false, + "Should not have succeeded unregistering an unknown about: page." + ); + }, + () => { + ok( + true, + "Should have returned a rejected promise trying to unregister an unknown about page" + ); + } + ); +}); + +add_task(async function testWaitForEvent() { + // A promise returned by BrowserTestUtils.waitForEvent should not be resolved + // in the same event tick as the event listener is called. + let eventListenerCalled = false; + let waitForEventResolved = false; + // Use capturing phase to make sure the event listener added by + // BrowserTestUtils.waitForEvent is called before the normal event listener + // below. + let eventPromise = BrowserTestUtils.waitForEvent( + gBrowser, + "dummyevent", + true + ); + eventPromise.then(() => { + waitForEventResolved = true; + }); + // Add normal event listener that is called after the event listener added by + // BrowserTestUtils.waitForEvent. + gBrowser.addEventListener( + "dummyevent", + () => { + eventListenerCalled = true; + is( + waitForEventResolved, + false, + "BrowserTestUtils.waitForEvent promise resolution handler shouldn't be called at this point." + ); + }, + { once: true } + ); + + var event = new CustomEvent("dummyevent"); + gBrowser.dispatchEvent(event); + + await eventPromise; + + is(eventListenerCalled, true, "dummyevent listener should be called"); +}); diff --git a/testing/mochitest/tests/browser/browser_add_task.js b/testing/mochitest/tests/browser/browser_add_task.js new file mode 100644 index 0000000000..c3b56b6d85 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_add_task.js @@ -0,0 +1,31 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +var test1Complete = false; +var test2Complete = false; + +function executeWithTimeout() { + return new Promise(resolve => + executeSoon(function() { + ok(true, "we get here after a timeout"); + resolve(); + }) + ); +} + +add_task(async function asyncTest_no1() { + await executeWithTimeout(); + test1Complete = true; +}); + +add_task(async function asyncTest_no2() { + await executeWithTimeout(); + test2Complete = true; +}); + +add_task(function() { + ok(test1Complete, "We have been through test 1"); + ok(test2Complete, "We have been through test 2"); +}); diff --git a/testing/mochitest/tests/browser/browser_async.js b/testing/mochitest/tests/browser/browser_async.js new file mode 100644 index 0000000000..d07cb21740 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_async.js @@ -0,0 +1,9 @@ +function test() { + waitForExplicitFinish(); + function done() { + ok(true, "timeout ran"); + finish(); + } + // eslint-disable-next-line mozilla/no-arbitrary-setTimeout + setTimeout(done, 500); +} diff --git a/testing/mochitest/tests/browser/browser_browserLoaded_content_loaded.js b/testing/mochitest/tests/browser/browser_browserLoaded_content_loaded.js new file mode 100644 index 0000000000..21a7ad4ac3 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_browserLoaded_content_loaded.js @@ -0,0 +1,53 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ +"use strict"; + +function isDOMLoaded(browser) { + return SpecialPowers.spawn(browser, [], async function() { + Assert.equal( + content.document.readyState, + "complete", + "Browser should be loaded." + ); + }); +} + +// It checks if calling BrowserTestUtils.browserLoaded() yields +// browser object. +add_task(async function() { + let tab = BrowserTestUtils.addTab(gBrowser, "http://example.com"); + let browser = tab.linkedBrowser; + await BrowserTestUtils.browserLoaded(browser); + await isDOMLoaded(browser); + gBrowser.removeTab(tab); +}); + +// It checks that BrowserTestUtils.browserLoaded() works well with +// promise.all(). +add_task(async function() { + let tabURLs = [ + `http://example.org`, + `http://mochi.test:8888`, + `http://test:80`, + ]; + // Add tabs, get the respective browsers + let browsers = tabURLs.map( + u => BrowserTestUtils.addTab(gBrowser, u).linkedBrowser + ); + + // wait for promises to settle + await Promise.all( + (function*() { + for (let b of browsers) { + yield BrowserTestUtils.browserLoaded(b); + } + })() + ); + for (const browser of browsers) { + await isDOMLoaded(browser); + } + // cleanup + browsers + .map(browser => gBrowser.getTabForBrowser(browser)) + .forEach(tab => gBrowser.removeTab(tab)); +}); diff --git a/testing/mochitest/tests/browser/browser_fail.js b/testing/mochitest/tests/browser/browser_fail.js new file mode 100644 index 0000000000..f6b439fb0d --- /dev/null +++ b/testing/mochitest/tests/browser/browser_fail.js @@ -0,0 +1,10 @@ +setExpectedFailuresForSelfTest(6); + +function test() { + ok(false, "fail ok"); + is(true, false, "fail is"); + isnot(true, true, "fail isnot"); + todo(true, "fail todo"); + todo_is(true, true, "fail todo_is"); + todo_isnot(true, false, "fail todo_isnot"); +} diff --git a/testing/mochitest/tests/browser/browser_fail_add_task.js b/testing/mochitest/tests/browser/browser_fail_add_task.js new file mode 100644 index 0000000000..aeb1129943 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_fail_add_task.js @@ -0,0 +1,27 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +setExpectedFailuresForSelfTest(4); + +function rejectOnNextTick(error) { + return new Promise((resolve, reject) => executeSoon(() => reject(error))); +} + +add_task(async function failWithoutError() { + await rejectOnNextTick(undefined); +}); + +add_task(async function failWithString() { + await rejectOnNextTick("This is a string"); +}); + +add_task(async function failWithInt() { + await rejectOnNextTick(42); +}); + +// This one should display a stack trace +add_task(async function failWithError() { + await rejectOnNextTick(new Error("This is an error")); +}); diff --git a/testing/mochitest/tests/browser/browser_fail_add_task_uncaught_rejection.js b/testing/mochitest/tests/browser/browser_fail_add_task_uncaught_rejection.js new file mode 100644 index 0000000000..f420b37037 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_fail_add_task_uncaught_rejection.js @@ -0,0 +1,34 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +setExpectedFailuresForSelfTest(8); + +// Keep "JSMPromise" separate so "Promise" still refers to native Promises. +let JSMPromise = ChromeUtils.import("resource://gre/modules/Promise.jsm", {}) + .Promise; + +async function rejectOnNextTick(error) { + await Promise.resolve(); + + Promise.reject(error); + JSMPromise.reject(error); +} + +add_task(async function failWithoutError() { + await rejectOnNextTick(undefined); +}); + +add_task(async function failWithString() { + await rejectOnNextTick("This is a string"); +}); + +add_task(async function failWithInt() { + await rejectOnNextTick(42); +}); + +// This one should display a stack trace +add_task(async function failWithError() { + await rejectOnNextTick(new Error("This is an error")); +}); diff --git a/testing/mochitest/tests/browser/browser_fail_async.js b/testing/mochitest/tests/browser/browser_fail_async.js new file mode 100644 index 0000000000..6f284bf2aa --- /dev/null +++ b/testing/mochitest/tests/browser/browser_fail_async.js @@ -0,0 +1,9 @@ +setExpectedFailuresForSelfTest(1); + +function test() { + waitForExplicitFinish(); + executeSoon(() => { + ok(false, "fail"); + finish(); + }); +} diff --git a/testing/mochitest/tests/browser/browser_fail_if.js b/testing/mochitest/tests/browser/browser_fail_if.js new file mode 100644 index 0000000000..dad56e7dac --- /dev/null +++ b/testing/mochitest/tests/browser/browser_fail_if.js @@ -0,0 +1,4 @@ +// We expect this test to fail because it is marked as fail-if in the manifest. +function test() { + ok(false, "fail ok"); +} diff --git a/testing/mochitest/tests/browser/browser_fail_throw.js b/testing/mochitest/tests/browser/browser_fail_throw.js new file mode 100644 index 0000000000..585b47561d --- /dev/null +++ b/testing/mochitest/tests/browser/browser_fail_throw.js @@ -0,0 +1,5 @@ +setExpectedFailuresForSelfTest(1); + +function test() { + throw new Error("thrown exception"); +} diff --git a/testing/mochitest/tests/browser/browser_fail_timeout.js b/testing/mochitest/tests/browser/browser_fail_timeout.js new file mode 100644 index 0000000000..44030a00f0 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_fail_timeout.js @@ -0,0 +1,9 @@ +function test() { + function end() { + ok(false, "should have timed out"); + finish(); + } + waitForExplicitFinish(); + // eslint-disable-next-line mozilla/no-arbitrary-setTimeout + setTimeout(end, 40000); +} diff --git a/testing/mochitest/tests/browser/browser_fail_uncaught_rejection.js b/testing/mochitest/tests/browser/browser_fail_uncaught_rejection.js new file mode 100644 index 0000000000..4fd36ade64 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_fail_uncaught_rejection.js @@ -0,0 +1,20 @@ +setExpectedFailuresForSelfTest(3); + +// Keep "JSMPromise" separate so "Promise" still refers to native Promises. +let JSMPromise = ChromeUtils.import("resource://gre/modules/Promise.jsm", {}) + .Promise; + +function test() { + Promise.reject(new Error("Promise rejection.")); + JSMPromise.reject(new Error("Promise.jsm rejection.")); + (async () => { + throw new Error("Synchronous rejection from async function."); + })(); + + // The following rejections are caught, so they won't result in failures. + Promise.reject(new Error("Promise rejection.")).catch(() => {}); + JSMPromise.reject(new Error("Promise.jsm rejection.")).catch(() => {}); + (async () => { + throw new Error("Synchronous rejection from async function."); + })().catch(() => {}); +} diff --git a/testing/mochitest/tests/browser/browser_fail_uncaught_rejection_expected.js b/testing/mochitest/tests/browser/browser_fail_uncaught_rejection_expected.js new file mode 100644 index 0000000000..d062acad5a --- /dev/null +++ b/testing/mochitest/tests/browser/browser_fail_uncaught_rejection_expected.js @@ -0,0 +1,10 @@ +setExpectedFailuresForSelfTest(1); + +// The test will fail because there is only one of two expected rejections. +ChromeUtils.import("resource://testing-common/PromiseTestUtils.jsm", this); +PromiseTestUtils.expectUncaughtRejection(/Promise rejection./); +PromiseTestUtils.expectUncaughtRejection(/Promise rejection./); + +function test() { + Promise.reject(new Error("Promise rejection.")); +} diff --git a/testing/mochitest/tests/browser/browser_fail_uncaught_rejection_expected_multi.js b/testing/mochitest/tests/browser/browser_fail_uncaught_rejection_expected_multi.js new file mode 100644 index 0000000000..60fed40c92 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_fail_uncaught_rejection_expected_multi.js @@ -0,0 +1,9 @@ +setExpectedFailuresForSelfTest(1); + +// The test will fail because an expected uncaught rejection is actually caught. +ChromeUtils.import("resource://testing-common/PromiseTestUtils.jsm", this); +PromiseTestUtils.expectUncaughtRejection(/Promise rejection./); + +function test() { + Promise.reject(new Error("Promise rejection.")).catch(() => {}); +} diff --git a/testing/mochitest/tests/browser/browser_fail_unexpectedTimeout.js b/testing/mochitest/tests/browser/browser_fail_unexpectedTimeout.js new file mode 100644 index 0000000000..d0aef231bd --- /dev/null +++ b/testing/mochitest/tests/browser/browser_fail_unexpectedTimeout.js @@ -0,0 +1,14 @@ +function test() { + function message() { + info("This should delay timeout"); + } + function end() { + ok(true, "Should have not timed out, but notified long running test"); + finish(); + } + waitForExplicitFinish(); + // eslint-disable-next-line mozilla/no-arbitrary-setTimeout + setTimeout(message, 20000); + // eslint-disable-next-line mozilla/no-arbitrary-setTimeout + setTimeout(end, 40000); +} diff --git a/testing/mochitest/tests/browser/browser_getTestFile.js b/testing/mochitest/tests/browser/browser_getTestFile.js new file mode 100644 index 0000000000..d1b0287dde --- /dev/null +++ b/testing/mochitest/tests/browser/browser_getTestFile.js @@ -0,0 +1,55 @@ +function test() { + let { Promise } = ChromeUtils.import("resource://gre/modules/Promise.jsm"); + const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm"); + let decoder = new TextDecoder(); + + waitForExplicitFinish(); + + SimpleTest.doesThrow(function() { + getTestFilePath("/browser_getTestFile.js"); + }, "getTestFilePath rejects absolute paths"); + + Promise.all([ + OS.File.exists(getTestFilePath("browser_getTestFile.js")).then(function( + exists + ) { + ok(exists, "getTestFilePath consider the path as being relative"); + }), + + OS.File.exists(getTestFilePath("./browser_getTestFile.js")).then(function( + exists + ) { + ok(exists, "getTestFilePath also accepts explicit relative path"); + }), + + OS.File.exists(getTestFilePath("./browser_getTestFileTypo.xul")).then( + function(exists) { + ok(!exists, "getTestFilePath do not throw if the file doesn't exists"); + } + ), + + OS.File.read(getTestFilePath("test-dir/test-file")).then(function(array) { + is( + decoder.decode(array), + "foo\n", + "getTestFilePath can reach sub-folder files 1/2" + ); + }), + + OS.File.read(getTestFilePath("./test-dir/test-file")).then(function(array) { + is( + decoder.decode(array), + "foo\n", + "getTestFilePath can reach sub-folder files 2/2" + ); + }), + ]).then( + function() { + finish(); + }, + function(error) { + ok(false, error); + finish(); + } + ); +} diff --git a/testing/mochitest/tests/browser/browser_head.js b/testing/mochitest/tests/browser/browser_head.js new file mode 100644 index 0000000000..b7330a0fc0 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_head.js @@ -0,0 +1,16 @@ +var testVar; + +registerCleanupFunction(function() { + ok(true, "I'm a cleanup function in test file"); + is( + this.testVar, + "I'm a var in test file", + "Test cleanup function scope is correct" + ); +}); + +function test() { + is(headVar, "I'm a var in head file", "Head variables are set"); + ok(headMethod(), "Head methods are imported"); + testVar = "I'm a var in test file"; +} diff --git a/testing/mochitest/tests/browser/browser_parameters.js b/testing/mochitest/tests/browser/browser_parameters.js new file mode 100644 index 0000000000..813cd662ba --- /dev/null +++ b/testing/mochitest/tests/browser/browser_parameters.js @@ -0,0 +1,3 @@ +function test() { + ok(SimpleTest.harnessParameters, "Should have parameters"); +} diff --git a/testing/mochitest/tests/browser/browser_pass.js b/testing/mochitest/tests/browser/browser_pass.js new file mode 100644 index 0000000000..e512ff374a --- /dev/null +++ b/testing/mochitest/tests/browser/browser_pass.js @@ -0,0 +1,13 @@ +function test() { + SimpleTest.requestCompleteLog(); + ok(true, "pass ok"); + is(true, true, "pass is"); + isnot(false, true, "pass isnot"); + todo(false, "pass todo"); + todo_is(false, true, "pass todo_is"); + todo_isnot(true, true, "pass todo_isnot"); + info("info message"); + + var func = is; + func(true, true, "pass indirect is"); +} diff --git a/testing/mochitest/tests/browser/browser_popupNode.js b/testing/mochitest/tests/browser/browser_popupNode.js new file mode 100644 index 0000000000..323657aea6 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_popupNode.js @@ -0,0 +1,4 @@ +function test() { + document.popupNode = document.documentElement; + isnot(document.popupNode, null, "document.popupNode has been correctly set"); +} diff --git a/testing/mochitest/tests/browser/browser_popupNode_check.js b/testing/mochitest/tests/browser/browser_popupNode_check.js new file mode 100644 index 0000000000..fb85378d9d --- /dev/null +++ b/testing/mochitest/tests/browser/browser_popupNode_check.js @@ -0,0 +1,3 @@ +function test() { + is(document.popupNode, null, "document.popupNode has been correctly cleared"); +} diff --git a/testing/mochitest/tests/browser/browser_privileges.js b/testing/mochitest/tests/browser/browser_privileges.js new file mode 100644 index 0000000000..042a928b9c --- /dev/null +++ b/testing/mochitest/tests/browser/browser_privileges.js @@ -0,0 +1,17 @@ +function test() { + // simple test to confirm we have chrome privileges + let hasPrivileges = true; + + // this will throw an exception if we are not running with privileges + try { + // eslint-disable-next-line no-unused-vars, mozilla/use-services + var prefs = Cc["@mozilla.org/preferences-service;1"].getService( + Ci.nsIPrefBranch + ); + } catch (e) { + hasPrivileges = false; + } + + // if we get here, we must have chrome privileges + ok(hasPrivileges, "running with chrome privileges"); +} diff --git a/testing/mochitest/tests/browser/browser_requestLongerTimeout.js b/testing/mochitest/tests/browser/browser_requestLongerTimeout.js new file mode 100644 index 0000000000..4107e11fd0 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_requestLongerTimeout.js @@ -0,0 +1,10 @@ +function test() { + requestLongerTimeout(2); + function end() { + ok(true, "should not time out"); + finish(); + } + waitForExplicitFinish(); + // eslint-disable-next-line mozilla/no-arbitrary-setTimeout + setTimeout(end, 40000); +} diff --git a/testing/mochitest/tests/browser/browser_sanityException.js b/testing/mochitest/tests/browser/browser_sanityException.js new file mode 100644 index 0000000000..2678dd80ad --- /dev/null +++ b/testing/mochitest/tests/browser/browser_sanityException.js @@ -0,0 +1,5 @@ +function test() { + ok(true, "ok called"); + expectUncaughtException(); + throw new Error("this is a deliberately thrown exception"); +} diff --git a/testing/mochitest/tests/browser/browser_sanityException2.js b/testing/mochitest/tests/browser/browser_sanityException2.js new file mode 100644 index 0000000000..dea405d97d --- /dev/null +++ b/testing/mochitest/tests/browser/browser_sanityException2.js @@ -0,0 +1,11 @@ +function test() { + waitForExplicitFinish(); + ok(true, "ok called"); + executeSoon(function() { + expectUncaughtException(); + throw new Error("this is a deliberately thrown exception"); + }); + executeSoon(function() { + finish(); + }); +} diff --git a/testing/mochitest/tests/browser/browser_tasks_skip.js b/testing/mochitest/tests/browser/browser_tasks_skip.js new file mode 100644 index 0000000000..99f3e8d2c2 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_tasks_skip.js @@ -0,0 +1,21 @@ +"use strict"; + +add_task(async function skipMeNot1() { + Assert.ok(true, "Well well well."); +}); + +add_task(async function skipMe1() { + Assert.ok(false, "Not skipped after all."); +}).skip(); + +add_task(async function skipMeNot2() { + Assert.ok(true, "Well well well."); +}); + +add_task(async function skipMeNot3() { + Assert.ok(true, "Well well well."); +}); + +add_task(async function skipMe2() { + Assert.ok(false, "Not skipped after all."); +}).skip(); diff --git a/testing/mochitest/tests/browser/browser_tasks_skipall.js b/testing/mochitest/tests/browser/browser_tasks_skipall.js new file mode 100644 index 0000000000..0562834751 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_tasks_skipall.js @@ -0,0 +1,21 @@ +"use strict"; + +add_task(async function skipMe1() { + Assert.ok(false, "Not skipped after all."); +}); + +add_task(async function skipMe2() { + Assert.ok(false, "Not skipped after all."); +}).skip(); + +add_task(async function skipMe3() { + Assert.ok(false, "Not skipped after all."); +}).only(); + +add_task(async function skipMeNot() { + Assert.ok(true, "Well well well."); +}).only(); + +add_task(async function skipMe4() { + Assert.ok(false, "Not skipped after all."); +}); diff --git a/testing/mochitest/tests/browser/browser_uncaught_rejection_expected.js b/testing/mochitest/tests/browser/browser_uncaught_rejection_expected.js new file mode 100644 index 0000000000..1a8d28c237 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_uncaught_rejection_expected.js @@ -0,0 +1,19 @@ +// Keep "JSMPromise" separate so "Promise" still refers to native Promises. +let JSMPromise = ChromeUtils.import("resource://gre/modules/Promise.jsm", {}) + .Promise; + +ChromeUtils.import("resource://testing-common/PromiseTestUtils.jsm", this); +PromiseTestUtils.allowMatchingRejectionsGlobally(/Allowed rejection./); +PromiseTestUtils.expectUncaughtRejection(/Promise.jsm rejection./); +PromiseTestUtils.expectUncaughtRejection(/Promise.jsm rejection./); +PromiseTestUtils.expectUncaughtRejection(/Promise rejection./); +PromiseTestUtils.expectUncaughtRejection(/Promise rejection./); + +function test() { + Promise.reject(new Error("Promise rejection.")); + Promise.reject(new Error("Promise rejection.")); + Promise.reject(new Error("Allowed rejection.")); + JSMPromise.reject(new Error("Promise.jsm rejection.")); + JSMPromise.reject(new Error("Promise.jsm rejection.")); + JSMPromise.reject(new Error("Allowed rejection.")); +} diff --git a/testing/mochitest/tests/browser/browser_waitForFocus.js b/testing/mochitest/tests/browser/browser_waitForFocus.js new file mode 100644 index 0000000000..1e0bac5866 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_waitForFocus.js @@ -0,0 +1,101 @@ +const gBaseURL = "https://example.com/browser/testing/mochitest/tests/browser/"; + +function promiseTabLoadEvent(tab, url) { + let promise = BrowserTestUtils.browserLoaded(tab.linkedBrowser, false, url); + if (url) { + tab.linkedBrowser.loadURI(url); + } + return promise; +} + +// Load a new blank tab +add_task(async function() { + await BrowserTestUtils.openNewForegroundTab(gBrowser); + + gURLBar.focus(); + + let browser = gBrowser.selectedBrowser; + await SimpleTest.promiseFocus(browser, true); + + is( + document.activeElement, + browser, + "Browser is focused when about:blank is loaded" + ); + + gBrowser.removeCurrentTab(); + gURLBar.focus(); +}); + +add_task(async function() { + await BrowserTestUtils.openNewForegroundTab(gBrowser); + + gURLBar.focus(); + + let browser = gBrowser.selectedBrowser; + // If we're running in e10s, we don't have access to the content + // window, so only test window arguments in non-e10s mode. + if (browser.contentWindow) { + await SimpleTest.promiseFocus(browser.contentWindow, true); + + is( + document.activeElement, + browser, + "Browser is focused when about:blank is loaded" + ); + } + + gBrowser.removeCurrentTab(); + gURLBar.focus(); +}); + +// Load a tab with a subframe inside it and wait until the subframe is focused +add_task(async function() { + let tab = BrowserTestUtils.addTab(gBrowser); + gBrowser.selectedTab = tab; + + let browser = gBrowser.getBrowserForTab(tab); + // If we're running in e10s, we don't have access to the content + // window, so only test <iframe> arguments in non-e10s mode. + if (browser.contentWindow) { + await promiseTabLoadEvent(tab, gBaseURL + "waitForFocusPage.html"); + + await SimpleTest.promiseFocus(browser.contentWindow); + + is( + document.activeElement, + browser, + "Browser is focused when page is loaded" + ); + + await SimpleTest.promiseFocus(browser.contentWindow.frames[0]); + + is( + browser.contentWindow.document.activeElement.localName, + "iframe", + "Child iframe is focused" + ); + } + + gBrowser.removeCurrentTab(); +}); + +// Pass a browser to promiseFocus +add_task(async function() { + await BrowserTestUtils.openNewForegroundTab( + gBrowser, + gBaseURL + "waitForFocusPage.html" + ); + + gURLBar.focus(); + + await SimpleTest.promiseFocus(gBrowser.selectedBrowser); + + is( + document.activeElement, + gBrowser.selectedBrowser, + "Browser is focused when promiseFocus is passed a browser" + ); + + gBrowser.removeCurrentTab(); +}); diff --git a/testing/mochitest/tests/browser/browser_zz_fail_openwindow.js b/testing/mochitest/tests/browser/browser_zz_fail_openwindow.js new file mode 100644 index 0000000000..2f7fb04d78 --- /dev/null +++ b/testing/mochitest/tests/browser/browser_zz_fail_openwindow.js @@ -0,0 +1,13 @@ +function test() { + waitForExplicitFinish(); + function done() { + ok(true, "timeout ran"); + finish(); + } + + ok(OpenBrowserWindow(), "opened browser window"); + // and didn't close it! + + // eslint-disable-next-line mozilla/no-arbitrary-setTimeout + setTimeout(done, 10000); +} diff --git a/testing/mochitest/tests/browser/dummy.html b/testing/mochitest/tests/browser/dummy.html new file mode 100644 index 0000000000..7954185285 --- /dev/null +++ b/testing/mochitest/tests/browser/dummy.html @@ -0,0 +1,7 @@ +<!DOCTYPE html> +<html> + <meta http-equiv="Content-Security-Policy" content="default-src 'none'; object-src 'none'"></meta> + <title>This is a dummy page</title> + <meta charset="utf-8"> + <body>This is a dummy page</body> +</html> diff --git a/testing/mochitest/tests/browser/head.js b/testing/mochitest/tests/browser/head.js new file mode 100644 index 0000000000..cd9565f743 --- /dev/null +++ b/testing/mochitest/tests/browser/head.js @@ -0,0 +1,16 @@ +var headVar = "I'm a var in head file"; + +function headMethod() { + return true; +} + +ok(true, "I'm a test in head file"); + +registerCleanupFunction(function() { + ok(true, "I'm a cleanup function in head file"); + is( + this.headVar, + "I'm a var in head file", + "Head cleanup function scope is correct" + ); +}); diff --git a/testing/mochitest/tests/browser/test-dir/test-file b/testing/mochitest/tests/browser/test-dir/test-file new file mode 100644 index 0000000000..257cc5642c --- /dev/null +++ b/testing/mochitest/tests/browser/test-dir/test-file @@ -0,0 +1 @@ +foo diff --git a/testing/mochitest/tests/browser/waitForFocusPage.html b/testing/mochitest/tests/browser/waitForFocusPage.html new file mode 100644 index 0000000000..286ad7849c --- /dev/null +++ b/testing/mochitest/tests/browser/waitForFocusPage.html @@ -0,0 +1,4 @@ +<body> + <input> + <iframe id="f" src="data:text/plain,Test" width=80 height=80></iframe> +</body> |