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