browser_aboutdebugging_sidebar_connection_state.js (5830B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const RUNTIME_ID = "test-runtime-id"; 7 const RUNTIME_NAME = "test runtime name"; 8 const RUNTIME_DEVICE_NAME = "test device name"; 9 const RUNTIME_SHORT_NAME = "test short name"; 10 11 const CONNECTION_TIMING_OUT_DELAY = 1000; 12 const CONNECTION_CANCEL_DELAY = 2000; 13 14 // Test following connection state tests. 15 // * Connect button label and state will change during connecting. 16 // * Show error message if connection failed. 17 // * Show warninng if connection has been taken time. 18 add_task(async function () { 19 await setupPreferences(); 20 21 const mocks = new Mocks(); 22 23 const { document, tab } = await openAboutDebugging(); 24 25 mocks.createUSBRuntime(RUNTIME_ID, { 26 name: RUNTIME_NAME, 27 deviceName: RUNTIME_DEVICE_NAME, 28 shortName: RUNTIME_SHORT_NAME, 29 }); 30 mocks.emitUSBUpdate(); 31 32 info("Wait until the USB sidebar item appears"); 33 await waitUntil(() => findSidebarItemByText(RUNTIME_DEVICE_NAME, document)); 34 const usbRuntimeSidebarItem = findSidebarItemByText( 35 RUNTIME_DEVICE_NAME, 36 document 37 ); 38 const connectButton = 39 usbRuntimeSidebarItem.querySelector(".qa-connect-button"); 40 41 info("Simulate to happen connection error"); 42 mocks.runtimeClientFactoryMock.createClientForRuntime = async () => { 43 throw new Error("Dummy connection error"); 44 }; 45 46 info( 47 "Check whether the error message displayed after clicking connect button" 48 ); 49 connectButton.click(); 50 await waitUntil(() => document.querySelector(".qa-connection-error")); 51 ok(true, "Error message displays when connection failed"); 52 53 info("Simulate to wait for the connection prompt on remote runtime"); 54 let resumeConnection; 55 const resumeConnectionPromise = new Promise(r => { 56 resumeConnection = r; 57 }); 58 mocks.runtimeClientFactoryMock.createClientForRuntime = async runtime => { 59 await resumeConnectionPromise; 60 return mocks._clients[runtime.type][runtime.id]; 61 }; 62 63 info("Click on the connect button and wait until it disappears"); 64 connectButton.click(); 65 info("Check whether a warning of connection not responding displays"); 66 await waitUntil(() => 67 document.querySelector(".qa-connection-not-responding") 68 ); 69 ok( 70 document.querySelector(".qa-connection-not-responding"), 71 "A warning of connection not responding displays" 72 ); 73 ok(connectButton.disabled, "Connect button is disabled"); 74 ok( 75 connectButton.textContent.startsWith("Connecting"), 76 "Label of the connect button changes" 77 ); 78 ok( 79 !document.querySelector(".qa-connection-error"), 80 "Error message disappears" 81 ); 82 83 info( 84 "Unblock the connection and check the message and connect button disappear" 85 ); 86 resumeConnection(); 87 await waitUntil( 88 () => !usbRuntimeSidebarItem.querySelector(".qa-connect-button") 89 ); 90 ok(!document.querySelector(".qa-connection-error"), "Error disappears"); 91 ok( 92 !document.querySelector(".qa-connection-not-responding"), 93 "Warning disappears" 94 ); 95 96 info("Remove a USB runtime"); 97 mocks.removeUSBRuntime(RUNTIME_ID); 98 mocks.emitUSBUpdate(); 99 await waitUntilUsbDeviceIsUnplugged(RUNTIME_DEVICE_NAME, document); 100 101 await removeTab(tab); 102 }); 103 104 // Test whether the status of all will be reverted after a certain period of time during 105 // waiting connection. 106 add_task(async function () { 107 await setupPreferences(); 108 109 const mocks = new Mocks(); 110 111 const { document, tab } = await openAboutDebugging(); 112 113 mocks.createUSBRuntime(RUNTIME_ID, { 114 name: RUNTIME_NAME, 115 deviceName: RUNTIME_DEVICE_NAME, 116 shortName: RUNTIME_SHORT_NAME, 117 }); 118 mocks.emitUSBUpdate(); 119 120 info("Wait until the USB sidebar item appears"); 121 await waitUntil(() => findSidebarItemByText(RUNTIME_DEVICE_NAME, document)); 122 const usbRuntimeSidebarItem = findSidebarItemByText( 123 RUNTIME_DEVICE_NAME, 124 document 125 ); 126 const connectButton = 127 usbRuntimeSidebarItem.querySelector(".qa-connect-button"); 128 129 let resumeConnection; 130 const resumeConnectionPromise = new Promise(r => { 131 resumeConnection = r; 132 }); 133 mocks.runtimeClientFactoryMock.createClientForRuntime = async runtime => { 134 await resumeConnectionPromise; 135 return mocks._clients[runtime.type][runtime.id]; 136 }; 137 138 info("Click on the connect button and wait until it disappears"); 139 connectButton.click(); 140 await waitUntil(() => 141 document.querySelector(".qa-connection-not-responding") 142 ); 143 info("Check whether the all status will be reverted"); 144 await waitUntil( 145 () => !document.querySelector(".qa-connection-not-responding") 146 ); 147 ok( 148 document.querySelector(".qa-connection-timeout"), 149 "Connection timeout message displays" 150 ); 151 ok(!connectButton.disabled, "Connect button is enabled"); 152 is( 153 connectButton.textContent, 154 "Connect", 155 "Label of the connect button reverted" 156 ); 157 ok( 158 !document.querySelector(".qa-connection-error"), 159 "Error message disappears" 160 ); 161 162 info("Check whether the timeout message disappears"); 163 resumeConnection(); 164 await waitUntil(() => !document.querySelector(".qa-connection-timeout")); 165 166 info("Remove a USB runtime"); 167 mocks.removeUSBRuntime(RUNTIME_ID); 168 mocks.emitUSBUpdate(); 169 170 info("Wait until the USB sidebar item disappears"); 171 await waitUntilUsbDeviceIsUnplugged(RUNTIME_DEVICE_NAME, document); 172 173 await removeTab(tab); 174 }); 175 176 async function setupPreferences() { 177 if (SpecialPowers.isDebugBuild) { 178 // On debug builds, reducing the timings might lead to skip the "warning" 179 // state and will block the test execution. 180 // Do not change the timings in debug builds. 181 return; 182 } 183 184 await pushPref( 185 "devtools.aboutdebugging.test-connection-timing-out-delay", 186 CONNECTION_TIMING_OUT_DELAY 187 ); 188 await pushPref( 189 "devtools.aboutdebugging.test-connection-cancel-delay", 190 CONNECTION_CANCEL_DELAY 191 ); 192 }