1/**
2 * Copyright 2020 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import expect from 'expect';
18import {
19  getTestState,
20  setupTestBrowserHooks,
21  setupTestPageAndContextHooks,
22  describeFailsFirefox,
23} from './mocha-utils'; // eslint-disable-line import/extensions
24
25describe('Emulate idle state', () => {
26  setupTestBrowserHooks();
27  setupTestPageAndContextHooks();
28
29  async function getIdleState() {
30    const { page } = getTestState();
31
32    const stateElement = await page.$('#state');
33    return await page.evaluate((element: HTMLElement) => {
34      return element.innerText;
35    }, stateElement);
36  }
37
38  async function verifyState(expectedState: string) {
39    const actualState = await getIdleState();
40    expect(actualState).toEqual(expectedState);
41  }
42
43  it('changing idle state emulation causes change of the IdleDetector state', async () => {
44    const { page, server, context } = getTestState();
45    await context.overridePermissions(server.PREFIX + '/idle-detector.html', [
46      'idle-detection',
47    ]);
48
49    await page.goto(server.PREFIX + '/idle-detector.html');
50
51    // Store initial state, as soon as it is not guaranteed to be `active, unlocked`.
52    const initialState = await getIdleState();
53
54    // Emulate Idle states and verify IdleDetector updates state accordingly.
55    await page.emulateIdleState({
56      isUserActive: false,
57      isScreenUnlocked: false,
58    });
59    await verifyState('Idle state: idle, locked.');
60
61    await page.emulateIdleState({
62      isUserActive: true,
63      isScreenUnlocked: false,
64    });
65    await verifyState('Idle state: active, locked.');
66
67    await page.emulateIdleState({
68      isUserActive: true,
69      isScreenUnlocked: true,
70    });
71    await verifyState('Idle state: active, unlocked.');
72
73    await page.emulateIdleState({
74      isUserActive: false,
75      isScreenUnlocked: true,
76    });
77    await verifyState('Idle state: idle, unlocked.');
78
79    // Remove Idle emulation and verify IdleDetector is in initial state.
80    await page.emulateIdleState();
81    await verifyState(initialState);
82
83    // Emulate idle state again after removing emulation.
84    await page.emulateIdleState({
85      isUserActive: false,
86      isScreenUnlocked: false,
87    });
88    await verifyState('Idle state: idle, locked.');
89
90    // Remove emulation second time.
91    await page.emulateIdleState();
92    await verifyState(initialState);
93  });
94});
95