Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "chrome/test/ui_test_utils.h" | 5 #include "chrome/test/ui_test_utils.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 #endif | 41 #endif |
| 42 #include "gfx/size.h" | 42 #include "gfx/size.h" |
| 43 #include "googleurl/src/gurl.h" | 43 #include "googleurl/src/gurl.h" |
| 44 #include "net/base/net_util.h" | 44 #include "net/base/net_util.h" |
| 45 #include "testing/gtest/include/gtest/gtest.h" | 45 #include "testing/gtest/include/gtest/gtest.h" |
| 46 #include "third_party/skia/include/core/SkBitmap.h" | 46 #include "third_party/skia/include/core/SkBitmap.h" |
| 47 #include "third_party/skia/include/core/SkColor.h" | 47 #include "third_party/skia/include/core/SkColor.h" |
| 48 | 48 |
| 49 namespace ui_test_utils { | 49 namespace ui_test_utils { |
| 50 | 50 |
| 51 // Navigates the specified tab (via |disposition|) of |browser| to |url|, | |
| 52 // blocking until the |number_of_navigations| specified complete. | |
| 53 // |disposition| indicates what tab the download occurs in, and | |
| 54 // |browser_test_flags| controls what to wait for before continuing. | |
| 55 void NavigateToURLWithDispositionBlockUntilNavigationsComplete( | |
|
Paweł Hajdan Jr.
2011/01/08 08:07:56
Instead of the forward-declaration, could you just
ahendrickson
2011/01/09 01:14:16
Done.
| |
| 56 Browser* browser, | |
| 57 const GURL& url, | |
| 58 int number_of_navigations, | |
| 59 WindowOpenDisposition disposition, | |
| 60 int browser_test_flags); | |
| 61 | |
| 51 namespace { | 62 namespace { |
| 52 | 63 |
| 53 // Used to block until a navigation completes. | 64 // Used to block until a navigation completes. |
| 54 class NavigationNotificationObserver : public NotificationObserver { | 65 class NavigationNotificationObserver : public NotificationObserver { |
| 55 public: | 66 public: |
| 56 NavigationNotificationObserver(NavigationController* controller, | 67 NavigationNotificationObserver(NavigationController* controller, |
| 57 int number_of_navigations) | 68 int number_of_navigations) |
| 58 : navigation_started_(false), | 69 : navigation_started_(false), |
| 59 navigations_completed_(0), | 70 navigations_completed_(0), |
| 60 number_of_navigations_(number_of_navigations) { | 71 number_of_navigations_(number_of_navigations) { |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 425 Source<NavigationController>(controller)); | 436 Source<NavigationController>(controller)); |
| 426 } | 437 } |
| 427 | 438 |
| 428 Browser* WaitForNewBrowser() { | 439 Browser* WaitForNewBrowser() { |
| 429 TestNotificationObserver observer; | 440 TestNotificationObserver observer; |
| 430 RegisterAndWait(&observer, NotificationType::BROWSER_WINDOW_READY, | 441 RegisterAndWait(&observer, NotificationType::BROWSER_WINDOW_READY, |
| 431 NotificationService::AllSources()); | 442 NotificationService::AllSources()); |
| 432 return Source<Browser>(observer.source()).ptr(); | 443 return Source<Browser>(observer.source()).ptr(); |
| 433 } | 444 } |
| 434 | 445 |
| 446 Browser* WaitForBrowserNotInSet(std::set<Browser*> excluded_browsers) { | |
| 447 TestNotificationObserver observer; | |
| 448 Browser* new_browser = GetBrowserNotInSet(excluded_browsers); | |
| 449 if (new_browser == NULL) { | |
| 450 new_browser = WaitForNewBrowser(); | |
| 451 // The new browser should never be in |excluded_browsers|. | |
| 452 DCHECK(!ContainsKey(excluded_browsers, new_browser)); | |
| 453 } | |
| 454 return new_browser; | |
| 455 } | |
| 456 | |
| 435 void OpenURLOffTheRecord(Profile* profile, const GURL& url) { | 457 void OpenURLOffTheRecord(Profile* profile, const GURL& url) { |
| 436 Browser::OpenURLOffTheRecord(profile, url); | 458 Browser::OpenURLOffTheRecord(profile, url); |
| 437 Browser* browser = BrowserList::FindBrowserWithType( | 459 Browser* browser = BrowserList::FindBrowserWithType( |
| 438 profile->GetOffTheRecordProfile(), Browser::TYPE_NORMAL, false); | 460 profile->GetOffTheRecordProfile(), Browser::TYPE_NORMAL, false); |
| 439 WaitForNavigations(&browser->GetSelectedTabContents()->controller(), 1); | 461 WaitForNavigations(&browser->GetSelectedTabContents()->controller(), 1); |
| 440 } | 462 } |
| 441 | 463 |
| 442 void NavigateToURL(Browser* browser, const GURL& url) { | 464 void NavigateToURL(Browser* browser, const GURL& url) { |
| 443 NavigateToURLBlockUntilNavigationsComplete(browser, url, 1); | 465 NavigateToURLWithDisposition(browser, url, CURRENT_TAB, |
| 466 BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 467 } | |
| 468 | |
| 469 void NavigateToURLWithDisposition(Browser* browser, | |
| 470 const GURL& url, | |
| 471 WindowOpenDisposition disposition, | |
| 472 int browser_test_flags) { | |
| 473 NavigateToURLWithDispositionBlockUntilNavigationsComplete( | |
| 474 browser, | |
| 475 url, | |
| 476 1, | |
| 477 disposition, | |
| 478 browser_test_flags); | |
| 444 } | 479 } |
| 445 | 480 |
| 446 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, | 481 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, |
| 447 const GURL& url, | 482 const GURL& url, |
| 448 int number_of_navigations) { | 483 int number_of_navigations) { |
| 449 NavigationController* controller = | 484 NavigateToURLWithDispositionBlockUntilNavigationsComplete( |
| 450 &browser->GetSelectedTabContents()->controller(); | 485 browser, |
| 451 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED); | 486 url, |
| 452 WaitForNavigations(controller, number_of_navigations); | 487 number_of_navigations, |
| 488 CURRENT_TAB, | |
| 489 BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 490 } | |
| 491 | |
| 492 void NavigateToURLWithDispositionBlockUntilNavigationsComplete( | |
| 493 Browser* browser, | |
| 494 const GURL& url, | |
| 495 int number_of_navigations, | |
| 496 WindowOpenDisposition disposition, | |
| 497 int browser_test_flags) { | |
| 498 std::set<Browser*> initial_browsers; | |
| 499 for (std::vector<Browser*>::const_iterator iter = BrowserList::begin(); | |
| 500 iter != BrowserList::end(); | |
| 501 ++iter) { | |
| 502 initial_browsers.insert(*iter); | |
| 503 } | |
| 504 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED); | |
| 505 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_BROWSER) | |
| 506 browser = WaitForBrowserNotInSet(initial_browsers); | |
| 507 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_TAB) | |
| 508 WaitForNotification(NotificationType::TAB_ADDED); | |
| 509 if (!(browser_test_flags & BROWSER_TEST_WAIT_FOR_NAVIGATION)) { | |
| 510 // Some other flag caused the wait prior to this. | |
| 511 return; | |
| 512 } | |
| 513 TabContents* tab_contents = NULL; | |
| 514 if (disposition == NEW_BACKGROUND_TAB) { | |
| 515 // We've opened up a new tab, but not selected it. | |
| 516 tab_contents = browser->GetTabContentsAt(browser->selected_index() + 1); | |
| 517 EXPECT_TRUE(tab_contents != NULL) | |
| 518 << " Unable to wait for navigation to \"" << url.spec() | |
| 519 << "\" because the new tab is not available yet"; | |
| 520 return; | |
| 521 } else if ((disposition == CURRENT_TAB) || | |
| 522 (disposition == NEW_FOREGROUND_TAB) || | |
| 523 (disposition == SINGLETON_TAB)) { | |
| 524 // The currently selected tab is the right one. | |
| 525 tab_contents = browser->GetSelectedTabContents(); | |
| 526 } | |
| 527 if (tab_contents) { | |
| 528 NavigationController* controller = &tab_contents->controller(); | |
| 529 WaitForNavigations(controller, number_of_navigations); | |
| 530 return; | |
| 531 } | |
| 532 EXPECT_TRUE(NULL != tab_contents) << " Unable to wait for navigation to \"" | |
| 533 << url.spec() << "\"" | |
| 534 << " because we can't get the tab contents"; | |
| 453 } | 535 } |
| 454 | 536 |
| 455 DOMElementProxyRef GetActiveDOMDocument(Browser* browser) { | 537 DOMElementProxyRef GetActiveDOMDocument(Browser* browser) { |
| 456 JavaScriptExecutionController* executor = | 538 JavaScriptExecutionController* executor = |
| 457 new InProcessJavaScriptExecutionController( | 539 new InProcessJavaScriptExecutionController( |
| 458 browser->GetSelectedTabContents()->render_view_host()); | 540 browser->GetSelectedTabContents()->render_view_host()); |
| 459 int element_handle; | 541 int element_handle; |
| 460 executor->ExecuteJavaScriptAndGetReturn("document;", &element_handle); | 542 executor->ExecuteJavaScriptAndGetReturn("document;", &element_handle); |
| 461 return executor->GetObjectProxy<DOMElementProxy>(element_handle); | 543 return executor->GetObjectProxy<DOMElementProxy>(element_handle); |
| 462 } | 544 } |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 556 if (ordinal) | 638 if (ordinal) |
| 557 *ordinal = observer.active_match_ordinal(); | 639 *ordinal = observer.active_match_ordinal(); |
| 558 return observer.number_of_matches(); | 640 return observer.number_of_matches(); |
| 559 } | 641 } |
| 560 | 642 |
| 561 void WaitForNotification(NotificationType type) { | 643 void WaitForNotification(NotificationType type) { |
| 562 TestNotificationObserver observer; | 644 TestNotificationObserver observer; |
| 563 RegisterAndWait(&observer, type, NotificationService::AllSources()); | 645 RegisterAndWait(&observer, type, NotificationService::AllSources()); |
| 564 } | 646 } |
| 565 | 647 |
| 648 void WaitForNotificationFrom(NotificationType type, | |
| 649 const NotificationSource& source) { | |
| 650 TestNotificationObserver observer; | |
| 651 RegisterAndWait(&observer, type, source); | |
| 652 } | |
| 653 | |
| 566 void RegisterAndWait(NotificationObserver* observer, | 654 void RegisterAndWait(NotificationObserver* observer, |
| 567 NotificationType type, | 655 NotificationType type, |
| 568 const NotificationSource& source) { | 656 const NotificationSource& source) { |
| 569 NotificationRegistrar registrar; | 657 NotificationRegistrar registrar; |
| 570 registrar.Add(observer, type, source); | 658 registrar.Add(observer, type, source); |
| 571 RunMessageLoop(); | 659 RunMessageLoop(); |
| 572 } | 660 } |
| 573 | 661 |
| 574 void WaitForBookmarkModelToLoad(BookmarkModel* model) { | 662 void WaitForBookmarkModelToLoad(BookmarkModel* model) { |
| 575 if (model->IsLoaded()) | 663 if (model->IsLoaded()) |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 599 | 687 |
| 600 bool BringBrowserWindowToFront(const Browser* browser) { | 688 bool BringBrowserWindowToFront(const Browser* browser) { |
| 601 gfx::NativeWindow window = NULL; | 689 gfx::NativeWindow window = NULL; |
| 602 if (!GetNativeWindow(browser, &window)) | 690 if (!GetNativeWindow(browser, &window)) |
| 603 return false; | 691 return false; |
| 604 | 692 |
| 605 ui_test_utils::ShowAndFocusNativeWindow(window); | 693 ui_test_utils::ShowAndFocusNativeWindow(window); |
| 606 return true; | 694 return true; |
| 607 } | 695 } |
| 608 | 696 |
| 697 Browser* GetBrowserNotInSet(std::set<Browser*> excluded_browsers) { | |
| 698 for (BrowserList::const_iterator iter = BrowserList::begin(); | |
| 699 iter != BrowserList::end(); | |
| 700 ++iter) { | |
| 701 if (excluded_browsers.find(*iter) == excluded_browsers.end()) | |
| 702 return *iter; | |
| 703 } | |
| 704 | |
| 705 return NULL; | |
| 706 } | |
| 707 | |
| 609 bool SendKeyPressSync(const Browser* browser, | 708 bool SendKeyPressSync(const Browser* browser, |
| 610 app::KeyboardCode key, | 709 app::KeyboardCode key, |
| 611 bool control, | 710 bool control, |
| 612 bool shift, | 711 bool shift, |
| 613 bool alt, | 712 bool alt, |
| 614 bool command) { | 713 bool command) { |
| 615 base::TimeTicks start_time = base::TimeTicks::Now(); | 714 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 616 | 715 |
| 617 gfx::NativeWindow window = NULL; | 716 gfx::NativeWindow window = NULL; |
| 618 if (!GetNativeWindow(browser, &window)) | 717 if (!GetNativeWindow(browser, &window)) |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 912 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); | 1011 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); |
| 913 } | 1012 } |
| 914 | 1013 |
| 915 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { | 1014 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { |
| 916 DCHECK(bitmap); | 1015 DCHECK(bitmap); |
| 917 SnapshotTaker taker; | 1016 SnapshotTaker taker; |
| 918 return taker.TakeEntirePageSnapshot(rvh, bitmap); | 1017 return taker.TakeEntirePageSnapshot(rvh, bitmap); |
| 919 } | 1018 } |
| 920 | 1019 |
| 921 } // namespace ui_test_utils | 1020 } // namespace ui_test_utils |
| OLD | NEW |