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