browser_windowactivation.js (4847B)
1 /* 2 * This test checks that window activation state is set properly with multiple tabs. 3 */ 4 5 const testPageChrome = 6 getRootDirectory(gTestPath) + "file_window_activation.html"; 7 const testPageHttp = testPageChrome.replace( 8 "chrome://mochitests/content", 9 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 10 "http://example.com" 11 ); 12 const testPageWindow = 13 getRootDirectory(gTestPath) + "file_window_activation2.html"; 14 15 add_task(async function reallyRunTests() { 16 let chromeTab1 = await BrowserTestUtils.openNewForegroundTab( 17 gBrowser, 18 testPageChrome 19 ); 20 let chromeBrowser1 = chromeTab1.linkedBrowser; 21 22 // This can't use openNewForegroundTab because if we focus chromeTab2 now, we 23 // won't send a focus event during test 6, further down in this file. 24 let chromeTab2 = BrowserTestUtils.addTab(gBrowser, testPageChrome); 25 let chromeBrowser2 = chromeTab2.linkedBrowser; 26 await BrowserTestUtils.browserLoaded(chromeBrowser2); 27 28 let httpTab = BrowserTestUtils.addTab(gBrowser, testPageHttp); 29 let httpBrowser = httpTab.linkedBrowser; 30 await BrowserTestUtils.browserLoaded(httpBrowser); 31 32 function failTest() { 33 ok(false, "Test received unexpected activate/deactivate event"); 34 } 35 36 // chrome:// url tabs should not receive "activate" or "deactivate" events 37 // as they should be sent to the top-level window in the parent process. 38 for (let b of [chromeBrowser1, chromeBrowser2]) { 39 BrowserTestUtils.waitForContentEvent(b, "activate", true).then(failTest); 40 BrowserTestUtils.waitForContentEvent(b, "deactivate", true).then(failTest); 41 } 42 43 gURLBar.focus(); 44 45 gBrowser.selectedTab = chromeTab1; 46 47 // The test performs four checks, using -moz-window-inactive on three child 48 // tabs (2 loading chrome:// urls and one loading an http:// url). 49 // First, the initial state should be transparent. The second check is done 50 // while another window is focused. The third check is done after that window 51 // is closed and the main window focused again. The fourth check is done after 52 // switching to the second tab. 53 54 // Step 1 - check the initial state 55 let colorChromeBrowser1 = await getBackgroundColor(chromeBrowser1, true); 56 let colorChromeBrowser2 = await getBackgroundColor(chromeBrowser2, true); 57 let colorHttpBrowser = await getBackgroundColor(httpBrowser, true); 58 is(colorChromeBrowser1, "rgba(0, 0, 0, 0)", "first tab initial"); 59 is(colorChromeBrowser2, "rgba(0, 0, 0, 0)", "second tab initial"); 60 is(colorHttpBrowser, "rgba(0, 0, 0, 0)", "third tab initial"); 61 62 // Step 2 - open and focus another window 63 let otherWindow = window.open(testPageWindow, "", "chrome"); 64 await SimpleTest.promiseFocus(otherWindow); 65 colorChromeBrowser1 = await getBackgroundColor(chromeBrowser1, false); 66 colorChromeBrowser2 = await getBackgroundColor(chromeBrowser2, false); 67 colorHttpBrowser = await getBackgroundColor(httpBrowser, false); 68 is(colorChromeBrowser1, "rgb(255, 0, 0)", "first tab lowered"); 69 is(colorChromeBrowser2, "rgb(255, 0, 0)", "second tab lowered"); 70 is(colorHttpBrowser, "rgb(255, 0, 0)", "third tab lowered"); 71 72 // Step 3 - close the other window again 73 otherWindow.close(); 74 colorChromeBrowser1 = await getBackgroundColor(chromeBrowser1, true); 75 colorChromeBrowser2 = await getBackgroundColor(chromeBrowser2, true); 76 colorHttpBrowser = await getBackgroundColor(httpBrowser, true); 77 is(colorChromeBrowser1, "rgba(0, 0, 0, 0)", "first tab raised"); 78 is(colorChromeBrowser2, "rgba(0, 0, 0, 0)", "second tab raised"); 79 is(colorHttpBrowser, "rgba(0, 0, 0, 0)", "third tab raised"); 80 81 // Step 4 - switch to the second tab 82 gBrowser.selectedTab = chromeTab2; 83 colorChromeBrowser1 = await getBackgroundColor(chromeBrowser1, true); 84 colorChromeBrowser2 = await getBackgroundColor(chromeBrowser2, true); 85 colorHttpBrowser = await getBackgroundColor(httpBrowser, true); 86 is(colorChromeBrowser1, "rgba(0, 0, 0, 0)", "first tab after tab switch"); 87 is(colorChromeBrowser2, "rgba(0, 0, 0, 0)", "second tab after tab switch"); 88 is(colorHttpBrowser, "rgba(0, 0, 0, 0)", "third tab after tab switch"); 89 90 BrowserTestUtils.removeTab(chromeTab1); 91 BrowserTestUtils.removeTab(chromeTab2); 92 BrowserTestUtils.removeTab(httpTab); 93 otherWindow = null; 94 }); 95 96 function getBackgroundColor(browser, expectedActive) { 97 return SpecialPowers.spawn( 98 browser, 99 [!expectedActive], 100 async hasPseudoClass => { 101 let area = content.document.getElementById("area"); 102 await ContentTaskUtils.waitForCondition(() => { 103 return area; 104 }, "Page has loaded"); 105 await ContentTaskUtils.waitForCondition( 106 () => { 107 return area.matches(":-moz-window-inactive") == hasPseudoClass; 108 }, 109 `Window is considered ${hasPseudoClass ? "inactive" : "active"}` 110 ); 111 112 return content.getComputedStyle(area).backgroundColor; 113 } 114 ); 115 }