11cb0ef41Sopenharmony_ci// 21cb0ef41Sopenharmony_ci// Helper functions for User Timing tests 31cb0ef41Sopenharmony_ci// 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_civar mark_names = [ 61cb0ef41Sopenharmony_ci '', 71cb0ef41Sopenharmony_ci '1', 81cb0ef41Sopenharmony_ci 'abc', 91cb0ef41Sopenharmony_ci]; 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_civar measures = [ 121cb0ef41Sopenharmony_ci [''], 131cb0ef41Sopenharmony_ci ['2', 1], 141cb0ef41Sopenharmony_ci ['aaa', 'navigationStart', ''], 151cb0ef41Sopenharmony_ci]; 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_cifunction test_method_exists(method, method_name, properties) 181cb0ef41Sopenharmony_ci{ 191cb0ef41Sopenharmony_ci var msg; 201cb0ef41Sopenharmony_ci if (typeof method === 'function') 211cb0ef41Sopenharmony_ci msg = 'performance.' + method.name + ' is supported!'; 221cb0ef41Sopenharmony_ci else 231cb0ef41Sopenharmony_ci msg = 'performance.' + method_name + ' is supported!'; 241cb0ef41Sopenharmony_ci wp_test(function() { assert_equals(typeof method, 'function', msg); }, msg, properties); 251cb0ef41Sopenharmony_ci} 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_cifunction test_method_throw_exception(func_str, exception, msg) 281cb0ef41Sopenharmony_ci{ 291cb0ef41Sopenharmony_ci let exception_name; 301cb0ef41Sopenharmony_ci let test_func; 311cb0ef41Sopenharmony_ci if (typeof exception == "function") { 321cb0ef41Sopenharmony_ci exception_name = exception.name; 331cb0ef41Sopenharmony_ci test_func = assert_throws_js; 341cb0ef41Sopenharmony_ci } else { 351cb0ef41Sopenharmony_ci exception_name = exception; 361cb0ef41Sopenharmony_ci test_func = assert_throws_dom; 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci var msg = 'Invocation of ' + func_str + ' should throw ' + exception_name + ' Exception.'; 391cb0ef41Sopenharmony_ci wp_test(function() { test_func(exception, function() {eval(func_str)}, msg); }, msg); 401cb0ef41Sopenharmony_ci} 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_cifunction test_noless_than(value, greater_than, msg, properties) 431cb0ef41Sopenharmony_ci{ 441cb0ef41Sopenharmony_ci wp_test(function () { assert_true(value >= greater_than, msg); }, msg, properties); 451cb0ef41Sopenharmony_ci} 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_cifunction test_fail(msg, properties) 481cb0ef41Sopenharmony_ci{ 491cb0ef41Sopenharmony_ci wp_test(function() { assert_unreached(); }, msg, properties); 501cb0ef41Sopenharmony_ci} 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_cifunction test_resource_entries(entries, expected_entries) 531cb0ef41Sopenharmony_ci{ 541cb0ef41Sopenharmony_ci // This is slightly convoluted so that we can sort the output. 551cb0ef41Sopenharmony_ci var actual_entries = {}; 561cb0ef41Sopenharmony_ci var origin = window.location.protocol + "//" + window.location.host; 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci for (var i = 0; i < entries.length; ++i) { 591cb0ef41Sopenharmony_ci var entry = entries[i]; 601cb0ef41Sopenharmony_ci var found = false; 611cb0ef41Sopenharmony_ci for (var expected_entry in expected_entries) { 621cb0ef41Sopenharmony_ci if (entry.name == origin + expected_entry) { 631cb0ef41Sopenharmony_ci found = true; 641cb0ef41Sopenharmony_ci if (expected_entry in actual_entries) { 651cb0ef41Sopenharmony_ci test_fail(expected_entry + ' is not expected to have duplicate entries'); 661cb0ef41Sopenharmony_ci } 671cb0ef41Sopenharmony_ci actual_entries[expected_entry] = entry; 681cb0ef41Sopenharmony_ci break; 691cb0ef41Sopenharmony_ci } 701cb0ef41Sopenharmony_ci } 711cb0ef41Sopenharmony_ci if (!found) { 721cb0ef41Sopenharmony_ci test_fail(entries[i].name + ' is not expected to be in the Resource Timing buffer'); 731cb0ef41Sopenharmony_ci } 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci sorted_urls = []; 771cb0ef41Sopenharmony_ci for (var i in actual_entries) { 781cb0ef41Sopenharmony_ci sorted_urls.push(i); 791cb0ef41Sopenharmony_ci } 801cb0ef41Sopenharmony_ci sorted_urls.sort(); 811cb0ef41Sopenharmony_ci for (var i in sorted_urls) { 821cb0ef41Sopenharmony_ci var url = sorted_urls[i]; 831cb0ef41Sopenharmony_ci test_equals(actual_entries[url].initiatorType, 841cb0ef41Sopenharmony_ci expected_entries[url], 851cb0ef41Sopenharmony_ci origin + url + ' is expected to have initiatorType ' + expected_entries[url]); 861cb0ef41Sopenharmony_ci } 871cb0ef41Sopenharmony_ci for (var j in expected_entries) { 881cb0ef41Sopenharmony_ci if (!(j in actual_entries)) { 891cb0ef41Sopenharmony_ci test_fail(origin + j + ' is expected to be in the Resource Timing buffer'); 901cb0ef41Sopenharmony_ci } 911cb0ef41Sopenharmony_ci } 921cb0ef41Sopenharmony_ci} 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_cifunction performance_entrylist_checker(type) 951cb0ef41Sopenharmony_ci{ 961cb0ef41Sopenharmony_ci const entryType = type; 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ci function entry_check(entry, expectedNames, testDescription = '') 991cb0ef41Sopenharmony_ci { 1001cb0ef41Sopenharmony_ci const msg = testDescription + 'Entry \"' + entry.name + '\" should be one that we have set.'; 1011cb0ef41Sopenharmony_ci wp_test(function() { assert_in_array(entry.name, expectedNames, msg); }, msg); 1021cb0ef41Sopenharmony_ci test_equals(entry.entryType, entryType, testDescription + 'entryType should be \"' + entryType + '\".'); 1031cb0ef41Sopenharmony_ci if (type === "measure") { 1041cb0ef41Sopenharmony_ci test_true(isFinite(entry.startTime), testDescription + 'startTime should be a number.'); 1051cb0ef41Sopenharmony_ci test_true(isFinite(entry.duration), testDescription + 'duration should be a number.'); 1061cb0ef41Sopenharmony_ci } else if (type === "mark") { 1071cb0ef41Sopenharmony_ci test_greater_than(entry.startTime, 0, testDescription + 'startTime should greater than 0.'); 1081cb0ef41Sopenharmony_ci test_equals(entry.duration, 0, testDescription + 'duration of mark should be 0.'); 1091cb0ef41Sopenharmony_ci } 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci function entrylist_order_check(entryList) 1131cb0ef41Sopenharmony_ci { 1141cb0ef41Sopenharmony_ci let inOrder = true; 1151cb0ef41Sopenharmony_ci for (let i = 0; i < entryList.length - 1; ++i) 1161cb0ef41Sopenharmony_ci { 1171cb0ef41Sopenharmony_ci if (entryList[i + 1].startTime < entryList[i].startTime) { 1181cb0ef41Sopenharmony_ci inOrder = false; 1191cb0ef41Sopenharmony_ci break; 1201cb0ef41Sopenharmony_ci } 1211cb0ef41Sopenharmony_ci } 1221cb0ef41Sopenharmony_ci return inOrder; 1231cb0ef41Sopenharmony_ci } 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci function entrylist_check(entryList, expectedLength, expectedNames, testDescription = '') 1261cb0ef41Sopenharmony_ci { 1271cb0ef41Sopenharmony_ci test_equals(entryList.length, expectedLength, testDescription + 'There should be ' + expectedLength + ' entries.'); 1281cb0ef41Sopenharmony_ci test_true(entrylist_order_check(entryList), testDescription + 'Entries in entrylist should be in order.'); 1291cb0ef41Sopenharmony_ci for (let i = 0; i < entryList.length; ++i) 1301cb0ef41Sopenharmony_ci { 1311cb0ef41Sopenharmony_ci entry_check(entryList[i], expectedNames, testDescription + 'Entry_list ' + i + '. '); 1321cb0ef41Sopenharmony_ci } 1331cb0ef41Sopenharmony_ci } 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci return{"entrylist_check":entrylist_check}; 1361cb0ef41Sopenharmony_ci} 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_cifunction PerformanceContext(context) 1391cb0ef41Sopenharmony_ci{ 1401cb0ef41Sopenharmony_ci this.performanceContext = context; 1411cb0ef41Sopenharmony_ci} 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ciPerformanceContext.prototype = 1441cb0ef41Sopenharmony_ci{ 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci initialMeasures: function(item, index, array) 1471cb0ef41Sopenharmony_ci { 1481cb0ef41Sopenharmony_ci this.performanceContext.measure.apply(this.performanceContext, item); 1491cb0ef41Sopenharmony_ci }, 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ci mark: function() 1521cb0ef41Sopenharmony_ci { 1531cb0ef41Sopenharmony_ci this.performanceContext.mark.apply(this.performanceContext, arguments); 1541cb0ef41Sopenharmony_ci }, 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ci measure: function() 1571cb0ef41Sopenharmony_ci { 1581cb0ef41Sopenharmony_ci this.performanceContext.measure.apply(this.performanceContext, arguments); 1591cb0ef41Sopenharmony_ci }, 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci clearMarks: function() 1621cb0ef41Sopenharmony_ci { 1631cb0ef41Sopenharmony_ci this.performanceContext.clearMarks.apply(this.performanceContext, arguments); 1641cb0ef41Sopenharmony_ci }, 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ci clearMeasures: function() 1671cb0ef41Sopenharmony_ci { 1681cb0ef41Sopenharmony_ci this.performanceContext.clearMeasures.apply(this.performanceContext, arguments); 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ci }, 1711cb0ef41Sopenharmony_ci 1721cb0ef41Sopenharmony_ci getEntries: function() 1731cb0ef41Sopenharmony_ci { 1741cb0ef41Sopenharmony_ci return this.performanceContext.getEntries.apply(this.performanceContext, arguments); 1751cb0ef41Sopenharmony_ci }, 1761cb0ef41Sopenharmony_ci 1771cb0ef41Sopenharmony_ci getEntriesByType: function() 1781cb0ef41Sopenharmony_ci { 1791cb0ef41Sopenharmony_ci return this.performanceContext.getEntriesByType.apply(this.performanceContext, arguments); 1801cb0ef41Sopenharmony_ci }, 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ci getEntriesByName: function() 1831cb0ef41Sopenharmony_ci { 1841cb0ef41Sopenharmony_ci return this.performanceContext.getEntriesByName.apply(this.performanceContext, arguments); 1851cb0ef41Sopenharmony_ci }, 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_ci setResourceTimingBufferSize: function() 1881cb0ef41Sopenharmony_ci { 1891cb0ef41Sopenharmony_ci return this.performanceContext.setResourceTimingBufferSize.apply(this.performanceContext, arguments); 1901cb0ef41Sopenharmony_ci }, 1911cb0ef41Sopenharmony_ci 1921cb0ef41Sopenharmony_ci registerResourceTimingBufferFullCallback: function(func) 1931cb0ef41Sopenharmony_ci { 1941cb0ef41Sopenharmony_ci this.performanceContext.onresourcetimingbufferfull = func; 1951cb0ef41Sopenharmony_ci }, 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci clearResourceTimings: function() 1981cb0ef41Sopenharmony_ci { 1991cb0ef41Sopenharmony_ci this.performanceContext.clearResourceTimings.apply(this.performanceContext, arguments); 2001cb0ef41Sopenharmony_ci } 2011cb0ef41Sopenharmony_ci 2021cb0ef41Sopenharmony_ci}; 203