summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/embedded-content/the-img-element/list-of-available-images-matching.https.html
blob: 4843d219156ddc92d3460cf04b7e8acfab7c8368 (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
<!doctype html>
<html>
<title>List of available images tuple-matching logic</title>
<link rel="author" title="Dom Farolino" href="mailto:dom@chromium.org">
<link rel="help" href="https://html.spec.whatwg.org/multipage/#the-list-of-available-images">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
<script src="/common/get-host-info.sub.js"></script>

<script>
const path = location.origin + '/html/semantics/embedded-content/the-img-element/';
const image_url = path + 'image-1.jpg';

function syncWait(ms) {
  const start = Date.now();
  while (Date.now() - start < ms);
}

let sawNoCorsRequest = false;

navigator.serviceWorker.onmessage = ({data}) => {
  if (data.url === image_url && data.mode === 'no-cors') {
    sawNoCorsRequest = true;
  }
};

promise_test(t => {
  return service_worker_unregister_and_register(t, 'resources/sw.js', path)
    .then(r => {
      return wait_for_state(t, r.installing, 'activated');
    });
}, 'registering service worker');

promise_test(async t => {
  const img = new Image();

  function load_img_promise() {
    return new Promise((resolve, reject) => {
      img.onload = resolve;
      img.onerror = e => { reject("The img should not fail to load") };

      img.src = image_url;
      // If there is not a matching image in the list of available images, the
      // actual fetch operation is queued as a microtask, so we will see a
      // request with mode 'cors' due to setting the crossorigin attribute
      // below.
      syncWait(500);
      img.crossOrigin = 'anonymous';
    });
  };

  await load_img_promise();
  assert_false(sawNoCorsRequest, "The image is not fetched with mode: no-cors");
  await new Promise(resolve => {
    img.onload = img.onerror = resolve;
    img.src = '';
    img.crossOrigin = null;
  });
  await load_img_promise();
  assert_false(sawNoCorsRequest, "The image is not fetched with mode: no-cors");

}, 'list of available images tuple-matching logic');

promise_test(t => {
  return service_worker_unregister(t, path);
}, 'unregistering service worker');
</script>