| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_TEST_UI_TEST_UTILS_H_ | 5 #ifndef CHROME_TEST_UI_TEST_UTILS_H_ |
| 6 #define CHROME_TEST_UI_TEST_UTILS_H_ | 6 #define CHROME_TEST_UI_TEST_UTILS_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <queue> | 10 #include <queue> |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 class BookmarkModel; | 30 class BookmarkModel; |
| 31 class BookmarkNode; | 31 class BookmarkNode; |
| 32 class Browser; | 32 class Browser; |
| 33 class CommandLine; | 33 class CommandLine; |
| 34 class DownloadManager; | 34 class DownloadManager; |
| 35 class ExtensionAction; | 35 class ExtensionAction; |
| 36 class FilePath; | 36 class FilePath; |
| 37 class GURL; | 37 class GURL; |
| 38 class MessageLoop; | 38 class MessageLoop; |
| 39 class NavigationController; | 39 class NavigationController; |
| 40 class NotificationType; | |
| 41 class Profile; | 40 class Profile; |
| 42 class RenderViewHost; | 41 class RenderViewHost; |
| 43 class RenderWidgetHost; | 42 class RenderWidgetHost; |
| 44 class ScopedTempDir; | 43 class ScopedTempDir; |
| 45 class SkBitmap; | 44 class SkBitmap; |
| 46 class TabContents; | 45 class TabContents; |
| 47 class TabContentsWrapper; | 46 class TabContentsWrapper; |
| 48 class Value; | 47 class Value; |
| 49 | 48 |
| 50 namespace gfx { | 49 namespace gfx { |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 bool case_sensitive, | 209 bool case_sensitive, |
| 211 int* ordinal); | 210 int* ordinal); |
| 212 | 211 |
| 213 // Returns true if the View is focused. | 212 // Returns true if the View is focused. |
| 214 bool IsViewFocused(const Browser* browser, ViewID vid); | 213 bool IsViewFocused(const Browser* browser, ViewID vid); |
| 215 | 214 |
| 216 // Simulates a mouse click on a View in the browser. | 215 // Simulates a mouse click on a View in the browser. |
| 217 void ClickOnView(const Browser* browser, ViewID vid); | 216 void ClickOnView(const Browser* browser, ViewID vid); |
| 218 | 217 |
| 219 // Blocks until a notification for given |type| is received. | 218 // Blocks until a notification for given |type| is received. |
| 220 void WaitForNotification(NotificationType type); | 219 void WaitForNotification(int type); |
| 221 | 220 |
| 222 // Blocks until a notification for given |type| from the specified |source| | 221 // Blocks until a notification for given |type| from the specified |source| |
| 223 // is received. | 222 // is received. |
| 224 void WaitForNotificationFrom(NotificationType type, | 223 void WaitForNotificationFrom(int type, |
| 225 const NotificationSource& source); | 224 const NotificationSource& source); |
| 226 | 225 |
| 227 // Register |observer| for the given |type| and |source| and run | 226 // Register |observer| for the given |type| and |source| and run |
| 228 // the message loop until the observer posts a quit task. | 227 // the message loop until the observer posts a quit task. |
| 229 void RegisterAndWait(NotificationObserver* observer, | 228 void RegisterAndWait(NotificationObserver* observer, |
| 230 NotificationType type, | 229 int type, |
| 231 const NotificationSource& source); | 230 const NotificationSource& source); |
| 232 | 231 |
| 233 // Blocks until |model| finishes loading. | 232 // Blocks until |model| finishes loading. |
| 234 void WaitForBookmarkModelToLoad(BookmarkModel* model); | 233 void WaitForBookmarkModelToLoad(BookmarkModel* model); |
| 235 | 234 |
| 236 // Blocks until the |browser|'s history finishes loading. | 235 // Blocks until the |browser|'s history finishes loading. |
| 237 void WaitForHistoryToLoad(Browser* browser); | 236 void WaitForHistoryToLoad(Browser* browser); |
| 238 | 237 |
| 239 // Puts the native window for |browser| in |native_window|. Returns true on | 238 // Puts the native window for |browser| in |native_window|. Returns true on |
| 240 // success. | 239 // success. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 261 // Sends a key press, blocking until both the key press and a notification from | 260 // Sends a key press, blocking until both the key press and a notification from |
| 262 // |source| of type |type| are received, or until the test times out. This uses | 261 // |source| of type |type| are received, or until the test times out. This uses |
| 263 // ui_controls::SendKeyPress, see it for details. Returns true if the event was | 262 // ui_controls::SendKeyPress, see it for details. Returns true if the event was |
| 264 // successfully sent and both the event and notification were received. | 263 // successfully sent and both the event and notification were received. |
| 265 bool SendKeyPressAndWait(const Browser* browser, | 264 bool SendKeyPressAndWait(const Browser* browser, |
| 266 ui::KeyboardCode key, | 265 ui::KeyboardCode key, |
| 267 bool control, | 266 bool control, |
| 268 bool shift, | 267 bool shift, |
| 269 bool alt, | 268 bool alt, |
| 270 bool command, | 269 bool command, |
| 271 NotificationType type, | 270 int type, |
| 272 const NotificationSource& source) WARN_UNUSED_RESULT; | 271 const NotificationSource& source) WARN_UNUSED_RESULT; |
| 273 | 272 |
| 274 // Run a message loop only for the specified amount of time. | 273 // Run a message loop only for the specified amount of time. |
| 275 class TimedMessageLoopRunner { | 274 class TimedMessageLoopRunner { |
| 276 public: | 275 public: |
| 277 // Create new MessageLoopForUI and attach to it. | 276 // Create new MessageLoopForUI and attach to it. |
| 278 TimedMessageLoopRunner(); | 277 TimedMessageLoopRunner(); |
| 279 | 278 |
| 280 // Attach to an existing message loop. | 279 // Attach to an existing message loop. |
| 281 explicit TimedMessageLoopRunner(MessageLoop* loop) | 280 explicit TimedMessageLoopRunner(MessageLoop* loop) |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 | 356 |
| 358 const NotificationSource& source() const { | 357 const NotificationSource& source() const { |
| 359 return source_; | 358 return source_; |
| 360 } | 359 } |
| 361 | 360 |
| 362 const NotificationDetails& details() const { | 361 const NotificationDetails& details() const { |
| 363 return details_; | 362 return details_; |
| 364 } | 363 } |
| 365 | 364 |
| 366 // NotificationObserver: | 365 // NotificationObserver: |
| 367 virtual void Observe(NotificationType type, | 366 virtual void Observe(int type, |
| 368 const NotificationSource& source, | 367 const NotificationSource& source, |
| 369 const NotificationDetails& details); | 368 const NotificationDetails& details); |
| 370 | 369 |
| 371 private: | 370 private: |
| 372 NotificationSource source_; | 371 NotificationSource source_; |
| 373 NotificationDetails details_; | 372 NotificationDetails details_; |
| 374 }; | 373 }; |
| 375 | 374 |
| 376 // A WindowedNotificationObserver allows code to watch for a notification | 375 // A WindowedNotificationObserver allows code to watch for a notification |
| 377 // over a window of time. Typically testing code will need to do something | 376 // over a window of time. Typically testing code will need to do something |
| 378 // like this: | 377 // like this: |
| 379 // PerformAction() | 378 // PerformAction() |
| 380 // WaitForCompletionNotification() | 379 // WaitForCompletionNotification() |
| 381 // This leads to flakiness as there's a window between PerformAction returning | 380 // This leads to flakiness as there's a window between PerformAction returning |
| 382 // and the observers getting registered, where a notification will be missed. | 381 // and the observers getting registered, where a notification will be missed. |
| 383 // | 382 // |
| 384 // Rather, one can do this: | 383 // Rather, one can do this: |
| 385 // WindowedNotificationObserver signal(...) | 384 // WindowedNotificationObserver signal(...) |
| 386 // PerformAction() | 385 // PerformAction() |
| 387 // signal.Wait() | 386 // signal.Wait() |
| 388 class WindowedNotificationObserver : public NotificationObserver { | 387 class WindowedNotificationObserver : public NotificationObserver { |
| 389 public: | 388 public: |
| 390 // Register to listen for notifications of the given type from either a | 389 // Register to listen for notifications of the given type from either a |
| 391 // specific source, or from all sources if |source| is | 390 // specific source, or from all sources if |source| is |
| 392 // NotificationService::AllSources(). | 391 // NotificationService::AllSources(). |
| 393 WindowedNotificationObserver(NotificationType notification_type, | 392 WindowedNotificationObserver(int notification_type, |
| 394 const NotificationSource& source); | 393 const NotificationSource& source); |
| 395 virtual ~WindowedNotificationObserver(); | 394 virtual ~WindowedNotificationObserver(); |
| 396 | 395 |
| 397 // Wait until the specified notification occurs. If the notification was | 396 // Wait until the specified notification occurs. If the notification was |
| 398 // emitted between the construction of this object and this call then it | 397 // emitted between the construction of this object and this call then it |
| 399 // returns immediately. | 398 // returns immediately. |
| 400 void Wait(); | 399 void Wait(); |
| 401 | 400 |
| 402 // WaitFor waits until the given notification type is received from the | 401 // WaitFor waits until the given notification type is received from the |
| 403 // given object. If the notification was emitted between the construction of | 402 // given object. If the notification was emitted between the construction of |
| 404 // this object and this call then it returns immediately. | 403 // this object and this call then it returns immediately. |
| 405 // | 404 // |
| 406 // Use this variant when you supply AllSources to the constructor but want | 405 // Use this variant when you supply AllSources to the constructor but want |
| 407 // to wait for notification from a specific observer. | 406 // to wait for notification from a specific observer. |
| 408 // | 407 // |
| 409 // Beware that this is inheriently plagued by ABA issues. Consider: | 408 // Beware that this is inheriently plagued by ABA issues. Consider: |
| 410 // WindowedNotificationObserver is created, listening for notifications from | 409 // WindowedNotificationObserver is created, listening for notifications from |
| 411 // all sources | 410 // all sources |
| 412 // Object A is created with address x and fires a notification | 411 // Object A is created with address x and fires a notification |
| 413 // Object A is freed | 412 // Object A is freed |
| 414 // Object B is created with the same address | 413 // Object B is created with the same address |
| 415 // WaitFor is called with the address of B | 414 // WaitFor is called with the address of B |
| 416 // | 415 // |
| 417 // In this case, WaitFor will return immediately because of the | 416 // In this case, WaitFor will return immediately because of the |
| 418 // notification from A (because they shared an address), despite being | 417 // notification from A (because they shared an address), despite being |
| 419 // different objects. | 418 // different objects. |
| 420 void WaitFor(const NotificationSource& source); | 419 void WaitFor(const NotificationSource& source); |
| 421 | 420 |
| 422 // NotificationObserver: | 421 // NotificationObserver: |
| 423 virtual void Observe(NotificationType type, | 422 virtual void Observe(int type, |
| 424 const NotificationSource& source, | 423 const NotificationSource& source, |
| 425 const NotificationDetails& details) OVERRIDE; | 424 const NotificationDetails& details) OVERRIDE; |
| 426 | 425 |
| 427 private: | 426 private: |
| 428 bool seen_; | 427 bool seen_; |
| 429 bool running_; | 428 bool running_; |
| 430 std::set<uintptr_t> sources_seen_; | 429 std::set<uintptr_t> sources_seen_; |
| 431 NotificationSource waiting_for_; | 430 NotificationSource waiting_for_; |
| 432 NotificationRegistrar registrar_; | 431 NotificationRegistrar registrar_; |
| 433 | 432 |
| 434 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserver); | 433 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserver); |
| 435 }; | 434 }; |
| 436 | 435 |
| 437 // Similar to WindowedNotificationObserver but also provides a way of retrieving | 436 // Similar to WindowedNotificationObserver but also provides a way of retrieving |
| 438 // the details associated with the notification. | 437 // the details associated with the notification. |
| 439 // Note that in order to use that class the details class should be copiable, | 438 // Note that in order to use that class the details class should be copiable, |
| 440 // which is the case with most notifications. | 439 // which is the case with most notifications. |
| 441 template <class U> | 440 template <class U> |
| 442 class WindowedNotificationObserverWithDetails | 441 class WindowedNotificationObserverWithDetails |
| 443 : public WindowedNotificationObserver { | 442 : public WindowedNotificationObserver { |
| 444 public: | 443 public: |
| 445 WindowedNotificationObserverWithDetails(NotificationType notification_type, | 444 WindowedNotificationObserverWithDetails(int notification_type, |
| 446 const NotificationSource& source) | 445 const NotificationSource& source) |
| 447 : WindowedNotificationObserver(notification_type, source) {} | 446 : WindowedNotificationObserver(notification_type, source) {} |
| 448 | 447 |
| 449 // Fills |details| with the details of the notification received for |source|. | 448 // Fills |details| with the details of the notification received for |source|. |
| 450 bool GetDetailsFor(uintptr_t source, U* details) { | 449 bool GetDetailsFor(uintptr_t source, U* details) { |
| 451 typename std::map<uintptr_t, U>::const_iterator iter = | 450 typename std::map<uintptr_t, U>::const_iterator iter = |
| 452 details_.find(source); | 451 details_.find(source); |
| 453 if (iter == details_.end()) | 452 if (iter == details_.end()) |
| 454 return false; | 453 return false; |
| 455 *details = iter->second; | 454 *details = iter->second; |
| 456 return true; | 455 return true; |
| 457 } | 456 } |
| 458 | 457 |
| 459 virtual void Observe(NotificationType type, | 458 virtual void Observe(int type, |
| 460 const NotificationSource& source, | 459 const NotificationSource& source, |
| 461 const NotificationDetails& details) { | 460 const NotificationDetails& details) { |
| 462 const U* details_ptr = Details<U>(details).ptr(); | 461 const U* details_ptr = Details<U>(details).ptr(); |
| 463 if (details_ptr) | 462 if (details_ptr) |
| 464 details_[source.map_key()] = *details_ptr; | 463 details_[source.map_key()] = *details_ptr; |
| 465 WindowedNotificationObserver::Observe(type, source, details); | 464 WindowedNotificationObserver::Observe(type, source, details); |
| 466 } | 465 } |
| 467 | 466 |
| 468 private: | 467 private: |
| 469 std::map<uintptr_t, U> details_; | 468 std::map<uintptr_t, U> details_; |
| 470 | 469 |
| 471 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserverWithDetails); | 470 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserverWithDetails); |
| 472 }; | 471 }; |
| 473 | 472 |
| 474 // Watches title changes on a tab, blocking until an expected title is set. | 473 // Watches title changes on a tab, blocking until an expected title is set. |
| 475 class TitleWatcher : public NotificationObserver { | 474 class TitleWatcher : public NotificationObserver { |
| 476 public: | 475 public: |
| 477 // |tab_contents| must be non-NULL and needs to stay alive for the | 476 // |tab_contents| must be non-NULL and needs to stay alive for the |
| 478 // entire lifetime of |this|. |expected_title| is the title that |this| | 477 // entire lifetime of |this|. |expected_title| is the title that |this| |
| 479 // will wait for. | 478 // will wait for. |
| 480 TitleWatcher(TabContents* tab_contents, const string16& expected_title); | 479 TitleWatcher(TabContents* tab_contents, const string16& expected_title); |
| 481 virtual ~TitleWatcher(); | 480 virtual ~TitleWatcher(); |
| 482 | 481 |
| 483 // Waits until the title for the tab is set to the |expected_title| | 482 // Waits until the title for the tab is set to the |expected_title| |
| 484 // passed into the constructor. | 483 // passed into the constructor. |
| 485 bool Wait() WARN_UNUSED_RESULT; | 484 bool Wait() WARN_UNUSED_RESULT; |
| 486 | 485 |
| 487 private: | 486 private: |
| 488 // NotificationObserver | 487 // NotificationObserver |
| 489 virtual void Observe(NotificationType type, | 488 virtual void Observe(int type, |
| 490 const NotificationSource& source, | 489 const NotificationSource& source, |
| 491 const NotificationDetails& details) OVERRIDE; | 490 const NotificationDetails& details) OVERRIDE; |
| 492 | 491 |
| 493 TabContents* expected_tab_; | 492 TabContents* expected_tab_; |
| 494 string16 expected_title_; | 493 string16 expected_title_; |
| 495 NotificationRegistrar notification_registrar_; | 494 NotificationRegistrar notification_registrar_; |
| 496 bool title_observed_; | 495 bool title_observed_; |
| 497 bool quit_loop_on_observation_; | 496 bool quit_loop_on_observation_; |
| 498 | 497 |
| 499 DISALLOW_COPY_AND_ASSIGN(TitleWatcher); | 498 DISALLOW_COPY_AND_ASSIGN(TitleWatcher); |
| 500 }; | 499 }; |
| 501 | 500 |
| 502 // See SendKeyPressAndWait. This function additionally performs a check on the | 501 // See SendKeyPressAndWait. This function additionally performs a check on the |
| 503 // NotificationDetails using the provided Details<U>. | 502 // NotificationDetails using the provided Details<U>. |
| 504 template <class U> | 503 template <class U> |
| 505 bool SendKeyPressAndWaitWithDetails( | 504 bool SendKeyPressAndWaitWithDetails( |
| 506 const Browser* browser, | 505 const Browser* browser, |
| 507 ui::KeyboardCode key, | 506 ui::KeyboardCode key, |
| 508 bool control, | 507 bool control, |
| 509 bool shift, | 508 bool shift, |
| 510 bool alt, | 509 bool alt, |
| 511 bool command, | 510 bool command, |
| 512 NotificationType type, | 511 int type, |
| 513 const NotificationSource& source, | 512 const NotificationSource& source, |
| 514 const Details<U>& details) WARN_UNUSED_RESULT; | 513 const Details<U>& details) WARN_UNUSED_RESULT; |
| 515 | 514 |
| 516 template <class U> | 515 template <class U> |
| 517 bool SendKeyPressAndWaitWithDetails( | 516 bool SendKeyPressAndWaitWithDetails( |
| 518 const Browser* browser, | 517 const Browser* browser, |
| 519 ui::KeyboardCode key, | 518 ui::KeyboardCode key, |
| 520 bool control, | 519 bool control, |
| 521 bool shift, | 520 bool shift, |
| 522 bool alt, | 521 bool alt, |
| 523 bool command, | 522 bool command, |
| 524 NotificationType type, | 523 int type, |
| 525 const NotificationSource& source, | 524 const NotificationSource& source, |
| 526 const Details<U>& details) { | 525 const Details<U>& details) { |
| 527 WindowedNotificationObserverWithDetails<U> observer(type, source); | 526 WindowedNotificationObserverWithDetails<U> observer(type, source); |
| 528 | 527 |
| 529 if (!SendKeyPressSync(browser, key, control, shift, alt, command)) | 528 if (!SendKeyPressSync(browser, key, control, shift, alt, command)) |
| 530 return false; | 529 return false; |
| 531 | 530 |
| 532 observer.Wait(); | 531 observer.Wait(); |
| 533 | 532 |
| 534 U my_details; | 533 U my_details; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 552 // DOMAutomationController. Do not construct this until the browser has | 551 // DOMAutomationController. Do not construct this until the browser has |
| 553 // started. | 552 // started. |
| 554 DOMMessageQueue(); | 553 DOMMessageQueue(); |
| 555 virtual ~DOMMessageQueue(); | 554 virtual ~DOMMessageQueue(); |
| 556 | 555 |
| 557 // Wait for the next message to arrive. |message| will be set to the next | 556 // Wait for the next message to arrive. |message| will be set to the next |
| 558 // message, if not null. Returns true on success. | 557 // message, if not null. Returns true on success. |
| 559 bool WaitForMessage(std::string* message) WARN_UNUSED_RESULT; | 558 bool WaitForMessage(std::string* message) WARN_UNUSED_RESULT; |
| 560 | 559 |
| 561 // Overridden NotificationObserver methods. | 560 // Overridden NotificationObserver methods. |
| 562 virtual void Observe(NotificationType type, | 561 virtual void Observe(int type, |
| 563 const NotificationSource& source, | 562 const NotificationSource& source, |
| 564 const NotificationDetails& details); | 563 const NotificationDetails& details); |
| 565 | 564 |
| 566 private: | 565 private: |
| 567 NotificationRegistrar registrar_; | 566 NotificationRegistrar registrar_; |
| 568 std::queue<std::string> message_queue_; | 567 std::queue<std::string> message_queue_; |
| 569 bool waiting_for_message_; | 568 bool waiting_for_message_; |
| 570 | 569 |
| 571 DISALLOW_COPY_AND_ASSIGN(DOMMessageQueue); | 570 DISALLOW_COPY_AND_ASSIGN(DOMMessageQueue); |
| 572 }; | 571 }; |
| 573 | 572 |
| 574 // Takes a snapshot of the given render widget, rendered at |page_size|. The | 573 // Takes a snapshot of the given render widget, rendered at |page_size|. The |
| 575 // snapshot is set to |bitmap|. Returns true on success. | 574 // snapshot is set to |bitmap|. Returns true on success. |
| 576 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh, | 575 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh, |
| 577 const gfx::Size& page_size, | 576 const gfx::Size& page_size, |
| 578 SkBitmap* bitmap) WARN_UNUSED_RESULT; | 577 SkBitmap* bitmap) WARN_UNUSED_RESULT; |
| 579 | 578 |
| 580 // Takes a snapshot of the entire page, according to the width and height | 579 // Takes a snapshot of the entire page, according to the width and height |
| 581 // properties of the DOM's document. Returns true on success. DOMAutomation | 580 // properties of the DOM's document. Returns true on success. DOMAutomation |
| 582 // must be enabled. | 581 // must be enabled. |
| 583 bool TakeEntirePageSnapshot(RenderViewHost* rvh, | 582 bool TakeEntirePageSnapshot(RenderViewHost* rvh, |
| 584 SkBitmap* bitmap) WARN_UNUSED_RESULT; | 583 SkBitmap* bitmap) WARN_UNUSED_RESULT; |
| 585 | 584 |
| 586 } // namespace ui_test_utils | 585 } // namespace ui_test_utils |
| 587 | 586 |
| 588 #endif // CHROME_TEST_UI_TEST_UTILS_H_ | 587 #endif // CHROME_TEST_UI_TEST_UTILS_H_ |
| OLD | NEW |