fingerprintjs

Форк
0
/
webgl.test.ts 
103 строки · 2.8 Кб
1
import { withMockProperties } from '../../tests/utils'
2
import { getWebGlBasics, getWebGlExtensions, STATUS_GET_PARAMETER_NOT_A_FUNCTION, STATUS_NO_GL_CONTEXT } from './webgl'
3

4
function isWebGLSupported() {
5
  const canvas = document.createElement('canvas')
6
  const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl')
7
  // Report the result.
8
  return gl && gl instanceof WebGLRenderingContext
9
}
10

11
describe('Sources', () => {
12
  const options = {
13
    cache: {},
14
  }
15

16
  function withMissingGetParameter(action: () => void) {
17
    return withMockProperties(
18
      document,
19
      { createElement: { value: () => ({ getContext: () => ({}), addEventListener: () => undefined }) } },
20
      action,
21
    )
22
  }
23

24
  describe('webGlBasics', () => {
25
    it('should work', () => {
26
      if (!isWebGLSupported()) {
27
        expect(getWebGlBasics(options)).toBe(STATUS_NO_GL_CONTEXT)
28
        return
29
      }
30

31
      const result = getWebGlBasics(options)
32

33
      // Type guard
34
      if (typeof result === 'number') {
35
        throw new Error('WebGL is missing')
36
      }
37

38
      expect(result.vendor.length).toBeGreaterThan(0)
39
      expect(result.version.toLocaleLowerCase()).toContain('webgl')
40
    })
41

42
    it('returns corresponding status when `context.getParameter` is absent', async () => {
43
      await withMissingGetParameter(() => {
44
        expect(
45
          getWebGlBasics({
46
            cache: {},
47
          }),
48
        ).toBe(STATUS_GET_PARAMETER_NOT_A_FUNCTION)
49
      })
50
    })
51

52
    it('is stable', () => {
53
      const first = getWebGlBasics({ cache: {} })
54
      const second = getWebGlBasics({ cache: {} })
55

56
      expect(first).toEqual(second)
57
    })
58
  })
59

60
  describe('webGlExtensions', () => {
61
    it('should work', () => {
62
      if (!isWebGLSupported()) {
63
        expect(getWebGlExtensions(options)).toBe(STATUS_NO_GL_CONTEXT)
64
        return
65
      }
66

67
      const result = getWebGlExtensions(options)
68

69
      // Type guard
70
      if (typeof result === 'number') {
71
        throw new Error('WebGL is missing')
72
      }
73

74
      for (const [key, value] of Object.entries(result)) {
75
        if (value === null) {
76
          expect(key).toBe('extensions')
77
        } else {
78
          expect(value).withContext(key).toBeInstanceOf(Array)
79
          for (let i = 0; i < value.length; ++i) {
80
            expect(value[i]).withContext(`${key}[${i}]`).toBeInstanceOf(String)
81
          }
82
        }
83
      }
84
    })
85

86
    it('returns corresponding status when `context.getParameter` is absent', async () => {
87
      await withMissingGetParameter(() => {
88
        expect(
89
          getWebGlExtensions({
90
            cache: {},
91
          }),
92
        ).toBe(STATUS_GET_PARAMETER_NOT_A_FUNCTION)
93
      })
94
    })
95

96
    it('is stable', () => {
97
      const first = getWebGlExtensions({ cache: {} })
98
      const second = getWebGlExtensions({ cache: {} })
99

100
      expect(first).toEqual(second)
101
    })
102
  })
103
})
104

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

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

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

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