summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/shape-detection/detection-security-test.https.html
blob: 4d87238dad5d5dfb74ee1d9af0257f8853d038c9 (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
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/shapedetection-helpers.js"></script>
<script>

// Detectors should reject undecodable images with an InvalidStateError.
const badImageTests =
    [
      {
        createDetector: () => { return new FaceDetector(); },
        name: "Face - detect(broken image)"
      },
      {
        createDetector: () => { return new BarcodeDetector(); },
        name: "Barcode - detect(broken image)"
      },
      {
        createDetector: () => { return new TextDetector(); },
        name: "Text - detect(broken image)"
      }
    ];

for (let badImageTest of badImageTests) {
  // This test verifies that a Detector will reject an undecodable image.
  promise_test(async t => {
    const img = new Image();
    const error =
        await detectOnElementAndExpectError(badImageTest.createDetector,
                                            img, "/images/broken.png");
    assert_equals(error.name, "InvalidStateError");
  }, badImageTest.name);
}

// Detectors should reject undecodable videos with an InvalidStateError.
const badVideoTests =
    [
      {
        createDetector: () => { return new FaceDetector(); },
        name: "Face - detect(broken video)"
      },
      {
        createDetector: () => { return new BarcodeDetector(); },
        name: "Barcode - detect(broken video)"
      },
      {
        createDetector: () => { return new TextDetector(); },
        name: "Text - detect(broken video)"
      }
    ];

for (let badVideoTest of badVideoTests) {
  // This test verifies that a Detector will reject a broken video.
  promise_test(async t => {
    const video = document.createElement('video');
    const error =
        await detectOnElementAndExpectError(badVideoTest.createDetector,
                                            video, "garbage.webm");
    assert_equals(error.name, "InvalidStateError");
  }, badVideoTest.name);
}

// Returns a Promise that is resolve()d if detect() is rejected. Needs an input
// |element| (e.g. an HTMLImageElement or HTMLVideoElement) and a |url| to load.
function detectOnElementAndExpectError(createDetector, element, url) {
  return new Promise((resolve, reject) => {
    const tryDetection = async () => {
      const detector = createDetector();
      try {
        const detectionResult = await detector.detect(element);
        reject("Promise should have been rejected.");
      } catch (error) {
        resolve(error);
      }
    };
    element.onload = tryDetection;
    element.onerror = tryDetection;
    element.src = url;
  });
};

</script>