• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE HTML>
2<html>
3<!--
4Copyright (c) 2012 The Chromium Authors. All rights reserved.
5Use of this source code is governed by a BSD-style license that can be
6found in the LICENSE file.
7-->
8<head>
9<title>SliceGroup tests</title>
10<script src="/src/base.js"></script>
11<script>
12  base.require('unittest');
13  base.require('test_utils');
14  base.require('model.slice_group');
15</script>
16</head>
17<body>
18<script>
19  'use strict';
20
21  var Slice = tracing.model.Slice;
22  var SliceGroup = tracing.model.SliceGroup;
23  var newSlice = test_utils.newSlice;
24  var newSliceNamed = test_utils.newSliceNamed;
25
26  function testBasicBeginEnd() {
27    var group = new SliceGroup();
28    assertEquals(group.openSliceCount, 0);
29    var sliceA = group.beginSlice('', 'a', 1, {a: 1});
30    assertEquals(1, group.openSliceCount);
31    assertEquals('a', sliceA.title);
32    assertEquals(1, sliceA.start);
33    assertEquals(1, sliceA.args.a);
34
35    var sliceB = group.endSlice(3);
36    assertEquals(sliceA, sliceB);
37    assertEquals(2, sliceB.duration);
38  }
39
40  function testNestedBeginEnd() {
41    var group = new SliceGroup();
42    assertEquals(group.openSliceCount, 0);
43    group.beginSlice('', 'a', 1);
44    group.beginSlice('', 'b', 2);
45    group.endSlice(2.5);
46    group.endSlice(3);
47
48    assertEquals(2, group.slices.length);
49    assertEquals('b', group.slices[0].title);
50    assertEquals(0.5, group.slices[0].duration);
51
52    assertEquals('a', group.slices[1].title);
53    assertEquals(2, group.slices[1].duration);
54  }
55
56  function testBasicMerge() {
57    var a = new SliceGroup();
58    var b = new SliceGroup();
59    a.beginSlice('', 'one', 1);
60    a.endSlice(2);
61    b.beginSlice('', 'two', 3);
62    b.endSlice(5);
63
64    var m = SliceGroup.merge(a, b);
65    assertEquals(2, m.slices.length);
66
67    assertEquals('one', m.slices[0].title);
68    assertEquals(1, m.slices[0].start);
69    assertEquals(1, m.slices[0].duration);
70
71    assertEquals('two', m.slices[1].title);
72    assertEquals(3, m.slices[1].start);
73    assertEquals(2, m.slices[1].duration);
74  }
75
76  function testNestedMerge() {
77    var a = new SliceGroup();
78    var b = new SliceGroup();
79    a.beginSlice('', 'one', 1);
80    a.endSlice(4);
81    b.beginSlice('', 'two', 2);
82    b.endSlice(3);
83
84    var m = SliceGroup.merge(a, b);
85    assertEquals(2, m.slices.length);
86
87    assertEquals('two', m.slices[0].title);
88    assertEquals(2, m.slices[0].start);
89    assertEquals(1, m.slices[0].duration);
90
91    assertEquals('one', m.slices[1].title);
92    assertEquals(1, m.slices[1].start);
93    assertEquals(3, m.slices[1].duration);
94  }
95
96  function testStartSplitMerge() {
97    var a = new SliceGroup();
98    var b = new SliceGroup();
99    a.beginSlice('', 'one', 2);
100    a.endSlice(4);
101    b.beginSlice('', 'two', 1);
102    b.endSlice(3);
103
104    var m = SliceGroup.merge(a, b);
105    assertEquals(3, m.slices.length);
106
107    assertEquals('two', m.slices[0].title);
108    assertEquals(1, m.slices[0].start);
109    assertEquals(1, m.slices[0].duration);
110
111    assertEquals('two (cont.)', m.slices[1].title);
112    assertEquals(2, m.slices[1].start);
113    assertEquals(1, m.slices[1].duration);
114
115    assertEquals('one', m.slices[2].title);
116    assertEquals(2, m.slices[2].start);
117    assertEquals(2, m.slices[2].duration);
118  }
119
120  function testStartSplitTwoMerge() {
121    var a = new SliceGroup();
122    var b = new SliceGroup();
123    a.beginSlice('', 'one', 3);
124    a.endSlice(6);
125    b.beginSlice('', 'two', 1);
126    b.beginSlice('', 'three', 2);
127    b.endSlice(4);
128    b.endSlice(5);
129
130    var m = SliceGroup.merge(a, b);
131    assertEquals(5, m.slices.length);
132
133    assertEquals('three', m.slices[0].title);
134    assertEquals(2, m.slices[0].start);
135    assertEquals(1, m.slices[0].duration);
136
137    assertEquals('two', m.slices[1].title);
138    assertEquals(1, m.slices[1].start);
139    assertEquals(2, m.slices[1].duration);
140
141    assertEquals('three (cont.)', m.slices[2].title);
142    assertEquals(3, m.slices[2].start);
143    assertEquals(1, m.slices[2].duration);
144
145    assertEquals('two (cont.)', m.slices[3].title);
146    assertEquals(3, m.slices[3].start);
147    assertEquals(2, m.slices[3].duration);
148
149    assertEquals('one', m.slices[4].title);
150    assertEquals(3, m.slices[4].start);
151    assertEquals(3, m.slices[4].duration);
152  }
153
154  function testStartSplitTwiceMerge() {
155    var a = new SliceGroup();
156    var b = new SliceGroup();
157    a.beginSlice('', 'one', 2);
158    a.beginSlice('', 'two', 3);
159    a.endSlice(5);
160    a.endSlice(6);
161    b.beginSlice('', 'three', 1);
162    b.endSlice(4);
163
164    var m = SliceGroup.merge(a, b);
165    assertEquals(5, m.slices.length);
166
167    assertEquals('three', m.slices[0].title);
168    assertEquals(1, m.slices[0].start);
169    assertEquals(1, m.slices[0].duration);
170
171    assertEquals('three (cont.)', m.slices[1].title);
172    assertEquals(2, m.slices[1].start);
173    assertEquals(1, m.slices[1].duration);
174
175    assertEquals('three (cont.)', m.slices[2].title);
176    assertEquals(3, m.slices[2].start);
177    assertEquals(1, m.slices[2].duration);
178
179    assertEquals('two', m.slices[3].title);
180    assertEquals(3, m.slices[3].start);
181    assertEquals(2, m.slices[3].duration);
182
183    assertEquals('one', m.slices[4].title);
184    assertEquals(2, m.slices[4].start);
185    assertEquals(4, m.slices[4].duration);
186  }
187
188  function testEndSplitMerge() {
189    var a = new SliceGroup();
190    var b = new SliceGroup();
191    a.beginSlice('', 'one', 1);
192    a.endSlice(3);
193    b.beginSlice('', 'two', 2);
194    b.endSlice(4);
195
196    var m = SliceGroup.merge(a, b);
197    assertEquals(3, m.slices.length);
198
199    assertEquals('two', m.slices[0].title);
200    assertEquals(2, m.slices[0].start);
201    assertEquals(1, m.slices[0].duration);
202
203    assertEquals('one', m.slices[1].title);
204    assertEquals(1, m.slices[1].start);
205    assertEquals(2, m.slices[1].duration);
206
207    assertEquals('two (cont.)', m.slices[2].title);
208    assertEquals(3, m.slices[2].start);
209    assertEquals(1, m.slices[2].duration);
210  }
211
212  function testEndSplitTwoMerge() {
213    var a = new SliceGroup();
214    var b = new SliceGroup();
215    a.beginSlice('', 'one', 1);
216    a.endSlice(4);
217    b.beginSlice('', 'two', 2);
218    b.beginSlice('', 'three', 3);
219    b.endSlice(5);
220    b.endSlice(6);
221
222    var m = SliceGroup.merge(a, b);
223    assertEquals(5, m.slices.length);
224
225    assertEquals('three', m.slices[0].title);
226    assertEquals(3, m.slices[0].start);
227    assertEquals(1, m.slices[0].duration);
228
229    assertEquals('two', m.slices[1].title);
230    assertEquals(2, m.slices[1].start);
231    assertEquals(2, m.slices[1].duration);
232
233    assertEquals('one', m.slices[2].title);
234    assertEquals(1, m.slices[2].start);
235    assertEquals(3, m.slices[2].duration);
236
237    assertEquals('three (cont.)', m.slices[3].title);
238    assertEquals(4, m.slices[3].start);
239    assertEquals(1, m.slices[3].duration);
240
241    assertEquals('two (cont.)', m.slices[4].title);
242    assertEquals(4, m.slices[4].start);
243    assertEquals(2, m.slices[4].duration);
244  }
245
246  function testEndSplitTwiceMerge() {
247    var a = new SliceGroup();
248    var b = new SliceGroup();
249    a.beginSlice('', 'one', 1);
250    a.beginSlice('', 'two', 2);
251    a.endSlice(4);
252    a.endSlice(5);
253    b.beginSlice('', 'three', 3);
254    b.endSlice(6);
255
256    var m = SliceGroup.merge(a, b);
257    assertEquals(5, m.slices.length);
258
259    assertEquals('three', m.slices[0].title);
260    assertEquals(3, m.slices[0].start);
261    assertEquals(1, m.slices[0].duration);
262
263    assertEquals('two', m.slices[1].title);
264    assertEquals(2, m.slices[1].start);
265    assertEquals(2, m.slices[1].duration);
266
267    assertEquals('three (cont.)', m.slices[2].title);
268    assertEquals(4, m.slices[2].start);
269    assertEquals(1, m.slices[2].duration);
270
271    assertEquals('one', m.slices[3].title);
272    assertEquals(1, m.slices[3].start);
273    assertEquals(4, m.slices[3].duration);
274
275    assertEquals('three (cont.)', m.slices[4].title);
276    assertEquals(5, m.slices[4].start);
277    assertEquals(1, m.slices[4].duration);
278  }
279
280  //
281  // Input:
282  // A:  |    one     |       |     two     |
283  //
284  // B:       |         three         |
285  //
286  // Output:
287  //     |    one     | three |     two     |
288  //          | three |       | three |
289  //
290  function testSplitTwiceMerge() {
291    var a = new SliceGroup();
292    var b = new SliceGroup();
293    a.beginSlice('', 'one', 1);
294    a.endSlice(3);
295    a.beginSlice('', 'two', 4);
296    a.endSlice(6);
297    b.beginSlice('', 'three', 2);
298    b.endSlice(5);
299
300    var m = SliceGroup.merge(a, b);
301    assertEquals(5, m.slices.length);
302
303    assertEquals('three', m.slices[0].title);
304    assertEquals(2, m.slices[0].start);
305    assertEquals(1, m.slices[0].duration);
306
307    assertEquals('one', m.slices[1].title);
308    assertEquals(1, m.slices[1].start);
309    assertEquals(2, m.slices[1].duration);
310
311    assertEquals('three (cont.)', m.slices[2].title);
312    assertEquals(3, m.slices[2].start);
313    assertEquals(1, m.slices[2].duration);
314
315    assertEquals('three (cont.)', m.slices[3].title);
316    assertEquals(4, m.slices[3].start);
317    assertEquals(1, m.slices[3].duration);
318
319    assertEquals('two', m.slices[4].title);
320    assertEquals(4, m.slices[4].start);
321    assertEquals(2, m.slices[4].duration);
322  }
323
324  function testBounds() {
325    var group = new SliceGroup();
326    group.updateBounds();
327    assertEquals(group.bounds.min, undefined);
328    assertEquals(group.bounds.max, undefined);
329
330    group.pushSlice(newSlice(1, 3));
331    group.pushSlice(newSlice(7, 2));
332    group.updateBounds();
333    assertEquals(1, group.bounds.min);
334    assertEquals(9, group.bounds.max);
335  }
336
337  function testBoundsWithPartial() {
338    var group = new SliceGroup();
339    group.beginSlice('', 'a', 7);
340    group.updateBounds();
341    assertEquals(7, group.bounds.min);
342    assertEquals(7, group.bounds.max);
343  }
344
345  function testBoundsWithTwoPartials() {
346    var group = new SliceGroup();
347    group.beginSlice('', 'a', 0);
348    group.beginSlice('', 'a', 1);
349    group.updateBounds();
350    assertEquals(0, group.bounds.min);
351    assertEquals(1, group.bounds.max);
352  }
353
354  function testBoundsWithBothPartialAndRegular() {
355    var group = new SliceGroup();
356    group.updateBounds();
357    assertEquals(undefined, group.bounds.min);
358    assertEquals(undefined, group.bounds.max);
359
360    group.pushSlice(newSlice(1, 3));
361    group.beginSlice('', 'a', 7);
362    group.updateBounds();
363    assertEquals(1, group.bounds.min);
364    assertEquals(7, group.bounds.max);
365  }
366
367  function testAutocloserBasic() {
368    var group = new SliceGroup();
369    assertEquals(group.openSliceCount, 0);
370
371    group.pushSlice(newSliceNamed('a', 1, 0.5));
372
373    group.beginSlice('', 'b', 2);
374    group.beginSlice('', 'c', 2.5);
375    group.endSlice(3);
376
377    group.autoCloseOpenSlices();
378    group.updateBounds();
379
380    assertEquals(1, group.bounds.min);
381    assertEquals(3, group.bounds.max);
382    assertEquals(3, group.slices.length);
383    assertEquals('a', group.slices[0].title);
384    assertEquals('c', group.slices[1].title);
385    assertEquals('b', group.slices[2].title);
386    assertTrue(group.slices[2].didNotFinish);
387    assertEquals(1, group.slices[2].duration);
388  }
389
390  function testAutocloserWithSubTasks() {
391    var group = new SliceGroup();
392    assertEquals(group.openSliceCount, 0);
393
394    group.beginSlice('', 'a', 1);
395    group.beginSlice('', 'b1', 2);
396    group.endSlice(3);
397    group.beginSlice('', 'b2', 3);
398
399    group.autoCloseOpenSlices();
400    assertEquals(3, group.slices.length);
401    assertEquals('b1', group.slices[0].title);
402
403    assertEquals('b2', group.slices[1].title);
404    assertTrue(group.slices[1].didNotFinish);
405    assertEquals(0, group.slices[1].duration);
406
407    assertEquals('a', group.slices[2].title);
408    assertTrue(group.slices[2].didNotFinish);
409    assertEquals(2, group.slices[2].duration);
410  }
411
412  // TODO: test cretion of subSlices
413</script>
414</body>
415</html>
416