webperftestharness.js (4123B)
1 // 2 // Helper functions for User Timing tests 3 // 4 5 var timingAttributes = [ 6 "navigationStart", 7 "unloadEventStart", 8 "unloadEventEnd", 9 "redirectStart", 10 "redirectEnd", 11 "fetchStart", 12 "domainLookupStart", 13 "domainLookupEnd", 14 "connectStart", 15 "connectEnd", 16 "secureConnectionStart", 17 "requestStart", 18 "responseStart", 19 "responseEnd", 20 "domLoading", 21 "domInteractive", 22 "domContentLoadedEventStart", 23 "domContentLoadedEventEnd", 24 "domComplete", 25 "loadEventStart", 26 "loadEventEnd" 27 ]; 28 29 function has_required_interfaces() 30 { 31 if (window.performance.mark == undefined || 32 window.performance.clearMarks == undefined || 33 window.performance.measure == undefined || 34 window.performance.clearMeasures == undefined || 35 window.performance.getEntriesByName == undefined || 36 window.performance.getEntriesByType == undefined || 37 window.performance.getEntries == undefined) { 38 return false; 39 } 40 41 return true; 42 } 43 44 function test_namespace(child_name, skip_root) 45 { 46 if (skip_root === undefined) { 47 var msg = 'window.performance is defined'; 48 wp_test(function () { assert_not_equals(performanceNamespace, undefined, msg); }, msg); 49 } 50 51 if (child_name !== undefined) { 52 var msg2 = 'window.performance.' + child_name + ' is defined'; 53 wp_test(function() { assert_not_equals(performanceNamespace[child_name], undefined, msg2); }, msg2); 54 } 55 } 56 57 function test_attribute_exists(parent_name, attribute_name, properties) 58 { 59 var msg = 'window.performance.' + parent_name + '.' + attribute_name + ' is defined.'; 60 wp_test(function() { assert_not_equals(performanceNamespace[parent_name][attribute_name], undefined, msg); }, msg, properties); 61 } 62 63 function test_enum(parent_name, enum_name, value, properties) 64 { 65 var msg = 'window.performance.' + parent_name + '.' + enum_name + ' is defined.'; 66 wp_test(function() { assert_not_equals(performanceNamespace[parent_name][enum_name], undefined, msg); }, msg, properties); 67 68 msg = 'window.performance.' + parent_name + '.' + enum_name + ' = ' + value; 69 wp_test(function() { assert_equals(performanceNamespace[parent_name][enum_name], value, msg); }, msg, properties); 70 } 71 72 function test_timing_order(attribute_name, greater_than_attribute, properties) 73 { 74 // ensure it's not 0 first 75 var msg = "window.performance.timing." + attribute_name + " > 0"; 76 wp_test(function() { assert_true(performanceNamespace.timing[attribute_name] > 0, msg); }, msg, properties); 77 78 // ensure it's in the right order 79 msg = "window.performance.timing." + attribute_name + " >= window.performance.timing." + greater_than_attribute; 80 wp_test(function() { assert_true(performanceNamespace.timing[attribute_name] >= performanceNamespace.timing[greater_than_attribute], msg); }, msg, properties); 81 } 82 83 function test_timing_greater_than(attribute_name, greater_than, properties) 84 { 85 var msg = "window.performance.timing." + attribute_name + " > " + greater_than; 86 test_greater_than(performanceNamespace.timing[attribute_name], greater_than, msg, properties); 87 } 88 89 function test_timing_equals(attribute_name, equals, msg, properties) 90 { 91 var test_msg = msg || "window.performance.timing." + attribute_name + " == " + equals; 92 test_equals(performanceNamespace.timing[attribute_name], equals, test_msg, properties); 93 } 94 95 // 96 // Non-test related helper functions 97 // 98 99 function sleep_milliseconds(n) 100 { 101 var start = new Date().getTime(); 102 while (true) { 103 if ((new Date().getTime() - start) >= n) break; 104 } 105 } 106 107 // 108 // Common helper functions 109 // 110 111 function test_greater_than(value, greater_than, msg, properties) 112 { 113 wp_test(function () { assert_greater_than(value, greater_than, msg); }, msg, properties); 114 } 115 116 function test_greater_or_equals(value, greater_than, msg, properties) 117 { 118 wp_test(function () { assert_greater_than_equal(value, greater_than, msg); }, msg, properties); 119 } 120 121 function test_not_equals(value, notequals, msg, properties) 122 { 123 wp_test(function() { assert_not_equals(value, notequals, msg); }, msg, properties); 124 }