summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resources/chromium/mock-barcodedetection.js
blob: b0d2e0af0ae40c620c952fe71e4d3818e3a908e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {BarcodeDetectionReceiver, BarcodeFormat} from '/gen/services/shape_detection/public/mojom/barcodedetection.mojom.m.js';
import {BarcodeDetectionProvider, BarcodeDetectionProviderReceiver} from '/gen/services/shape_detection/public/mojom/barcodedetection_provider.mojom.m.js';

self.BarcodeDetectionTest = (() => {
  // Class that mocks BarcodeDetectionProvider interface defined in
  // https://cs.chromium.org/chromium/src/services/shape_detection/public/mojom/barcodedetection_provider.mojom
  class MockBarcodeDetectionProvider {
    constructor() {
      this.receiver_ = new BarcodeDetectionProviderReceiver(this);

      this.interceptor_ = new MojoInterfaceInterceptor(
          BarcodeDetectionProvider.$interfaceName);
      this.interceptor_.oninterfacerequest = e => {
        if (this.should_close_pipe_on_request_)
          e.handle.close();
        else
          this.receiver_.$.bindHandle(e.handle);
      }
      this.interceptor_.start();
      this.should_close_pipe_on_request_ = false;
    }

    createBarcodeDetection(request, options) {
      this.mockService_ = new MockBarcodeDetection(request, options);
    }

    enumerateSupportedFormats() {
      return {
        supportedFormats: [
          BarcodeFormat.AZTEC,
          BarcodeFormat.DATA_MATRIX,
          BarcodeFormat.QR_CODE,
        ]
      };
    }

    getFrameData() {
      return this.mockService_.bufferData_;
    }

    getFormats() {
     return this.mockService_.options_.formats;
    }

    reset() {
      this.mockService_ = null;
      this.should_close_pipe_on_request_ = false;
      this.receiver_.$.close();
      this.interceptor_.stop();
    }

    // simulate a 'no implementation available' case
    simulateNoImplementation() {
      this.should_close_pipe_on_request_ = true;
    }
  }

  // Class that mocks BarcodeDetection interface defined in
  // https://cs.chromium.org/chromium/src/services/shape_detection/public/mojom/barcodedetection.mojom
  class MockBarcodeDetection {
    constructor(request, options) {
      this.options_ = options;
      this.receiver_ = new BarcodeDetectionReceiver(this);
      this.receiver_.$.bindHandle(request.handle);
    }

    detect(bitmapData) {
      this.bufferData_ =
          new Uint32Array(getArrayBufferFromBigBuffer(bitmapData.pixelData));
      return {
        results: [
          {
            rawValue : "cats",
            boundingBox: { x: 1.0, y: 1.0, width: 100.0, height: 100.0 },
            format: BarcodeFormat.QR_CODE,
            cornerPoints: [
              { x: 1.0, y: 1.0 },
              { x: 101.0, y: 1.0 },
              { x: 101.0, y: 101.0 },
              { x: 1.0, y: 101.0 }
            ],
          },
          {
            rawValue : "dogs",
            boundingBox: { x: 2.0, y: 2.0, width: 50.0, height: 50.0 },
            format: BarcodeFormat.CODE_128,
            cornerPoints: [
              { x: 2.0, y: 2.0 },
              { x: 52.0, y: 2.0 },
              { x: 52.0, y: 52.0 },
              { x: 2.0, y: 52.0 }
            ],
          },
        ],
      };
    }
  }

  let testInternal = {
    initialized: false,
    MockBarcodeDetectionProvider: null
  }

  class BarcodeDetectionTestChromium {
    constructor() {
      Object.freeze(this); // Make it immutable.
    }

    initialize() {
      if (testInternal.initialized)
        throw new Error('Call reset() before initialize().');

      testInternal.MockBarcodeDetectionProvider = new MockBarcodeDetectionProvider;
      testInternal.initialized = true;
    }

    // Resets state of barcode detection mocks between test runs.
    async reset() {
      if (!testInternal.initialized)
        throw new Error('Call initialize() before reset().');
      testInternal.MockBarcodeDetectionProvider.reset();
      testInternal.MockBarcodeDetectionProvider = null;
      testInternal.initialized = false;

      await new Promise(resolve => setTimeout(resolve, 0));
    }

    MockBarcodeDetectionProvider() {
      return testInternal.MockBarcodeDetectionProvider;
    }
  }

  return BarcodeDetectionTestChromium;
})();

self.BarcodeFormat = BarcodeFormat;