diff --git a/test/content/support.js b/test/content/support.js index 8b629e2751..07d8991584 100644 --- a/test/content/support.js +++ b/test/content/support.js @@ -125,6 +125,28 @@ var loadZoteroPane = async function (win) { return win; }; +/** + * Bring the main window to the front, for tests that rely on focus (e.g., editable-text). + */ +async function activateZoteroPane() { + let win = Zotero.getMainWindow(); + if (!win) { + throw new Error('Main window is not open'); + } + // If the window is already active, we're done. + // The main window is active from the start on Linux, but opens in the + // background on other platforms. + if (Services.focus.activeWindow === win) { + return; + } + let activatePromise = new Promise( + resolve => win.addEventListener('activate', resolve, { once: true }) + ); + Zotero.Utilities.Internal.activate(); + Zotero.Utilities.Internal.activate(win); + await activatePromise; +} + var loadPrefPane = async function (paneName) { var id = 'zotero-prefpane-' + paneName; var win = await loadWindow("chrome://zotero/content/preferences/preferences.xhtml", { diff --git a/test/tests/itemPaneTest.js b/test/tests/itemPaneTest.js index 80ea4f5b01..27117f0553 100644 --- a/test/tests/itemPaneTest.js +++ b/test/tests/itemPaneTest.js @@ -237,17 +237,7 @@ describe("Item pane", function () { describe("Info pane", function () { before(async () => { - if (!doc.hasFocus()) { - // editable-text behavior relies on focus, so we first need to bring the window to the front. - // Not required on all platforms. In some cases (e.g. Linux), the window is at the front from the start. - let win = Zotero.getMainWindow(); - let activatePromise = new Promise( - resolve => win.addEventListener('activate', resolve, { once: true }) - ); - Zotero.Utilities.Internal.activate(); - Zotero.Utilities.Internal.activate(win); - await activatePromise; - } + await activateZoteroPane(); }); it("should place Title after Item Type and before creators", async function () { var item = await createDataObject('item'); diff --git a/test/tests/tagsboxTest.js b/test/tests/tagsboxTest.js index 3b3228f33f..82ba1f5f1a 100644 --- a/test/tests/tagsboxTest.js +++ b/test/tests/tagsboxTest.js @@ -19,17 +19,7 @@ describe("Item Tags Box", function () { describe("Tag Editing", function () { before(async () => { - if (!doc.hasFocus()) { - // editable-text behavior relies on focus, so we first need to bring the window to the front. - // Not required on all platforms. In some cases (e.g. Linux), the window is at the front from the start. - let win = Zotero.getMainWindow(); - let activatePromise = new Promise( - resolve => win.addEventListener('activate', resolve, { once: true }) - ); - Zotero.Utilities.Internal.activate(); - Zotero.Utilities.Internal.activate(win); - await activatePromise; - } + await activateZoteroPane(); }); it("should update tag when pressing Enter in textbox", async function () { var tag = Zotero.Utilities.randomString();