blob: b319fa41c5d6f44c238489233a942bf8dec5431a [file] [log] [blame]
Chris Craik93216d02015-03-05 13:58:42 -08001<!DOCTYPE html>
2<!--
3Copyright (c) 2013 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/base/task.html">
9<link rel="import" href="/core/find_controller.html">
10<link rel="import" href="/core/test_utils.html">
11<link rel="import" href="/core/timeline_track_view.html">
Chris Craik44c28202015-05-12 17:25:16 -070012<link rel="import" href="/core/timeline_view.html">
Chris Craik93216d02015-03-05 13:58:42 -080013
14<script>
15'use strict';
16
17tv.b.unittest.testSuite(function() {
18 var Task = tv.b.Task;
19
20 /*
Chris Craik44c28202015-05-12 17:25:16 -070021 * Just enough of the SelectionController to support the tests below.
Chris Craik93216d02015-03-05 13:58:42 -080022 */
Chris Craik44c28202015-05-12 17:25:16 -070023 function FakeSelectionController() {
24 this.addAllEventsMatchingFilterToSelectionReturnValue = [];
Chris Craik93216d02015-03-05 13:58:42 -080025
Chris Craik44c28202015-05-12 17:25:16 -070026 this.viewport = undefined;
27 this.model = undefined;
28 this.selection = new tv.c.Selection();
29 this.findMatches = new tv.c.Selection();
30 }
Chris Craik93216d02015-03-05 13:58:42 -080031
Chris Craik44c28202015-05-12 17:25:16 -070032 FakeSelectionController.prototype = {
33 addAllEventsMatchingFilterToSelectionAsTask: function(filter, selection) {
Chris Craik93216d02015-03-05 13:58:42 -080034 return new Task(function() {
Chris Craik44c28202015-05-12 17:25:16 -070035 var n = this.addAllEventsMatchingFilterToSelectionReturnValue.length;
Chris Craik93216d02015-03-05 13:58:42 -080036 for (var i = 0; i < n; i++) {
37 selection.push(
Chris Craik44c28202015-05-12 17:25:16 -070038 this.addAllEventsMatchingFilterToSelectionReturnValue[i]);
Chris Craik93216d02015-03-05 13:58:42 -080039 }
40 }, this);
41 },
42
Chris Craik44c28202015-05-12 17:25:16 -070043 uiStateFromString: function(string) {
44 return undefined;
45 },
46
47 findTextChangedTo: function(selection) {
48 this.findMatches = selection;
49 this.selection = new tv.c.Selection();
50 },
51
52 findFocusChangedTo: function(selection) {
53 this.selection = selection;
54 },
55
56 findTextCleared: function(selection) {
57 this.selection = new tv.c.Selection();
58 this.findMatches = new tv.c.Selection();
Chris Craik93216d02015-03-05 13:58:42 -080059 }
60 };
61
Chris Craikf516a622015-04-01 17:52:39 -070062 function assertArrayShallowEquals(a, b, opt_message) {
63 if (a.length === b.length) {
64 var ok = true;
65 for (var i = 0; i < a.length; i++) {
66 ok &= (a[i] === b[i]);
67 }
68 if (ok)
69 return;
70 }
71
72 var message = opt_message || 'Expected array ' + a + ', got array ' + b;
73 throw new tv.b.unittest.TestError(message);
74 };
75
Chris Craik44c28202015-05-12 17:25:16 -070076 test('findControllerNoModel', function() {
77 var selectionController = new FakeSelectionController();
78 var controller = new tv.c.FindController(selectionController);
Chris Craik93216d02015-03-05 13:58:42 -080079 controller.findNext();
80 controller.findPrevious();
81 });
82
83 test('findControllerEmptyHit', function() {
Chris Craik44c28202015-05-12 17:25:16 -070084 var selectionController = new FakeSelectionController();
85 var controller = new tv.c.FindController(selectionController);
Chris Craik93216d02015-03-05 13:58:42 -080086
Chris Craik44c28202015-05-12 17:25:16 -070087 selectionController.selection = new tv.c.Selection();
88 selectionController.findMatches = new tv.c.Selection();
Chris Craik93216d02015-03-05 13:58:42 -080089 controller.findNext();
Chris Craik44c28202015-05-12 17:25:16 -070090 assertArrayShallowEquals([], selectionController.selection);
91 assertArrayShallowEquals([], selectionController.findMatches);
Chris Craik93216d02015-03-05 13:58:42 -080092 controller.findPrevious();
Chris Craik44c28202015-05-12 17:25:16 -070093 assertArrayShallowEquals([], selectionController.selection);
94 assertArrayShallowEquals([], selectionController.findMatches);
Chris Craik93216d02015-03-05 13:58:42 -080095 });
96
97 test('findControllerOneHit', function() {
Chris Craik44c28202015-05-12 17:25:16 -070098 var selectionController = new FakeSelectionController();
99 var controller = new tv.c.FindController(selectionController);
Chris Craik93216d02015-03-05 13:58:42 -0800100
101 var s1 = {guid: 1};
Chris Craik44c28202015-05-12 17:25:16 -0700102 selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [
103 s1
104 ];
Chris Craik93216d02015-03-05 13:58:42 -0800105 controller.filterText = 'asdf';
106 var promise = controller.updateFilterHits();
107 promise.then(function() {
Chris Craik44c28202015-05-12 17:25:16 -0700108 assertArrayShallowEquals([], selectionController.selection);
109 assertArrayShallowEquals([s1], selectionController.findMatches);
Chris Craik93216d02015-03-05 13:58:42 -0800110 controller.findNext();
Chris Craik44c28202015-05-12 17:25:16 -0700111 assertArrayShallowEquals([s1], selectionController.selection);
112 assertArrayShallowEquals([s1], selectionController.findMatches);
Chris Craik93216d02015-03-05 13:58:42 -0800113 controller.findNext();
Chris Craik44c28202015-05-12 17:25:16 -0700114 assertArrayShallowEquals([s1], selectionController.selection);
115 assertArrayShallowEquals([s1], selectionController.findMatches);
Chris Craik93216d02015-03-05 13:58:42 -0800116 controller.findPrevious();
Chris Craik44c28202015-05-12 17:25:16 -0700117 assertArrayShallowEquals([s1], selectionController.selection);
118 assertArrayShallowEquals([s1], selectionController.findMatches);
Chris Craik93216d02015-03-05 13:58:42 -0800119 });
120 return promise;
121 });
122
123 test('findControllerMultipleHits', function() {
Chris Craik44c28202015-05-12 17:25:16 -0700124 var selectionController = new FakeSelectionController();
125 var controller = new tv.c.FindController(selectionController);
Chris Craik93216d02015-03-05 13:58:42 -0800126
127 var s1 = {guid: 1};
128 var s2 = {guid: 2};
129 var s3 = {guid: 3};
130
Chris Craik44c28202015-05-12 17:25:16 -0700131 selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [
132 s1, s2, s3
133 ];
Chris Craik93216d02015-03-05 13:58:42 -0800134 controller.filterText = 'asdf';
135 var promise = controller.updateFilterHits();
136 promise.then(function() {
137 // Loop through hits then when we wrap, try moving backward.
Chris Craik44c28202015-05-12 17:25:16 -0700138 assertArrayShallowEquals([], selectionController.selection);
139 assertArrayShallowEquals([s1, s2, s3], selectionController.findMatches);
Chris Craik93216d02015-03-05 13:58:42 -0800140 controller.findNext();
Chris Craik44c28202015-05-12 17:25:16 -0700141 assertArrayShallowEquals([s1], selectionController.selection);
Chris Craik93216d02015-03-05 13:58:42 -0800142 controller.findNext();
Chris Craik44c28202015-05-12 17:25:16 -0700143 assertArrayShallowEquals([s2], selectionController.selection);
Chris Craik93216d02015-03-05 13:58:42 -0800144 controller.findNext();
Chris Craik44c28202015-05-12 17:25:16 -0700145 assertArrayShallowEquals([s3], selectionController.selection);
Chris Craik93216d02015-03-05 13:58:42 -0800146 controller.findNext();
Chris Craik44c28202015-05-12 17:25:16 -0700147 assertArrayShallowEquals([s1], selectionController.selection);
Chris Craik93216d02015-03-05 13:58:42 -0800148 controller.findPrevious();
Chris Craik44c28202015-05-12 17:25:16 -0700149 assertArrayShallowEquals([s3], selectionController.selection);
Chris Craik93216d02015-03-05 13:58:42 -0800150 controller.findPrevious();
Chris Craik44c28202015-05-12 17:25:16 -0700151 assertArrayShallowEquals([s2], selectionController.selection);
152 assertArrayShallowEquals([s1, s2, s3], selectionController.findMatches);
Chris Craik93216d02015-03-05 13:58:42 -0800153 });
154 return promise;
155 });
156
157 test('findControllerChangeFilterAfterNext', function() {
Chris Craik44c28202015-05-12 17:25:16 -0700158 var selectionController = new FakeSelectionController();
159 var controller = new tv.c.FindController(selectionController);
Chris Craik93216d02015-03-05 13:58:42 -0800160
161 var s1 = {guid: 1};
162 var s2 = {guid: 2};
163 var s3 = {guid: 3};
164 var s4 = {guid: 4};
165
Chris Craik44c28202015-05-12 17:25:16 -0700166 selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [
167 s1, s2, s3
168 ];
Chris Craik93216d02015-03-05 13:58:42 -0800169 controller.filterText = 'asdf';
170 var promise = controller.updateFilterHits();
171 promise.then(function() {
172 // Loop through hits then when we wrap, try moving backward.
173 controller.findNext();
Chris Craik44c28202015-05-12 17:25:16 -0700174 selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [
175 s4
176 ];
Chris Craik93216d02015-03-05 13:58:42 -0800177
178 controller.filterText = 'asdfsf';
179 var nextPromise = controller.updateFilterHits();
180 nextPromise.then(function() {
181 controller.findNext();
Chris Craik44c28202015-05-12 17:25:16 -0700182 assertArrayShallowEquals([s4], selectionController.selection);
Chris Craik93216d02015-03-05 13:58:42 -0800183 });
184 });
185 return promise;
186 });
187
188 test('findControllerSelectsAllItemsFirst', function() {
Chris Craik44c28202015-05-12 17:25:16 -0700189 var selectionController = new FakeSelectionController();
190 var controller = new tv.c.FindController(selectionController);
Chris Craik93216d02015-03-05 13:58:42 -0800191
192 var s1 = {guid: 1};
193 var s2 = {guid: 2};
194 var s3 = {guid: 3};
Chris Craik44c28202015-05-12 17:25:16 -0700195 selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [
196 s1, s2, s3
197 ];
Chris Craik93216d02015-03-05 13:58:42 -0800198 controller.filterText = 'asdfsf';
199 var promise = controller.updateFilterHits();
200 promise.then(function() {
Chris Craik44c28202015-05-12 17:25:16 -0700201 assertArrayShallowEquals([], selectionController.selection);
202 assertArrayShallowEquals([s1, s2, s3], selectionController.findMatches);
Chris Craik93216d02015-03-05 13:58:42 -0800203 controller.findNext();
Chris Craik44c28202015-05-12 17:25:16 -0700204 assertArrayShallowEquals([s1], selectionController.selection);
Chris Craik93216d02015-03-05 13:58:42 -0800205 controller.findNext();
Chris Craik44c28202015-05-12 17:25:16 -0700206 assertArrayShallowEquals([s2], selectionController.selection);
207 assertArrayShallowEquals([s1, s2, s3], selectionController.findMatches);
Chris Craik93216d02015-03-05 13:58:42 -0800208 });
209 return promise;
210 });
211
212 test('findControllerWithRealTimeline', function() {
Chris Craik44c28202015-05-12 17:25:16 -0700213 var model = tv.c.test_utils.newModel(function(model) {
214 var p1 = model.getOrCreateProcess(1);
215 var t1 = p1.getOrCreateThread(1);
216 t1.sliceGroup.pushSlice(new tv.c.trace_model.ThreadSlice(
217 '', 'a', 0, 1, {}, 3));
218 model.t1 = t1;
219 });
Chris Craik93216d02015-03-05 13:58:42 -0800220
Chris Craik44c28202015-05-12 17:25:16 -0700221 var timeline = new tv.c.TimelineView();
Chris Craik93216d02015-03-05 13:58:42 -0800222 timeline.model = model;
223
Chris Craik44c28202015-05-12 17:25:16 -0700224 var selectionController = timeline.selectionController;
225 var controller = timeline.findCtl_.controller;
Chris Craik93216d02015-03-05 13:58:42 -0800226
227 // Test find with no filterText.
228 controller.findNext();
229
230 // Test find with filter txt.
231 controller.filterText = 'a';
232 var promise = controller.updateFilterHits();
Chris Craik44c28202015-05-12 17:25:16 -0700233 promise = promise.then(function() {
234 assert.equal(selectionController.selection.length, 0);
235 assert.deepEqual(tv.b.asArray(selectionController.findMatches),
236 model.t1.sliceGroup.slices);
Chris Craik93216d02015-03-05 13:58:42 -0800237
238 controller.findNext();
Chris Craik44c28202015-05-12 17:25:16 -0700239 assert.equal(selectionController.selection.length, 1);
240 assert.equal(selectionController.selection[0],
241 model.t1.sliceGroup.slices[0]);
Chris Craik93216d02015-03-05 13:58:42 -0800242
243 controller.filterText = 'xxx';
244 var nextPromise = controller.updateFilterHits();
245 nextPromise.then(function() {
Chris Craik44c28202015-05-12 17:25:16 -0700246 assert.equal(selectionController.findMatches.length, 0);
247 assert.equal(selectionController.selection.length, 1);
Chris Craik93216d02015-03-05 13:58:42 -0800248 controller.findNext();
Chris Craik44c28202015-05-12 17:25:16 -0700249 assert.equal(selectionController.selection.length, 0);
Chris Craik93216d02015-03-05 13:58:42 -0800250 controller.findNext();
Chris Craik44c28202015-05-12 17:25:16 -0700251 assert.equal(selectionController.selection.length, 0);
Chris Craik93216d02015-03-05 13:58:42 -0800252 });
253 return nextPromise;
254 });
255 return promise;
256 });
Chris Craikf516a622015-04-01 17:52:39 -0700257
258 test('findControllerNavigation', function() {
Chris Craik44c28202015-05-12 17:25:16 -0700259 var selectionController = new FakeSelectionController();
260 var controller = new tv.c.FindController(selectionController);
Chris Craikf516a622015-04-01 17:52:39 -0700261
Chris Craik44c28202015-05-12 17:25:16 -0700262 var navToPositionCallCount = 0;
263 var fakeUIState = {};
264 selectionController.uiStateFromString = function(string) {
265 if (string === '')
Chris Craikf516a622015-04-01 17:52:39 -0700266 return undefined;
Chris Craik44c28202015-05-12 17:25:16 -0700267 assert.equal(string, '2000@1.2x7');
268 return fakeUIState;
269 }
270 selectionController.navToPosition = function(uiState) {
271 assert.equal(uiState, fakeUIState);
272 navToPositionCallCount++;
Chris Craikf516a622015-04-01 17:52:39 -0700273 };
274
Chris Craikf516a622015-04-01 17:52:39 -0700275 controller.filterText = '2000@1.2x7';
Chris Craikf516a622015-04-01 17:52:39 -0700276 controller.updateFilterHits();
Chris Craik44c28202015-05-12 17:25:16 -0700277 assert.equal(navToPositionCallCount, 1);
Chris Craikf516a622015-04-01 17:52:39 -0700278
Chris Craik44c28202015-05-12 17:25:16 -0700279 var findTextClearedCallCount = 0;
280 selectionController.findTextCleared = function() {
281 findTextClearedCallCount++;
Chris Craikf516a622015-04-01 17:52:39 -0700282 };
283 controller.filterText = '';
284 controller.updateFilterHits();
Chris Craik44c28202015-05-12 17:25:16 -0700285 assert.equal(findTextClearedCallCount, 1);
Chris Craikf516a622015-04-01 17:52:39 -0700286 });
Chris Craik93216d02015-03-05 13:58:42 -0800287});
288</script>