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 <script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js"></script> 11 <script src="../shared/js/cr.js"></script> 12 <script src="../shared/js/cr/event_target.js"></script> 13 <script src="test_utils.js"></script> 14 <script src="timeline_model.js"></script> 15 <script> 16 goog.require('goog.testing.jsunit'); 17 </script> 18 19 </head> 20 <body> 21 <script> 22 'use strict'; 23 var TimelineCpu = tracing.TimelineCpu; 24 var TimelineSlice = tracing.TimelineSlice; 25 var TimelineThreadSlice = tracing.TimelineThreadSlice; 26 var TimelineProcess = tracing.TimelineProcess; 27 var TimelineThread = tracing.TimelineThread; 28 var TimelineModel = tracing.TimelineModel; 29 var TimelineFilter = tracing.TimelineFilter; 30 var TimelineAsyncSlice = tracing.TimelineAsyncSlice; 31 var TimelineAsyncSliceGroup = tracing.TimelineAsyncSliceGroup; 32 var newAsyncSlice = test_utils.newAsyncSlice; 33 34 function testThreadBounds_Empty() { 35 var t = new TimelineThread(new TimelineProcess(7), 1); 36 t.updateBounds(); 37 assertEquals(undefined, t.minTimestamp); 38 assertEquals(undefined, t.maxTimestamp); 39 } 40 41 function testThreadBounds_SubRow() { 42 var t = new TimelineThread(new TimelineProcess(7), 1); 43 t.subRows[0].push(new TimelineThreadSlice('a', 0, 1, {}, 3)); 44 t.updateBounds(); 45 assertEquals(1, t.minTimestamp); 46 assertEquals(4, t.maxTimestamp); 47 } 48 49 function testThreadBounds_AsyncSliceGroup() { 50 var t = new TimelineThread(new TimelineProcess(7), 1); 51 t.subRows[0].push(new TimelineThreadSlice('a', 0, 1, {}, 3)); 52 t.asyncSlices.push(newAsyncSlice(0.1, 5, t, t)); 53 t.updateBounds(); 54 assertEquals(0.1, t.minTimestamp); 55 assertEquals(5.1, t.maxTimestamp); 56 } 57 58 function testModelBounds_EmptyModel() { 59 var m = new TimelineModel(); 60 m.updateBounds(); 61 assertEquals(undefined, m.minTimestamp); 62 assertEquals(undefined, m.maxTimestamp); 63 } 64 65 function testModelBounds_OneEmptyThread() { 66 var m = new TimelineModel(); 67 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 68 m.updateBounds(); 69 assertEquals(undefined, m.minTimestamp); 70 assertEquals(undefined, m.maxTimestamp); 71 } 72 73 function testModelBounds_OneThread() { 74 var m = new TimelineModel(); 75 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 76 t.subRows[0].push(new TimelineThreadSlice('a', 0, 1, {}, 3)); 77 m.updateBounds(); 78 assertEquals(1, m.minTimestamp); 79 assertEquals(4, m.maxTimestamp); 80 } 81 82 function testModelBounds_OneThreadAndOneEmptyThread() { 83 var m = new TimelineModel(); 84 var t1 = m.getOrCreateProcess(1).getOrCreateThread(1); 85 t1.subRows[0].push(new TimelineThreadSlice('a', 0, 1, {}, 3)); 86 var t2 = m.getOrCreateProcess(1).getOrCreateThread(1); 87 m.updateBounds(); 88 assertEquals(1, m.minTimestamp); 89 assertEquals(4, m.maxTimestamp); 90 } 91 92 function testCpuBounds_Empty() { 93 var cpu = new TimelineCpu(undefined, 1); 94 cpu.updateBounds(); 95 assertEquals(undefined, cpu.minTimestamp); 96 assertEquals(undefined, cpu.maxTimestamp); 97 } 98 99 function testCpuBounds_OneSlice() { 100 var cpu = new TimelineCpu(undefined, 1); 101 cpu.slices.push(new TimelineSlice('a', 0, 1, {}, 3)); 102 cpu.updateBounds(); 103 assertEquals(1, cpu.minTimestamp); 104 assertEquals(4, cpu.maxTimestamp); 105 } 106 107 function testModelBounds_OneCpu() { 108 var m = new TimelineModel(); 109 var cpu = m.getOrCreateCpu(1); 110 cpu.slices.push(new TimelineSlice('a', 0, 1, {}, 3)); 111 m.updateBounds(); 112 assertEquals(1, m.minTimestamp); 113 assertEquals(4, m.maxTimestamp); 114 } 115 116 117 function testModelBounds_OneCpuOneThread() { 118 var m = new TimelineModel(); 119 var cpu = m.getOrCreateCpu(1); 120 cpu.slices.push(new TimelineSlice('a', 0, 1, {}, 3)); 121 122 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 123 t.subRows[0].push(new TimelineThreadSlice('a', 0, 1, {}, 4)); 124 125 m.updateBounds(); 126 assertEquals(1, m.minTimestamp); 127 assertEquals(5, m.maxTimestamp); 128 } 129 130 function testPTIDFromPidAndTid() { 131 assertEquals('1:2', TimelineThread.getPTIDFromPidAndTid(1, 2)); 132 } 133 134 function testAsyncSliceGroupBounds_Empty() { 135 var g = new TimelineAsyncSliceGroup(name); 136 g.updateBounds(); 137 assertEquals(undefined, g.minTimestamp); 138 assertEquals(undefined, g.maxTimestamp); 139 } 140 141 function testAsyncSliceGroupBounds_Basic() { 142 var p1 = new TimelineProcess(1); 143 var t1 = new TimelineThread(p1, 1); 144 var g = new TimelineAsyncSliceGroup('a'); 145 g.push(newAsyncSlice(0, 1, t1, t1)); 146 g.push(newAsyncSlice(1, 1.5, t1, t1)); 147 assertEquals(2, g.length); 148 g.updateBounds(); 149 assertEquals(0, g.minTimestamp); 150 assertEquals(2.5, g.maxTimestamp); 151 } 152 153 function testAsyncSliceGroup_rebuildSubRows_twoNonOverlappingSlices() { 154 var p1 = new TimelineProcess(1); 155 var t1 = new TimelineThread(p1, 1); 156 var g = new TimelineAsyncSliceGroup('a'); 157 g.slices.push(newAsyncSlice(0, 1, t1, t1)); 158 g.slices.push(newAsyncSlice(1, 1, t1, t1)); 159 160 assertEquals(1, g.subRows.length); 161 assertEquals(2, g.subRows[0].length); 162 assertEquals(g.slices[0].subSlices[0], g.subRows[0][0]); 163 assertEquals(g.slices[1].subSlices[0], g.subRows[0][1]); 164 } 165 166 function testAsyncSliceGroup_rebuildSubRows_twoOverlappingSlices() { 167 var p1 = new TimelineProcess(1); 168 var t1 = new TimelineThread(p1, 1); 169 var g = new TimelineAsyncSliceGroup('a'); 170 g.slices.push(newAsyncSlice(0, 1, t1, t1)); 171 g.slices.push(newAsyncSlice(0, 1.5, t1, t1)); 172 g.updateBounds(); 173 174 assertEquals(2, g.subRows.length); 175 assertEquals(1, g.subRows[0].length); 176 assertEquals(g.slices[0], g.subRows[0][0]); 177 assertEquals(1, g.subRows[1].length); 178 assertEquals(g.slices[1], g.subRows[1][0]); 179 } 180 181 function testAsyncSliceGroup_rebuildSubRows_threePartlyOverlappingSlices() { 182 var p1 = new TimelineProcess(1); 183 var t1 = new TimelineThread(p1, 1); 184 var g = new TimelineAsyncSliceGroup('a'); 185 g.slices.push(newAsyncSlice(0, 1, t1, t1)); 186 g.slices.push(newAsyncSlice(0, 1.5, t1, t1)); 187 g.slices.push(newAsyncSlice(1, 1.5, t1, t1)); 188 g.updateBounds(); 189 190 assertEquals(2, g.subRows.length); 191 assertEquals(2, g.subRows[0].length); 192 assertEquals(g.slices[0].subSlices[0], g.subRows[0][0]); 193 assertEquals(g.slices[2].subSlices[0], g.subRows[0][1]); 194 assertEquals(1, g.subRows[1].length); 195 assertEquals(g.slices[1].subSlices[0], g.subRows[1][0]); 196 } 197 198 function testAsyncSliceGroup_rebuildSubRows_twoOverlappingSlices() { 199 var p1 = new TimelineProcess(1); 200 var t1 = new TimelineThread(p1, 1); 201 var g = new TimelineAsyncSliceGroup('a'); 202 g.slices.push(newAsyncSlice(0, 1, t1, t1)); 203 g.slices.push(newAsyncSlice(0, 1.5, t1, t1)); 204 g.slices.push(newAsyncSlice(2, 1, t1, t1)); 205 g.updateBounds(); 206 207 assertEquals(2, g.subRows.length); 208 assertEquals(2, g.subRows[0].length); 209 assertEquals(g.slices[0].subSlices[0], g.subRows[0][0]); 210 assertEquals(g.slices[2].subSlices[0], g.subRows[0][1]); 211 assertEquals(1, g.subRows[1].length); 212 assertEquals(g.slices[1].subSlices[0], g.subRows[1][0]); 213 } 214 215 function testAsyncSliceGroup_computeSubGroups_twoThreadSpecificSlices() { 216 var p1 = new TimelineProcess(1); 217 var t1 = new TimelineThread(p1, 1); 218 var t2 = new TimelineThread(p1, 2); 219 var g = new TimelineAsyncSliceGroup('a'); 220 g.slices.push(newAsyncSlice(0, 1, t1, t1)); 221 g.slices.push(newAsyncSlice(0, 1, t2, t2)); 222 223 var subGroups = g.computeSubGroups(); 224 assertEquals(2, subGroups.length); 225 226 assertEquals(g.name, subGroups[0].name); 227 assertEquals(1, subGroups[0].slices.length); 228 assertEquals(g.slices[0], subGroups[0].slices[0]); 229 230 assertEquals(g.name, subGroups[1].name); 231 assertEquals(1, subGroups[1].slices.length); 232 assertEquals(g.slices[1], subGroups[1].slices[0]); 233 } 234 235 function testModelCanImportEmpty() { 236 var m; 237 m = new TimelineModel([]); 238 m = new TimelineModel(''); 239 } 240 241 function testTimelineFilter() { 242 var s0 = new TimelineSlice('a', 0, 1, {}, 3); 243 assertFalse(new TimelineFilter('').matchSlice(s0)); 244 245 assertTrue(new TimelineFilter('a').matchSlice(s0)); 246 assertFalse(new TimelineFilter('x').matchSlice(s0)); 247 248 var s1 = new TimelineSlice('ba', 0, 1, {}, 3); 249 assertTrue(new TimelineFilter('a').matchSlice(s1)); 250 assertTrue(new TimelineFilter('ba').matchSlice(s1)); 251 assertFalse(new TimelineFilter('x').matchSlice(s1)); 252 } 253 254 </script> 255 </body> 256 </html> 257