2019-03-08 13:46:38 +01:00
|
|
|
/*
|
2021-01-20 14:07:52 +01:00
|
|
|
Copyright 2019, 2021 New Vector Ltd
|
2019-03-08 13:46:38 +01:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import FakeTimers from "@sinonjs/fake-timers";
|
|
|
|
import EventEmitter from "events";
|
2021-10-23 00:23:32 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import UserActivity from "../src/UserActivity";
|
2019-03-08 13:46:38 +01:00
|
|
|
|
|
|
|
class FakeDomEventEmitter extends EventEmitter {
|
2023-02-13 12:39:16 +01:00
|
|
|
addEventListener(what: string, l: (...args: any[]) => void) {
|
2019-03-08 13:46:38 +01:00
|
|
|
this.on(what, l);
|
|
|
|
}
|
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
removeEventListener(what: string, l: (...args: any[]) => void) {
|
2019-03-08 13:46:38 +01:00
|
|
|
this.removeListener(what, l);
|
|
|
|
}
|
2019-03-08 14:52:04 +01:00
|
|
|
}
|
2019-03-08 13:46:38 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("UserActivity", function () {
|
2023-02-13 12:39:16 +01:00
|
|
|
let fakeWindow: FakeDomEventEmitter;
|
|
|
|
let fakeDocument: FakeDomEventEmitter & { hasFocus?(): boolean };
|
|
|
|
let userActivity: UserActivity;
|
|
|
|
let clock: FakeTimers.InstalledClock;
|
2019-03-08 13:46:38 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
beforeEach(function () {
|
2020-01-18 02:39:14 +01:00
|
|
|
fakeWindow = new FakeDomEventEmitter();
|
|
|
|
fakeDocument = new FakeDomEventEmitter();
|
2023-02-13 12:39:16 +01:00
|
|
|
userActivity = new UserActivity(fakeWindow as unknown as Window, fakeDocument as unknown as Document);
|
2019-03-08 13:46:38 +01:00
|
|
|
userActivity.start();
|
2021-01-20 14:07:52 +01:00
|
|
|
clock = FakeTimers.install();
|
2019-03-08 13:46:38 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
afterEach(function () {
|
2019-03-08 13:46:38 +01:00
|
|
|
userActivity.stop();
|
|
|
|
clock.uninstall();
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should return the same shared instance", function () {
|
2019-03-08 13:46:38 +01:00
|
|
|
expect(UserActivity.sharedInstance()).toBe(UserActivity.sharedInstance());
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should consider user inactive if no activity", function () {
|
2019-03-11 12:38:54 +01:00
|
|
|
expect(userActivity.userActiveNow()).toBe(false);
|
2019-03-08 13:46:38 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should consider user not active recently if no activity", function () {
|
2019-03-11 12:38:54 +01:00
|
|
|
expect(userActivity.userActiveRecently()).toBe(false);
|
2019-03-08 13:46:38 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should not consider user active after activity if no window focus", function () {
|
2019-03-08 13:46:38 +01:00
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(false);
|
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2019-03-11 12:38:54 +01:00
|
|
|
expect(userActivity.userActiveNow()).toBe(false);
|
|
|
|
expect(userActivity.userActiveRecently()).toBe(false);
|
2019-03-08 13:46:38 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should consider user active shortly after activity", function () {
|
2019-03-08 13:46:38 +01:00
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
|
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2019-03-11 12:38:54 +01:00
|
|
|
expect(userActivity.userActiveNow()).toBe(true);
|
|
|
|
expect(userActivity.userActiveRecently()).toBe(true);
|
2019-03-08 13:46:38 +01:00
|
|
|
clock.tick(200);
|
2019-03-11 12:38:54 +01:00
|
|
|
expect(userActivity.userActiveNow()).toBe(true);
|
|
|
|
expect(userActivity.userActiveRecently()).toBe(true);
|
2019-03-08 13:46:38 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should consider user not active after 10s of no activity", function () {
|
2019-03-08 13:46:38 +01:00
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
|
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2019-03-08 13:46:38 +01:00
|
|
|
clock.tick(10000);
|
2019-03-11 12:38:54 +01:00
|
|
|
expect(userActivity.userActiveNow()).toBe(false);
|
2019-03-08 13:46:38 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should consider user passive after 10s of no activity", function () {
|
2019-03-08 13:46:38 +01:00
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
|
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2019-03-08 13:46:38 +01:00
|
|
|
clock.tick(10000);
|
2019-03-11 12:38:54 +01:00
|
|
|
expect(userActivity.userActiveRecently()).toBe(true);
|
2019-03-08 13:46:38 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should not consider user passive after 10s if window un-focused", function () {
|
2019-03-08 13:46:38 +01:00
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
|
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2019-03-08 13:46:38 +01:00
|
|
|
clock.tick(10000);
|
|
|
|
|
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(false);
|
2022-12-12 12:24:14 +01:00
|
|
|
fakeWindow.emit("blur", {});
|
2019-03-08 13:46:38 +01:00
|
|
|
|
2019-03-11 12:38:54 +01:00
|
|
|
expect(userActivity.userActiveRecently()).toBe(false);
|
2019-03-08 13:46:38 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should not consider user passive after 3 mins", function () {
|
2019-03-08 13:46:38 +01:00
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
|
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2019-03-08 13:46:38 +01:00
|
|
|
clock.tick(3 * 60 * 1000);
|
|
|
|
|
2019-03-11 12:38:54 +01:00
|
|
|
expect(userActivity.userActiveRecently()).toBe(false);
|
2019-03-08 13:46:38 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should extend timer on activity", function () {
|
2019-03-08 13:46:38 +01:00
|
|
|
fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
|
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2019-03-08 13:46:38 +01:00
|
|
|
clock.tick(1 * 60 * 1000);
|
2023-02-13 12:39:16 +01:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2019-03-08 13:46:38 +01:00
|
|
|
clock.tick(1 * 60 * 1000);
|
2023-02-13 12:39:16 +01:00
|
|
|
userActivity.onUserActivity({ type: "event" } as Event);
|
2019-03-08 13:46:38 +01:00
|
|
|
clock.tick(1 * 60 * 1000);
|
|
|
|
|
2019-03-11 12:38:54 +01:00
|
|
|
expect(userActivity.userActiveRecently()).toBe(true);
|
2019-03-08 13:46:38 +01:00
|
|
|
});
|
|
|
|
});
|