From ea934a6d68db1fc5e983f0b9d55f4bc4d4a0cc14 Mon Sep 17 00:00:00 2001 From: Gijs Kruitbosch Date: Mon, 26 Oct 2015 14:16:46 +0100 Subject: [PATCH] Bug 332195 - part 5: fix test for e10s, r=mconley --HG-- extra : commitid : 5Cb58g4sU6s extra : rebase_source : e22ceac2b58b2ad62587c7db0e7e8bc90565da72 --- .../browser_openPromptInBackgroundTab.js | 10 +++++++ .../BrowserTestUtils/BrowserTestUtils.jsm | 28 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/browser/base/content/test/general/browser_openPromptInBackgroundTab.js b/browser/base/content/test/general/browser_openPromptInBackgroundTab.js index aee47011c7ba..ee188e7cf01c 100644 --- a/browser/base/content/test/general/browser_openPromptInBackgroundTab.js +++ b/browser/base/content/test/general/browser_openPromptInBackgroundTab.js @@ -19,8 +19,11 @@ add_task(function*() { let firstTab = gBrowser.selectedTab; // load page that opens prompt when page is hidden let openedTab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, pageWithAlert, true); + let openedTabGotAttentionPromise = BrowserTestUtils.waitForAttribute("attention", openedTab, "true"); // switch away from that tab again - this triggers the alert. yield BrowserTestUtils.switchTab(gBrowser, firstTab); + // ... but that's async on e10s... + yield openedTabGotAttentionPromise; // check for attention attribute is(openedTab.getAttribute("attention"), "true", "Tab with alert should have 'attention' attribute."); ok(!openedTab.selected, "Tab with alert should not be selected"); @@ -43,8 +46,15 @@ add_task(function*() { let ps = Services.perms; is(ps.ALLOW_ACTION, ps.testPermission(makeURI(pageWithAlert), "focus-tab-by-prompt"), "Tab switching should now be allowed"); + + let openedTabSelectedPromise = BrowserTestUtils.waitForAttribute("selected", openedTab, "true"); // switch to other tab again yield BrowserTestUtils.switchTab(gBrowser, firstTab); + + // This is sync in non-e10s, but in e10s we need to wait for this, so yield anyway. + // Note that the switchTab promise doesn't actually guarantee anything about *which* + // tab ends up as selected when its event fires, so using that here wouldn't work. + yield openedTabSelectedPromise; // should be switched back ok(openedTab.selected, "Ta-dah, the other tab should now be selected again!"); diff --git a/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm b/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm index 881384c2e2a9..72740ee5e9f9 100644 --- a/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm +++ b/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm @@ -603,6 +603,34 @@ this.BrowserTestUtils = { return extra; }), + /** + * Returns a promise that is resolved when element gains attribute (or, + * optionally, when it is set to value). + * @param {String} attr + * The attribute to wait for + * @param {Element} element + * The element which should gain the attribute + * @param {String} value (optional) + * Optional, the value the attribute should have. + * + * @returns {Promise} + */ + waitForAttribute(attr, element, value) { + let MutationObserver = element.ownerDocument.defaultView.MutationObserver; + return new Promise(resolve => { + let mut = new MutationObserver(mutations => { + if ((!value && element.getAttribute(attr)) || + (value && element.getAttribute(attr) === value)) { + resolve(); + mut.disconnect(); + return; + } + }); + + mut.observe(element, {attributeFilter: [attr]}); + }); + }, + /** * Version of EventUtils' `sendChar` function; it will synthesize a keypress * event in a child process and returns a Promise that will result when the