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