webperftestharness.js (6240B)
1 /* 2 author: W3C http://www.w3.org/ 3 help: http://www.w3.org/TR/navigation-timing/#sec-window.performance-attribute 4 */ 5 // 6 // Helper Functions for ResourceTiming W3C tests 7 // 8 9 var performanceNamespace = window.performance; 10 var timingAttributes = [ 11 'connectEnd', 12 'connectStart', 13 'domComplete', 14 'domContentLoadedEventEnd', 15 'domContentLoadedEventStart', 16 'domInteractive', 17 'domLoading', 18 'domainLookupEnd', 19 'domainLookupStart', 20 'fetchStart', 21 'loadEventEnd', 22 'loadEventStart', 23 'navigationStart', 24 'redirectEnd', 25 'redirectStart', 26 'requestStart', 27 'responseEnd', 28 'responseStart', 29 'unloadEventEnd', 30 'unloadEventStart' 31 ]; 32 33 var namespace_check = false; 34 35 // 36 // All test() functions in the WebPerf test suite should use wp_test() instead. 37 // 38 // wp_test() validates the window.performance namespace exists prior to running tests and 39 // immediately shows a single failure if it does not. 40 // 41 42 function wp_test(func, msg, properties) 43 { 44 // only run the namespace check once 45 if (!namespace_check) 46 { 47 namespace_check = true; 48 49 if (performanceNamespace === undefined || performanceNamespace == null) 50 { 51 // show a single error that window.performance is undefined 52 // The window.performance attribute provides a hosting area for performance related attributes. 53 test(function() { assert_true(performanceNamespace !== undefined && performanceNamespace != null, "window.performance is defined and not null"); }, "window.performance is defined and not null."); 54 } 55 } 56 57 test(func, msg, properties); 58 } 59 60 function test_namespace(child_name, skip_root) 61 { 62 if (skip_root === undefined) { 63 var msg = 'window.performance is defined'; 64 // The window.performance attribute provides a hosting area for performance related attributes. 65 wp_test(function () { assert_not_equals(performanceNamespace, undefined, msg); }, msg); 66 } 67 68 if (child_name !== undefined) { 69 var msg2 = 'window.performance.' + child_name + ' is defined'; 70 // The window.performance attribute provides a hosting area for performance related attributes. 71 wp_test(function() { assert_not_equals(performanceNamespace[child_name], undefined, msg2); }, msg2); 72 } 73 } 74 75 function test_attribute_exists(parent_name, attribute_name, properties) 76 { 77 var msg = 'window.performance.' + parent_name + '.' + attribute_name + ' is defined.'; 78 wp_test(function() { assert_not_equals(performanceNamespace[parent_name][attribute_name], undefined, msg); }, msg, properties); 79 } 80 81 function test_enum(parent_name, enum_name, value, properties) 82 { 83 var msg = 'window.performance.' + parent_name + '.' + enum_name + ' is defined.'; 84 wp_test(function() { assert_not_equals(performanceNamespace[parent_name][enum_name], undefined, msg); }, msg, properties); 85 86 msg = 'window.performance.' + parent_name + '.' + enum_name + ' = ' + value; 87 wp_test(function() { assert_equals(performanceNamespace[parent_name][enum_name], value, msg); }, msg, properties); 88 } 89 90 function test_timing_order(attribute_name, greater_than_attribute, properties) 91 { 92 // ensure it's not 0 first 93 var msg = "window.performance.timing." + attribute_name + " > 0"; 94 wp_test(function() { assert_true(performanceNamespace.timing[attribute_name] > 0, msg); }, msg, properties); 95 96 // ensure it's in the right order 97 msg = "window.performance.timing." + attribute_name + " >= window.performance.timing." + greater_than_attribute; 98 wp_test(function() { assert_true(performanceNamespace.timing[attribute_name] >= performanceNamespace.timing[greater_than_attribute], msg); }, msg, properties); 99 } 100 101 function test_timing_greater_than(attribute_name, greater_than, properties) 102 { 103 var msg = "window.performance.timing." + attribute_name + " > " + greater_than; 104 test_greater_than(performanceNamespace.timing[attribute_name], greater_than, msg, properties); 105 } 106 107 function test_timing_equals(attribute_name, equals, msg, properties) 108 { 109 var test_msg = msg || "window.performance.timing." + attribute_name + " == " + equals; 110 test_equals(performanceNamespace.timing[attribute_name], equals, test_msg, properties); 111 } 112 113 // 114 // Non-test related helper functions 115 // 116 117 function sleep_milliseconds(n) 118 { 119 var start = new Date().getTime(); 120 while (true) { 121 if ((new Date().getTime() - start) >= n) break; 122 } 123 } 124 125 // 126 // Common helper functions 127 // 128 129 function test_true(value, msg, properties) 130 { 131 wp_test(function () { assert_true(value, msg); }, msg, properties); 132 } 133 134 function test_equals(value, equals, msg, properties) 135 { 136 wp_test(function () { assert_equals(value, equals, msg); }, msg, properties); 137 } 138 139 function test_greater_than(value, greater_than, msg, properties) 140 { 141 wp_test(function () { assert_true(value > greater_than, msg); }, msg, properties); 142 } 143 144 function test_greater_or_equals(value, greater_than, msg, properties) 145 { 146 wp_test(function () { assert_true(value >= greater_than, msg); }, msg, properties); 147 } 148 149 function test_not_equals(value, notequals, msg, properties) 150 { 151 wp_test(function() { assert_not_equals(value, notequals, msg); }, msg, properties); 152 } 153 154 function test_tao_pass(entry) { 155 test_greater_than(entry.redirectStart, 0, 'redirectStart > 0 in cross-origin redirect with Timing-Allow-Origin.'); 156 test_greater_than(entry.redirectEnd, 0, 'redirectEnd > 0 in cross-origin redirect with Timing-Allow-Origin.'); 157 test_greater_than(entry.fetchStart, 0, 'fetchStart > 0 in cross-origin redirect with Timing-Allow-Origin.'); 158 test_greater_than(entry.fetchStart, entry.startTime, 'startTime < fetchStart in cross-origin redirect with Timing-Allow-Origin.'); 159 } 160 161 function test_tao_fail(entry) { 162 test_equals(entry.redirectStart, 0, 'redirectStart == 0 in cross-origin redirect with failing Timing-Allow-Origin.'); 163 test_equals(entry.redirectEnd, 0, 'redirectEnd == 0 in cross-origin redirect with failing Timing-Allow-Origin.'); 164 test_greater_than(entry.fetchStart, 0, 'fetchStart > 0 in cross-origin redirect with failing Timing-Allow-Origin.'); 165 test_equals(entry.fetchStart, entry.startTime, 'startTime == fetchStart in cross-origin redirect with failing Timing-Allow-Origin.'); 166 }