fingerprintjs
189 строк · 5.8 Кб
1import { getBrowserMajorVersion, isSafari, withMockProperties } from '../../tests/utils'2import getScreenFrame, {3FrameSize,4getUnstableScreenFrame,5resetScreenFrameWatch,6screenFrameCheckInterval,7} from './screen_frame'8
9describe('Sources', () => {10describe('screenFrame', () => {11beforeEach(() => {12resetScreenFrameWatch()13})14
15it('gets non-zero frame', async () => {16await withMockProperties(17screen,18{19width: { get: () => 1440 },20height: { get: () => 900 },21availWidth: { get: () => 1367 },22availHeight: { get: () => 853 },23availLeft: { get: () => 49 },24availTop: { get: () => 11 },25},26async () => {27const frame = await getUnstableScreenFrame()()28expect(frame).toEqual([11, 24, 36, 49])29
30const roundedFrame = await getScreenFrame()()31expect(roundedFrame).toEqual(shouldTurnOff() ? undefined : [10, 20, 40, 50])32},33)34})35
36it('gets null frame', async () => {37await withMockProperties(38screen,39{40width: { get: () => 1920 },41height: { get: () => 1080 },42availWidth: { get: () => 1920 },43availHeight: { get: () => 1080 },44availLeft: { get: () => 0 },45availTop: { get: () => 0 },46},47async () => {48const frame = await getUnstableScreenFrame()()49expect(frame).toEqual([0, 0, 0, 0])50
51const roundedFrame = await getScreenFrame()()52expect(roundedFrame).toEqual(shouldTurnOff() ? undefined : [0, 0, 0, 0])53},54)55})56
57it('gets frame with unavailable position (IE and Edge ≤18)', async () => {58await withMockProperties(59screen,60{61width: { get: () => 1280 },62height: { get: () => 1024 },63availWidth: { get: () => 1182 },64availHeight: { get: () => 1000 },65availLeft: { get: () => undefined },66availTop: { get: () => undefined },67},68async () => {69const frame = await getUnstableScreenFrame()()70expect(frame).toEqual([null, 98, 24, null])71
72const roundedFrame = await getScreenFrame()()73expect(roundedFrame).toEqual(shouldTurnOff() ? undefined : [null, 100, 20, null])74},75)76})77
78it('watches for screen frame', async () => {79try {80jasmine.clock().install()81jasmine.clock().mockDate()82
83let screenFrameGetter: () => Promise<FrameSize>84
85await withMockProperties(86screen,87{88width: { get: () => 640 },89height: { get: () => 480 },90availWidth: { get: () => 640 },91availHeight: { get: () => 480 },92availLeft: { get: () => 0 },93availTop: { get: () => 0 },94},95async () => {96screenFrameGetter = getUnstableScreenFrame()97
98// The screen frame is null now99const frame = await screenFrameGetter()100expect(frame).toEqual([0, 0, 0, 0])101},102)103
104await withMockProperties(105screen,106{107width: { get: () => 640 },108height: { get: () => 480 },109availWidth: { get: () => 600 },110availHeight: { get: () => 400 },111availLeft: { get: () => 10 },112availTop: { get: () => 30 },113},114async () => {115// The screen frame has become non-null, the frame watch shall remember it116jasmine.clock().tick(screenFrameCheckInterval)117},118)119
120await withMockProperties(121screen,122{123width: { get: () => 640 },124height: { get: () => 480 },125availWidth: { get: () => 640 },126availHeight: { get: () => 480 },127availLeft: { get: () => 0 },128availTop: { get: () => 0 },129},130async () => {131// The screen frame is null again, `getUnstableScreenFrame` shall return the remembered non-null frame132jasmine.clock().tick(screenFrameCheckInterval)133const frame = await screenFrameGetter()134expect(frame).toEqual([30, 30, 50, 10])135},136)137
138await withMockProperties(139screen,140{141width: { get: () => 640 },142height: { get: () => 480 },143availWidth: { get: () => 600 },144availHeight: { get: () => 400 },145availLeft: { get: () => 30 },146availTop: { get: () => 10 },147},148async () => {149// The screen frame has become non-null, `getUnstableScreenFrame` shall return the new size and remember it150jasmine.clock().tick(screenFrameCheckInterval)151const frame = await screenFrameGetter()152expect(frame).toEqual([10, 10, 70, 30])153},154)155
156await withMockProperties(157screen,158{159width: { get: () => 640 },160height: { get: () => 480 },161availWidth: { get: () => 640 },162availHeight: { get: () => 480 },163availLeft: { get: () => 0 },164availTop: { get: () => 0 },165},166async () => {167// The screen frame has become null again, `getUnstableScreenFrame` shall return the new non-null frame168jasmine.clock().tick(screenFrameCheckInterval)169const frame = await screenFrameGetter()170expect(frame).toEqual([10, 10, 70, 30])171},172)173} finally {174jasmine.clock().uninstall()175}176})177
178it('returns stable values', async () => {179const first = getScreenFrame()180const second = getScreenFrame()181
182expect(await second()).toEqual(await first())183})184})185})186
187function shouldTurnOff() {188return isSafari() && (getBrowserMajorVersion() ?? 0) >= 17189}
190