Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <!-- |
| 3 | Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 4 | Use of this source code is governed by a BSD-style license that can be |
| 5 | found 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 Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 12 | <link rel="import" href="/core/timeline_view.html"> |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 13 | |
| 14 | <script> |
| 15 | 'use strict'; |
| 16 | |
| 17 | tv.b.unittest.testSuite(function() { |
| 18 | var Task = tv.b.Task; |
| 19 | |
| 20 | /* |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 21 | * Just enough of the SelectionController to support the tests below. |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 22 | */ |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 23 | function FakeSelectionController() { |
| 24 | this.addAllEventsMatchingFilterToSelectionReturnValue = []; |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 25 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 26 | this.viewport = undefined; |
| 27 | this.model = undefined; |
| 28 | this.selection = new tv.c.Selection(); |
| 29 | this.findMatches = new tv.c.Selection(); |
| 30 | } |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 31 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 32 | FakeSelectionController.prototype = { |
| 33 | addAllEventsMatchingFilterToSelectionAsTask: function(filter, selection) { |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 34 | return new Task(function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 35 | var n = this.addAllEventsMatchingFilterToSelectionReturnValue.length; |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 36 | for (var i = 0; i < n; i++) { |
| 37 | selection.push( |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 38 | this.addAllEventsMatchingFilterToSelectionReturnValue[i]); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 39 | } |
| 40 | }, this); |
| 41 | }, |
| 42 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 43 | 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 Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 59 | } |
| 60 | }; |
| 61 | |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 62 | 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 Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 76 | test('findControllerNoModel', function() { |
| 77 | var selectionController = new FakeSelectionController(); |
| 78 | var controller = new tv.c.FindController(selectionController); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 79 | controller.findNext(); |
| 80 | controller.findPrevious(); |
| 81 | }); |
| 82 | |
| 83 | test('findControllerEmptyHit', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 84 | var selectionController = new FakeSelectionController(); |
| 85 | var controller = new tv.c.FindController(selectionController); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 86 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 87 | selectionController.selection = new tv.c.Selection(); |
| 88 | selectionController.findMatches = new tv.c.Selection(); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 89 | controller.findNext(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 90 | assertArrayShallowEquals([], selectionController.selection); |
| 91 | assertArrayShallowEquals([], selectionController.findMatches); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 92 | controller.findPrevious(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 93 | assertArrayShallowEquals([], selectionController.selection); |
| 94 | assertArrayShallowEquals([], selectionController.findMatches); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 95 | }); |
| 96 | |
| 97 | test('findControllerOneHit', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 98 | var selectionController = new FakeSelectionController(); |
| 99 | var controller = new tv.c.FindController(selectionController); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 100 | |
| 101 | var s1 = {guid: 1}; |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 102 | selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [ |
| 103 | s1 |
| 104 | ]; |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 105 | controller.filterText = 'asdf'; |
| 106 | var promise = controller.updateFilterHits(); |
| 107 | promise.then(function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 108 | assertArrayShallowEquals([], selectionController.selection); |
| 109 | assertArrayShallowEquals([s1], selectionController.findMatches); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 110 | controller.findNext(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 111 | assertArrayShallowEquals([s1], selectionController.selection); |
| 112 | assertArrayShallowEquals([s1], selectionController.findMatches); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 113 | controller.findNext(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 114 | assertArrayShallowEquals([s1], selectionController.selection); |
| 115 | assertArrayShallowEquals([s1], selectionController.findMatches); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 116 | controller.findPrevious(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 117 | assertArrayShallowEquals([s1], selectionController.selection); |
| 118 | assertArrayShallowEquals([s1], selectionController.findMatches); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 119 | }); |
| 120 | return promise; |
| 121 | }); |
| 122 | |
| 123 | test('findControllerMultipleHits', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 124 | var selectionController = new FakeSelectionController(); |
| 125 | var controller = new tv.c.FindController(selectionController); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 126 | |
| 127 | var s1 = {guid: 1}; |
| 128 | var s2 = {guid: 2}; |
| 129 | var s3 = {guid: 3}; |
| 130 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 131 | selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [ |
| 132 | s1, s2, s3 |
| 133 | ]; |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 134 | controller.filterText = 'asdf'; |
| 135 | var promise = controller.updateFilterHits(); |
| 136 | promise.then(function() { |
| 137 | // Loop through hits then when we wrap, try moving backward. |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 138 | assertArrayShallowEquals([], selectionController.selection); |
| 139 | assertArrayShallowEquals([s1, s2, s3], selectionController.findMatches); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 140 | controller.findNext(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 141 | assertArrayShallowEquals([s1], selectionController.selection); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 142 | controller.findNext(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 143 | assertArrayShallowEquals([s2], selectionController.selection); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 144 | controller.findNext(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 145 | assertArrayShallowEquals([s3], selectionController.selection); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 146 | controller.findNext(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 147 | assertArrayShallowEquals([s1], selectionController.selection); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 148 | controller.findPrevious(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 149 | assertArrayShallowEquals([s3], selectionController.selection); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 150 | controller.findPrevious(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 151 | assertArrayShallowEquals([s2], selectionController.selection); |
| 152 | assertArrayShallowEquals([s1, s2, s3], selectionController.findMatches); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 153 | }); |
| 154 | return promise; |
| 155 | }); |
| 156 | |
| 157 | test('findControllerChangeFilterAfterNext', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 158 | var selectionController = new FakeSelectionController(); |
| 159 | var controller = new tv.c.FindController(selectionController); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 160 | |
| 161 | var s1 = {guid: 1}; |
| 162 | var s2 = {guid: 2}; |
| 163 | var s3 = {guid: 3}; |
| 164 | var s4 = {guid: 4}; |
| 165 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 166 | selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [ |
| 167 | s1, s2, s3 |
| 168 | ]; |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 169 | 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 Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 174 | selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [ |
| 175 | s4 |
| 176 | ]; |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 177 | |
| 178 | controller.filterText = 'asdfsf'; |
| 179 | var nextPromise = controller.updateFilterHits(); |
| 180 | nextPromise.then(function() { |
| 181 | controller.findNext(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 182 | assertArrayShallowEquals([s4], selectionController.selection); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 183 | }); |
| 184 | }); |
| 185 | return promise; |
| 186 | }); |
| 187 | |
| 188 | test('findControllerSelectsAllItemsFirst', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 189 | var selectionController = new FakeSelectionController(); |
| 190 | var controller = new tv.c.FindController(selectionController); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 191 | |
| 192 | var s1 = {guid: 1}; |
| 193 | var s2 = {guid: 2}; |
| 194 | var s3 = {guid: 3}; |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 195 | selectionController.addAllEventsMatchingFilterToSelectionReturnValue = [ |
| 196 | s1, s2, s3 |
| 197 | ]; |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 198 | controller.filterText = 'asdfsf'; |
| 199 | var promise = controller.updateFilterHits(); |
| 200 | promise.then(function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 201 | assertArrayShallowEquals([], selectionController.selection); |
| 202 | assertArrayShallowEquals([s1, s2, s3], selectionController.findMatches); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 203 | controller.findNext(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 204 | assertArrayShallowEquals([s1], selectionController.selection); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 205 | controller.findNext(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 206 | assertArrayShallowEquals([s2], selectionController.selection); |
| 207 | assertArrayShallowEquals([s1, s2, s3], selectionController.findMatches); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 208 | }); |
| 209 | return promise; |
| 210 | }); |
| 211 | |
| 212 | test('findControllerWithRealTimeline', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 213 | 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 Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 220 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 221 | var timeline = new tv.c.TimelineView(); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 222 | timeline.model = model; |
| 223 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 224 | var selectionController = timeline.selectionController; |
| 225 | var controller = timeline.findCtl_.controller; |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 226 | |
| 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 Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 233 | 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 Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 237 | |
| 238 | controller.findNext(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 239 | assert.equal(selectionController.selection.length, 1); |
| 240 | assert.equal(selectionController.selection[0], |
| 241 | model.t1.sliceGroup.slices[0]); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 242 | |
| 243 | controller.filterText = 'xxx'; |
| 244 | var nextPromise = controller.updateFilterHits(); |
| 245 | nextPromise.then(function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 246 | assert.equal(selectionController.findMatches.length, 0); |
| 247 | assert.equal(selectionController.selection.length, 1); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 248 | controller.findNext(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 249 | assert.equal(selectionController.selection.length, 0); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 250 | controller.findNext(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 251 | assert.equal(selectionController.selection.length, 0); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 252 | }); |
| 253 | return nextPromise; |
| 254 | }); |
| 255 | return promise; |
| 256 | }); |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 257 | |
| 258 | test('findControllerNavigation', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 259 | var selectionController = new FakeSelectionController(); |
| 260 | var controller = new tv.c.FindController(selectionController); |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 261 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 262 | var navToPositionCallCount = 0; |
| 263 | var fakeUIState = {}; |
| 264 | selectionController.uiStateFromString = function(string) { |
| 265 | if (string === '') |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 266 | return undefined; |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 267 | assert.equal(string, '2000@1.2x7'); |
| 268 | return fakeUIState; |
| 269 | } |
| 270 | selectionController.navToPosition = function(uiState) { |
| 271 | assert.equal(uiState, fakeUIState); |
| 272 | navToPositionCallCount++; |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 273 | }; |
| 274 | |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 275 | controller.filterText = '2000@1.2x7'; |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 276 | controller.updateFilterHits(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 277 | assert.equal(navToPositionCallCount, 1); |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 278 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 279 | var findTextClearedCallCount = 0; |
| 280 | selectionController.findTextCleared = function() { |
| 281 | findTextClearedCallCount++; |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 282 | }; |
| 283 | controller.filterText = ''; |
| 284 | controller.updateFilterHits(); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 285 | assert.equal(findTextClearedCallCount, 1); |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 286 | }); |
Chris Craik | 93216d0 | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 287 | }); |
| 288 | </script> |