summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resource-timing/cached-image-gets-single-entry.html
blob: 2d8c4e2e83944dc576484e3840614faa55d65564 (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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Resource Timing: test behavior for cached resources</title>
<link rel="author" title="Google" href="http://www.google.com/" />
<link rel="help" href="https://www.w3.org/TR/resource-timing-2/"/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/observe-entry.js"></script>
</head>
<body>
<h1>Description</h1>
<p>Test that a reused resource only appears in the buffer once.</p>
<script>
// Need our own image loading helper because the one in resource-loaders.js
// is desgined to always side-step the HTTP cache but this test relies on the
// second request being resolved from the cache.
const load_image = path => new Promise(resolve => {
  const img = document.createElement('img');
  img.onload = img.onerror = () => resolve();
  img.src = path;
  document.body.append(img);
});

promise_test(async () => {
  const blue = "resources/blue.png";

  // First request. Should appear in the timeline.
  await load_image(blue + "?cacheable");

  // Second request. Should not appear in the timeline.
  await load_image(blue + "?cacheable");

  // Third request. When this request shows up in the timeline, we know that, if
  // the second request would generate an entry, that entry would have already
  // shown up in the timeline. Without this, we'd need to guess at how long to
  // wait which tends to be flaky.
  await load_image(blue + "?avoid-cache");

  const entries = await new Promise(resolve => {
    const accumulator = [];
    new PerformanceObserver(entry_list => {
      entry_list.getEntries().forEach(entry => {
        if (!entry.name.includes("blue.png")) {
          // Ignore resources other than blue images.
          return;
        }
        accumulator.push(entry);

        // Once we see the 'canary' resource, we don't need to wait anymore.
        if (entry.name.endsWith('avoid-cache')) {
          resolve(accumulator);
        }
      });
    }).observe({'type': 'resource', 'buffered': true});
  });

  assert_equals(entries.length, 2, "There must be exactly 2 entries in the " +
    "Performance Timeline");
  assert_true(entries[0].name.endsWith("blue.png?cacheable"));
  assert_true(entries[1].name.endsWith("blue.png?avoid-cache"));
}, "When a resource is resolved from cache, there must not be a " +
  "corresponding entry in the Performance Timeline");
</script>
</body>
</html>