summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/shape-detection/detection-on-worker.https.worker.js
blob: 3981c6fdc8e12839d3aa30206fa16d8af1faf513 (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
importScripts("/resources/testharness.js");
importScripts("/resources/test-only-api.js");
importScripts("resources/shapedetection-helpers.js");

'use strict';

// These tests verify that a Detector's detect() works on an
// ImageBitmap on workers.
const imageBitmapTests =
    [
      {
        createDetector: () => { return new FaceDetector(); },
        mockTestName: "FaceDetectionTest",
        resultSize: 3, // Number of faces
        detectorType: "Face"
      },
      {
        createDetector: () => { return new BarcodeDetector(); },
        mockTestName: "BarcodeDetectionTest",
        resultSize: 2, // Number of barcodes
        detectorType: "Barcode"
      },
      {
        createDetector: () => { return new TextDetector(); },
        mockTestName: "TextDetectionTest",
        resultSize: 2, // Number of text blocks
        detectorType: "Text"
      }
    ];

for (let imageBitmapTest of imageBitmapTests) {
  // ImageBitmap is of transferable type and can be sent to and
  // tested on worker.
  detection_test(imageBitmapTest.mockTestName, async (t, detectionTest) => {
    const img = createTestImage();
    const theImageBitmap = await createImageBitmap(img);
    const detector = imageBitmapTest.createDetector();
    const detectionResult = await detector.detect(theImageBitmap);
    assert_equals(detectionResult.length, imageBitmapTest.resultSize,
      `Number of ${imageBitmapTest.detectorType}`);
  }, `${imageBitmapTest.detectorType} Detector detect(ImageBitmap) on worker`);
}

function createTestImage() {
  const image = new OffscreenCanvas(100, 50);
  const imgctx = image.getContext('2d');
  imgctx.fillStyle = "#F00";
  imgctx.fillRect(0, 0, 2, 2);
  imgctx.fillStyle = "#0F0";
  imgctx.fillRect(0, 0, 1, 1);
  return image;
}

done();