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