OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Anonymous namespace |
| 6 (function() { |
| 7 |
| 8 // Range of time set on a log once loaded used by sanity checks. |
| 9 // Used by sanityCheckWithTimeRange. |
| 10 var startTime = null; |
| 11 var endTime = null; |
| 12 |
| 13 var timelineView = TimelineView.getInstance(); |
| 14 var graphView = timelineView.graphView_; |
| 15 var scrollbar = graphView.scrollbar_; |
| 16 var canvas = graphView.canvas_; |
| 17 |
| 18 /** |
| 19 * A Task that creates a log dump, modifies it so |timeTicks| are all in UTC, |
| 20 * clears all events from the log, and then adds two new SOCKET events, which |
| 21 * have the specified start and end times. |
| 22 * |
| 23 * Most of these tests start with this task first. This gives us a known |
| 24 * starting state, and prevents the data from automatically updating. |
| 25 * |
| 26 * @param {int} startTime Time of the begin event. |
| 27 * @param {int} endTime Time of the end event. |
| 28 * @extends {netInternalsTest.Task} |
| 29 */ |
| 30 function LoadLogWithNewEventsTask(startTime, endTime) { |
| 31 netInternalsTest.Task.call(this); |
| 32 this.startTime_ = startTime; |
| 33 this.endTime_ = endTime; |
| 34 } |
| 35 |
| 36 LoadLogWithNewEventsTask.prototype = { |
| 37 __proto__: netInternalsTest.Task.prototype, |
| 38 |
| 39 /** |
| 40 * Starts creating a log dump. |
| 41 */ |
| 42 start: function() { |
| 43 logutil.createLogDumpAsync('test', this.onLogDumpCreated.bind(this)); |
| 44 }, |
| 45 |
| 46 /** |
| 47 * Modifies the log dump and loads it. |
| 48 */ |
| 49 onLogDumpCreated: function(logDumpText) { |
| 50 var logDump = JSON.parse(logDumpText); |
| 51 |
| 52 logDump.constants.timeTickOffset = '0'; |
| 53 logDump.events = []; |
| 54 |
| 55 var source = new netInternalsTest.Source(1, LogSourceType.SOCKET); |
| 56 logDump.events.push( |
| 57 netInternalsTest.CreateBeginEvent(source, LogEventType.SOCKET_ALIVE, |
| 58 this.startTime_, null)); |
| 59 logDump.events.push( |
| 60 netInternalsTest.CreateMatchingEndEvent(logDump.events[0], |
| 61 this.endTime_, null)); |
| 62 logDumpText = JSON.stringify(logDump); |
| 63 |
| 64 assertEquals('Log loaded.', logutil.loadLogFile(logDumpText)); |
| 65 |
| 66 endTime = this.endTime_; |
| 67 startTime = this.startTime_; |
| 68 if (startTime >= endTime) |
| 69 --startTime; |
| 70 |
| 71 sanityCheckWithTimeRange(false); |
| 72 |
| 73 this.onTaskDone(); |
| 74 } |
| 75 }; |
| 76 |
| 77 /** |
| 78 * Checks certain invariant properties of the TimelineGraphView and the |
| 79 * scroll bar. |
| 80 */ |
| 81 function sanityCheck() { |
| 82 expectLT(graphView.startTime_, graphView.endTime_); |
| 83 expectLE(0, scrollbar.getPosition()); |
| 84 expectLE(scrollbar.getPosition(), scrollbar.getRange()); |
| 85 } |
| 86 |
| 87 /** |
| 88 * Checks what sanityCheck does, but also checks that |startTime| and |endTime| |
| 89 * are the same as those used by the graph, as well as whether we have a timer |
| 90 * running to update the graph's end time. To avoid flake, this should only |
| 91 * be used synchronously relative to when |startTime| and |endTime| were set, |
| 92 * unless |expectUpdateTimer| is false. |
| 93 * @param {bool} expectUpdateTimer true if the TimelineView should currently |
| 94 * have an update end time timer running. |
| 95 */ |
| 96 function sanityCheckWithTimeRange(expectUpdateTimer) { |
| 97 if (!expectUpdateTimer) { |
| 98 expectEquals(null, timelineView.updateIntervalId_); |
| 99 } else { |
| 100 expectNotEquals(null, timelineView.updateIntervalId_); |
| 101 } |
| 102 assertNotEquals(startTime, null); |
| 103 assertNotEquals(endTime, null); |
| 104 expectEquals(startTime, graphView.startTime_); |
| 105 expectEquals(endTime, graphView.endTime_); |
| 106 sanityCheck(false); |
| 107 } |
| 108 |
| 109 /** |
| 110 * Checks what sanityCheck does, but also checks that |startTime| and |endTime| |
| 111 * are the same as those used by the graph. |
| 112 */ |
| 113 function sanityCheckNotUpdating() { |
| 114 expectEquals(null, timelineView.updateIntervalId_); |
| 115 sanityCheckWithTimeRange(); |
| 116 } |
| 117 |
| 118 /** |
| 119 * Simulates mouse wheel movement over the canvas element. |
| 120 * @param {number} mouseWheelMovement Amount of movement to simulate. |
| 121 */ |
| 122 function mouseZoom(mouseWheelMovement) { |
| 123 var scrollbarStartedAtEnd = |
| 124 (scrollbar.getRange() == scrollbar.getPosition()); |
| 125 |
| 126 var event = document.createEvent('WheelEvent'); |
| 127 event.initWebKitWheelEvent(0, mouseWheelMovement, window, 0, 0, 0, 0, |
| 128 false, false, false, false); |
| 129 canvas.dispatchEvent(event); |
| 130 |
| 131 // If the scrollbar started at the end of the range, make sure it ends there |
| 132 // as well. |
| 133 if (scrollbarStartedAtEnd) |
| 134 expectEquals(scrollbar.getRange(), scrollbar.getPosition()); |
| 135 |
| 136 sanityCheck(); |
| 137 } |
| 138 |
| 139 /** |
| 140 * Simulates moving the mouse wheel up. |
| 141 */ |
| 142 function mouseZoomIn() { |
| 143 var oldScale = graphView.scale_; |
| 144 var oldRange = scrollbar.getRange(); |
| 145 |
| 146 mouseZoom(1); |
| 147 |
| 148 if (oldScale == graphView.scale_) { |
| 149 expectEquals(oldScale, TimelineGraphView.MIN_SCALE); |
| 150 } else { |
| 151 expectLT(graphView.scale_, oldScale); |
| 152 } |
| 153 expectGE(scrollbar.getRange(), oldRange); |
| 154 } |
| 155 |
| 156 /** |
| 157 * Simulates moving the mouse wheel down. |
| 158 */ |
| 159 function mouseZoomOut() { |
| 160 var oldScale = graphView.scale_; |
| 161 var oldRange = scrollbar.getRange(); |
| 162 |
| 163 mouseZoom(-1); |
| 164 |
| 165 expectGT(graphView.scale_, oldScale); |
| 166 expectLE(scrollbar.getRange(), oldRange); |
| 167 } |
| 168 |
| 169 /** |
| 170 * Simulates zooming all the way by multiple mouse wheel events. |
| 171 */ |
| 172 function mouseZoomAllTheWayIn() { |
| 173 expectLT(TimelineGraphView.MIN_SCALE, graphView.scale_); |
| 174 while (graphView.scale_ != TimelineGraphView.MIN_SCALE) |
| 175 mouseZoomIn(); |
| 176 // Verify that zooming in when already at max zoom works. |
| 177 mouseZoomIn(); |
| 178 } |
| 179 |
| 180 /** |
| 181 * A Task that scrolls the scrollbar by manipulating the DOM, and then waits |
| 182 * for the scroll to complete. Has to be a task because onscroll and DOM |
| 183 * manipulations both occur asynchronously. |
| 184 * |
| 185 * Not safe to use when other asynchronously running code may try to |
| 186 * manipulate the scrollbar itself, or adjust the length of the scrollbar. |
| 187 * |
| 188 * @param {int} position Position to scroll to. |
| 189 * @extends {netInternalsTest.Task} |
| 190 */ |
| 191 function MouseScrollTask(position) { |
| 192 netInternalsTest.Task.call(this); |
| 193 this.position_ = position; |
| 194 // If the scrollbar's |position| and its node's |scrollLeft| values don't |
| 195 // currently match, we set this to true and wait for |scrollLeft| to be |
| 196 // updated, which will trigger an onscroll event. |
| 197 this.waitingToStart_ = false; |
| 198 } |
| 199 |
| 200 MouseScrollTask.prototype = { |
| 201 __proto__: netInternalsTest.Task.prototype, |
| 202 |
| 203 start: function() { |
| 204 this.waitingToStart_ = false; |
| 205 // If the scrollbar is already in the correct position, do nothing. |
| 206 if (scrollbar.getNode().scrollLeft == this.position_) { |
| 207 // We may still have a timer going to adjust the position of the |
| 208 // scrollbar to some other value. If so, this will clear it. |
| 209 scrollbar.setPosition(this.position_); |
| 210 this.onTaskDone(); |
| 211 return; |
| 212 } |
| 213 |
| 214 // Replace the onscroll event handler with our own. |
| 215 this.oldOnScroll_ = scrollbar.getNode().onscroll; |
| 216 scrollbar.getNode().onscroll = this.onScroll_.bind(this); |
| 217 if (scrollbar.getNode().scrollLeft != scrollbar.getPosition()) { |
| 218 this.waitingToStart_ = true; |
| 219 return; |
| 220 } |
| 221 |
| 222 window.setTimeout(this.startScrolling_.bind(this), 0); |
| 223 }, |
| 224 |
| 225 onScroll_: function(event) { |
| 226 // Restore the original onscroll function. |
| 227 scrollbar.getNode().onscroll = this.oldOnScroll_; |
| 228 // Call the original onscroll function. |
| 229 this.oldOnScroll_(event); |
| 230 |
| 231 if (this.waitingToStart_) { |
| 232 this.start(); |
| 233 return; |
| 234 } |
| 235 |
| 236 assertEquals(this.position_, scrollbar.getNode().scrollLeft); |
| 237 assertEquals(this.position_, scrollbar.getPosition()); |
| 238 |
| 239 sanityCheck(); |
| 240 this.onTaskDone(); |
| 241 }, |
| 242 |
| 243 startScrolling_: function() { |
| 244 scrollbar.getNode().scrollLeft = this.position_; |
| 245 } |
| 246 }; |
| 247 |
| 248 netInternalsTest.test('netInternalsTimelineViewRange', function() { |
| 249 netInternalsTest.switchToView('timeline'); |
| 250 |
| 251 // Set startTime/endTime for sanity checks. |
| 252 startTime = graphView.startTime_; |
| 253 endTime = graphView.endTime_; |
| 254 sanityCheckWithTimeRange(true); |
| 255 |
| 256 startTime = 0; |
| 257 endTime = 10; |
| 258 graphView.setDateRange(new Date(startTime), new Date(endTime)); |
| 259 sanityCheckWithTimeRange(true); |
| 260 |
| 261 endTime = (new Date()).getTime(); |
| 262 graphView.updateEndDate(); |
| 263 |
| 264 expectGE(graphView.endTime_, endTime); |
| 265 sanityCheck(); |
| 266 |
| 267 testDone(); |
| 268 }); |
| 269 |
| 270 netInternalsTest.test('netInternalsTimelineViewScrollbar', function() { |
| 271 // The range we want the graph to have. |
| 272 var expectedGraphRange = canvas.width; |
| 273 |
| 274 function checkGraphRange() { |
| 275 expectEquals(expectedGraphRange, scrollbar.getRange()); |
| 276 } |
| 277 |
| 278 var taskQueue = new netInternalsTest.TaskQueue(true); |
| 279 // Load a log and then switch to the timeline view. The end time is |
| 280 // calculated so that the range is exactly |expectedGraphRange|. |
| 281 taskQueue.addTask( |
| 282 new LoadLogWithNewEventsTask( |
| 283 55, 55 + graphView.scale_ * (canvas.width + expectedGraphRange))); |
| 284 taskQueue.addFunctionTask( |
| 285 netInternalsTest.switchToView.bind(null, 'timeline')); |
| 286 taskQueue.addFunctionTask(checkGraphRange); |
| 287 |
| 288 taskQueue.addTask(new MouseScrollTask(0)); |
| 289 taskQueue.addTask(new MouseScrollTask(expectedGraphRange)); |
| 290 taskQueue.addTask(new MouseScrollTask(1)); |
| 291 taskQueue.addTask(new MouseScrollTask(expectedGraphRange - 1)); |
| 292 |
| 293 taskQueue.addFunctionTask(checkGraphRange); |
| 294 taskQueue.addFunctionTask(sanityCheckWithTimeRange.bind(null, false)); |
| 295 taskQueue.run(); |
| 296 }); |
| 297 |
| 298 netInternalsTest.test('netInternalsTimelineViewLoadLog', function() { |
| 299 // After loading the log file, the rest of the test runs synchronously. |
| 300 function testBody() { |
| 301 netInternalsTest.switchToView('timeline'); |
| 302 sanityCheckWithTimeRange(false); |
| 303 |
| 304 // Make sure everything's still fine when we switch to another view. |
| 305 netInternalsTest.switchToView('events'); |
| 306 sanityCheckWithTimeRange(false); |
| 307 } |
| 308 |
| 309 // Load a log and then run the rest of the test. |
| 310 var taskQueue = new netInternalsTest.TaskQueue(true); |
| 311 taskQueue.addTask(new LoadLogWithNewEventsTask(55, 10055)); |
| 312 taskQueue.addFunctionTask(testBody); |
| 313 taskQueue.run(); |
| 314 }); |
| 315 |
| 316 netInternalsTest.test('netInternalsTimelineViewZoomOut', function() { |
| 317 // After loading the log file, the rest of the test runs synchronously. |
| 318 function testBody() { |
| 319 netInternalsTest.switchToView('timeline'); |
| 320 mouseZoomOut(); |
| 321 mouseZoomOut(); |
| 322 mouseZoomIn(); |
| 323 sanityCheckWithTimeRange(false); |
| 324 } |
| 325 |
| 326 // Load a log and then run the rest of the test. |
| 327 var taskQueue = new netInternalsTest.TaskQueue(true); |
| 328 taskQueue.addTask(new LoadLogWithNewEventsTask(55, 10055)); |
| 329 taskQueue.addFunctionTask(testBody); |
| 330 taskQueue.run(); |
| 331 }); |
| 332 |
| 333 netInternalsTest.test('netInternalsTimelineViewZoomIn', function() { |
| 334 // After loading the log file, the rest of the test runs synchronously. |
| 335 function testBody() { |
| 336 netInternalsTest.switchToView('timeline'); |
| 337 mouseZoomAllTheWayIn(); |
| 338 mouseZoomOut(); |
| 339 sanityCheckWithTimeRange(false); |
| 340 } |
| 341 |
| 342 // Load a log and then run the rest of the test. |
| 343 var taskQueue = new netInternalsTest.TaskQueue(true); |
| 344 taskQueue.addTask(new LoadLogWithNewEventsTask(55, 10055)); |
| 345 taskQueue.addFunctionTask(testBody); |
| 346 taskQueue.run(); |
| 347 }); |
| 348 |
| 349 netInternalsTest.test('netInternalsTimelineViewDegenerate', function() { |
| 350 // After loading the log file, the rest of the test runs synchronously. |
| 351 function testBody() { |
| 352 netInternalsTest.switchToView('timeline'); |
| 353 mouseZoomOut(); |
| 354 mouseZoomAllTheWayIn(); |
| 355 mouseZoomOut(); |
| 356 sanityCheckWithTimeRange(false); |
| 357 } |
| 358 |
| 359 // Load a log and then run the rest of the test. |
| 360 var taskQueue = new netInternalsTest.TaskQueue(true); |
| 361 taskQueue.addTask(new LoadLogWithNewEventsTask(55, 10055)); |
| 362 taskQueue.addFunctionTask(testBody); |
| 363 taskQueue.run(); |
| 364 }); |
| 365 |
| 366 /** |
| 367 * Since we don't need to load a log file, this test can run synchronously. |
| 368 */ |
| 369 netInternalsTest.test('netInternalsTimelineViewNoEvents', function() { |
| 370 // Click the events view's delete all button, and then switch to timeline |
| 371 // view. |
| 372 netInternalsTest.switchToView('events'); |
| 373 $(EventsView.DELETE_ALL_ID).click(); |
| 374 netInternalsTest.switchToView('timeline'); |
| 375 |
| 376 // Set startTime/endTime for sanity checks. |
| 377 startTime = graphView.startTime_; |
| 378 endTime = graphView.endTime_; |
| 379 |
| 380 sanityCheckWithTimeRange(true); |
| 381 |
| 382 mouseZoomOut(); |
| 383 sanityCheckWithTimeRange(true); |
| 384 |
| 385 mouseZoomAllTheWayIn(); |
| 386 sanityCheckWithTimeRange(true); |
| 387 |
| 388 mouseZoomOut(); |
| 389 sanityCheckWithTimeRange(true); |
| 390 |
| 391 testDone(); |
| 392 }); |
| 393 |
| 394 })(); // Anonymous namespace |
OLD | NEW |