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('timeline_track_view');
     20 </script>
     21 </head>
     22 <body>
     23 <script>
     24   'use strict';
     25 
     26   var Counter = tracing.model.Counter;
     27   var Viewport = tracing.TimelineViewport;
     28   var CounterTrack = tracing.tracks.CounterTrack;
     29 
     30 
     31   function testBasicCounter() {
     32     var testEl = this.addHTMLOutput();
     33 
     34     var ctr = new Counter(undefined,
     35                           'testBasicCounter', '', 'testBasicCounter');
     36     ctr.seriesNames = ['value1', 'value2'];
     37     ctr.seriesColors = [tracing.getStringColorId('testBasicCounter.value1'),
     38                         tracing.getStringColorId('testBasicCounter.value2')];
     39     ctr.timestamps = [0, 1, 2, 3, 4, 5, 6, 7];
     40     ctr.samples = [0, 5,
     41                    3, 3,
     42                    1, 1,
     43                    2, 1.1,
     44                    3, 0,
     45                    1, 7,
     46                    3, 0,
     47                    3.1, 0.5];
     48     ctr.updateBounds();
     49 
     50     var track = new CounterTrack();
     51     testEl.appendChild(track);
     52     track.heading = ctr.name;
     53     track.counter = ctr;
     54     track.viewport = new Viewport(testEl);
     55     track.viewport.xSetWorldBounds(0, 7.7, track.clientWidth);
     56   }
     57 
     58   function runTest(timestamps, samples, testFn) {
     59     var testEl = document.createElement('div');
     60     var ctr = new Counter(undefined,
     61                                   'foo', '', 'foo');
     62     var n = samples.length / timestamps.length;
     63     ctr.timestamps = timestamps;
     64     ctr.samples = samples;
     65     ctr.seriesNames = []
     66     ctr.seriesColors = []
     67     for (var i = 0; i < n; ++i) {
     68       ctr.seriesNames.push('value' + i);
     69       ctr.seriesColors.push(tracing.getStringColorId(ctr.seriesNames[i]));
     70     }
     71     ctr.updateBounds();
     72 
     73     var track = new CounterTrack();
     74     testEl.appendChild(track);
     75     this.addHTMLOutput(undefined, testEl);
     76 
     77     track.heading = ctr.name;
     78     track.counter = ctr;
     79     track.viewport = new Viewport(testEl);
     80     track.viewport.xSetWorldBounds(0, 10,
     81         track.firstCanvas.getBoundingClientRect().width);
     82 
     83     testFn(ctr, track);
     84   }
     85 
     86   function testBasicCounterXPointPicking() {
     87     var timestamps = [0, 1, 2, 3, 4, 5, 6, 7];
     88     var samples = [0, 5,
     89                    3, 3,
     90                    1, 1,
     91                    2, 1.1,
     92                    3, 0,
     93                    1, 7,
     94                    3, 0,
     95                    3.1, 0.5];
     96     runTest.call(this, timestamps, samples, function(ctr, track) {
     97       var clientRect = track.firstCanvas.getBoundingClientRect();
     98       var y75 = clientRect.top + 0.75 * clientRect.height;
     99       var sel;
    100       var vW = 10;
    101       var wW = clientRect.width;
    102 
    103       // In bounds.
    104       sel = new tracing.Selection();
    105       track.addIntersectingItemsToSelection((1.5/vW)*wW, y75, sel);
    106       assertEquals(1, sel.length);
    107       assertEquals(track, sel[0].track);
    108       assertEquals(ctr, sel[0].counter);
    109       assertEquals(1, sel[0].sampleIndex);
    110 
    111       // Outside bouds.
    112       sel = new tracing.Selection();
    113       track.addIntersectingItemsToSelection((-1/vW)*wW, y75, sel);
    114       assertEquals(0, sel.length);
    115 
    116       sel = new tracing.Selection();
    117       track.addIntersectingItemsToSelection((8/vW)*wW, y75, sel);
    118       assertEquals(0, sel.length);
    119     });
    120   }
    121 </script>
    122 </body>
    123 </html>
    124