2019-03-08 15:46:38 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2019-2024 New Vector Ltd.
|
2019-03-08 15:46:38 +03:00
|
|
|
|
2024-09-09 16:57:16 +03:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-03-08 15:46:38 +03:00
|
|
|
*/
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
import EventEmitter from "events";
|
2021-10-23 01:23:32 +03:00
|
|
|
|
2024-10-15 16:57:26 +03:00
|
|
|
import UserActivity from "../../src/UserActivity";
|
2019-03-08 15:46:38 +03:00
|
|
|
|
|
|
|
class FakeDomEventEmitter extends EventEmitter {
|
2023-02-13 14:39:16 +03:00
|
|
|
addEventListener(what: string, l: (...args: any[]) => void) {
|
2019-03-08 15:46:38 +03:00
|
|
|
this.on(what, l);
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:39:16 +03:00
|
|
|
removeEventListener(what: string, l: (...args: any[]) => void) {
|
2019-03-08 15:46:38 +03:00
|
|
|
this.removeListener(what, l);
|
|
|
|
}
|
2019-03-08 16:52:04 +03:00
|
|
|
}
|
2019-03-08 15:46:38 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("UserActivity", function () {
|
2023-02-13 14:39:16 +03:00
|
|
|
let fakeWindow: FakeDomEventEmitter;
|
|
|
|
let fakeDocument: FakeDomEventEmitter & { hasFocus?(): boolean };
|
|
|
|
let userActivity: UserActivity;
|
2019-03-08 15:46:38 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
beforeEach(function () {
|
2020-01-18 04:39:14 +03:00
|
|
|
fakeWindow = new FakeDomEventEmitter();
|
|
|
|
fakeDocument = new FakeDomEventEmitter();
|
2023-02-13 14:39:16 +03:00
|
|
|
userActivity = new UserActivity(fakeWindow as unknown as Window, fakeDocument as unknown as Document);
|
2019-03-08 15:46:38 +03:00
|
|
|
userActivity.start();
|
2023-07-17 15:29:51 +03:00
|
|
|
jest.useFakeTimers();
|
2019-03-08 15:46:38 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
afterEach(function () {
|
2019-03-08 15:46:38 +03:00
|
|
|
userActivity.stop();
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should return the same shared instance", function () {
|
2019-03-08 15:46:38 +03:00
|
|
|
expect(UserActivity.sharedInstance()).toBe(UserActivity.sharedInstance());
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should consider user inactive if no activity", function () {
|
2019-03-11 14:38:54 +03:00
|
|
|
expect(userActivity.userActiveNow()).toBe(false);
|
2019-03-08 15:46:38 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should consider user not active recently if no activity", function () {
|
2019-03-11 14:38:54 +03:00
|
|
|
expect(userActivity.userActiveRecently()).toBe(false);
|
2019-03-08 15:46:38 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should not consider user active after activity if no window focus", function () {
|
2019-03-08 15:46:38 +03:00
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(false);
|
|
|
|
|
2023-02-13 14:39:16 +03:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2019-03-11 14:38:54 +03:00
|
|
|
expect(userActivity.userActiveNow()).toBe(false);
|
|
|
|
expect(userActivity.userActiveRecently()).toBe(false);
|
2019-03-08 15:46:38 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should consider user active shortly after activity", function () {
|
2019-03-08 15:46:38 +03:00
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
|
|
|
|
|
2023-02-13 14:39:16 +03:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2019-03-11 14:38:54 +03:00
|
|
|
expect(userActivity.userActiveNow()).toBe(true);
|
|
|
|
expect(userActivity.userActiveRecently()).toBe(true);
|
2023-07-17 15:29:51 +03:00
|
|
|
jest.advanceTimersByTime(200);
|
2019-03-11 14:38:54 +03:00
|
|
|
expect(userActivity.userActiveNow()).toBe(true);
|
|
|
|
expect(userActivity.userActiveRecently()).toBe(true);
|
2019-03-08 15:46:38 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should consider user not active after 10s of no activity", function () {
|
2019-03-08 15:46:38 +03:00
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
|
|
|
|
|
2023-02-13 14:39:16 +03:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2023-07-17 15:29:51 +03:00
|
|
|
jest.advanceTimersByTime(10000);
|
2019-03-11 14:38:54 +03:00
|
|
|
expect(userActivity.userActiveNow()).toBe(false);
|
2019-03-08 15:46:38 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should consider user passive after 10s of no activity", function () {
|
2019-03-08 15:46:38 +03:00
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
|
|
|
|
|
2023-02-13 14:39:16 +03:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2023-07-17 15:29:51 +03:00
|
|
|
jest.advanceTimersByTime(10000);
|
2019-03-11 14:38:54 +03:00
|
|
|
expect(userActivity.userActiveRecently()).toBe(true);
|
2019-03-08 15:46:38 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should not consider user passive after 10s if window un-focused", function () {
|
2019-03-08 15:46:38 +03:00
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
|
|
|
|
|
2023-02-13 14:39:16 +03:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2023-07-17 15:29:51 +03:00
|
|
|
jest.advanceTimersByTime(10000);
|
2019-03-08 15:46:38 +03:00
|
|
|
|
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(false);
|
2022-12-12 14:24:14 +03:00
|
|
|
fakeWindow.emit("blur", {});
|
2019-03-08 15:46:38 +03:00
|
|
|
|
2019-03-11 14:38:54 +03:00
|
|
|
expect(userActivity.userActiveRecently()).toBe(false);
|
2019-03-08 15:46:38 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should not consider user passive after 3 mins", function () {
|
2019-03-08 15:46:38 +03:00
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
|
|
|
|
|
2023-02-13 14:39:16 +03:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2023-07-17 15:29:51 +03:00
|
|
|
jest.advanceTimersByTime(3 * 60 * 1000);
|
2019-03-08 15:46:38 +03:00
|
|
|
|
2019-03-11 14:38:54 +03:00
|
|
|
expect(userActivity.userActiveRecently()).toBe(false);
|
2019-03-08 15:46:38 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should extend timer on activity", function () {
|
2019-03-08 15:46:38 +03:00
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
|
|
|
|
|
2023-02-13 14:39:16 +03:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2023-07-17 15:29:51 +03:00
|
|
|
jest.advanceTimersByTime(1 * 60 * 1000);
|
2023-02-13 14:39:16 +03:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2023-07-17 15:29:51 +03:00
|
|
|
jest.advanceTimersByTime(1 * 60 * 1000);
|
2023-02-13 14:39:16 +03:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2023-07-17 15:29:51 +03:00
|
|
|
jest.advanceTimersByTime(1 * 60 * 1000);
|
2019-03-08 15:46:38 +03:00
|
|
|
|
2019-03-11 14:38:54 +03:00
|
|
|
expect(userActivity.userActiveRecently()).toBe(true);
|
2019-03-08 15:46:38 +03:00
|
|
|
});
|
|
|
|
});
|