browser_bug1236512.js (3759B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 6 "use strict"; 7 8 add_setup(async function () { 9 await SpecialPowers.pushPrefEnv({ 10 set: [["test.wait300msAfterTabSwitch", true]], 11 }); 12 }); 13 14 const testPageURL = 15 "http://mochi.test:8888/browser/dom/tests/browser/dummy.html"; 16 17 async function testContentVisibilityState(aIsHidden, aBrowser) { 18 await SpecialPowers.spawn( 19 aBrowser.selectedBrowser, 20 [aIsHidden], 21 aExpectedResult => { 22 is(content.document.hidden, aExpectedResult, "document.hidden"); 23 is( 24 content.document.visibilityState, 25 aExpectedResult ? "hidden" : "visible", 26 "document.visibilityState" 27 ); 28 } 29 ); 30 } 31 32 async function waitContentVisibilityChange(aIsHidden, aBrowser) { 33 await SpecialPowers.spawn( 34 aBrowser.selectedBrowser, 35 [aIsHidden], 36 async function (aExpectedResult) { 37 let visibilityState = aExpectedResult ? "hidden" : "visible"; 38 if ( 39 content.document.hidden === aExpectedResult && 40 content.document.visibilityState === visibilityState 41 ) { 42 ok(true, "already changed to expected visibility state"); 43 return; 44 } 45 46 info("wait visibilitychange event"); 47 await ContentTaskUtils.waitForEvent( 48 content.document, 49 "visibilitychange", 50 true /* capture */, 51 () => { 52 info( 53 `visibilitychange: ${content.document.hidden} ${content.document.visibilityState}` 54 ); 55 return ( 56 content.document.hidden === aExpectedResult && 57 content.document.visibilityState === visibilityState 58 ); 59 } 60 ); 61 } 62 ); 63 } 64 65 /** 66 * This test is to test the visibility state will change to "hidden" when browser 67 * window is fully covered by another non-translucent application. Note that we 68 * only support this on Mac for now, other platforms don't support reporting 69 * occlusion state. 70 */ 71 add_task(async function () { 72 info("creating test window"); 73 let winTest = await BrowserTestUtils.openNewBrowserWindow(); 74 // Specify the width, height, left and top, so that the new window can be 75 // fully covered by "window". 76 let resizePromise = BrowserTestUtils.waitForEvent( 77 winTest, 78 "resize", 79 false, 80 () => { 81 return winTest.innerHeight <= 500 && winTest.innerWidth <= 500; 82 } 83 ); 84 winTest.moveTo(200, 200); 85 winTest.resizeTo(500, 500); 86 await resizePromise; 87 88 let browserTest = winTest.gBrowser; 89 90 info(`loading test page: ${testPageURL}`); 91 BrowserTestUtils.startLoadingURIString( 92 browserTest.selectedBrowser, 93 testPageURL 94 ); 95 await BrowserTestUtils.browserLoaded(browserTest.selectedBrowser); 96 97 info("test init visibility state"); 98 await testContentVisibilityState(false /* isHidden */, browserTest); 99 100 info( 101 "test window should report 'hidden' if it is fully covered by another " + 102 "window" 103 ); 104 await new Promise(resolve => waitForFocus(resolve, window)); 105 await waitContentVisibilityChange(true /* isHidden */, browserTest); 106 107 info( 108 "test window should still report 'hidden' since it is still fully covered " + 109 "by another window" 110 ); 111 let tab = BrowserTestUtils.addTab(browserTest); 112 await BrowserTestUtils.switchTab(browserTest, tab); 113 BrowserTestUtils.removeTab(browserTest.selectedTab); 114 await testContentVisibilityState(true /* isHidden */, browserTest); 115 116 info( 117 "test window should report 'visible' if it is not fully covered by " + 118 "another window" 119 ); 120 await new Promise(resolve => waitForFocus(resolve, winTest)); 121 await waitContentVisibilityChange(false /* isHidden */, browserTest); 122 123 info("closing test window"); 124 await BrowserTestUtils.closeWindow(winTest); 125 });