webperftestharness.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. [1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license
  6. [2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license
  7. [3] http://www.w3.org/2004/10/27-testcases
  8. */
  9. //
  10. // Helper Functions for NavigationTiming W3C tests
  11. //
  12. var performanceNamespace = window.performance;
  13. var timingAttributes = [
  14. 'connectEnd',
  15. 'connectStart',
  16. 'domComplete',
  17. 'domContentLoadedEventEnd',
  18. 'domContentLoadedEventStart',
  19. 'domInteractive',
  20. 'domLoading',
  21. 'domainLookupEnd',
  22. 'domainLookupStart',
  23. 'fetchStart',
  24. 'loadEventEnd',
  25. 'loadEventStart',
  26. 'navigationStart',
  27. 'redirectEnd',
  28. 'redirectStart',
  29. 'requestStart',
  30. 'responseEnd',
  31. 'responseStart',
  32. 'unloadEventEnd',
  33. 'unloadEventStart'
  34. ];
  35. var namespace_check = false;
  36. //
  37. // All test() functions in the WebPerf test suite should use wp_test() instead.
  38. //
  39. // wp_test() validates the window.performance namespace exists prior to running tests and
  40. // immediately shows a single failure if it does not.
  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. if (performanceNamespace === undefined || performanceNamespace == null)
  49. {
  50. // show a single error that window.performance is undefined
  51. 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. "});
  52. }
  53. }
  54. test(func, msg, properties);
  55. }
  56. function test_namespace(child_name, skip_root)
  57. {
  58. if (skip_root === undefined) {
  59. var msg = 'window.performance is defined';
  60. wp_test(function () { assert_true(performanceNamespace !== undefined, msg); }, msg,{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. "});
  61. }
  62. if (child_name !== undefined) {
  63. var msg2 = 'window.performance.' + child_name + ' is defined';
  64. wp_test(function() { assert_true(performanceNamespace[child_name] !== undefined, msg2); }, msg2,{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. "});
  65. }
  66. }
  67. function test_attribute_exists(parent_name, attribute_name, properties)
  68. {
  69. var msg = 'window.performance.' + parent_name + '.' + attribute_name + ' is defined.';
  70. wp_test(function() { assert_true(performanceNamespace[parent_name][attribute_name] !== undefined, msg); }, msg, properties);
  71. }
  72. function test_enum(parent_name, enum_name, value, properties)
  73. {
  74. var msg = 'window.performance.' + parent_name + '.' + enum_name + ' is defined.';
  75. wp_test(function() { assert_true(performanceNamespace[parent_name][enum_name] !== undefined, msg); }, msg, properties);
  76. msg = 'window.performance.' + parent_name + '.' + enum_name + ' = ' + value;
  77. wp_test(function() { assert_equals(performanceNamespace[parent_name][enum_name], value, msg); }, msg, properties);
  78. }
  79. function test_timing_order(attribute_name, greater_than_attribute, properties)
  80. {
  81. // ensure it's not 0 first
  82. var msg = "window.performance.timing." + attribute_name + " > 0";
  83. wp_test(function() { assert_true(performanceNamespace.timing[attribute_name] > 0, msg); }, msg, properties);
  84. // ensure it's in the right order
  85. msg = "window.performance.timing." + attribute_name + " >= window.performance.timing." + greater_than_attribute;
  86. wp_test(function() { assert_true(performanceNamespace.timing[attribute_name] >= performanceNamespace.timing[greater_than_attribute], msg); }, msg, properties);
  87. }
  88. function test_timing_greater_than(attribute_name, greater_than, properties)
  89. {
  90. var msg = "window.performance.timing." + attribute_name + " > " + greater_than;
  91. test_greater_than(performanceNamespace.timing[attribute_name], greater_than, msg, properties);
  92. }
  93. function test_timing_equals(attribute_name, equals, msg, properties)
  94. {
  95. var test_msg = msg || "window.performance.timing." + attribute_name + " == " + equals;
  96. test_equals(performanceNamespace.timing[attribute_name], equals, test_msg, properties);
  97. }
  98. //
  99. // Non-test related helper functions
  100. //
  101. function sleep_milliseconds(n)
  102. {
  103. var start = new Date().getTime();
  104. while (true) {
  105. if ((new Date().getTime() - start) >= n) break;
  106. }
  107. }
  108. //
  109. // Common helper functions
  110. //
  111. function test_true(value, msg, properties)
  112. {
  113. wp_test(function () { assert_true(value, msg); }, msg, properties);
  114. }
  115. function test_equals(value, equals, msg, properties)
  116. {
  117. wp_test(function () { assert_equals(value, equals, msg); }, msg, properties);
  118. }
  119. function test_greater_than(value, greater_than, msg, properties)
  120. {
  121. wp_test(function () { assert_true(value > greater_than, msg); }, msg, properties);
  122. }
  123. function test_greater_or_equals(value, greater_than, msg, properties)
  124. {
  125. wp_test(function () { assert_true(value >= greater_than, msg); }, msg, properties);
  126. }
  127. function test_not_equals(value, notequals, msg, properties)
  128. {
  129. wp_test(function() { assert_true(value !== notequals, msg); }, msg, properties);
  130. }