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>Model tests</title> 10 <style> 11 .view { 12 border: 1px solid black; 13 margin: 10px; 14 } 15 .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('model'); 25 </script> 26 </head> 27 <body> 28 <script> 29 'use strict'; 30 31 var Slice = tracing.model.Slice; 32 var ThreadSlice = tracing.model.ThreadSlice; 33 var Thread = tracing.model.Thread; 34 var Model = tracing.Model; 35 var TitleFilter = tracing.TitleFilter; 36 37 function createModelWithOneOfEverything() { 38 var m = new Model(); 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 ThreadSlice('', '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 Model(); 66 m.updateBounds(); 67 assertEquals(undefined, m.bounds.min); 68 assertEquals(undefined, m.bounds.max); 69 } 70 71 function testModelBounds_OneEmptyThread() { 72 var m = new Model(); 73 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 74 m.updateBounds(); 75 assertEquals(undefined, m.bounds.min); 76 assertEquals(undefined, m.bounds.max); 77 } 78 79 function testModelBounds_OneThread() { 80 var m = new Model(); 81 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 82 t.pushSlice(new ThreadSlice('', 'a', 0, 1, {}, 3)); 83 m.updateBounds(); 84 assertEquals(1, m.bounds.min); 85 assertEquals(4, m.bounds.max); 86 } 87 88 function testModelBounds_OneThreadAndOneEmptyThread() { 89 var m = new Model(); 90 var t1 = m.getOrCreateProcess(1).getOrCreateThread(1); 91 t1.pushSlice(new ThreadSlice('', 'a', 0, 1, {}, 3)); 92 var t2 = m.getOrCreateProcess(1).getOrCreateThread(1); 93 m.updateBounds(); 94 assertEquals(1, m.bounds.min); 95 assertEquals(4, m.bounds.max); 96 } 97 98 function testModelBounds_OneCpu() { 99 var m = new Model(); 100 var cpu = m.getOrCreateCpu(1); 101 cpu.slices.push(test_utils.newSlice(1, 3)); 102 m.updateBounds(); 103 assertEquals(1, m.bounds.min); 104 assertEquals(4, m.bounds.max); 105 } 106 107 108 function testModelBounds_OneCpuOneThread() { 109 var m = new Model(); 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 ThreadSlice('', 'a', 0, 1, {}, 4)); 115 116 m.updateBounds(); 117 assertEquals(1, m.bounds.min); 118 assertEquals(5, m.bounds.max); 119 } 120 121 function testModelCanImportEmpty() { 122 var m; 123 m = new Model([]); 124 m = new Model(''); 125 } 126 127 function testTitleFilter() { 128 var s0 = test_utils.newSlice(1, 3); 129 assertFalse(new TitleFilter('').matchSlice(s0)); 130 131 assertTrue(new TitleFilter('a').matchSlice(s0)); 132 assertFalse(new TitleFilter('x').matchSlice(s0)); 133 134 var s1 = test_utils.newSliceNamed('ba', 1, 3); 135 assertTrue(new TitleFilter('a').matchSlice(s1)); 136 assertTrue(new TitleFilter('ba').matchSlice(s1)); 137 assertFalse(new TitleFilter('x').matchSlice(s1)); 138 } 139 140 function testModel_toJSON() { 141 var m = createModelWithOneOfEverything(); 142 assertNotNull(JSON.stringify(m)); 143 } 144 145 function testModel_findAllThreadsNamed() { 146 var m = new Model(); 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 testModel_updateCategories() { 158 var m = new Model(); 159 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 160 t.pushSlice(new ThreadSlice('categoryA', 'a', 0, 1, {}, 3)); 161 t.pushSlice(new ThreadSlice('categoryA', 'a', 0, 1, {}, 3)); 162 t.pushSlice(new ThreadSlice('categoryB', 'a', 0, 1, {}, 3)); 163 t.pushSlice(new ThreadSlice('categoryA', 'a', 0, 1, {}, 3)); 164 t.pushSlice(new ThreadSlice('', 'a', 0, 1, {}, 3)); 165 m.updateCategories_(); 166 assertArrayEquals(['categoryA', 'categoryB'], m.categories); 167 } 168 </script> 169 </body> 170 </html> 171