browser_captivePortal_certErrorUI.js (7301B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 add_setup(async function () { 7 await SpecialPowers.pushPrefEnv({ 8 set: [ 9 ["test.wait300msAfterTabSwitch", true], 10 ["captivedetect.canonicalURL", CANONICAL_URL], 11 ["captivedetect.canonicalContent", CANONICAL_CONTENT], 12 ], 13 }); 14 }); 15 16 // This tests the alternate cert error UI when we are behind a captive portal. 17 add_task(async function checkCaptivePortalCertErrorUI() { 18 info( 19 "Checking that the alternate cert error UI is shown when we are behind a captive portal" 20 ); 21 22 // Open a second window in the background. Later, we'll check that 23 // when we click the button to open the captive portal tab, the tab 24 // only opens in the active window and not in the background one. 25 let secondWindow = await openWindowAndWaitForFocus(); 26 await SimpleTest.promiseFocus(window); 27 28 await portalDetected(); 29 30 // Check that we didn't open anything in the background window. 31 ensureNoPortalTab(secondWindow); 32 33 let tab = await openCaptivePortalErrorTab(); 34 let browser = tab.linkedBrowser; 35 let portalTabPromise = BrowserTestUtils.waitForNewTab( 36 gBrowser, 37 CANONICAL_URL 38 ); 39 40 await SpecialPowers.spawn(browser, [], async () => { 41 let doc = content.document; 42 let loginButton = doc.getElementById("openPortalLoginPageButton"); 43 await ContentTaskUtils.waitForCondition( 44 () => ContentTaskUtils.isVisible(loginButton), 45 "Captive portal error page UI is visible" 46 ); 47 48 if (!Services.focus.focusedElement == loginButton) { 49 await ContentTaskUtils.waitForEvent(loginButton, "focus"); 50 } 51 52 Assert.ok(true, "openPortalLoginPageButton has focus"); 53 info("Clicking the Open Login Page button"); 54 await EventUtils.synthesizeMouseAtCenter(loginButton, {}, content); 55 }); 56 57 let portalTab = await portalTabPromise; 58 is( 59 gBrowser.selectedTab, 60 portalTab, 61 "Login page should be open in a new foreground tab." 62 ); 63 64 // Check that we didn't open anything in the background window. 65 ensureNoPortalTab(secondWindow); 66 67 // Make sure clicking the "Open Login Page" button again focuses the existing portal tab. 68 await BrowserTestUtils.switchTab(gBrowser, tab); 69 // Passing an empty function to BrowserTestUtils.switchTab lets us wait for an arbitrary 70 // tab switch. 71 portalTabPromise = BrowserTestUtils.switchTab(gBrowser, () => {}); 72 await SpecialPowers.spawn(browser, [], async () => { 73 info("Clicking the Open Login Page button."); 74 let loginButton = content.document.getElementById( 75 "openPortalLoginPageButton" 76 ); 77 await EventUtils.synthesizeMouseAtCenter(loginButton, {}, content); 78 }); 79 80 info("Opening captive portal login page"); 81 let portalTab2 = await portalTabPromise; 82 is(portalTab2, portalTab, "The existing portal tab should be focused."); 83 84 // Check that we didn't open anything in the background window. 85 ensureNoPortalTab(secondWindow); 86 87 let portalTabClosing = BrowserTestUtils.waitForTabClosing(portalTab); 88 let errorTabReloaded = BrowserTestUtils.waitForErrorPage(browser); 89 90 Services.obs.notifyObservers(null, "captive-portal-login-success"); 91 await portalTabClosing; 92 93 info( 94 "Waiting for error tab to be reloaded after the captive portal was freed." 95 ); 96 await errorTabReloaded; 97 await SpecialPowers.spawn(browser, [], () => { 98 let doc = content.document; 99 ok( 100 !doc.body.classList.contains("captiveportal"), 101 "Captive portal error page UI is not visible." 102 ); 103 }); 104 105 await BrowserTestUtils.removeTab(tab); 106 await BrowserTestUtils.closeWindow(secondWindow); 107 }); 108 109 add_task(async function testCaptivePortalAdvancedPanel() { 110 info( 111 "Checking that the advanced section of the about:certerror UI is shown when we are behind a captive portal." 112 ); 113 await portalDetected(); 114 let tab = await openCaptivePortalErrorTab(); 115 let browser = tab.linkedBrowser; 116 117 const waitForLocationChange = (async () => { 118 await BrowserTestUtils.waitForLocationChange(gBrowser, BAD_CERT_PAGE); 119 info("(waitForLocationChange resolved)"); 120 })(); 121 await SpecialPowers.spawn(browser, [BAD_CERT_PAGE], async () => { 122 const doc = content.document; 123 let advancedButton = doc.getElementById("advancedButton"); 124 await ContentTaskUtils.waitForCondition( 125 () => ContentTaskUtils.isVisible(advancedButton), 126 "Captive portal UI is visible" 127 ); 128 129 info("Clicking on the advanced button"); 130 const advPanel = doc.getElementById("badCertAdvancedPanel"); 131 ok( 132 !ContentTaskUtils.isVisible(advPanel), 133 "Advanced panel is not yet visible" 134 ); 135 await EventUtils.synthesizeMouseAtCenter(advancedButton, {}, content); 136 ok(ContentTaskUtils.isVisible(advPanel), "Advanced panel is now visible"); 137 138 let advPanelContent = doc.getElementById("badCertTechnicalInfo"); 139 ok( 140 ContentTaskUtils.isVisible(advPanelContent) && 141 advPanelContent.textContent.includes("expired.example.com"), 142 "Advanced panel text content is visible" 143 ); 144 145 let advPanelErrorCode = doc.getElementById("errorCode"); 146 ok( 147 advPanelErrorCode.textContent, 148 "Cert error code is visible in the advanced panel" 149 ); 150 151 // - 152 153 const advPanelExceptionButton = doc.getElementById("exceptionDialogButton"); 154 155 function isOnCertErrorPage() { 156 return ContentTaskUtils.isVisible(advPanel); 157 } 158 159 ok(isOnCertErrorPage(), "On cert error page before adding exception"); 160 ok( 161 advPanelExceptionButton.disabled, 162 "Exception button should start disabled" 163 ); 164 await EventUtils.synthesizeMouseAtCenter( 165 advPanelExceptionButton, 166 {}, 167 content 168 ); // Click 169 const clickTime = content.performance.now(); 170 ok( 171 isOnCertErrorPage(), 172 "Still on cert error page because clicked too early" 173 ); 174 175 // Now waitForCondition now that it's possible. 176 try { 177 await ContentTaskUtils.waitForCondition( 178 () => !advPanelExceptionButton.disabled, 179 "Wait for exception button enabled" 180 ); 181 } catch (rejected) { 182 ok(false, rejected); 183 return; 184 } 185 ok( 186 !advPanelExceptionButton.disabled, 187 "Exception button should be enabled after waiting" 188 ); 189 const msSinceClick = content.performance.now() - clickTime; 190 Assert.greater( 191 msSinceClick, 192 1000, 193 `Exception button should stay disabled for ${msSinceClick} > 1000 ms` 194 ); 195 196 await EventUtils.synthesizeMouseAtCenter( 197 advPanelExceptionButton, 198 {}, 199 content 200 ); // Click 201 info("Clicked"); 202 }); 203 await waitForLocationChange; 204 info("Page reloaded after adding cert exception"); 205 206 // Clear the certificate exception. 207 let certOverrideService = Cc[ 208 "@mozilla.org/security/certoverride;1" 209 ].getService(Ci.nsICertOverrideService); 210 certOverrideService.clearValidityOverride("expired.example.com", -1, {}); 211 212 info("After clearing cert override, asking for reload..."); 213 const waitForErrorPage = BrowserTestUtils.waitForErrorPage(browser); 214 await SpecialPowers.spawn(browser, [], async () => { 215 info("reload..."); 216 content.location.reload(); 217 }); 218 info("waitForErrorPage..."); 219 await waitForErrorPage; 220 221 info("removeTab..."); 222 await BrowserTestUtils.removeTab(tab); 223 info("Done!"); 224 });