blob: 379d5fe5d3aa62f74d998fac2886cd7bf3ce2c77 [file] [log] [blame]
<!DOCTYPE html>
<!--
Copyright (c) 2013 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<link rel="import" href="/base/task.html">
<link rel="import" href="/core/find_controller.html">
<link rel="import" href="/core/test_utils.html">
<link rel="import" href="/core/timeline_track_view.html">
<link rel="import" href="/core/timeline_view.html">
<script>
'use strict';
tr.b.unittest.testSuite(function() {
var Task = tr.b.Task;
/*
* Just enough of the SelectionController to support the tests below.
*/
function FakeSelectionController() {
this.addAllEventsMatchingFilterToSelectionReturnValue = [];
this.viewport = undefined;
this.model = undefined;
this.selection = new tr.c.Selection();
this.findMatches = new tr.c.Selection();
}
FakeSelectionController.prototype = {
addAllEventsMatchingFilterToSelectionAsTask: function(filter, selection) {
return new Task(function() {
var n = this.addAllEventsMatchingFilterToSelectionReturnValue.length;
for (var i = 0; i < n; i++) {
selection.push(
this.addAllEventsMatchingFilterToSelectionReturnValue[i]);
}
}, this);
},
uiStateFromString: function(string) {
return undefined;
},
findTextChangedTo: function(selection) {
this.findMatches = selection;
this.selection = new tr.c.Selection();
},
findFocusChangedTo: function(selection) {
this.selection = selection;
},
findTextCleared: function(selection) {
this.selection = new tr.c.Selection();
this.findMatches = new tr.c.Selection();
}
};
function assertArrayShallowEquals(a, b, opt_message) {
if (a.length === b.length) {
var ok = true;
for (var i = 0; i < a.length; i++) {
ok &= (a[i] === b[i]);
}
if (ok)
return;
}
var message = opt_message || 'Expected array ' + a + ', got array ' + b;
throw new tr.b.unittest.TestError(message);
};
test('findControllerNoModel', function() {
var selectionController = new FakeSelectionController();
var controller = new tr.c.FindController(selectionController);
controller.findNext();
controller.findPrevious();
});
test('findControllerEmptyHit', function() {
var selectionController = new FakeSelectionController();
var controller = new tr.c.FindController(selectionController);
selectionController.selection = new tr.c.Selection();
selectionController.findMatches = new tr.c.Selection();
controller.findNext();
assertArrayShallowEquals([], selectionController.selection);
assertArrayShallowEquals([], selectionController.findMatches);
controller.findPrevious();
assertArrayShallowEquals([], selectionController.selection);
assertArrayShallowEquals([], selectionController.findMatches);
});
test('findControllerOneHit', function() {
var selectionController = new FakeSelectionController();
var controller = new tr.c.FindController(selectionController);
var s1 = {guid: 1};
selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [
s1
];
var promise = controller.startFiltering('asdf');
promise.then(function() {
assertArrayShallowEquals([], selectionController.selection);
assertArrayShallowEquals([s1], selectionController.findMatches);
controller.findNext();
assertArrayShallowEquals([s1], selectionController.selection);
assertArrayShallowEquals([s1], selectionController.findMatches);
controller.findNext();
assertArrayShallowEquals([s1], selectionController.selection);
assertArrayShallowEquals([s1], selectionController.findMatches);
controller.findPrevious();
assertArrayShallowEquals([s1], selectionController.selection);
assertArrayShallowEquals([s1], selectionController.findMatches);
});
return promise;
});
test('findControllerMultipleHits', function() {
var selectionController = new FakeSelectionController();
var controller = new tr.c.FindController(selectionController);
var s1 = {guid: 1};
var s2 = {guid: 2};
var s3 = {guid: 3};
selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [
s1, s2, s3
];
var promise = controller.startFiltering('asdf');
promise.then(function() {
// Loop through hits then when we wrap, try moving backward.
assertArrayShallowEquals([], selectionController.selection);
assertArrayShallowEquals([s1, s2, s3], selectionController.findMatches);
controller.findNext();
assertArrayShallowEquals([s1], selectionController.selection);
controller.findNext();
assertArrayShallowEquals([s2], selectionController.selection);
controller.findNext();
assertArrayShallowEquals([s3], selectionController.selection);
controller.findNext();
assertArrayShallowEquals([s1], selectionController.selection);
controller.findPrevious();
assertArrayShallowEquals([s3], selectionController.selection);
controller.findPrevious();
assertArrayShallowEquals([s2], selectionController.selection);
assertArrayShallowEquals([s1, s2, s3], selectionController.findMatches);
});
return promise;
});
test('findControllerChangeFilterAfterNext', function() {
var selectionController = new FakeSelectionController();
var controller = new tr.c.FindController(selectionController);
var s1 = {guid: 1};
var s2 = {guid: 2};
var s3 = {guid: 3};
var s4 = {guid: 4};
selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [
s1, s2, s3
];
var promise = controller.startFiltering('asdf');
promise.then(function() {
// Loop through hits then when we wrap, try moving backward.
controller.findNext();
selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [
s4
];
var nextPromise = controller.startFiltering('asdfsf');
nextPromise.then(function() {
controller.findNext();
assertArrayShallowEquals([s4], selectionController.selection);
});
});
return promise;
});
test('findControllerSelectsAllItemsFirst', function() {
var selectionController = new FakeSelectionController();
var controller = new tr.c.FindController(selectionController);
var s1 = {guid: 1};
var s2 = {guid: 2};
var s3 = {guid: 3};
selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [
s1, s2, s3
];
var promise = controller.startFiltering('asdfsf');
promise.then(function() {
assertArrayShallowEquals([], selectionController.selection);
assertArrayShallowEquals([s1, s2, s3], selectionController.findMatches);
controller.findNext();
assertArrayShallowEquals([s1], selectionController.selection);
controller.findNext();
assertArrayShallowEquals([s2], selectionController.selection);
assertArrayShallowEquals([s1, s2, s3], selectionController.findMatches);
});
return promise;
});
test('findControllerWithRealTimeline', function() {
var model = tr.c.test_utils.newModel(function(model) {
var p1 = model.getOrCreateProcess(1);
var t1 = p1.getOrCreateThread(1);
t1.sliceGroup.pushSlice(new tr.model.ThreadSlice(
'', 'a', 0, 1, {}, 3));
model.t1 = t1;
});
var timeline = new tr.c.TimelineView();
timeline.model = model;
var selectionController = timeline.selectionController;
var controller = timeline.findCtl_.controller;
// Test find with no filterText.
controller.findNext();
// Test find with filter txt.
var promise = controller.startFiltering('a');
promise = promise.then(function() {
assert.equal(selectionController.selection.length, 0);
assert.deepEqual(tr.b.asArray(selectionController.findMatches),
model.t1.sliceGroup.slices);
controller.findNext();
assert.equal(selectionController.selection.length, 1);
assert.equal(selectionController.selection[0],
model.t1.sliceGroup.slices[0]);
var nextPromise = controller.startFiltering('xxx');
nextPromise.then(function() {
assert.equal(selectionController.findMatches.length, 0);
assert.equal(selectionController.selection.length, 1);
controller.findNext();
assert.equal(selectionController.selection.length, 0);
controller.findNext();
assert.equal(selectionController.selection.length, 0);
});
return nextPromise;
});
return promise;
});
test('findControllerNavigation', function() {
var selectionController = new FakeSelectionController();
var controller = new tr.c.FindController(selectionController);
var navToPositionCallCount = 0;
var findTextClearedCallCount = 0;
var fakeUIState = {};
selectionController.uiStateFromString = function(string) {
if (string === '')
return undefined;
assert.equal(string, '2000@1.2x7');
return fakeUIState;
}
selectionController.navToPosition = function(uiState) {
assert.equal(uiState, fakeUIState);
navToPositionCallCount++;
};
selectionController.findTextCleared = function() {
findTextClearedCallCount++;
};
var promise = controller.startFiltering('2000@1.2x7').then(function() {
assert.equal(navToPositionCallCount, 1);
});
promise = promise.then(controller.startFiltering.bind(controller, ''));
promise = promise.then(function() {
assert.equal(findTextClearedCallCount, 1);
});
return promise;
});
test('findControllerClearAfterSet', function() {
var selectionController = new FakeSelectionController();
var controller = new tr.c.FindController(selectionController);
var findTextChangedToCalled = false;
selectionController.findTextChangedTo = function(selection) {
findTextChangedToCalled = true;
};
selectionController.findTextCleared = function() {
assert.equal(findTextChangedToCalled, true);
};
controller.startFiltering('1');
controller.startFiltering('');
});
});
</script>