blob: 5e48605bf7a1b7300a8cf818b62bc0c922bd13ac [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">
<script>
'use strict';
tv.b.unittest.testSuite(function() {
var Task = tv.b.Task;
/*
* Just enough of the Timeline to support the tests below.
*/
var FakeTimeline = tv.b.ui.define('div');
FakeTimeline.prototype = {
__proto__: HTMLDivElement.prototype,
decorate: function() {
this.addAllObjectsMatchingFilterToSelectionReturnValue = [];
this.selection = new tv.c.Selection();
this.highlight = new tv.c.Selection();
this.keyHelp = '<keyHelp>';
// Put some simple UI in for testing purposes.
var noteEl = document.createElement('div');
noteEl.textContent = 'FakeTimeline:';
this.appendChild(noteEl);
this.statusEl_ = document.createElement('div');
this.appendChild(this.statusEl_);
this.refresh_();
},
refresh_: function() {
var status;
if (this.model)
status = 'model=set';
else
status = 'model=undefined';
this.statusEl_.textContent = status;
},
addAllObjectsMatchingFilterToSelectionAsTask: function(filter, selection) {
return new Task(function() {
var n = this.addAllObjectsMatchingFilterToSelectionReturnValue.length;
for (var i = 0; i < n; i++) {
selection.push(
this.addAllObjectsMatchingFilterToSelectionReturnValue[i]);
}
}, this);
},
setHighlightAndClearSelection: function(highlight) {
this.highlight = highlight;
}
};
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 tv.b.unittest.TestError(message);
};
test('findControllerNoTimeline', function() {
var controller = new tv.c.FindController();
controller.findNext();
controller.findPrevious();
});
test('findControllerEmptyHit', function() {
var timeline = new FakeTimeline();
var controller = new tv.c.FindController();
controller.timeline = timeline;
timeline.selection = new tv.c.Selection();
timeline.highlight = new tv.c.Selection();
controller.findNext();
assertArrayShallowEquals([], timeline.selection);
assertArrayShallowEquals([], timeline.highlight);
controller.findPrevious();
assertArrayShallowEquals([], timeline.selection);
assertArrayShallowEquals([], timeline.highlight);
});
test('findControllerOneHit', function() {
var timeline = new FakeTimeline();
var controller = new tv.c.FindController();
controller.timeline = timeline;
var s1 = {guid: 1};
timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [s1];
controller.filterText = 'asdf';
var promise = controller.updateFilterHits();
promise.then(function() {
assertArrayShallowEquals([], timeline.selection);
assertArrayShallowEquals([s1], timeline.highlight);
controller.findNext();
assertArrayShallowEquals([s1], timeline.selection);
assertArrayShallowEquals([s1], timeline.highlight);
controller.findNext();
assertArrayShallowEquals([s1], timeline.selection);
assertArrayShallowEquals([s1], timeline.highlight);
controller.findPrevious();
assertArrayShallowEquals([s1], timeline.selection);
assertArrayShallowEquals([s1], timeline.highlight);
});
return promise;
});
test('findControllerMultipleHits', function() {
var timeline = new FakeTimeline();
var controller = new tv.c.FindController();
controller.timeline = timeline;
var s1 = {guid: 1};
var s2 = {guid: 2};
var s3 = {guid: 3};
timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [s1, s2, s3];
controller.filterText = 'asdf';
var promise = controller.updateFilterHits();
promise.then(function() {
// Loop through hits then when we wrap, try moving backward.
assertArrayShallowEquals([], timeline.selection);
assertArrayShallowEquals([s1, s2, s3], timeline.highlight);
controller.findNext();
assertArrayShallowEquals([s1], timeline.selection);
controller.findNext();
assertArrayShallowEquals([s2], timeline.selection);
controller.findNext();
assertArrayShallowEquals([s3], timeline.selection);
controller.findNext();
assertArrayShallowEquals([s1], timeline.selection);
controller.findPrevious();
assertArrayShallowEquals([s3], timeline.selection);
controller.findPrevious();
assertArrayShallowEquals([s2], timeline.selection);
assertArrayShallowEquals([s1, s2, s3], timeline.highlight);
});
return promise;
});
test('findControllerChangeFilterAfterNext', function() {
var timeline = new FakeTimeline();
var controller = new tv.c.FindController();
controller.timeline = timeline;
var s1 = {guid: 1};
var s2 = {guid: 2};
var s3 = {guid: 3};
var s4 = {guid: 4};
timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [s1, s2, s3];
controller.filterText = 'asdf';
var promise = controller.updateFilterHits();
promise.then(function() {
// Loop through hits then when we wrap, try moving backward.
controller.findNext();
timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [s4];
controller.filterText = 'asdfsf';
var nextPromise = controller.updateFilterHits();
nextPromise.then(function() {
controller.findNext();
assertArrayShallowEquals([s4], timeline.selection);
});
});
return promise;
});
test('findControllerSelectsAllItemsFirst', function() {
var timeline = new FakeTimeline();
var controller = new tv.c.FindController();
controller.timeline = timeline;
var s1 = {guid: 1};
var s2 = {guid: 2};
var s3 = {guid: 3};
timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [s1, s2, s3];
controller.filterText = 'asdfsf';
var promise = controller.updateFilterHits();
promise.then(function() {
assertArrayShallowEquals([], timeline.selection);
assertArrayShallowEquals([s1, s2, s3], timeline.highlight);
controller.findNext();
assertArrayShallowEquals([s1], timeline.selection);
controller.findNext();
assertArrayShallowEquals([s2], timeline.selection);
assertArrayShallowEquals([s1, s2, s3], timeline.highlight);
});
return promise;
});
test('findControllerWithRealTimeline', function() {
var model = new tv.c.TraceModel();
var p1 = model.getOrCreateProcess(1);
var t1 = p1.getOrCreateThread(1);
t1.sliceGroup.pushSlice(new tv.c.trace_model.ThreadSlice(
'', 'a', 0, 1, {}, 3));
var timeline = new tv.c.TimelineTrackView();
timeline.model = model;
var controller = new tv.c.FindController();
controller.timeline = timeline;
// Test find with no filterText.
controller.findNext();
// Test find with filter txt.
controller.filterText = 'a';
var promise = controller.updateFilterHits();
promise.then(function() {
assert.deepEqual(timeline.selection, []);
assert.deepEqual(timeline.highlight, t1.sliceGroup.slices);
controller.findNext();
assert.equal(timeline.selection.length, 1);
assert.equal(timeline.selection[0], t1.sliceGroup.slices[0]);
controller.filterText = 'xxx';
var nextPromise = controller.updateFilterHits();
nextPromise.then(function() {
assert.equal(timeline.highlight.length, 0);
assert.equal(timeline.selection.length, 0);
controller.findNext();
assert.equal(timeline.selection.length, 0);
controller.findNext();
assert.equal(timeline.selection.length, 0);
});
return nextPromise;
});
return promise;
});
test('findControllerNavigation', function() {
var model = new tv.c.TraceModel();
var p1 = model.getOrCreateProcess(1);
var t1 = p1.getOrCreateThread(2);
t1.sliceGroup.pushSlice(new tv.c.trace_model.ThreadSlice(
'', 'a', 0, 1, {}, 3));
var timeline = new tv.c.TimelineTrackView();
var vp = new tv.c.TimelineViewport(timeline);
timeline.model = model;
timeline.focusElement = timeline;
timeline.tabIndex = 0;
timeline.style.maxHeight = '600px';
this.addHTMLOutput(timeline);
vp.containerToTrackObj = {
getTrackByStableId: function(stableId) {
if (stableId === '1.2')
return { eventContainer: { stableId: '1.2' } };
return undefined;
}
};
var controller = new tv.c.FindController();
controller.timeline = timeline;
controller.filterText = '2000@1.2x7';
var cbCalls = 0;
timeline.navToPosition = function(uiState) {
assert.equal(uiState.location.xWorld, 2000);
assert.equal(
uiState.location.getContainingTrack(vp).eventContainer.stableId,
'1.2');
assert.equal(uiState.scaleX, 7);
cbCalls++;
};
controller.updateFilterHits();
assert.equal(cbCalls, 1);
cbCalls = 0;
timeline.removeXNavStringMarker = function() {
cbCalls++;
};
controller.filterText = '';
controller.updateFilterHits();
assert.equal(cbCalls, 1);
});
});
</script>