tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

webperftestharness.js (4062B)


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