1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 Copyright (c) 2012 The Chromium Authors. All rights reserved. 5 Use of this source code is governed by a BSD-style license that can be 6 found in the LICENSE file. 7 --> 8 <head> 9 <title>TimelineModel tests</title> 10 <style> 11 .timeline-view { 12 border: 1px solid black; 13 margin: 10px; 14 } 15 .timeline-find-dialog { 16 border: 1px solid black; 17 margin: 10px; 18 } 19 </style> 20 <script src="base.js"></script> 21 <script> 22 base.require('unittest'); 23 base.require('test_utils'); 24 base.require('timeline_model'); 25 </script> 26 </head> 27 <body> 28 <script> 29 'use strict'; 30 31 var TimelineSlice = tracing.TimelineSlice; 32 var TimelineThreadSlice = tracing.TimelineThreadSlice; 33 var TimelineThread = tracing.TimelineThread; 34 var TimelineModel = tracing.TimelineModel; 35 var TimelineTitleFilter = tracing.TimelineTitleFilter; 36 37 function createModelWithOneOfEverything() { 38 var m = new TimelineModel(); 39 var cpu = m.getOrCreateCpu(1); 40 cpu.slices.push(test_utils.newSlice(1, 3)); 41 42 var p = m.getOrCreateProcess(1); 43 var t = p.getOrCreateThread(1); 44 t.pushSlice(new TimelineThreadSlice('', 'a', 0, 1, {}, 4)); 45 t.asyncSlices.push(test_utils.newAsyncSlice(0, 1, t, t)); 46 47 var c = p.getOrCreateCounter('', 'ProcessCounter'); 48 c.seriesNames.push('a', 'b'); 49 c.seriesColors.push(0, 1); 50 c.timestamps.push(0, 1, 2, 3); 51 c.samples.push(5, 10, 6, 15, 5, 12, 7, 16); 52 53 var c1 = cpu.getOrCreateCounter('', 'CpuCounter'); 54 c1.seriesNames.push('a', 'b'); 55 c1.seriesColors.push(0, 1); 56 c1.timestamps.push(0, 1, 2, 3); 57 c1.samples.push(5, 10, 6, 15, 5, 12, 7, 16); 58 59 m.updateBounds(); 60 61 return m; 62 } 63 64 function testModelBounds_EmptyModel() { 65 var m = new TimelineModel(); 66 m.updateBounds(); 67 assertEquals(undefined, m.minTimestamp); 68 assertEquals(undefined, m.maxTimestamp); 69 } 70 71 function testModelBounds_OneEmptyThread() { 72 var m = new TimelineModel(); 73 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 74 m.updateBounds(); 75 assertEquals(undefined, m.minTimestamp); 76 assertEquals(undefined, m.maxTimestamp); 77 } 78 79 function testModelBounds_OneThread() { 80 var m = new TimelineModel(); 81 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 82 t.pushSlice(new TimelineThreadSlice('', 'a', 0, 1, {}, 3)); 83 m.updateBounds(); 84 assertEquals(1, m.minTimestamp); 85 assertEquals(4, m.maxTimestamp); 86 } 87 88 function testModelBounds_OneThreadAndOneEmptyThread() { 89 var m = new TimelineModel(); 90 var t1 = m.getOrCreateProcess(1).getOrCreateThread(1); 91 t1.pushSlice(new TimelineThreadSlice('', 'a', 0, 1, {}, 3)); 92 var t2 = m.getOrCreateProcess(1).getOrCreateThread(1); 93 m.updateBounds(); 94 assertEquals(1, m.minTimestamp); 95 assertEquals(4, m.maxTimestamp); 96 } 97 98 function testModelBounds_OneCpu() { 99 var m = new TimelineModel(); 100 var cpu = m.getOrCreateCpu(1); 101 cpu.slices.push(test_utils.newSlice(1, 3)); 102 m.updateBounds(); 103 assertEquals(1, m.minTimestamp); 104 assertEquals(4, m.maxTimestamp); 105 } 106 107 108 function testModelBounds_OneCpuOneThread() { 109 var m = new TimelineModel(); 110 var cpu = m.getOrCreateCpu(1); 111 cpu.slices.push(test_utils.newSlice(1, 3)); 112 113 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 114 t.pushSlice(new TimelineThreadSlice('', 'a', 0, 1, {}, 4)); 115 116 m.updateBounds(); 117 assertEquals(1, m.minTimestamp); 118 assertEquals(5, m.maxTimestamp); 119 } 120 121 function testModelCanImportEmpty() { 122 var m; 123 m = new TimelineModel([]); 124 m = new TimelineModel(''); 125 } 126 127 function testTimelineTitleFilter() { 128 var s0 = test_utils.newSlice(1, 3); 129 assertFalse(new TimelineTitleFilter('').matchSlice(s0)); 130 131 assertTrue(new TimelineTitleFilter('a').matchSlice(s0)); 132 assertFalse(new TimelineTitleFilter('x').matchSlice(s0)); 133 134 var s1 = test_utils.newSliceNamed('ba', 1, 3); 135 assertTrue(new TimelineTitleFilter('a').matchSlice(s1)); 136 assertTrue(new TimelineTitleFilter('ba').matchSlice(s1)); 137 assertFalse(new TimelineTitleFilter('x').matchSlice(s1)); 138 } 139 140 function testTimelineModel_toJSON() { 141 var m = createModelWithOneOfEverything(); 142 assertNotNull(JSON.stringify(m)); 143 } 144 145 function testTimelineModel_findAllThreadsNamed() { 146 var m = new TimelineModel(); 147 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 148 t.name = 'CrBrowserMain'; 149 150 m.updateBounds(); 151 var f = m.findAllThreadsNamed('CrBrowserMain'); 152 assertArrayEquals([t], f); 153 f = m.findAllThreadsNamed('NoSuchThread'); 154 assertEquals(0, f.length); 155 } 156 157 function testTimelineModel_updateCategories() { 158 var m = new TimelineModel(); 159 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 160 t.pushSlice(new TimelineThreadSlice('categoryA', 'a', 0, 1, {}, 3)); 161 t.pushSlice(new TimelineThreadSlice('categoryA', 'a', 0, 1, {}, 3)); 162 t.pushSlice(new TimelineThreadSlice('categoryB', 'a', 0, 1, {}, 3)); 163 t.pushSlice(new TimelineThreadSlice('categoryA', 'a', 0, 1, {}, 3)); 164 t.pushSlice(new TimelineThreadSlice('', 'a', 0, 1, {}, 3)); 165 m.updateCategories_(); 166 assertArrayEquals(['categoryA', 'categoryB'], m.categories); 167 } 168 </script> 169 </body> 170 </html> 171