TabGroupTestUtils.sys.mjs (3908B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 import { BrowserTestUtils } from "resource://testing-common/BrowserTestUtils.sys.mjs"; 6 import { SessionStore } from "resource:///modules/sessionstore/SessionStore.sys.mjs"; 7 import { TabStateFlusher } from "resource:///modules/sessionstore/TabStateFlusher.sys.mjs"; 8 9 /** 10 * TabGroupTestUtils providers helpers for working with tab groups 11 * in browser chrome tests. 12 * 13 * @class 14 */ 15 export const TabGroupTestUtils = { 16 /** 17 * Change a group's collapsed state and wait for its 18 * animation to finish. 19 * 20 * @param {MozTabbrowserTabGroup} group 21 * @param {boolean} [force] 22 * force the group to be collapsed (true) or expanded (false) 23 * instead of toggling previous state. 24 * @returns {Promise<void>} 25 */ 26 async toggleCollapsed(group, force) { 27 let shouldCollapse; 28 if (typeof force !== "undefined") { 29 shouldCollapse = force; 30 } else { 31 shouldCollapse = !group.collapsed; 32 } 33 if (group.collapsed == shouldCollapse) { 34 return; 35 } 36 let animationComplete = BrowserTestUtils.waitForEvent( 37 group, 38 "TabGroupAnimationComplete" 39 ); 40 group.collapsed = shouldCollapse; 41 await animationComplete; 42 }, 43 44 /** 45 * Ensures that tabs have all browsers fully loaded and their browser states 46 * flushed into session state. This is a prerequisite for any code that 47 * relies on session state, e.g. saved or closed tab groups. 48 * 49 * @param {MozTabbrowserTab[]} tabs 50 */ 51 async ensureTabsLoaded(tabs) { 52 await Promise.all( 53 tabs.map(async tab => { 54 await BrowserTestUtils.browserLoaded(tab.linkedBrowser); 55 await TabStateFlusher.flush(tab.linkedBrowser); 56 }) 57 ); 58 }, 59 60 /** 61 * Ensures that a tab group has all browsers fully loaded and their 62 * browser states flushed into session state. This is a prerequisite for any 63 * code that relies on session state, e.g. saved or closed tab groups. 64 * 65 * @param {MozTabbrowserTabGroup} tabGroup 66 */ 67 ensureTabGroupLoaded(tabGroup) { 68 return this.ensureTabsLoaded(tabGroup.tabs); 69 }, 70 71 /** 72 * Removes a tab group, along with its tabs. Resolves when the tab group 73 * is gone. 74 * 75 * @param {MozTabbrowserTabGroup} group group to be removed 76 * @returns {Promise<void>} 77 */ 78 async removeTabGroup(group) { 79 if (!group.parentNode) { 80 // group was already removed 81 return; 82 } 83 let removePromise = BrowserTestUtils.waitForEvent(group, "TabGroupRemoved"); 84 await group.ownerGlobal.gBrowser.removeTabGroup(group, { animate: false }); 85 await removePromise; 86 }, 87 88 /** 89 * Saves and closes a tab group. Resolves when the tab group is saved and 90 * available in session state. 91 * 92 * @param {MozTabbrowserTabGroup} group 93 * @returns {Promise<void>} 94 */ 95 async saveAndCloseTabGroup(group) { 96 // The session's cached tab state may not reflect the current state of the 97 // group's tabs. If `group` was just created in a test, it's possible that 98 // a tab group won't be saved if the group's tabs haven't had a non-empty 99 // state flushed to the session. 100 await Promise.allSettled( 101 group.tabs.map(tab => TabStateFlusher.flush(tab.linkedBrowser)) 102 ); 103 104 let promises = [ 105 BrowserTestUtils.waitForEvent(group, "TabGroupSaved"), 106 BrowserTestUtils.waitForEvent(group, "TabGroupRemoved"), 107 ]; 108 group.saveAndClose(); 109 await Promise.allSettled(promises); 110 }, 111 112 /** 113 * Forgets any saved tab groups that may have been automatically saved when 114 * closing a test window that still had open tab group(s). 115 */ 116 forgetSavedTabGroups() { 117 for (const savedGroup of SessionStore.getSavedTabGroups()) { 118 SessionStore.forgetSavedTabGroup(savedGroup.id); 119 } 120 }, 121 };