webperftestharnessextension.js (5710B)
1 // 2 // Helper functions for Resource Timing tests 3 // 4 5 var mark_names = [ 6 '', 7 '1', 8 'abc', 9 ]; 10 11 var measures = [ 12 [''], 13 ['2', 1], 14 ['aaa', 'navigationStart', ''], 15 ]; 16 17 function test_method_exists(method, method_name, properties) 18 { 19 var msg; 20 if (typeof method === 'function') 21 msg = 'performance.' + method.name + ' is supported!'; 22 else 23 msg = 'performance.' + method_name + ' is supported!'; 24 wp_test(function() { assert_equals(typeof method, 'function', msg); }, msg, properties); 25 } 26 27 function test_noless_than(value, greater_than, msg, properties) 28 { 29 wp_test(function () { assert_true(value >= greater_than, msg); }, msg, properties); 30 } 31 32 function test_fail(msg, properties) 33 { 34 wp_test(function() { assert_unreached(); }, msg, properties); 35 } 36 37 function test_resource_entries(entries, expected_entries) 38 { 39 test(function() { 40 // This is slightly convoluted so that we can sort the output. 41 var actual_entries = {}; 42 var origin = window.location.protocol + "//" + window.location.host; 43 44 for (var i = 0; i < entries.length; ++i) { 45 var entry = entries[i]; 46 var found = false; 47 for (var expected_entry in expected_entries) { 48 if (entry.name == origin + expected_entry) { 49 found = true; 50 if (expected_entry in actual_entries) { 51 assert_unreached(expected_entry + ' is not expected to have duplicate entries'); 52 } 53 actual_entries[expected_entry] = entry; 54 break; 55 } 56 } 57 if (!found) { 58 assert_unreached(entries[i].name + ' is not expected to be in the Resource Timing buffer'); 59 } 60 } 61 62 sorted_urls = []; 63 for (var i in actual_entries) { 64 sorted_urls.push(i); 65 } 66 sorted_urls.sort(); 67 for (var i in sorted_urls) { 68 var url = sorted_urls[i]; 69 assert_equals(actual_entries[url].initiatorType, 70 expected_entries[url], 71 origin + url + ' is expected to have initiatorType ' + expected_entries[url]); 72 } 73 for (var j in expected_entries) { 74 if (!(j in actual_entries)) { 75 assert_unreached(origin + j + ' is expected to be in the Resource Timing buffer'); 76 } 77 } 78 }, "Testing resource entries"); 79 } 80 81 function performance_entrylist_checker(type) 82 { 83 var entryType = type; 84 85 function entry_check(entry, expectedNames) 86 { 87 var msg = 'Entry \"' + entry.name + '\" should be one that we have set.'; 88 wp_test(function() { assert_in_array(entry.name, expectedNames, msg); }, msg); 89 test_equals(entry.entryType, entryType, 'entryType should be \"' + entryType + '\".'); 90 if (type === "measure") { 91 test_true(isFinite(entry.startTime), 'startTime should be a number.'); 92 test_true(isFinite(entry.duration), 'duration should be a number.'); 93 } else if (type === "mark") { 94 test_greater_than(entry.startTime, 0, 'startTime should greater than 0.'); 95 test_equals(entry.duration, 0, 'duration of mark should be 0.'); 96 } 97 } 98 99 function entrylist_order_check(entryList) 100 { 101 var inOrder = true; 102 for (var i = 0; i < entryList.length - 1; ++i) 103 { 104 if (entryList[i + 1].startTime < entryList[i].startTime) { 105 inOrder = false; 106 break; 107 } 108 } 109 return inOrder; 110 } 111 112 function entrylist_check(entryList, expectedLength, expectedNames) 113 { 114 test_equals(entryList.length, expectedLength, 'There should be ' + expectedLength + ' entries.'); 115 test_true(entrylist_order_check(entryList), 'Entries in entrylist should be in order.'); 116 for (var i = 0; i < entryList.length; ++i) 117 { 118 entry_check(entryList[i], expectedNames); 119 } 120 } 121 122 return{"entrylist_check":entrylist_check}; 123 } 124 125 function PerformanceContext(context) 126 { 127 this.performanceContext = context; 128 } 129 130 PerformanceContext.prototype = { 131 initialMeasures: function(item, index, array) 132 { 133 this.performanceContext.measure.apply(this.performanceContext, item); 134 }, 135 136 mark: function() 137 { 138 this.performanceContext.mark.apply(this.performanceContext, arguments); 139 }, 140 141 measure: function() 142 { 143 this.performanceContext.measure.apply(this.performanceContext, arguments); 144 }, 145 146 clearMarks: function() 147 { 148 this.performanceContext.clearMarks.apply(this.performanceContext, arguments); 149 150 }, 151 152 clearMeasures: function() 153 { 154 this.performanceContext.clearMeasures.apply(this.performanceContext, arguments); 155 156 }, 157 158 getEntries: function() 159 { 160 return this.performanceContext.getEntries.apply(this.performanceContext, arguments); 161 }, 162 163 getEntriesByType: function() 164 { 165 return this.performanceContext.getEntriesByType.apply(this.performanceContext, arguments); 166 }, 167 168 getEntriesByName: function() 169 { 170 return this.performanceContext.getEntriesByName.apply(this.performanceContext, arguments); 171 }, 172 173 setResourceTimingBufferSize: function() 174 { 175 return this.performanceContext.setResourceTimingBufferSize.apply(this.performanceContext, arguments); 176 }, 177 178 registerResourceTimingBufferFullCallback: function(func) 179 { 180 this.performanceContext.onresourcetimingbufferfull = func; 181 }, 182 183 clearResourceTimings: function() 184 { 185 this.performanceContext.clearResourceTimings.apply(this.performanceContext, arguments); 186 } 187 188 };