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.
7 tvcm.require('tracing.test_utils');
8 tvcm.require('tracing.find_control');
10 tvcm.unittest.testSuite('tracing.find_control_test', function() {
12 * Just enough of the Timeline to support the tests below.
14 var FakeTimeline = tvcm.ui.define('div');
16 FakeTimeline.prototype = {
17 __proto__: HTMLDivElement.prototype,
19 decorate: function() {
20 this.addAllObjectsMatchingFilterToSelectionReturnValue = [];
22 this.selection = new tracing.Selection();
23 this.highlight = new tracing.Selection();
24 this.keyHelp = '<keyHelp>';
26 // Put some simple UI in for testing purposes.
27 var noteEl = document.createElement('div');
28 noteEl.textContent = 'FakeTimeline:';
29 this.appendChild(noteEl);
31 this.statusEl_ = document.createElement('div');
32 this.appendChild(this.statusEl_);
36 refresh_: function() {
41 status = 'model=undefined';
42 this.statusEl_.textContent = status;
45 addAllObjectsMatchingFilterToSelection: function(filter, selection) {
46 var n = this.addAllObjectsMatchingFilterToSelectionReturnValue.length;
47 for (var i = 0; i < n; i++) {
49 this.addAllObjectsMatchingFilterToSelectionReturnValue[i]);
53 setHighlightAndClearSelection: function(highlight) {
54 this.highlight = highlight;
58 test('instantiate', function() {
59 var ctl = new tracing.FindControl();
61 findNext: function() {
64 findPrevious: function() {
69 filterHits: ['a', 'b'],
74 this.addHTMLOutput(ctl);
77 test('findControllerNoTimeline', function() {
78 var controller = new tracing.FindController();
79 controller.findNext();
80 controller.findPrevious();
83 test('findControllerEmptyHit', function() {
84 var timeline = new FakeTimeline();
85 var controller = new tracing.FindController();
86 controller.timeline = timeline;
88 timeline.selection = new tracing.Selection();
89 timeline.highlight = new tracing.Selection();
90 controller.findNext();
91 assertArrayShallowEquals([], timeline.selection);
92 assertArrayShallowEquals([], timeline.highlight);
93 controller.findPrevious();
94 assertArrayShallowEquals([], timeline.selection);
95 assertArrayShallowEquals([], timeline.highlight);
98 test('findControllerOneHit', function() {
99 var timeline = new FakeTimeline();
100 var controller = new tracing.FindController();
101 controller.timeline = timeline;
103 timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1];
104 controller.filterText = 'asdf';
106 assertArrayShallowEquals([], timeline.selection);
107 assertArrayShallowEquals([1], timeline.highlight);
108 controller.findNext();
109 assertArrayShallowEquals([1], timeline.selection);
110 assertArrayShallowEquals([1], timeline.highlight);
111 controller.findNext();
112 assertArrayShallowEquals([1], timeline.selection);
113 assertArrayShallowEquals([1], timeline.highlight);
114 controller.findPrevious();
115 assertArrayShallowEquals([1], timeline.selection);
116 assertArrayShallowEquals([1], timeline.highlight);
119 test('findControllerMultipleHits', function() {
120 var timeline = new FakeTimeline();
121 var controller = new tracing.FindController();
122 controller.timeline = timeline;
124 timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1, 2, 3];
125 controller.filterText = 'asdf';
127 // Loop through hits then when we wrap, try moving backward.
128 assertArrayShallowEquals([], timeline.selection);
129 assertArrayShallowEquals([1, 2, 3], timeline.highlight);
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 assertArrayShallowEquals([1, 2, 3], timeline.highlight);
145 test('findControllerChangeFilterAfterNext', function() {
146 var timeline = new FakeTimeline();
147 var controller = new tracing.FindController();
148 controller.timeline = timeline;
150 timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1, 2, 3];
151 controller.filterText = 'asdf';
153 // Loop through hits then when we wrap, try moving backward.
154 controller.findNext();
155 timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [4];
156 controller.filterText = 'asdfsf';
157 controller.findNext();
158 assertArrayShallowEquals([4], timeline.selection);
161 test('findControllerSelectsAllItemsFirst', function() {
162 var timeline = new FakeTimeline();
163 var controller = new tracing.FindController();
164 controller.timeline = timeline;
166 timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1, 2, 3];
167 controller.filterText = 'asdfsf';
168 assertArrayShallowEquals([], timeline.selection);
169 assertArrayShallowEquals([1, 2, 3], timeline.highlight);
170 controller.findNext();
171 assertArrayShallowEquals([1], timeline.selection);
172 controller.findNext();
173 assertArrayShallowEquals([2], timeline.selection);
174 assertArrayShallowEquals([1, 2, 3], timeline.highlight);
177 test('findControllerWithRealTimeline', function() {
178 var model = new tracing.TraceModel();
179 var p1 = model.getOrCreateProcess(1);
180 var t1 = p1.getOrCreateThread(1);
181 t1.sliceGroup.pushSlice(new tracing.trace_model.ThreadSlice(
182 '', 'a', 0, 1, {}, 3));
184 var timeline = new tracing.TimelineTrackView();
185 timeline.model = model;
187 var controller = new tracing.FindController();
188 controller.timeline = timeline;
190 // Test find with no filterText.
191 controller.findNext();
193 // Test find with filter txt.
194 controller.filterText = 'a';
195 assertArrayEquals([], timeline.selection);
196 assertArrayEquals(t1.sliceGroup.slices, timeline.highlight);
198 controller.findNext();
199 assertEquals(1, timeline.selection.length);
200 assertEquals(t1.sliceGroup.slices[0], timeline.selection[0]);
202 controller.filterText = 'xxx';
203 assertEquals(0, timeline.highlight.length);
204 assertEquals(0, timeline.selection.length);
205 controller.findNext();
206 assertEquals(0, timeline.selection.length);
207 controller.findNext();
208 assertEquals(0, timeline.selection.length);