| <!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'; |
| |
| tv.b.unittest.testSuite(function() { |
| var Task = tv.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 tv.c.Selection(); |
| this.findMatches = new tv.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 tv.c.Selection(); |
| }, |
| |
| findFocusChangedTo: function(selection) { |
| this.selection = selection; |
| }, |
| |
| findTextCleared: function(selection) { |
| this.selection = new tv.c.Selection(); |
| this.findMatches = new tv.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 tv.b.unittest.TestError(message); |
| }; |
| |
| test('findControllerNoModel', function() { |
| var selectionController = new FakeSelectionController(); |
| var controller = new tv.c.FindController(selectionController); |
| controller.findNext(); |
| controller.findPrevious(); |
| }); |
| |
| test('findControllerEmptyHit', function() { |
| var selectionController = new FakeSelectionController(); |
| var controller = new tv.c.FindController(selectionController); |
| |
| selectionController.selection = new tv.c.Selection(); |
| selectionController.findMatches = new tv.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 tv.c.FindController(selectionController); |
| |
| var s1 = {guid: 1}; |
| selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [ |
| s1 |
| ]; |
| controller.filterText = 'asdf'; |
| var promise = controller.updateFilterHits(); |
| 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 tv.c.FindController(selectionController); |
| |
| var s1 = {guid: 1}; |
| var s2 = {guid: 2}; |
| var s3 = {guid: 3}; |
| |
| selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [ |
| s1, s2, s3 |
| ]; |
| controller.filterText = 'asdf'; |
| var promise = controller.updateFilterHits(); |
| 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 tv.c.FindController(selectionController); |
| |
| var s1 = {guid: 1}; |
| var s2 = {guid: 2}; |
| var s3 = {guid: 3}; |
| var s4 = {guid: 4}; |
| |
| selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [ |
| 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(); |
| selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [ |
| s4 |
| ]; |
| |
| controller.filterText = 'asdfsf'; |
| var nextPromise = controller.updateFilterHits(); |
| nextPromise.then(function() { |
| controller.findNext(); |
| assertArrayShallowEquals([s4], selectionController.selection); |
| }); |
| }); |
| return promise; |
| }); |
| |
| test('findControllerSelectsAllItemsFirst', function() { |
| var selectionController = new FakeSelectionController(); |
| var controller = new tv.c.FindController(selectionController); |
| |
| var s1 = {guid: 1}; |
| var s2 = {guid: 2}; |
| var s3 = {guid: 3}; |
| selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [ |
| s1, s2, s3 |
| ]; |
| controller.filterText = 'asdfsf'; |
| var promise = controller.updateFilterHits(); |
| 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 = tv.c.test_utils.newModel(function(model) { |
| var p1 = model.getOrCreateProcess(1); |
| var t1 = p1.getOrCreateThread(1); |
| t1.sliceGroup.pushSlice(new tv.c.trace_model.ThreadSlice( |
| '', 'a', 0, 1, {}, 3)); |
| model.t1 = t1; |
| }); |
| |
| var timeline = new tv.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. |
| controller.filterText = 'a'; |
| var promise = controller.updateFilterHits(); |
| promise = promise.then(function() { |
| assert.equal(selectionController.selection.length, 0); |
| assert.deepEqual(tv.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]); |
| |
| controller.filterText = 'xxx'; |
| var nextPromise = controller.updateFilterHits(); |
| 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 tv.c.FindController(selectionController); |
| |
| var navToPositionCallCount = 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++; |
| }; |
| |
| controller.filterText = '2000@1.2x7'; |
| controller.updateFilterHits(); |
| assert.equal(navToPositionCallCount, 1); |
| |
| var findTextClearedCallCount = 0; |
| selectionController.findTextCleared = function() { |
| findTextClearedCallCount++; |
| }; |
| controller.filterText = ''; |
| controller.updateFilterHits(); |
| assert.equal(findTextClearedCallCount, 1); |
| }); |
| }); |
| </script> |