fingerprintjs

Форк
0
/
screen_frame.test.ts 
189 строк · 5.8 Кб
1
import { getBrowserMajorVersion, isSafari, withMockProperties } from '../../tests/utils'
2
import getScreenFrame, {
3
  FrameSize,
4
  getUnstableScreenFrame,
5
  resetScreenFrameWatch,
6
  screenFrameCheckInterval,
7
} from './screen_frame'
8

9
describe('Sources', () => {
10
  describe('screenFrame', () => {
11
    beforeEach(() => {
12
      resetScreenFrameWatch()
13
    })
14

15
    it('gets non-zero frame', async () => {
16
      await withMockProperties(
17
        screen,
18
        {
19
          width: { get: () => 1440 },
20
          height: { get: () => 900 },
21
          availWidth: { get: () => 1367 },
22
          availHeight: { get: () => 853 },
23
          availLeft: { get: () => 49 },
24
          availTop: { get: () => 11 },
25
        },
26
        async () => {
27
          const frame = await getUnstableScreenFrame()()
28
          expect(frame).toEqual([11, 24, 36, 49])
29

30
          const roundedFrame = await getScreenFrame()()
31
          expect(roundedFrame).toEqual(shouldTurnOff() ? undefined : [10, 20, 40, 50])
32
        },
33
      )
34
    })
35

36
    it('gets null frame', async () => {
37
      await withMockProperties(
38
        screen,
39
        {
40
          width: { get: () => 1920 },
41
          height: { get: () => 1080 },
42
          availWidth: { get: () => 1920 },
43
          availHeight: { get: () => 1080 },
44
          availLeft: { get: () => 0 },
45
          availTop: { get: () => 0 },
46
        },
47
        async () => {
48
          const frame = await getUnstableScreenFrame()()
49
          expect(frame).toEqual([0, 0, 0, 0])
50

51
          const roundedFrame = await getScreenFrame()()
52
          expect(roundedFrame).toEqual(shouldTurnOff() ? undefined : [0, 0, 0, 0])
53
        },
54
      )
55
    })
56

57
    it('gets frame with unavailable position (IE and Edge ≤18)', async () => {
58
      await withMockProperties(
59
        screen,
60
        {
61
          width: { get: () => 1280 },
62
          height: { get: () => 1024 },
63
          availWidth: { get: () => 1182 },
64
          availHeight: { get: () => 1000 },
65
          availLeft: { get: () => undefined },
66
          availTop: { get: () => undefined },
67
        },
68
        async () => {
69
          const frame = await getUnstableScreenFrame()()
70
          expect(frame).toEqual([null, 98, 24, null])
71

72
          const roundedFrame = await getScreenFrame()()
73
          expect(roundedFrame).toEqual(shouldTurnOff() ? undefined : [null, 100, 20, null])
74
        },
75
      )
76
    })
77

78
    it('watches for screen frame', async () => {
79
      try {
80
        jasmine.clock().install()
81
        jasmine.clock().mockDate()
82

83
        let screenFrameGetter: () => Promise<FrameSize>
84

85
        await withMockProperties(
86
          screen,
87
          {
88
            width: { get: () => 640 },
89
            height: { get: () => 480 },
90
            availWidth: { get: () => 640 },
91
            availHeight: { get: () => 480 },
92
            availLeft: { get: () => 0 },
93
            availTop: { get: () => 0 },
94
          },
95
          async () => {
96
            screenFrameGetter = getUnstableScreenFrame()
97

98
            // The screen frame is null now
99
            const frame = await screenFrameGetter()
100
            expect(frame).toEqual([0, 0, 0, 0])
101
          },
102
        )
103

104
        await withMockProperties(
105
          screen,
106
          {
107
            width: { get: () => 640 },
108
            height: { get: () => 480 },
109
            availWidth: { get: () => 600 },
110
            availHeight: { get: () => 400 },
111
            availLeft: { get: () => 10 },
112
            availTop: { get: () => 30 },
113
          },
114
          async () => {
115
            // The screen frame has become non-null, the frame watch shall remember it
116
            jasmine.clock().tick(screenFrameCheckInterval)
117
          },
118
        )
119

120
        await withMockProperties(
121
          screen,
122
          {
123
            width: { get: () => 640 },
124
            height: { get: () => 480 },
125
            availWidth: { get: () => 640 },
126
            availHeight: { get: () => 480 },
127
            availLeft: { get: () => 0 },
128
            availTop: { get: () => 0 },
129
          },
130
          async () => {
131
            // The screen frame is null again, `getUnstableScreenFrame` shall return the remembered non-null frame
132
            jasmine.clock().tick(screenFrameCheckInterval)
133
            const frame = await screenFrameGetter()
134
            expect(frame).toEqual([30, 30, 50, 10])
135
          },
136
        )
137

138
        await withMockProperties(
139
          screen,
140
          {
141
            width: { get: () => 640 },
142
            height: { get: () => 480 },
143
            availWidth: { get: () => 600 },
144
            availHeight: { get: () => 400 },
145
            availLeft: { get: () => 30 },
146
            availTop: { get: () => 10 },
147
          },
148
          async () => {
149
            // The screen frame has become non-null, `getUnstableScreenFrame` shall return the new size and remember it
150
            jasmine.clock().tick(screenFrameCheckInterval)
151
            const frame = await screenFrameGetter()
152
            expect(frame).toEqual([10, 10, 70, 30])
153
          },
154
        )
155

156
        await withMockProperties(
157
          screen,
158
          {
159
            width: { get: () => 640 },
160
            height: { get: () => 480 },
161
            availWidth: { get: () => 640 },
162
            availHeight: { get: () => 480 },
163
            availLeft: { get: () => 0 },
164
            availTop: { get: () => 0 },
165
          },
166
          async () => {
167
            // The screen frame has become null again, `getUnstableScreenFrame` shall return the new non-null frame
168
            jasmine.clock().tick(screenFrameCheckInterval)
169
            const frame = await screenFrameGetter()
170
            expect(frame).toEqual([10, 10, 70, 30])
171
          },
172
        )
173
      } finally {
174
        jasmine.clock().uninstall()
175
      }
176
    })
177

178
    it('returns stable values', async () => {
179
      const first = getScreenFrame()
180
      const second = getScreenFrame()
181

182
      expect(await second()).toEqual(await first())
183
    })
184
  })
185
})
186

187
function shouldTurnOff() {
188
  return isSafari() && (getBrowserMajorVersion() ?? 0) >= 17
189
}
190

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.