interceptor.https.html (10202B)
1 <!DOCTYPE html> 2 <link rel="help" href="https://github.com/samuelgoto/idle-detection"> 3 <title>Tests the Idle Detection API</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src="/resources/test-only-api.js"></script> 7 <script src="/resources/testdriver.js"></script> 8 <script src="/resources/testdriver-vendor.js"></script> 9 <script src="resources/idle-detection-helper.js"></script> 10 <script> 11 'use strict'; 12 13 promise_setup(async t => { 14 await test_driver.set_permission({ name: 'idle-detection' }, 'granted'); 15 if (isChromiumBased) { 16 await loadChromiumResources(); 17 } 18 }) 19 20 promise_test(async t => { 21 // Basic test that expects start() to call internally 22 // addMonitor, which in turn return an ACTIVE state. 23 expect(addMonitor).andReturn(async (monitorPtr) => { 24 return { 25 error: IdleDetectorError.SUCCESS, 26 state: { 27 idleTime: null, 28 screenLocked: true 29 } 30 }; 31 }); 32 33 const controller = new AbortController(); 34 const detector = new IdleDetector(); 35 const watcher = new EventWatcher(t, detector, ["change"]); 36 const initial_state = watcher.wait_for("change"); 37 38 await detector.start({ signal: controller.signal }); 39 await initial_state; 40 41 assert_equals(detector.userState, "active"); 42 assert_equals(detector.screenState, "locked"); 43 44 controller.abort(); 45 }, 'start()'); 46 47 promise_test(async t => { 48 // Verifies that an event is thrown when a change of state from IDLE to ACTIVE 49 // is detected. 50 expect(addMonitor).andReturn(async (monitorPtr) => { 51 const first = { 52 error: IdleDetectorError.SUCCESS, 53 state: { 54 idleTime: null, 55 screenLocked: false 56 } 57 }; 58 59 t.step_timeout(() => { 60 monitorPtr.update( 61 { 62 idleTime: { milliseconds: 0 }, 63 screenLocked: false 64 }, 65 /*is_overridden_by_devtools=*/true 66 ); 67 }, 0); 68 69 return first; 70 }); 71 72 const controller = new AbortController(); 73 const detector = new IdleDetector(); 74 const watcher = new EventWatcher(t, detector, ["change"]); 75 const initial_state = watcher.wait_for("change"); 76 77 await detector.start({ signal: controller.signal }); 78 await initial_state; 79 assert_equals(detector.userState, "active"); 80 assert_equals(detector.screenState, "unlocked"); 81 82 // Wait for the first change in state. 83 await watcher.wait_for("change"); 84 85 assert_equals(detector.userState, "idle"); 86 assert_equals(detector.screenState, "unlocked"); 87 88 controller.abort(); 89 }, 'updates once'); 90 91 promise_test(async t => { 92 // Simulates the user being active, going idle and then going back active 93 // again. 94 expect(addMonitor).andReturn(async (monitorPtr) => { 95 const first = { 96 error: IdleDetectorError.SUCCESS, 97 state: { 98 idleTime: null, 99 screenLocked: false 100 } 101 }; 102 103 // Updates the client once with the user idle. 104 t.step_timeout(() => { 105 monitorPtr.update( 106 { 107 idleTime: { milliseconds: 0 }, 108 screenLocked: false 109 }, 110 /*is_overridden_by_devtools=*/true 111 ); 112 }, 0); 113 114 // Updates the client a second time with the user active. 115 t.step_timeout(() => { 116 monitorPtr.update( 117 { 118 idleTime: null, 119 screenLocked: false 120 }, 121 /*is_overridden_by_devtools=*/true 122 ); 123 }, 1); 124 125 return first; 126 }); 127 128 const controller = new AbortController(); 129 const detector = new IdleDetector(); 130 const watcher = new EventWatcher(t, detector, ["change"]); 131 const initial_state = watcher.wait_for("change"); 132 133 await detector.start({ signal: controller.signal }); 134 await initial_state; 135 136 // Waits for the first event. 137 await watcher.wait_for("change"); 138 assert_equals(detector.userState, "idle"); 139 140 // Waits for the second event. 141 await watcher.wait_for("change"); 142 assert_equals(detector.userState, "active"); 143 144 controller.abort(); 145 }, 'updates twice'); 146 147 promise_test(async t => { 148 // Simulates a locked screen. 149 expect(addMonitor).andReturn(async (monitorPtr) => { 150 return { 151 error: IdleDetectorError.SUCCESS, 152 state: { 153 idleTime: null, 154 screenLocked: true 155 } 156 }; 157 }); 158 159 const controller = new AbortController(); 160 const detector = new IdleDetector(); 161 const watcher = new EventWatcher(t, detector, ["change"]); 162 const initial_state = watcher.wait_for("change"); 163 164 await detector.start({ signal: controller.signal }); 165 await initial_state; 166 167 assert_equals(detector.screenState, "locked"); 168 169 controller.abort(); 170 }, 'locked screen'); 171 172 promise_test(async t => { 173 expect(addMonitor).andReturn(async (monitorPtr) => { 174 return { 175 error: IdleDetectorError.SUCCESS, 176 state: { 177 idleTime: null, 178 screenLocked: true 179 } 180 }; 181 }); 182 183 const controller = new AbortController(); 184 const detector = new IdleDetector(); 185 186 let event = new Promise((resolve, reject) => { 187 detector.onchange = resolve; 188 }); 189 190 await detector.start({ signal: controller.signal }); 191 192 // Waits for the first event. 193 await event; 194 195 assert_equals(detector.userState, "active"); 196 assert_equals(detector.screenState, "locked"); 197 198 controller.abort(); 199 }, 'IdleDetector.onchange'); 200 201 promise_test(async t => { 202 expect(addMonitor).andReturn(async (monitorPtr) => { 203 return { 204 error: IdleDetectorError.SUCCESS, 205 state: { 206 idleTime: null, 207 screenLocked: false 208 } 209 }; 210 }); 211 212 const controller = new AbortController(); 213 const detector = new IdleDetector({ signal: controller.signal }); 214 215 const watcher = new EventWatcher(t, detector, ["change"]); 216 const initial_state = watcher.wait_for("change"); 217 218 // Only the first call to start() is allowed. 219 const start_promise = detector.start({ signal: controller.signal }); 220 await promise_rejects_dom(t, 'InvalidStateError', detector.start()); 221 await start_promise; 222 223 await initial_state; 224 assert_equals(detector.userState, "active"); 225 assert_equals(detector.screenState, "unlocked"); 226 227 // Calling abort() multiple times is safe. 228 controller.abort(); 229 controller.abort(); 230 controller.abort(); 231 controller.abort(); 232 }, 'Calling start() and abort() multiple times'); 233 234 promise_test(async t => { 235 expect(addMonitor).andReturn(async (monitorPtr) => { 236 return { 237 error: IdleDetectorError.SUCCESS, 238 state: { 239 idleTime: null, 240 screenLocked: false 241 } 242 }; 243 }); 244 245 const controller = new AbortController(); 246 const detector = new IdleDetector(); 247 248 controller.abort(); 249 250 await promise_rejects_dom( 251 t, 'AbortError', detector.start({ signal: controller.signal })); 252 }, 'Calling abort() before start() makes it fail'); 253 254 promise_test(async t => { 255 expect(addMonitor).andReturn(async (monitorPtr) => { 256 return { 257 error: IdleDetectorError.SUCCESS, 258 state: { 259 idleTime: null, 260 screenLocked: false 261 } 262 }; 263 }); 264 265 const controller = new AbortController(); 266 const detector = new IdleDetector(); 267 268 const promise = promise_rejects_dom( 269 t, 'AbortError', detector.start({ signal: controller.signal })) 270 controller.abort(); 271 272 await promise; 273 }, 'Calling abort() after start() makes it fail'); 274 275 promise_test(async t => { 276 expect(addMonitor).andReturn(async (monitorPtr) => { 277 return { 278 error: IdleDetectorError.SUCCESS, 279 state: { 280 idleTime: null, 281 screenLocked: false 282 } 283 }; 284 }); 285 286 const detector = new IdleDetector(); 287 const watcher = new EventWatcher(t, detector, ["change"]); 288 289 let controller = new AbortController(); 290 const first_start = promise_rejects_dom( 291 t, 'AbortError', detector.start({ signal: controller.signal })) 292 controller.abort(); 293 294 controller = new AbortController(); 295 const initial_state = watcher.wait_for("change"); 296 const second_start = detector.start({ signal: controller.signal }); 297 298 await first_start; 299 await second_start; 300 await initial_state; 301 assert_equals(detector.userState, "active"); 302 assert_equals(detector.screenState, "unlocked"); 303 304 controller.abort(); 305 }, 'A start() that has been aborted can be retried'); 306 307 promise_test(async t => { 308 expect(addMonitor).andReturn(async (monitorPtr) => { 309 return { 310 error: IdleDetectorError.SUCCESS, 311 state: { 312 idleTime: null, 313 screenLocked: false 314 } 315 }; 316 }); 317 318 let controller = new AbortController(); 319 const detector = new IdleDetector(); 320 const watcher = new EventWatcher(t, detector, ["change"]); 321 let initial_state = watcher.wait_for("change"); 322 323 await detector.start({ signal: controller.signal }); 324 await initial_state; 325 assert_equals(detector.userState, "active"); 326 assert_equals(detector.screenState, "unlocked"); 327 328 controller.abort(); 329 330 expect(addMonitor).andReturn(async (monitorPtr) => { 331 return { 332 error: IdleDetectorError.SUCCESS, 333 state: { 334 idleTime: { milliseconds: 0 }, 335 screenLocked: true 336 } 337 }; 338 }); 339 340 // Restarting the monitor. 341 controller = new AbortController(); 342 343 initial_state = watcher.wait_for("change"); 344 await detector.start({ signal: controller.signal }); 345 await initial_state; 346 assert_equals(detector.userState, "idle"); 347 assert_equals(detector.screenState, "locked"); 348 349 // Abort in a new task and restart the monitor again. 350 const p = new Promise((resolve) => { 351 t.step_timeout(resolve, 1); 352 }); 353 await p; 354 controller.abort(); 355 356 expect(addMonitor).andReturn(async (monitorPtr) => { 357 return { 358 error: IdleDetectorError.SUCCESS, 359 state: { 360 idleTime: { milliseconds: 0 }, 361 screenLocked: false 362 } 363 }; 364 }); 365 366 // Restarting the monitor. 367 controller = new AbortController(); 368 369 initial_state = watcher.wait_for("change"); 370 await detector.start({ signal: controller.signal }); 371 await initial_state; 372 assert_equals(detector.userState, "idle"); 373 assert_equals(detector.screenState, "unlocked"); 374 375 controller.abort(); 376 }, 'Calling start() after abort(): re-starting monitor.'); 377 378 </script>