summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/navigation-preload/resource-timing.https.html
blob: 468a1f51e8573e13a91a55b1c747f4acbe11ce37 (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
<!DOCTYPE html>
<meta charset="utf-8">
<title>Navigation Preload Resource Timing</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/test-helpers.sub.js"></script>
<script>

function check_timing_entry(entry, url, decodedBodySize, encodedBodySize) {
  assert_equals(entry.name, url, 'The entry name of '+ url);

  assert_equals(
    entry.entryType, 'resource',
    'The entryType of preload response timing entry must be "resource' +
    '" :' + url);
  assert_equals(
    entry.initiatorType, 'navigation',
    'The initiatorType of preload response timing entry must be ' +
    '"navigation":' + url);

  // If the server returns the redirect response, |decodedBodySize| is null and
  // |entry.decodedBodySize| should be 0. Otherwise |entry.decodedBodySize| must
  // same as |decodedBodySize|
  assert_equals(
    entry.decodedBodySize, Number(decodedBodySize),
    'decodedBodySize must same as the decoded size in the server:' + url);

  // If the server returns the redirect response, |encodedBodySize| is null and
  // |entry.encodedBodySize| should be 0. Otherwise |entry.encodedBodySize| must
  // same as |encodedBodySize|
  assert_equals(
    entry.encodedBodySize, Number(encodedBodySize),
    'encodedBodySize must same as the encoded size in the server:' + url);

  assert_greater_than(
    entry.transferSize, entry.decodedBodySize,
    'transferSize must greater then encodedBodySize.');

  assert_greater_than(entry.startTime, 0, 'startTime of ' + url);
  assert_greater_than_equal(entry.fetchStart, entry.startTime,
                            'fetchStart >= startTime of ' + url);
  assert_greater_than_equal(entry.domainLookupStart, entry.fetchStart,
                            'domainLookupStart >= fetchStart of ' + url);
  assert_greater_than_equal(entry.domainLookupEnd, entry.domainLookupStart,
                            'domainLookupEnd >= domainLookupStart of ' + url);
  assert_greater_than_equal(entry.connectStart, entry.domainLookupEnd,
                            'connectStart >= domainLookupEnd of ' + url);
  assert_greater_than_equal(entry.connectEnd, entry.connectStart,
                            'connectEnd >= connectStart of ' + url);
  assert_greater_than_equal(entry.requestStart, entry.connectEnd,
                            'requestStart >= connectEnd of ' + url);
  assert_greater_than_equal(entry.responseStart, entry.requestStart,
                            'domainLookupStart >= requestStart of ' + url);
  assert_greater_than_equal(entry.responseEnd, entry.responseStart,
                            'responseEnd >= responseStart of ' + url);
  assert_greater_than(entry.duration, 0, 'duration of ' + url);
}

promise_test(t => {
    var script = 'resources/resource-timing-worker.js';
    var scope = 'resources/resource-timing-scope.py';
    var registration;
    var frames = [];
    return service_worker_unregister_and_register(t, script, scope)
      .then(reg => {
          registration = reg;
          add_completion_callback(_ => registration.unregister());
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(_ => with_iframe(scope + '?type=normal'))
      .then(frame => {
          frames.push(frame);
          return with_iframe(scope + '?type=redirect');
        })
      .then(frame => {
          frames.push(frame);
          frames.forEach(frame => {
            var result = JSON.parse(frame.contentDocument.body.textContent);
            assert_equals(
              result.timingEntries.length, 1,
              'performance.getEntriesByName() must returns one ' +
              'PerformanceResourceTiming entry for the navigation preload.');
            var entry = result.timingEntries[0];
            check_timing_entry(entry, frame.src, result.decodedBodySize,
                               result.encodedBodySize);
            frame.remove();
          });
          return registration.unregister();
        });
  }, 'Navigation Preload Resource Timing.');

</script>