Home | History | Annotate | Download | only in tracks
      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.timeline_track_view');
      9 
     10 base.unittest.testSuite('tracing.tracks.counter_track', function() {
     11   var Counter = tracing.trace_model.Counter;
     12   var Viewport = tracing.TimelineViewport;
     13   var CounterTrack = tracing.tracks.CounterTrack;
     14 
     15   var runTest = function(timestamps, samples, testFn) {
     16     var testEl = document.createElement('div');
     17     this.addHTMLOutput(testEl);
     18 
     19     var ctr = new Counter(undefined, 'foo', '', 'foo');
     20     var n = samples.length;
     21 
     22     for (var i = 0; i < n; ++i) {
     23       ctr.addSeries(new tracing.trace_model.CounterSeries('value' + i,
     24           tracing.getStringColorId('value' + i)));
     25     }
     26 
     27     for (var i = 0; i < samples.length; ++i) {
     28       for (var k = 0; k < timestamps.length; ++k) {
     29         ctr.series[i].addSample(timestamps[k], samples[i][k]);
     30       }
     31     }
     32 
     33     ctr.updateBounds();
     34 
     35     var viewport = new Viewport(testEl);
     36 
     37     var drawingContainer = new tracing.tracks.DrawingContainer(viewport);
     38     testEl.appendChild(drawingContainer);
     39 
     40     var track = new CounterTrack(viewport);
     41     drawingContainer.appendChild(track);
     42 
     43     // Force the container to update sizes so the test can use coordinates that
     44     // make sense. This has to be after the adding of the track as we need to
     45     // use the track header to figure out our positioning.
     46     drawingContainer.updateCanvasSizeIfNeeded_();
     47 
     48     var pixelRatio = window.devicePixelRatio || 1;
     49 
     50     track.heading = ctr.name;
     51     track.counter = ctr;
     52     track.viewport.xSetWorldBounds(0, 10, track.clientWidth * pixelRatio);
     53 
     54     testFn(ctr, drawingContainer, track);
     55   };
     56 
     57   test('instantiate', function() {
     58     var ctr = new Counter(undefined, 'testBasicCounter', '',
     59         'testBasicCounter');
     60     ctr.addSeries(new tracing.trace_model.CounterSeries('value1',
     61         tracing.getStringColorId('testBasicCounter.value1')));
     62     ctr.addSeries(new tracing.trace_model.CounterSeries('value2',
     63         tracing.getStringColorId('testBasicCounter.value2')));
     64 
     65     var timestamps = [0, 1, 2, 3, 4, 5, 6, 7];
     66     var samples = [[0, 3, 1, 2, 3, 1, 3, 3.1],
     67                    [5, 3, 1, 1.1, 0, 7, 0, 0.5]];
     68     for (var i = 0; i < samples.length; ++i) {
     69       for (var k = 0; k < timestamps.length; ++k) {
     70         ctr.series[i].addSample(timestamps[k], samples[i][k]);
     71       }
     72     }
     73 
     74     ctr.updateBounds();
     75 
     76     var div = document.createElement('div');
     77     this.addHTMLOutput(div);
     78 
     79     var viewport = new Viewport(div);
     80 
     81     var drawingContainer = new tracing.tracks.DrawingContainer(viewport);
     82     div.appendChild(drawingContainer);
     83     drawingContainer.invalidate();
     84 
     85     var track = new CounterTrack(viewport);
     86     drawingContainer.appendChild(track);
     87 
     88     track.heading = ctr.name;
     89     track.counter = ctr;
     90     track.viewport.xSetWorldBounds(0, 7.7, track.clientWidth);
     91   });
     92 
     93   test('basicCounterXPointPicking', function() {
     94     var timestamps = [0, 1, 2, 3, 4, 5, 6, 7];
     95     var samples = [[0, 3, 1, 2, 3, 1, 3, 3.1],
     96                    [5, 3, 1, 1.1, 0, 7, 0, 0.5]];
     97 
     98     runTest.call(this, timestamps, samples, function(ctr, container, track) {
     99       var clientRect = track.getBoundingClientRect();
    100       var y75 = clientRect.top + (0.75 * clientRect.height);
    101 
    102       // In bounds.
    103       var sel = new tracing.Selection();
    104       var x = 0.15 * clientRect.width;
    105       track.addIntersectingItemsInRangeToSelection(x, x + 1, y75, y75 + 1, sel);
    106 
    107       assertEquals(1, sel.length);
    108       assertEquals(track, sel[0].track);
    109       assertEquals(ctr, sel[0].counter);
    110       assertEquals(1, sel[0].sampleIndex);
    111 
    112       // Outside bounds.
    113       sel = new tracing.Selection();
    114       var x = -0.5 * clientRect.width;
    115       track.addIntersectingItemsInRangeToSelection(x, x + 1, y75, y75 + 1, sel);
    116       assertEquals(0, sel.length);
    117 
    118       sel = new tracing.Selection();
    119       var x = 0.8 * clientRect.width;
    120       track.addIntersectingItemsInRangeToSelection(x, x + 1, y75, y75 + 1, sel);
    121       assertEquals(0, sel.length);
    122     });
    123   });
    124 });
    125