<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>functionality test of window.performance.measure</title>
<link rel="author" title="Intel" href="http://www.intel.com/" />
<link rel="help" href="http://www.w3.org/TR/user-timing/#extensions-performance-interface"/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/performance-timeline-utils.js"></script>
<script src="resources/webperftestharness.js"></script>
<script src="resources/webperftestharnessextension.js"></script>
<script>
setup({ explicit_done: true });

function onload_test()
{
    const measures_for_timing_order = [
        ['nav2now', 'navigationStart'],
        ['loadTime', 'navigationStart', 'loadEventEnd'],
        ['loadEventEnd2a', 'loadEventEnd', 'abc'],
        ['nav2a', 'navigationStart', 'abc'],
        ['domComplete2a', 'domComplete', 'abc'],
        ['negativeValue', 1, 'navigationStart'],
    ];
    const context = new PerformanceContext(window.performance);

    mark_names.forEach(function(name) {
        context.mark(name);
    });
    measures_for_timing_order.forEach(context.initialMeasures, context);
    test_greater_than(context.getEntriesByName('nav2now', 'measure')[0].duration, 0, 'Measure of navigationStart to now should be positive value.');
    test_greater_than(context.getEntriesByName('loadTime', 'measure')[0].duration, 0, 'Measure of navigationStart to loadEventEnd should be positive value.');
    test_greater_than(0, context.getEntriesByName('negativeValue', 'measure')[0].duration, 'Measure of current mark to navigationStart should be negative value.');
    test_equals(context.getEntriesByName('loadTime', 'measure')[0].duration + context.getEntriesByName('loadEventEnd2a', 'measure')[0].duration, context.getEntriesByName('nav2a', 'measure')[0].duration, 'loadTime plus loadEventEnd to a mark "a" should equal to navigationStart to "a".');

    // We later assert that time has passed between setting one set of marks and another set.
    // However, this assertion will fail if the test executes fast enough such that the marks occur
    // at the same clock time. This is more likely in browsers such as Firefox that reduce the
    // precision of the clock exposed through this API to mitigate timing attacks. To mitigate the
    // test failure, we sleep. Firefox may round timestamps to the nearest millisecond in either
    // direction - e.g. 10ms & 11.999ms may both round to 11ms - so we need to sleep at least 2ms to
    // avoid test failures. To be safe, we sleep 3ms.
    sleep_milliseconds(3);

    // Following cases test for scenarios that measure names are tied twice.
    mark_names.forEach(function(name) {
        context.mark(name);
    });
    measures_for_timing_order.forEach(context.initialMeasures, context);

    test_greater_than(context.getEntriesByName('nav2now', 'measure')[1].duration, context.getEntriesByName('nav2now', 'measure')[0].duration, 'Second measure of current mark to navigationStart should be negative value.');
    test_equals(context.getEntriesByName('loadTime', 'measure')[0].duration, context.getEntriesByName('loadTime', 'measure')[1].duration, 'Measures of loadTime should have same duration.');
    test_greater_than(context.getEntriesByName('domComplete2a', 'measure')[1].duration, context.getEntriesByName('domComplete2a', 'measure')[0].duration, 'Measure from domComplete event to most recent mark "a" should have longer duration.');
    test_greater_than(context.getEntriesByName('negativeValue', 'measure')[0].duration, context.getEntriesByName('negativeValue', 'measure')[1].duration, 'Measure from most recent mark to navigationStart should have longer duration.');

    done();
}
</script>
</head>
<body onload="setTimeout(onload_test,0)">
    <h1>Description</h1>
    <p>This test validates functionality of the interface window.performance.measure using keywords from the Navigation Timing spec.</p>
    <div id="log"></div>
</body>
</html>
