11cb0ef41Sopenharmony_ci
21cb0ef41Sopenharmony_ci<!DOCTYPE html>
31cb0ef41Sopenharmony_ci<html>
41cb0ef41Sopenharmony_ci    <head>
51cb0ef41Sopenharmony_ci        <meta charset="UTF-8" />
61cb0ef41Sopenharmony_ci        <title>window.performance User Timing clearMeasures() method is working properly with navigation timing
71cb0ef41Sopenharmony_ci               attributes</title>
81cb0ef41Sopenharmony_ci        <link rel="author" title="Microsoft" href="http://www.microsoft.com/" />
91cb0ef41Sopenharmony_ci        <link rel="help" href="https://w3c.github.io/user-timing/#dom-performance-measure"/>
101cb0ef41Sopenharmony_ci        <script src="/resources/testharness.js"></script>
111cb0ef41Sopenharmony_ci        <script src="/resources/testharnessreport.js"></script>
121cb0ef41Sopenharmony_ci        <script src="/common/performance-timeline-utils.js"></script>
131cb0ef41Sopenharmony_ci        <script src="resources/webperftestharness.js"></script>
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci    <script>
161cb0ef41Sopenharmony_ci        // test data
171cb0ef41Sopenharmony_ci        var startMarkName = "mark_start";
181cb0ef41Sopenharmony_ci        var startMarkValue;
191cb0ef41Sopenharmony_ci        var endMarkName = "mark_end";
201cb0ef41Sopenharmony_ci        var endMarkValue;
211cb0ef41Sopenharmony_ci        var measures;
221cb0ef41Sopenharmony_ci        var testThreshold = 20;
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci        // test measures
251cb0ef41Sopenharmony_ci        measureTestDelay = 200;
261cb0ef41Sopenharmony_ci        var TEST_MEASURES =
271cb0ef41Sopenharmony_ci        [
281cb0ef41Sopenharmony_ci            {
291cb0ef41Sopenharmony_ci                name:                   "measure_nav_start_no_end",
301cb0ef41Sopenharmony_ci                startMark:              "navigationStart",
311cb0ef41Sopenharmony_ci                endMark:                undefined,
321cb0ef41Sopenharmony_ci                exceptionTestMessage:   "window.performance.measure(\"measure_nav_start_no_end\", " +
331cb0ef41Sopenharmony_ci                                        "\"navigationStart\") ran without throwing any exceptions.",
341cb0ef41Sopenharmony_ci                expectedStartTime:      undefined,
351cb0ef41Sopenharmony_ci                expectedDuration:       undefined,
361cb0ef41Sopenharmony_ci                entryMatch:             undefined
371cb0ef41Sopenharmony_ci            },
381cb0ef41Sopenharmony_ci            {
391cb0ef41Sopenharmony_ci                name:                   "measure_nav_start_mark_end",
401cb0ef41Sopenharmony_ci                startMark:              "navigationStart",
411cb0ef41Sopenharmony_ci                endMark:                "mark_end",
421cb0ef41Sopenharmony_ci                exceptionTestMessage:   "window.performance.measure(\"measure_nav_start_end\", \"navigationStart\", " +
431cb0ef41Sopenharmony_ci                                        "\"mark_end\") ran without throwing any exceptions.",
441cb0ef41Sopenharmony_ci                expectedStartTime:      undefined,
451cb0ef41Sopenharmony_ci                expectedDuration:       undefined,
461cb0ef41Sopenharmony_ci                entryMatch:             undefined
471cb0ef41Sopenharmony_ci            },
481cb0ef41Sopenharmony_ci            {
491cb0ef41Sopenharmony_ci                name:                   "measure_mark_start_nav_end",
501cb0ef41Sopenharmony_ci                startMark:              "mark_start",
511cb0ef41Sopenharmony_ci                endMark:                "responseEnd",
521cb0ef41Sopenharmony_ci                exceptionTestMessage:   "window.performance.measure(\"measure_start_nav_end\", \"mark_start\", " +
531cb0ef41Sopenharmony_ci                                        "\"responseEnd\") ran without throwing any exceptions.",
541cb0ef41Sopenharmony_ci                expectedStartTime:      undefined,
551cb0ef41Sopenharmony_ci                expectedDuration:       undefined,
561cb0ef41Sopenharmony_ci                entryMatch:             undefined
571cb0ef41Sopenharmony_ci            },
581cb0ef41Sopenharmony_ci            {
591cb0ef41Sopenharmony_ci                name:                   "measure_nav_start_nav_end",
601cb0ef41Sopenharmony_ci                startMark:              "navigationStart",
611cb0ef41Sopenharmony_ci                endMark:                "responseEnd",
621cb0ef41Sopenharmony_ci                exceptionTestMessage:   "window.performance.measure(\"measure_nav_start_nav_end\", " +
631cb0ef41Sopenharmony_ci                                        "\"navigationStart\", \"responseEnd\") ran without throwing any exceptions.",
641cb0ef41Sopenharmony_ci                expectedStartTime:      undefined,
651cb0ef41Sopenharmony_ci                expectedDuration:       undefined,
661cb0ef41Sopenharmony_ci                entryMatch:             undefined
671cb0ef41Sopenharmony_ci            }
681cb0ef41Sopenharmony_ci        ];
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci        setup({explicit_done: true});
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci        test_namespace();
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci        function onload_test()
751cb0ef41Sopenharmony_ci        {
761cb0ef41Sopenharmony_ci            // test for existence of User Timing and Performance Timeline interface
771cb0ef41Sopenharmony_ci            if (!has_required_interfaces())
781cb0ef41Sopenharmony_ci            {
791cb0ef41Sopenharmony_ci                test_true(false,
801cb0ef41Sopenharmony_ci                          "The User Timing and Performance Timeline interfaces, which are required for this test, " +
811cb0ef41Sopenharmony_ci                          "are defined.");
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci                done();
841cb0ef41Sopenharmony_ci            }
851cb0ef41Sopenharmony_ci            else
861cb0ef41Sopenharmony_ci            {
871cb0ef41Sopenharmony_ci                // create the start mark for the test measures
881cb0ef41Sopenharmony_ci                window.performance.mark(startMarkName);
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci                // get the start mark's value
911cb0ef41Sopenharmony_ci                startMarkValue = window.performance.getEntriesByName(startMarkName)[0].startTime;
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci                // create the test end mark using the test delay; this will allow for a significant difference between
941cb0ef41Sopenharmony_ci                // the mark values that should be represented in the duration of measures using these marks
951cb0ef41Sopenharmony_ci                step_timeout(measure_test_cb, measureTestDelay);
961cb0ef41Sopenharmony_ci            }
971cb0ef41Sopenharmony_ci        }
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci        function measure_test_cb()
1001cb0ef41Sopenharmony_ci        {
1011cb0ef41Sopenharmony_ci            // create the end mark for the test measures
1021cb0ef41Sopenharmony_ci            window.performance.mark(endMarkName);
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci            // get the end mark's value
1051cb0ef41Sopenharmony_ci            endMarkValue = window.performance.getEntriesByName(endMarkName)[0].startTime;
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci            // loop through measure scenarios
1081cb0ef41Sopenharmony_ci            for (var i in TEST_MEASURES)
1091cb0ef41Sopenharmony_ci            {
1101cb0ef41Sopenharmony_ci                var scenario = TEST_MEASURES[i];
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci                if (scenario.startMark != undefined && scenario.endMark == undefined)
1131cb0ef41Sopenharmony_ci                {
1141cb0ef41Sopenharmony_ci                    // only startMark is defined, provide startMark and don't provide endMark
1151cb0ef41Sopenharmony_ci                    window.performance.measure(scenario.name, scenario.startMark);
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci                    // when startMark is provided to the measure() call, the value of the mark or navigation
1181cb0ef41Sopenharmony_ci                    // timing attribute whose name is provided is used for the startMark
1191cb0ef41Sopenharmony_ci                    scenario.expectedStartTime = (timingAttributes.indexOf(scenario.startMark) != -1 ?
1201cb0ef41Sopenharmony_ci                                                  window.performance.timing[scenario.startMark] -
1211cb0ef41Sopenharmony_ci                                                  window.performance.timing.navigationStart :
1221cb0ef41Sopenharmony_ci                                                  startMarkValue);
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci                    // when endMark isn't provided to the measure() call, a DOMHighResTimeStamp corresponding to
1251cb0ef41Sopenharmony_ci                    // the current time with a timebase of the navigationStart attribute is used
1261cb0ef41Sopenharmony_ci                    scenario.expectedDuration = ((new Date()) - window.performance.timing.navigationStart) -
1271cb0ef41Sopenharmony_ci                                                scenario.expectedStartTime;
1281cb0ef41Sopenharmony_ci                }
1291cb0ef41Sopenharmony_ci                else if (scenario.startMark != undefined && scenario.endMark != undefined)
1301cb0ef41Sopenharmony_ci                {
1311cb0ef41Sopenharmony_ci                    // both startMark and endMark are defined, provide both parameters
1321cb0ef41Sopenharmony_ci                    window.performance.measure(scenario.name, scenario.startMark, scenario.endMark);
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci                    // when startMark is provided to the measure() call, the value of the mark or navigation
1351cb0ef41Sopenharmony_ci                    // timing attribute whose name is provided is used for the startMark
1361cb0ef41Sopenharmony_ci                    scenario.expectedStartTime = (timingAttributes.indexOf(scenario.startMark) != -1 ?
1371cb0ef41Sopenharmony_ci                                                  window.performance.timing[scenario.startMark] -
1381cb0ef41Sopenharmony_ci                                                  window.performance.timing.navigationStart :
1391cb0ef41Sopenharmony_ci                                                  startMarkValue);
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci                    // when endMark is provided to the measure() call, the value of the mark whose name is
1421cb0ef41Sopenharmony_ci                    // provided is used for the startMark
1431cb0ef41Sopenharmony_ci                    scenario.expectedDuration = (timingAttributes.indexOf(scenario.endMark) != -1 ?
1441cb0ef41Sopenharmony_ci                                                 window.performance.timing[scenario.endMark] -
1451cb0ef41Sopenharmony_ci                                                 window.performance.timing.navigationStart :
1461cb0ef41Sopenharmony_ci                                                 endMarkValue) - scenario.expectedStartTime;
1471cb0ef41Sopenharmony_ci                }
1481cb0ef41Sopenharmony_ci            }
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci            // test the test measures are returned by getEntriesByName
1511cb0ef41Sopenharmony_ci            for (var i in TEST_MEASURES)
1521cb0ef41Sopenharmony_ci            {
1531cb0ef41Sopenharmony_ci                entries = window.performance.getEntriesByName(TEST_MEASURES[i].name);
1541cb0ef41Sopenharmony_ci                test_measure(entries[0],
1551cb0ef41Sopenharmony_ci                            "window.performance.getEntriesByName(\"" + TEST_MEASURES[i].name + "\")[0]",
1561cb0ef41Sopenharmony_ci                            TEST_MEASURES[i].name,
1571cb0ef41Sopenharmony_ci                            TEST_MEASURES[i].expectedStartTime,
1581cb0ef41Sopenharmony_ci                            TEST_MEASURES[i].expectedDuration);
1591cb0ef41Sopenharmony_ci                TEST_MEASURES[i].entryMatch = entries[0];
1601cb0ef41Sopenharmony_ci            }
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci            done();
1631cb0ef41Sopenharmony_ci        }
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci        function test_measure(measureEntry, measureEntryCommand, expectedName, expectedStartTime, expectedDuration)
1661cb0ef41Sopenharmony_ci        {
1671cb0ef41Sopenharmony_ci            // test name
1681cb0ef41Sopenharmony_ci            test_true(measureEntry.name == expectedName, measureEntryCommand + ".name == \"" + expectedName + "\"");
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ci            // test startTime; since for a mark, the startTime is always equal to a mark's value or the value of a
1711cb0ef41Sopenharmony_ci            // navigation timing attribute, the actual startTime should match the expected value exactly
1721cb0ef41Sopenharmony_ci            test_true(Math.abs(measureEntry.startTime - expectedStartTime) == 0,
1731cb0ef41Sopenharmony_ci                      measureEntryCommand + ".startTime is correct");
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci            // test entryType
1761cb0ef41Sopenharmony_ci            test_true(measureEntry.entryType == "measure", measureEntryCommand + ".entryType == \"measure\"");
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci            // test duration, allow for an acceptable threshold in the difference between the actual duration and the
1791cb0ef41Sopenharmony_ci            // expected value for the duration
1801cb0ef41Sopenharmony_ci            test_true(Math.abs(measureEntry.duration - expectedDuration) <= testThreshold, measureEntryCommand +
1811cb0ef41Sopenharmony_ci                      ".duration is approximately correct (up to " + testThreshold + "ms difference allowed)");
1821cb0ef41Sopenharmony_ci        }
1831cb0ef41Sopenharmony_ci    </script>
1841cb0ef41Sopenharmony_ci    </head>
1851cb0ef41Sopenharmony_ci    <body onload="onload_test();">
1861cb0ef41Sopenharmony_ci        <h1>Description</h1>
1871cb0ef41Sopenharmony_ci        <p>This test validates that the performance.measure() method is working properly when navigation timing
1881cb0ef41Sopenharmony_ci           attributes are used in place of mark names. This test creates the following measures to test this method:
1891cb0ef41Sopenharmony_ci            <ul>
1901cb0ef41Sopenharmony_ci                <li>"measure_nav_start_no_end": created using a measure() call with a navigation timing attribute
1911cb0ef41Sopenharmony_ci                    provided as the startMark and nothing provided as the endMark</li>
1921cb0ef41Sopenharmony_ci                <li>"measure_nav_start_mark_end": created using a measure() call with a navigation timing attribute
1931cb0ef41Sopenharmony_ci                    provided as the startMark and a mark name provided as the endMark</li>
1941cb0ef41Sopenharmony_ci                <li>"measure_mark_start_nav_end": created using a measure() call with a mark name provided as the
1951cb0ef41Sopenharmony_ci                    startMark and a navigation timing attribute provided as the endMark</li>
1961cb0ef41Sopenharmony_ci                <li>"measure_nav_start_nav_end":created using a measure() call with a navigation timing attribute
1971cb0ef41Sopenharmony_ci                    provided as both the startMark and endMark</li>
1981cb0ef41Sopenharmony_ci            </ul>
1991cb0ef41Sopenharmony_ci           After creating each measure, the existence of these measures is validated by calling
2001cb0ef41Sopenharmony_ci           performance.getEntriesByName() with each measure name
2011cb0ef41Sopenharmony_ci        </p>
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_ci        <div id="log"></div>
2041cb0ef41Sopenharmony_ci    </body>
2051cb0ef41Sopenharmony_ci</html>
206