browser_stop_sharing_button.js (5416B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const TEST_ROOT = getRootDirectory(gTestPath).replace( 7 "chrome://mochitests/content/", 8 "https://example.com/" 9 ); 10 const TEST_PAGE = TEST_ROOT + "get_user_media.html"; 11 12 add_setup(async function () { 13 let prefs = [ 14 [PREF_PERMISSION_FAKE, true], 15 [PREF_AUDIO_LOOPBACK, ""], 16 [PREF_VIDEO_LOOPBACK, ""], 17 [PREF_FAKE_STREAMS, true], 18 [PREF_FOCUS_SOURCE, false], 19 ]; 20 await SpecialPowers.pushPrefEnv({ set: prefs }); 21 }); 22 23 /** 24 * Tests that if the user chooses to "Stop Sharing" a display while 25 * also sharing their microphone or camera, that only the display 26 * stream is stopped. 27 */ 28 add_task(async function test_stop_sharing() { 29 await BrowserTestUtils.withNewTab(TEST_PAGE, async browser => { 30 let indicatorPromise = promiseIndicatorWindow(); 31 32 await shareDevices( 33 browser, 34 true /* camera */, 35 true /* microphone */, 36 SHARE_SCREEN 37 ); 38 39 let indicator = await indicatorPromise; 40 41 let stopSharingButton = indicator.document.getElementById("stop-sharing"); 42 let stopSharingPromise = expectObserverCalled("recording-device-events"); 43 stopSharingButton.click(); 44 await stopSharingPromise; 45 46 // Ensure that we're still sharing the other streams. 47 await checkSharingUI({ audio: true, video: true }); 48 49 // Ensure that the "display-share" section of the indicator is now hidden 50 Assert.ok( 51 BrowserTestUtils.isHidden( 52 indicator.document.getElementById("display-share") 53 ), 54 "The display-share section of the indicator should now be hidden." 55 ); 56 }); 57 }); 58 59 /** 60 * Tests that if the user chooses to "Stop Sharing" a display while 61 * sharing their display on multiple sites, all of those display sharing 62 * streams are closed. 63 */ 64 add_task(async function test_stop_sharing_multiple() { 65 let indicatorPromise = promiseIndicatorWindow(); 66 67 info("Opening first tab"); 68 let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_PAGE); 69 info("Sharing camera, microphone and screen"); 70 await shareDevices(tab1.linkedBrowser, true, true, SHARE_SCREEN); 71 72 info("Opening second tab"); 73 let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_PAGE); 74 info("Sharing camera and screen"); 75 await shareDevices(tab2.linkedBrowser, true, false, SHARE_SCREEN); 76 77 let indicator = await indicatorPromise; 78 79 let stopSharingButton = indicator.document.getElementById("stop-sharing"); 80 let stopSharingPromise = TestUtils.waitForCondition(() => { 81 return !webrtcUI.showScreenSharingIndicator; 82 }); 83 stopSharingButton.click(); 84 await stopSharingPromise; 85 86 Assert.equal(gBrowser.selectedTab, tab2, "Should have tab2 selected."); 87 await checkSharingUI({ audio: false, video: true }, window, { 88 audio: true, 89 video: true, 90 }); 91 BrowserTestUtils.removeTab(tab2); 92 93 Assert.equal(gBrowser.selectedTab, tab1, "Should have tab1 selected."); 94 await checkSharingUI({ audio: true, video: true }, window, { 95 audio: true, 96 video: true, 97 }); 98 99 // Ensure that the "display-share" section of the indicator is now hidden 100 Assert.ok( 101 BrowserTestUtils.isHidden( 102 indicator.document.getElementById("display-share") 103 ), 104 "The display-share section of the indicator should now be hidden." 105 ); 106 107 BrowserTestUtils.removeTab(tab1); 108 }); 109 110 /** 111 * Tests that if the user chooses to "Stop Sharing" a display, persistent 112 * permissions are not removed for camera or microphone devices. 113 */ 114 add_task(async function test_keep_permissions() { 115 await BrowserTestUtils.withNewTab(TEST_PAGE, async browser => { 116 let indicatorPromise = promiseIndicatorWindow(); 117 118 await shareDevices( 119 browser, 120 true /* camera */, 121 true /* microphone */, 122 SHARE_SCREEN, 123 true /* remember */ 124 ); 125 126 let indicator = await indicatorPromise; 127 128 let stopSharingButton = indicator.document.getElementById("stop-sharing"); 129 let stopSharingPromise = expectObserverCalled("recording-device-events"); 130 stopSharingButton.click(); 131 await stopSharingPromise; 132 133 // Ensure that we're still sharing the other streams. 134 await checkSharingUI({ audio: true, video: true }, undefined, undefined, { 135 audio: { scope: SitePermissions.SCOPE_PERSISTENT }, 136 video: { scope: SitePermissions.SCOPE_PERSISTENT }, 137 }); 138 139 // Ensure that the "display-share" section of the indicator is now hidden 140 Assert.ok( 141 BrowserTestUtils.isHidden( 142 indicator.document.getElementById("display-share") 143 ), 144 "The display-share section of the indicator should now be hidden." 145 ); 146 147 let { state: micState, scope: micScope } = SitePermissions.getForPrincipal( 148 browser.contentPrincipal, 149 "microphone", 150 browser 151 ); 152 153 Assert.equal(micState, SitePermissions.ALLOW); 154 Assert.equal(micScope, SitePermissions.SCOPE_PERSISTENT); 155 156 let { state: camState, scope: camScope } = SitePermissions.getForPrincipal( 157 browser.contentPrincipal, 158 "camera", 159 browser 160 ); 161 Assert.equal(camState, SitePermissions.ALLOW); 162 Assert.equal(camScope, SitePermissions.SCOPE_PERSISTENT); 163 164 SitePermissions.removeFromPrincipal( 165 browser.contentPrincipal, 166 "camera", 167 browser 168 ); 169 SitePermissions.removeFromPrincipal( 170 browser.contentPrincipal, 171 "microphone", 172 browser 173 ); 174 }); 175 });