summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resources/chromium/mock-textdetection.js
blob: 52ca987e28639a93363b5496a0f0c14a46e2bb50 (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
import {TextDetection, TextDetectionReceiver} from '/gen/services/shape_detection/public/mojom/textdetection.mojom.m.js';

self.TextDetectionTest = (() => {
  // Class that mocks TextDetection interface defined in
  // https://cs.chromium.org/chromium/src/services/shape_detection/public/mojom/textdetection.mojom
  class MockTextDetection {
    constructor() {
      this.receiver_ = new TextDetectionReceiver(this);
      this.interceptor_ =
          new MojoInterfaceInterceptor(TextDetection.$interfaceName);
      this.interceptor_.oninterfacerequest =
          e => this.receiver_.$.bindHandle(e.handle);
      this.interceptor_.start();
    }

    detect(bitmapData) {
      this.bufferData_ =
          new Uint32Array(getArrayBufferFromBigBuffer(bitmapData.pixelData));
      return Promise.resolve({
        results: [
          {
            rawValue : "cats",
            boundingBox: { x: 1.0, y: 1.0, width: 100.0, height: 100.0 },
            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 },
            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 }
            ]
          },
        ],
      });
    }

    getFrameData() {
      return this.bufferData_;
    }

    reset() {
      this.receiver_.$.close();
      this.interceptor_.stop();
    }

  }

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

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

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

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

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

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

    MockTextDetection() {
      return testInternal.MockTextDetection;
    }
  }

  return TextDetectionTestChromium;

})();