summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/navigation-timing/nav2-test-attributes-values.html
blob: f13a8988fcb088ac59d7aacc6c1ec0ab7f7781a6 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Navigation Timing 2 WPT</title>
        <link rel="author" title="Google" href="http://www.google.com/" />
        <link rel="help" href="http://www.w3.org/TR/navigation-timing-2/#sec-PerformanceNavigationTiming"/>
        <script src="/resources/testharness.js"></script>
        <script src="/resources/testharnessreport.js"></script>
        <script src="/common/get-host-info.sub.js"></script>
    </head>
    <body>
        <h1>Description</h1>
        <p>This test validates that the values of nav timing 2 instance's timing-related attributes are in certain order and the others are of expected values.</p>

        <script>
        // Host names and ports may be configured at test execution time. The
        // web-platform-tests server offers two mechanisms for retrieving these
        // values dynamically: direct text substitution and the `get-host-info`
        // script. The former is inapproprate for this test because it
        // influences the size of the document, and this test includes static
        // assertions for that value.
        var host_info = get_host_info();
        var expectedUrl = "http://" + host_info.ORIGINAL_HOST + ":" +
            host_info.HTTP_PORT +
          "/navigation-timing/nav2-test-attributes-values.html";
        var navTiming2EventOrder1 = [
            'startTime',
            'redirectStart',
            //'unloadEventStart',
            'redirectEnd',
            //'unloadEventEnd',
            'fetchStart',
            'domainLookupStart',
            'domainLookupEnd',
            'connectStart',
            //'secureConnectionStart',
            'connectEnd',
            'requestStart',
            'responseStart',
            'responseEnd',
            'domInteractive',
            'domContentLoadedEventStart',
            'domContentLoadedEventEnd',
            'domComplete',
            'loadEventStart',
            'loadEventEnd'
        ];

        var navTiming2EventOrder2 = [
            'redirectStart',
            'unloadEventStart',
            'redirectEnd',
            'unloadEventEnd',
            'fetchStart'
        ];

        var navTiming2EventOrder3 = [
            'connectStart',
            'secureConnectionStart',
            'connectEnd'
        ];

        // Navigation Timing attributes for comparison.
        var navTiming1EventOrder = [
            'fetchStart',
            'domainLookupStart',
            'domainLookupEnd',
            'connectStart',
            'connectEnd',
            'requestStart',
            'responseStart',
            'responseEnd',
            'domInteractive',
            'domContentLoadedEventStart',
            'domContentLoadedEventEnd',
            'domComplete',
            'loadEventStart',
            'loadEventEnd'
        ];

        function verifyTimingEventOrder(eventOrder, timingEntry) {
            for (var i = 0; i < eventOrder.length - 1; i++) {
                assert_true(timingEntry[eventOrder[i]] <= timingEntry[eventOrder[i + 1]],
                    "Expected " + eventOrder[i] + " to be no greater than " + eventOrder[i + 1] + ".");
            }
        }

        async_test(function (t) {
            var observer = new PerformanceObserver(
                t.step_func(function (entryList) {
                    var entries = entryList.getEntries();
                    assert_equals(entries[0].entryType, "navigation",
                        "Expected entryType to be: navigation.");
                    assert_equals(entries[0].name, expectedUrl);
                    assert_equals(entries[0].startTime, 0,
                        "Expected startTime to be: 0.");
                    assert_equals(entries[0].duration, entries[0].loadEventEnd,
                        "Expected duration to be equal to loadEventEnd.");
                    assert_equals(entries[0].initiatorType, "navigation",
                        "Expected initiatorType to be: navigation.");
                    assert_equals(entries[0].nextHopProtocol, "http/1.1");
                    // This test may fail when response is from cach. Disable or clean cach before
                    // running this test.
                    assert_true(entries[0].transferSize > entries[0].encodedBodySize,
                        "Expected transferSize to be greater than encodedBodySize in uncached navigation.");
                  assert_equals(entries[0].encodedBodySize, 5949);
                  assert_equals(entries[0].decodedBodySize, 5949);
                    verifyTimingEventOrder(entries[0], navTiming2EventOrder1);
                    // Verify if the reported timing is not that different
                    // from what is reported by Navigation Timing 1.
                    navTiming1EventOrder.forEach(
                      function(event) {
                        if (window.performance.timing[event] -
                            window.performance.timing.navigationStart > 0) {
                          assert_greater_than(entries[0][event], 0,
                              "Expected " + event + " to be greater than 0");
                        }
                    });
                    // When unloadEvent happens
                    if (entries[0]["unloadEventStart"] != 0) {
                        verifyTimingEventOrder(entries[0], navTiming2EventOrder2);
                    }
                    // When a secure transport is used
                    if (entries[0]["secureConnectionStart"] != 0) {
                        verifyTimingEventOrder(entries[0], navTiming2EventOrder3);
                    }
                    observer.disconnect();
                    t.done();
                })
            );
            observer.observe({entryTypes: ["navigation"]});

        }, "Performance navigation timing instance's value is reasonable.");
        </script>
    </body>
</html>