1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 'use strict'; 6 7 base.require('tracing.test_utils'); 8 base.require('tracing.trace_model.slice'); 9 base.require('tracing.timeline_track_view'); 10 base.require('ui.dom_helpers'); 11 12 base.unittest.testSuite('tracing.tracks.slice_track', function() { 13 var Selection = tracing.Selection; 14 var SliceTrack = tracing.tracks.SliceTrack; 15 var Slice = tracing.trace_model.Slice; 16 var Viewport = tracing.TimelineViewport; 17 18 test('instantiate', function() { 19 var div = document.createElement('div'); 20 this.addHTMLOutput(div); 21 22 var viewport = new Viewport(div); 23 var drawingContainer = new tracing.tracks.DrawingContainer(viewport); 24 div.appendChild(drawingContainer); 25 26 var track = SliceTrack(viewport); 27 drawingContainer.appendChild(track); 28 drawingContainer.invalidate(); 29 30 track.heading = 'testBasicSlices'; 31 track.slices = [ 32 new Slice('', 'a', 0, 1, {}, 1), 33 new Slice('', 'b', 1, 2.1, {}, 4.8), 34 new Slice('', 'b', 1, 7, {}, 0.5), 35 new Slice('', 'c', 2, 7.6, {}, 0.4) 36 ]; 37 track.viewport.xSetWorldBounds(0, 8.8, track.clientWidth); 38 }); 39 40 test('instantiate_shrinkingSliceSize', function() { 41 var div = document.createElement('div'); 42 this.addHTMLOutput(div); 43 44 var viewport = new Viewport(div); 45 var drawingContainer = new tracing.tracks.DrawingContainer(viewport); 46 div.appendChild(drawingContainer); 47 48 var track = SliceTrack(viewport); 49 drawingContainer.appendChild(track); 50 drawingContainer.invalidate(); 51 52 track.heading = 'testShrinkingSliceSizes'; 53 var x = 0; 54 var widths = [10, 5, 4, 3, 2, 1, 0.5, 0.4, 0.3, 0.2, 0.1, 0.05]; 55 var slices = []; 56 for (var i = 0; i < widths.length; i++) { 57 var s = new Slice('', 'a', 1, x, {}, widths[i]); 58 x += s.duration + 0.5; 59 slices.push(s); 60 } 61 track.slices = slices; 62 track.viewport.xSetWorldBounds(0, 1.1 * x, track.clientWidth); 63 }); 64 65 test('instantiate_elide', function() { 66 var optDicts = [{ trackName: 'elideOff', elide: false }, 67 { trackName: 'elideOn', elide: true }]; 68 69 var tooLongTitle = 'Unless eliding this SHOULD NOT BE DISPLAYED. '; 70 var bigTitle = 'Very big title name that goes on longer ' + 71 'than you may expect'; 72 73 for (var dictIndex in optDicts) { 74 var dict = optDicts[dictIndex]; 75 76 var div = document.createElement('div'); 77 div.appendChild(document.createTextNode(dict.trackName)); 78 this.addHTMLOutput(div); 79 80 var viewport = new Viewport(div); 81 var drawingContainer = new tracing.tracks.DrawingContainer(viewport); 82 div.appendChild(drawingContainer); 83 84 var track = new SliceTrack(viewport); 85 drawingContainer.appendChild(track); 86 drawingContainer.invalidate(); 87 88 track.SHOULD_ELIDE_TEXT = dict.elide; 89 track.heading = 'Visual: ' + dict.trackName; 90 track.slices = [ 91 // title, colorId, start, args, opt_duration 92 new Slice('', 'a ' + tooLongTitle + bigTitle, 0, 1, {}, 1), 93 new Slice('', bigTitle, 1, 2.1, {}, 4.8), 94 new Slice('', 'cccc cccc cccc', 1, 7, {}, 0.5), 95 new Slice('', 'd', 2, 7.6, {}, 1.0) 96 ]; 97 track.viewport.xSetWorldBounds(0, 9.5, track.clientWidth); 98 } 99 }); 100 101 test('findAllObjectsMatchingInSliceTrack', function() { 102 var track = SliceTrack(new tracing.TimelineViewport()); 103 track.slices = [ 104 new Slice('', 'a', 0, 1, {}, 1), 105 new Slice('', 'b', 1, 2.1, {}, 4.8), 106 new Slice('', 'b', 1, 7, {}, 0.5), 107 new Slice('', 'c', 2, 7.6, {}, 0.4) 108 ]; 109 var selection = new Selection(); 110 track.addAllObjectsMatchingFilterToSelection( 111 new tracing.TitleFilter('b'), selection); 112 113 assertEquals(2, selection.length); 114 assertEquals(track.slices[1], selection[0].slice); 115 assertEquals(track.slices[2], selection[1].slice); 116 }); 117 118 test('selectionHitTesting', function() { 119 var testEl = document.createElement('div'); 120 testEl.appendChild(ui.createScopedStyle('heading { width: 100px; }')); 121 testEl.style.width = '600px'; 122 this.addHTMLOutput(testEl); 123 124 var viewport = new Viewport(testEl); 125 var drawingContainer = new tracing.tracks.DrawingContainer(viewport); 126 testEl.appendChild(drawingContainer); 127 128 var track = new SliceTrack(viewport); 129 drawingContainer.appendChild(track); 130 drawingContainer.updateCanvasSizeIfNeeded_(); 131 132 track.heading = 'testSelectionHitTesting'; 133 track.slices = [ 134 new Slice('', 'a', 0, 1, {}, 1), 135 new Slice('', 'b', 1, 5, {}, 4.8) 136 ]; 137 var y = track.getBoundingClientRect().top + 5; 138 var pixelRatio = window.devicePixelRatio || 1; 139 var wW = 10; 140 var vW = drawingContainer.canvas.getBoundingClientRect().width; 141 track.viewport.xSetWorldBounds(0, wW, vW * pixelRatio); 142 143 var selection = new Selection(); 144 var x = (1.5 / wW) * vW; 145 track.addIntersectingItemsInRangeToSelection(x, x + 1, y, y + 1, selection); 146 assertEquals(track.slices[0], selection[0].slice); 147 148 var selection = new Selection(); 149 x = (2.1 / wW) * vW; 150 track.addIntersectingItemsInRangeToSelection(x, x + 1, y, y + 1, selection); 151 assertEquals(0, selection.length); 152 153 var selection = new Selection(); 154 x = (6.8 / wW) * vW; 155 track.addIntersectingItemsInRangeToSelection(x, x + 1, y, y + 1, selection); 156 assertEquals(track.slices[1], selection[0].slice); 157 158 var selection = new Selection(); 159 x = (9.9 / wW) * vW; 160 track.addIntersectingItemsInRangeToSelection(x, x + 1, y, y + 1, selection); 161 assertEquals(0, selection.length); 162 }); 163 164 test('elide', function() { 165 var testEl = document.createElement('div'); 166 this.addHTMLOutput(testEl); 167 168 var viewport = new Viewport(testEl); 169 var drawingContainer = new tracing.tracks.DrawingContainer(viewport); 170 testEl.appendChild(drawingContainer); 171 172 var track = new SliceTrack(viewport); 173 drawingContainer.appendChild(track); 174 drawingContainer.updateCanvasSizeIfNeeded_(); 175 176 var bigtitle = 'Super duper long long title ' + 177 'holy moly when did you get so verbose?'; 178 var smalltitle = 'small'; 179 track.heading = 'testElide'; 180 track.slices = [ 181 // title, colorId, start, args, opt_duration 182 new Slice('', bigtitle, 0, 1, {}, 1), 183 new Slice('', smalltitle, 1, 2, {}, 1) 184 ]; 185 track.viewport.xSetWorldBounds(0, 3.3, track.clientWidth); 186 187 var stringWidthPair = undefined; 188 var pixWidth = track.viewport.xViewVectorToWorld(1); 189 190 // Small titles on big slices are not elided. 191 stringWidthPair = track.elidedTitleCache.get(track, pixWidth, smalltitle, 192 track.labelWidth(smalltitle), 1); 193 assertEquals(smalltitle, stringWidthPair.string); 194 195 // Keep shrinking the slice until eliding starts. 196 var elidedWhenSmallEnough = false; 197 for (var sliceLength = 1; sliceLength >= 0.00001; sliceLength /= 2.0) { 198 stringWidthPair = track.elidedTitleCache.get(track, pixWidth, smalltitle, 199 track.labelWidth(smalltitle), sliceLength); 200 if (stringWidthPair.string.length < smalltitle.length) { 201 elidedWhenSmallEnough = true; 202 break; 203 } 204 } 205 assertTrue(elidedWhenSmallEnough); 206 207 // Big titles are elided immediately. 208 var superBigTitle = ''; 209 for (var x = 0; x < 10; x++) { 210 superBigTitle += bigtitle; 211 } 212 stringWidthPair = track.elidedTitleCache.get(track, pixWidth, 213 superBigTitle, track.labelWidth(superBigTitle), 1); 214 assertTrue(stringWidthPair.string.length < superBigTitle.length); 215 216 // And elided text ends with ... 217 var len = stringWidthPair.string.length; 218 assertEquals('...', stringWidthPair.string.substring(len - 3, len)); 219 }); 220 221 test('sliceTrackAddItemNearToProvidedHit', function() { 222 var track = new SliceTrack(new tracing.TimelineViewport()); 223 track.slices = [ 224 new Slice('', 'a', 0, 1, {}, 1), 225 new Slice('', 'b', 1, 2.1, {}, 4.8), 226 new Slice('', 'b', 1, 7, {}, 0.5), 227 new Slice('', 'c', 2, 7.6, {}, 0.4) 228 ]; 229 var sel = new Selection(); 230 track.addAllObjectsMatchingFilterToSelection( 231 new tracing.TitleFilter('b'), sel); 232 var ret; 233 234 // Select to the right of B. 235 var selRight = new Selection(); 236 ret = track.addItemNearToProvidedHitToSelection(sel[0], 1, selRight); 237 assertTrue(ret); 238 assertEquals(track.slices[2], selRight[0].slice); 239 240 // Select to the right of the 2nd b. 241 var selRight2 = new Selection(); 242 ret = track.addItemNearToProvidedHitToSelection(sel[0], 2, selRight2); 243 assertTrue(ret); 244 assertEquals(track.slices[3], selRight2[0].slice); 245 246 // Select to 2 to the right of the 2nd b. 247 var selRightOfRight = new Selection(); 248 ret = track.addItemNearToProvidedHitToSelection( 249 selRight[0], 1, selRightOfRight); 250 assertTrue(ret); 251 assertEquals(track.slices[3], selRightOfRight[0].slice); 252 253 // Select to the right of the rightmost slice. 254 var selNone = new Selection(); 255 ret = track.addItemNearToProvidedHitToSelection( 256 selRightOfRight[0], 1, selNone); 257 assertFalse(ret); 258 assertEquals(0, selNone.length); 259 260 // Select A and then select left. 261 var sel = new Selection(); 262 track.addAllObjectsMatchingFilterToSelection( 263 new tracing.TitleFilter('a'), sel); 264 var ret; 265 266 selNone = new Selection(); 267 ret = track.addItemNearToProvidedHitToSelection(sel[0], -1, selNone); 268 assertFalse(ret); 269 assertEquals(0, selNone.length); 270 }); 271 }); 272