Home | History | Annotate | Download | only in src
      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>TimelineFindControl tests</title>
     10 <style>
     11   .timeline-view {
     12     border: 1px solid black;
     13     margin: 10px;
     14     height: 350px;
     15   }
     16   .timeline-find-dialog {
     17     border: 1px solid black;
     18     margin: 10px;
     19   }
     20 </style>
     21 <script src="base.js"></script>
     22 <script>
     23   base.require('unittest');
     24   base.require('test_utils');
     25   base.require('timeline_find_control');
     26 </script>
     27 </head>
     28 <body>
     29 <script>
     30     'use strict';
     31 
     32     var newSliceNamed = test_utils.newSliceNamed;
     33 
     34     /*
     35      * Just enough of the Timeline to support the tests below.
     36      */
     37     var FakeTimeline = base.ui.define('div');
     38 
     39     FakeTimeline.prototype = {
     40       __proto__: HTMLDivElement.prototype,
     41 
     42       decorate: function() {
     43         this.addAllObjectsMatchingFilterToSelectionReturnValue = [];
     44 
     45         this.selection = new tracing.TimelineSelection();
     46         this.keyHelp = '<keyHelp>';
     47 
     48         // Put some simple UI in for testing purposes.
     49         var noteEl = document.createElement('div');
     50         noteEl.textContent = 'FakeTimeline:';
     51         this.appendChild(noteEl);
     52 
     53         this.statusEl_ = document.createElement('div');
     54         this.appendChild(this.statusEl_);
     55         this.refresh_();
     56       },
     57 
     58       refresh_: function() {
     59         var status;
     60         if (this.model)
     61           status = 'model=set';
     62         else
     63           status = 'model=undefined';
     64         this.statusEl_.textContent = status;
     65       },
     66 
     67       setSelectionAndMakeVisible: function(selection, zoomAllowed) {
     68         this.selection = selection;
     69       },
     70 
     71       addAllObjectsMatchingFilterToSelection: function(filter, selection) {
     72         var n = this.addAllObjectsMatchingFilterToSelectionReturnValue.length;
     73         for (var i = 0; i < n; i++)
     74           selection.push_(
     75               this.addAllObjectsMatchingFilterToSelectionReturnValue[i]);
     76       }
     77     };
     78 
     79     /*
     80      * This test just instantiates a FindDialog and adds it to the DOM
     81      * to help with non-unittest UI work.
     82      */
     83     function testInstantiateTimelineFindControl() {
     84       var ctl = new tracing.TimelineFindControl();
     85       var didFindPrevious = false;
     86       var didFindNext = false;
     87       ctl.controller = {
     88         findNext: function() {
     89           didFindNext = true;
     90         },
     91 
     92         findPrevious: function() {
     93           didFindPrevious = true;
     94         },
     95 
     96         filterHits: [],
     97 
     98         currentHitIndex: 0
     99       };
    100       this.addHTMLOutput(undefined, ctl);
    101       ctl.querySelector('input').focus();
    102       ctl.querySelector('input').blur();
    103 
    104       ctl.querySelector('.find-previous').click();
    105       assertTrue(didFindPrevious);
    106       ctl.querySelector('.find-next').click();
    107       assertTrue(didFindNext);
    108     }
    109 
    110     function testFindControllerNoTimeline() {
    111       var controller = new tracing.TimelineFindController();
    112       controller.findNext();
    113       controller.findPrevious();
    114     }
    115 
    116     function testFindControllerEmptyHit() {
    117       var timeline = new FakeTimeline();
    118       var controller = new tracing.TimelineFindController();
    119       controller.timeline = timeline;
    120 
    121       timeline.selection = new tracing.TimelineSelection();
    122       controller.findNext();
    123       assertArrayShallowEquals([], timeline.selection);
    124       controller.findPrevious();
    125       assertArrayShallowEquals([], timeline.selection);
    126     }
    127 
    128     function testFindControllerOneHit() {
    129       var timeline = new FakeTimeline();
    130       var controller = new tracing.TimelineFindController();
    131       controller.timeline = timeline;
    132 
    133       timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1];
    134       controller.findNext();
    135       assertArrayShallowEquals([1], timeline.selection);
    136       controller.findNext();
    137       assertArrayShallowEquals([1], timeline.selection);
    138       controller.findPrevious();
    139       assertArrayShallowEquals([1], timeline.selection);
    140     }
    141 
    142     function testFindControllerMultipleHits() {
    143       var timeline = new FakeTimeline();
    144       var controller = new tracing.TimelineFindController();
    145       controller.timeline = timeline;
    146 
    147       timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1, 2, 3];
    148 
    149       // Loop through hits then when we wrap, try moving backward.
    150       controller.findNext();
    151       assertArrayShallowEquals([1], timeline.selection);
    152       controller.findNext();
    153       assertArrayShallowEquals([2], timeline.selection);
    154       controller.findNext();
    155       assertArrayShallowEquals([3], timeline.selection);
    156       controller.findNext();
    157       assertArrayShallowEquals([1], timeline.selection);
    158       controller.findPrevious();
    159       assertArrayShallowEquals([3], timeline.selection);
    160       controller.findPrevious();
    161       assertArrayShallowEquals([2], timeline.selection);
    162     }
    163 
    164     function testFindControllerChangeFilterAfterNext() {
    165       var timeline = new FakeTimeline();
    166       var controller = new tracing.TimelineFindController();
    167       controller.timeline = timeline;
    168 
    169       timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1, 2, 3];
    170 
    171       // Loop through hits then when we wrap, try moving backward.
    172       controller.findNext();
    173       timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [4];
    174       controller.filterText = 'asdfsf';
    175       controller.findNext();
    176       assertArrayShallowEquals([4], timeline.selection);
    177     }
    178 
    179     function testFindControllerSelectsFirstItemImmediately() {
    180       var timeline = new FakeTimeline();
    181       var controller = new tracing.TimelineFindController();
    182       controller.timeline = timeline;
    183       timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1, 2, 3];
    184       controller.filterText = 'asdfsf';
    185       assertArrayShallowEquals([1], timeline.selection);
    186       controller.findNext();
    187       assertArrayShallowEquals([2], timeline.selection);
    188     }
    189 
    190     function testFindControllerWithRealTimeline() {
    191       var model = new tracing.TimelineModel();
    192       var p1 = model.getOrCreateProcess(1);
    193       var t1 = p1.getOrCreateThread(1);
    194       t1.pushSlice(new tracing.TimelineThreadSlice('', 'a', 0, 1, {}, 3));
    195 
    196       var timeline = new tracing.Timeline();
    197       timeline.model = model;
    198 
    199       var controller = new tracing.TimelineFindController();
    200       controller.timeline = timeline;
    201 
    202       // Test find with no filterText.
    203       controller.findNext();
    204 
    205       // Test find with filter txt.
    206       controller.filterText = 'a';
    207       controller.findNext();
    208       assertEquals(1, timeline.selection.length);
    209       assertEquals(t1.slices[0], timeline.selection[0].slice);
    210 
    211       controller.filterText = 'xxx';
    212       controller.findNext();
    213       assertEquals(0, timeline.selection.length);
    214       controller.findNext();
    215       assertEquals(0, timeline.selection.length);
    216     }
    217 </script>
    218 </body>
    219 </html>
    220