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.find_control'); 9 10 base.unittest.testSuite('tracing.find_control', function() { 11 /* 12 * Just enough of the Timeline to support the tests below. 13 */ 14 var FakeTimeline = ui.define('div'); 15 16 FakeTimeline.prototype = { 17 __proto__: HTMLDivElement.prototype, 18 19 decorate: function() { 20 this.addAllObjectsMatchingFilterToSelectionReturnValue = []; 21 22 this.selection = new tracing.Selection(); 23 this.keyHelp = '<keyHelp>'; 24 25 // Put some simple UI in for testing purposes. 26 var noteEl = document.createElement('div'); 27 noteEl.textContent = 'FakeTimeline:'; 28 this.appendChild(noteEl); 29 30 this.statusEl_ = document.createElement('div'); 31 this.appendChild(this.statusEl_); 32 this.refresh_(); 33 }, 34 35 refresh_: function() { 36 var status; 37 if (this.model) 38 status = 'model=set'; 39 else 40 status = 'model=undefined'; 41 this.statusEl_.textContent = status; 42 }, 43 44 zoomToSelection: function() {}, 45 46 panToSelection: function() {}, 47 48 addAllObjectsMatchingFilterToSelection: function(filter, selection) { 49 var n = this.addAllObjectsMatchingFilterToSelectionReturnValue.length; 50 for (var i = 0; i < n; i++) 51 selection.push_( 52 this.addAllObjectsMatchingFilterToSelectionReturnValue[i]); 53 } 54 }; 55 56 test('instantiate', function() { 57 var ctl = new tracing.FindControl(); 58 var didFindPrevious = false; 59 var didFindNext = false; 60 ctl.controller = { 61 findNext: function() { 62 didFindNext = true; 63 }, 64 65 findPrevious: function() { 66 didFindPrevious = true; 67 }, 68 69 reset: function() {}, 70 71 filterHits: [], 72 73 currentHitIndex: 0 74 }; 75 76 this.addHTMLOutput(ctl); 77 78 ctl.querySelector('input').focus(); 79 ctl.querySelector('input').blur(); 80 81 ctl.querySelector('.find-previous').click(); 82 assertTrue(didFindPrevious); 83 ctl.querySelector('.find-next').click(); 84 assertTrue(didFindNext); 85 }); 86 87 test('findControllerNoTimeline', function() { 88 var controller = new tracing.FindController(); 89 controller.findNext(); 90 controller.findPrevious(); 91 }); 92 93 test('findControllerEmptyHit', function() { 94 var timeline = new FakeTimeline(); 95 var controller = new tracing.FindController(); 96 controller.timeline = timeline; 97 98 timeline.selection = new tracing.Selection(); 99 controller.findNext(); 100 assertArrayShallowEquals([], timeline.selection); 101 controller.findPrevious(); 102 assertArrayShallowEquals([], timeline.selection); 103 }); 104 105 test('findControllerOneHit', function() { 106 var timeline = new FakeTimeline(); 107 var controller = new tracing.FindController(); 108 controller.timeline = timeline; 109 110 timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1]; 111 controller.filterText = 'asdf'; 112 113 controller.findNext(); 114 assertArrayShallowEquals([1], timeline.selection); 115 controller.findNext(); 116 assertArrayShallowEquals([1], timeline.selection); 117 controller.findPrevious(); 118 assertArrayShallowEquals([1], timeline.selection); 119 }); 120 121 test('findControllerMultipleHits', function() { 122 var timeline = new FakeTimeline(); 123 var controller = new tracing.FindController(); 124 controller.timeline = timeline; 125 126 timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1, 2, 3]; 127 controller.filterText = 'asdf'; 128 129 // Loop through hits then when we wrap, try moving backward. 130 controller.findNext(); 131 assertArrayShallowEquals([1], timeline.selection); 132 controller.findNext(); 133 assertArrayShallowEquals([2], timeline.selection); 134 controller.findNext(); 135 assertArrayShallowEquals([3], timeline.selection); 136 controller.findNext(); 137 assertArrayShallowEquals([1], timeline.selection); 138 controller.findPrevious(); 139 assertArrayShallowEquals([3], timeline.selection); 140 controller.findPrevious(); 141 assertArrayShallowEquals([2], timeline.selection); 142 }); 143 144 test('findControllerChangeFilterAfterNext', function() { 145 var timeline = new FakeTimeline(); 146 var controller = new tracing.FindController(); 147 controller.timeline = timeline; 148 149 timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1, 2, 3]; 150 controller.filterText = 'asdf'; 151 152 // Loop through hits then when we wrap, try moving backward. 153 controller.findNext(); 154 timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [4]; 155 controller.filterText = 'asdfsf'; 156 controller.findNext(); 157 assertArrayShallowEquals([4], timeline.selection); 158 }); 159 160 test('findControllerSelectsAllItemsFirst', function() { 161 var timeline = new FakeTimeline(); 162 var controller = new tracing.FindController(); 163 controller.timeline = timeline; 164 165 timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1, 2, 3]; 166 controller.filterText = 'asdfsf'; 167 assertArrayShallowEquals([1, 2, 3], timeline.selection); 168 controller.findNext(); 169 assertArrayShallowEquals([1], timeline.selection); 170 controller.findNext(); 171 assertArrayShallowEquals([2], timeline.selection); 172 }); 173 174 test('findControllerWithRealTimeline', function() { 175 var model = new tracing.TraceModel(); 176 var p1 = model.getOrCreateProcess(1); 177 var t1 = p1.getOrCreateThread(1); 178 t1.sliceGroup.pushSlice(new tracing.trace_model.ThreadSlice( 179 '', 'a', 0, 1, {}, 3)); 180 181 var timeline = new tracing.TimelineTrackView(); 182 timeline.model = model; 183 184 var controller = new tracing.FindController(); 185 controller.timeline = timeline; 186 187 // Test find with no filterText. 188 controller.findNext(); 189 190 // Test find with filter txt. 191 controller.filterText = 'a'; 192 controller.findNext(); 193 assertEquals(1, timeline.selection.length); 194 assertEquals(t1.sliceGroup.slices[0], timeline.selection[0].slice); 195 196 controller.filterText = 'xxx'; 197 controller.findNext(); 198 assertEquals(0, timeline.selection.length); 199 controller.findNext(); 200 assertEquals(0, timeline.selection.length); 201 }); 202 }); 203