OLD | NEW |
(Empty) | |
| 1 /* |
| 2 Distributed under both the W3C Test Suite License [1] and the W3C |
| 3 3-clause BSD License [2]. To contribute to a W3C Test Suite, see the |
| 4 policies and contribution forms [3]. |
| 5 |
| 6 [1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license |
| 7 [2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license |
| 8 [3] http://www.w3.org/2004/10/27-testcases |
| 9 */ |
| 10 |
| 11 // |
| 12 // Helper Functions for NavigationTiming W3C tests |
| 13 // |
| 14 |
| 15 var performanceNamespace = window.performance; |
| 16 var timingAttributes = [ |
| 17 'connectEnd', |
| 18 'connectStart', |
| 19 'domComplete', |
| 20 'domContentLoadedEventEnd', |
| 21 'domContentLoadedEventStart', |
| 22 'domInteractive', |
| 23 'domLoading', |
| 24 'domainLookupEnd', |
| 25 'domainLookupStart', |
| 26 'fetchStart', |
| 27 'loadEventEnd', |
| 28 'loadEventStart', |
| 29 'navigationStart', |
| 30 'redirectEnd', |
| 31 'redirectStart', |
| 32 'requestStart', |
| 33 'responseEnd', |
| 34 'responseStart', |
| 35 'unloadEventEnd', |
| 36 'unloadEventStart' |
| 37 ]; |
| 38 |
| 39 var namespace_check = false; |
| 40 |
| 41 // |
| 42 // All test() functions in the WebPerf test suite should use wp_test() instead. |
| 43 // |
| 44 // wp_test() validates the window.performance namespace exists prior to running
tests and |
| 45 // immediately shows a single failure if it does not. |
| 46 // |
| 47 |
| 48 function wp_test(func, msg, properties) |
| 49 { |
| 50 // only run the namespace check once |
| 51 if (!namespace_check) |
| 52 { |
| 53 namespace_check = true; |
| 54 |
| 55 if (performanceNamespace === undefined || performanceNamespace == null) |
| 56 { |
| 57 // show a single error that window.performance is undefined |
| 58 test(function() { assert_true(performanceNamespace !== undefined &&
performanceNamespace != null, "window.performance is defined and not null"); },
"window.performance is defined and not null.", {author:"W3C http://www.w3.org/",
help:"http://www.w3.org/TR/navigation-timing/#sec-window.performance-attribute",
assert:"The window.performance attribute provides a hosting area for performance
related attributes. "}); |
| 59 } |
| 60 } |
| 61 |
| 62 test(func, msg, properties); |
| 63 } |
| 64 |
| 65 function test_namespace(child_name, skip_root) |
| 66 { |
| 67 if (skip_root === undefined) { |
| 68 var msg = 'window.performance is defined'; |
| 69 wp_test(function () { assert_true(performanceNamespace !== undefined, ms
g); }, msg,{author:"W3C http://www.w3.org/",help:"http://www.w3.org/TR/navigatio
n-timing/#sec-window.performance-attribute",assert:"The window.performance attri
bute provides a hosting area for performance related attributes. "}); |
| 70 } |
| 71 |
| 72 if (child_name !== undefined) { |
| 73 var msg2 = 'window.performance.' + child_name + ' is defined'; |
| 74 wp_test(function() { assert_true(performanceNamespace[child_name] !== un
defined, msg2); }, msg2,{author:"W3C http://www.w3.org/",help:"http://www.w3.org
/TR/navigation-timing/#sec-window.performance-attribute",assert:"The window.perf
ormance attribute provides a hosting area for performance related attributes. "}
); |
| 75 } |
| 76 } |
| 77 |
| 78 function test_attribute_exists(parent_name, attribute_name, properties) |
| 79 { |
| 80 var msg = 'window.performance.' + parent_name + '.' + attribute_name + ' is
defined.'; |
| 81 wp_test(function() { assert_true(performanceNamespace[parent_name][attribute
_name] !== undefined, msg); }, msg, properties); |
| 82 } |
| 83 |
| 84 function test_enum(parent_name, enum_name, value, properties) |
| 85 { |
| 86 var msg = 'window.performance.' + parent_name + '.' + enum_name + ' is defin
ed.'; |
| 87 wp_test(function() { assert_true(performanceNamespace[parent_name][enum_name
] !== undefined, msg); }, msg, properties); |
| 88 |
| 89 msg = 'window.performance.' + parent_name + '.' + enum_name + ' = ' + value; |
| 90 wp_test(function() { assert_equals(performanceNamespace[parent_name][enum_na
me], value, msg); }, msg, properties); |
| 91 } |
| 92 |
| 93 function test_timing_order(attribute_name, greater_than_attribute, properties) |
| 94 { |
| 95 // ensure it's not 0 first |
| 96 var msg = "window.performance.timing." + attribute_name + " > 0"; |
| 97 wp_test(function() { assert_true(performanceNamespace.timing[attribute_name]
> 0, msg); }, msg, properties); |
| 98 |
| 99 // ensure it's in the right order |
| 100 msg = "window.performance.timing." + attribute_name + " >= window.performanc
e.timing." + greater_than_attribute; |
| 101 wp_test(function() { assert_true(performanceNamespace.timing[attribute_name]
>= performanceNamespace.timing[greater_than_attribute], msg); }, msg, propertie
s); |
| 102 } |
| 103 |
| 104 function test_timing_greater_than(attribute_name, greater_than, properties) |
| 105 { |
| 106 var msg = "window.performance.timing." + attribute_name + " > " + greater_th
an; |
| 107 test_greater_than(performanceNamespace.timing[attribute_name], greater_than,
msg, properties); |
| 108 } |
| 109 |
| 110 function test_timing_equals(attribute_name, equals, msg, properties) |
| 111 { |
| 112 var test_msg = msg || "window.performance.timing." + attribute_name + " == "
+ equals; |
| 113 test_equals(performanceNamespace.timing[attribute_name], equals, test_msg, p
roperties); |
| 114 } |
| 115 |
| 116 // |
| 117 // Non-test related helper functions |
| 118 // |
| 119 |
| 120 function sleep_milliseconds(n) |
| 121 { |
| 122 var start = new Date().getTime(); |
| 123 while (true) { |
| 124 if ((new Date().getTime() - start) >= n) break; |
| 125 } |
| 126 } |
| 127 |
| 128 // |
| 129 // Common helper functions |
| 130 // |
| 131 |
| 132 function test_true(value, msg, properties) |
| 133 { |
| 134 wp_test(function () { assert_true(value, msg); }, msg, properties); |
| 135 } |
| 136 |
| 137 function test_equals(value, equals, msg, properties) |
| 138 { |
| 139 wp_test(function () { assert_equals(value, equals, msg); }, msg, properties)
; |
| 140 } |
| 141 |
| 142 function test_greater_than(value, greater_than, msg, properties) |
| 143 { |
| 144 wp_test(function () { assert_true(value > greater_than, msg); }, msg, proper
ties); |
| 145 } |
| 146 |
| 147 function test_greater_or_equals(value, greater_than, msg, properties) |
| 148 { |
| 149 wp_test(function () { assert_true(value >= greater_than, msg); }, msg, prope
rties); |
| 150 } |
| 151 |
| 152 function test_not_equals(value, notequals, msg, properties) |
| 153 { |
| 154 wp_test(function() { assert_true(value !== notequals, msg); }, msg, properti
es); |
| 155 } |
OLD | NEW |