From 9743258506b3294b6def918f27c5f768cb23c321 Mon Sep 17 00:00:00 2001 From: Tim Taubert Date: Mon, 4 May 2015 19:08:06 +0200 Subject: [PATCH] Bug 1109875 - Fix various other tests to properly wait for data from closed tabs r=billm --- .../content/test/general/browser_bug580956.js | 8 +++---- .../content/test/general/browser_bug817947.js | 15 ++++++------ .../general/browser_tab_detach_restore.js | 1 + browser/components/sessionstore/test/head.js | 12 +--------- browser/components/tabview/content.js | 6 +++-- browser/components/tabview/favicons.js | 4 +++- .../tabview/test/browser_tabview_bug608037.js | 23 ++++++++++--------- .../tabview/test/browser_tabview_bug624847.js | 21 +++++++++-------- .../tabview/test/browser_tabview_bug628270.js | 21 +++++++++++------ .../BrowserTestUtils/BrowserTestUtil.jsm | 0 .../BrowserTestUtils/BrowserTestUtils.jsm | 22 ++++++++++++++++++ 11 files changed, 80 insertions(+), 53 deletions(-) create mode 100644 testing/mochitest/BrowserTestUtils/BrowserTestUtil.jsm diff --git a/browser/base/content/test/general/browser_bug580956.js b/browser/base/content/test/general/browser_bug580956.js index e7573b0f132..6d203bf3406 100644 --- a/browser/base/content/test/general/browser_bug580956.js +++ b/browser/base/content/test/general/browser_bug580956.js @@ -19,9 +19,9 @@ function test() { browser.addEventListener("load", function() { browser.removeEventListener("load", arguments.callee, true); - gBrowser.removeTab(tab); - ok(isUndoCloseEnabled(), "Undo Close Tab should be enabled."); - - finish(); + BrowserTestUtils.removeTab(tab).then(() => { + ok(isUndoCloseEnabled(), "Undo Close Tab should be enabled."); + finish(); + }); }, true); } diff --git a/browser/base/content/test/general/browser_bug817947.js b/browser/base/content/test/general/browser_bug817947.js index 142b016b439..3a76e36d3b9 100644 --- a/browser/base/content/test/general/browser_bug817947.js +++ b/browser/base/content/test/general/browser_bug817947.js @@ -34,14 +34,15 @@ function preparePendingTab(aCallback) { let tab = gBrowser.addTab(URL); whenLoaded(tab.linkedBrowser, function () { - gBrowser.removeTab(tab); - let [{state}] = JSON.parse(SessionStore.getClosedTabData(window)); + BrowserTestUtils.removeTab(tab).then(() => { + let [{state}] = JSON.parse(SessionStore.getClosedTabData(window)); - tab = gBrowser.addTab("about:blank"); - whenLoaded(tab.linkedBrowser, function () { - SessionStore.setTabState(tab, JSON.stringify(state)); - ok(tab.hasAttribute("pending"), "tab should be pending"); - aCallback(tab); + tab = gBrowser.addTab("about:blank"); + whenLoaded(tab.linkedBrowser, function () { + SessionStore.setTabState(tab, JSON.stringify(state)); + ok(tab.hasAttribute("pending"), "tab should be pending"); + aCallback(tab); + }); }); }); } diff --git a/browser/base/content/test/general/browser_tab_detach_restore.js b/browser/base/content/test/general/browser_tab_detach_restore.js index 804be0869d7..ef3522046da 100644 --- a/browser/base/content/test/general/browser_tab_detach_restore.js +++ b/browser/base/content/test/general/browser_tab_detach_restore.js @@ -10,6 +10,7 @@ add_task(function*() { let tab = gBrowser.addTab(); tab.linkedBrowser.loadURI(uri); yield BrowserTestUtils.browserLoaded(tab.linkedBrowser); + TabState.flush(tab.linkedBrowser); let key = tab.linkedBrowser.permanentKey; let win = gBrowser.replaceTabWithWindow(tab); diff --git a/browser/components/sessionstore/test/head.js b/browser/components/sessionstore/test/head.js index 5dffc25cdc3..1c00f071462 100644 --- a/browser/components/sessionstore/test/head.js +++ b/browser/components/sessionstore/test/head.js @@ -529,15 +529,5 @@ for (let name of FORM_HELPERS) { } function promiseRemoveTab(tab) { - return new Promise(resolve => { - let {messageManager: mm, frameLoader} = tab.linkedBrowser; - mm.addMessageListener("SessionStore:update", function onMessage(msg) { - if (msg.targetFrameLoader == frameLoader && msg.data.isFinal) { - mm.removeMessageListener("SessionStore:update", onMessage); - resolve(); - } - }, true); - - tab.ownerDocument.defaultView.gBrowser.removeTab(tab); - }); + return BrowserTestUtils.removeTab(tab); } diff --git a/browser/components/tabview/content.js b/browser/components/tabview/content.js index 049cfe757dc..309ce6fbbd9 100644 --- a/browser/components/tabview/content.js +++ b/browser/components/tabview/content.js @@ -61,7 +61,8 @@ let WindowMessageHandler = { // Function: isDocumentLoaded // Checks if the currently active document is loaded. isDocumentLoaded: function WMH_isDocumentLoaded(cx) { - let isLoaded = (content.document.readyState != "uninitialized" && + let isLoaded = (content && + content.document.readyState != "uninitialized" && !webProgress.isLoadingDocument); sendAsyncMessage(cx.name, {isLoaded: isLoaded}); @@ -71,7 +72,8 @@ let WindowMessageHandler = { // Function: isImageDocument // Checks if the currently active document is an image document or not. isImageDocument: function WMH_isImageDocument(cx) { - let isImageDocument = (content.document instanceof Ci.nsIImageDocument); + let isImageDocument = (content && + content.document instanceof Ci.nsIImageDocument); sendAsyncMessage(cx.name, {isImageDocument: isImageDocument}); }, diff --git a/browser/components/tabview/favicons.js b/browser/components/tabview/favicons.js index 7ec26d4e84f..e744b3a7554 100644 --- a/browser/components/tabview/favicons.js +++ b/browser/components/tabview/favicons.js @@ -89,7 +89,9 @@ let FavIcons = { tabImage = this._favIconService.getFaviconLinkForIcon(tabImageURI).spec; } - tabImage = PlacesUtils.getImageURLForResolution(window, tabImage); + if (tabImage) { + tabImage = PlacesUtils.getImageURLForResolution(window, tabImage); + } callback(tabImage); }, diff --git a/browser/components/tabview/test/browser_tabview_bug608037.js b/browser/components/tabview/test/browser_tabview_bug608037.js index 8e02d611fed..664dfe2bf8d 100644 --- a/browser/components/tabview/test/browser_tabview_bug608037.js +++ b/browser/components/tabview/test/browser_tabview_bug608037.js @@ -24,19 +24,20 @@ function onTabViewWindowLoaded() { is(groupItems.length, 1, "There is only one group"); is(groupItems[0].getChildren().length, 3, "The group has three tab items"); - gBrowser.removeTab(tabTwo); - ok(TabView.isVisible(), "Tab View is still visible after removing a tab"); - is(groupItems[0].getChildren().length, 2, "The group has two tab items"); + BrowserTestUtils.removeTab(tabTwo).then(() => { + ok(TabView.isVisible(), "Tab View is still visible after removing a tab"); + is(groupItems[0].getChildren().length, 2, "The group has two tab items"); - restoreTab(function (tabTwo) { - ok(TabView.isVisible(), "Tab View is still visible after restoring a tab"); - is(groupItems[0].getChildren().length, 3, "The group still has three tab items"); + restoreTab(function (tabTwo) { + ok(TabView.isVisible(), "Tab View is still visible after restoring a tab"); + is(groupItems[0].getChildren().length, 3, "The group still has three tab items"); - // clean up and finish - hideTabView(function () { - gBrowser.removeTab(tabOne); - gBrowser.removeTab(tabTwo); - finish(); + // clean up and finish + hideTabView(function () { + gBrowser.removeTab(tabOne); + gBrowser.removeTab(tabTwo); + finish(); + }); }); }); } diff --git a/browser/components/tabview/test/browser_tabview_bug624847.js b/browser/components/tabview/test/browser_tabview_bug624847.js index 685a132d606..b614668ca1e 100644 --- a/browser/components/tabview/test/browser_tabview_bug624847.js +++ b/browser/components/tabview/test/browser_tabview_bug624847.js @@ -56,18 +56,19 @@ function test() { assertNumberOfTabs(2); afterAllTabsLoaded(function () { - win.gBrowser.removeTab(tab); - assertNumberOfTabs(1); - assertNumberOfPinnedTabs(0); + BrowserTestUtils.removeTab(tab).then(() => { + assertNumberOfTabs(1); + assertNumberOfPinnedTabs(0); - restoreTab(function () { - prefix = 'unpinned-restored'; - assertValidPrerequisites(); - assertGroupItemPreserved(); + restoreTab(function () { + prefix = 'unpinned-restored'; + assertValidPrerequisites(); + assertGroupItemPreserved(); - createBlankTab(); - afterAllTabsLoaded(testUndoCloseWithSelectedBlankPinnedTab, win); - }, 0, win); + createBlankTab(); + afterAllTabsLoaded(testUndoCloseWithSelectedBlankPinnedTab, win); + }, 0, win); + }); }, win); } diff --git a/browser/components/tabview/test/browser_tabview_bug628270.js b/browser/components/tabview/test/browser_tabview_bug628270.js index 4887c88a593..2119c3c2c6b 100644 --- a/browser/components/tabview/test/browser_tabview_bug628270.js +++ b/browser/components/tabview/test/browser_tabview_bug628270.js @@ -70,17 +70,24 @@ function test() { let groupItem = getGroupItem(1); let tabItem = groupItem.getChild(0); + // Wait until the tab has been removed but close it ourselves. + let promise = BrowserTestUtils.removeTab(tabItem.tab, {dontRemove: true}); + + // Close the tab. EventUtils.synthesizeMouseAtCenter( tabItem.$close[0], {}, TabView.getContentWindow()); - assertNumberOfTabsInGroup(groupItem, 1); - restoreTab(function () { - assertNumberOfTabsInGroup(groupItem, 2); + promise.then(() => { + assertNumberOfTabsInGroup(groupItem, 1); - activateFirstGroupItem(); - gBrowser.removeTab(gBrowser.tabs[1]); - gBrowser.removeTab(gBrowser.tabs[1]); - hideTabView(finishTest); + restoreTab(function () { + assertNumberOfTabsInGroup(groupItem, 2); + + activateFirstGroupItem(); + gBrowser.removeTab(gBrowser.tabs[1]); + gBrowser.removeTab(gBrowser.tabs[1]); + hideTabView(finishTest); + }); }); } diff --git a/testing/mochitest/BrowserTestUtils/BrowserTestUtil.jsm b/testing/mochitest/BrowserTestUtils/BrowserTestUtil.jsm new file mode 100644 index 00000000000..e69de29bb2d diff --git a/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm b/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm index 569b1cbf13c..4dfd93ebf77 100644 --- a/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm +++ b/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm @@ -339,5 +339,27 @@ this.BrowserTestUtils = { synthesizeMouseAtPoint(offsetX, offsetY, event, browser) { return BrowserTestUtils.synthesizeMouse(null, offsetX, offsetY, event, browser); + }, + + /** + * Removes the given tab from its parent tabbrowser and + * waits until its final message has reached the parent. + */ + removeTab(tab, options = {}) { + let dontRemove = options && options.dontRemove; + + return new Promise(resolve => { + let {messageManager: mm, frameLoader} = tab.linkedBrowser; + mm.addMessageListener("SessionStore:update", function onMessage(msg) { + if (msg.targetFrameLoader == frameLoader && msg.data.isFinal) { + mm.removeMessageListener("SessionStore:update", onMessage); + resolve(); + } + }, true); + + if (!dontRemove && !tab.closing) { + tab.ownerDocument.defaultView.gBrowser.removeTab(tab); + } + }); } };