| 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 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 Source<NavigationController>(controller)); | 425 Source<NavigationController>(controller)); |
| 426 } | 426 } |
| 427 | 427 |
| 428 Browser* WaitForNewBrowser() { | 428 Browser* WaitForNewBrowser() { |
| 429 TestNotificationObserver observer; | 429 TestNotificationObserver observer; |
| 430 RegisterAndWait(&observer, NotificationType::BROWSER_WINDOW_READY, | 430 RegisterAndWait(&observer, NotificationType::BROWSER_WINDOW_READY, |
| 431 NotificationService::AllSources()); | 431 NotificationService::AllSources()); |
| 432 return Source<Browser>(observer.source()).ptr(); | 432 return Source<Browser>(observer.source()).ptr(); |
| 433 } | 433 } |
| 434 | 434 |
| 435 Browser* WaitForBrowserNotInSet(std::set<Browser*> excluded_browsers) { |
| 436 TestNotificationObserver observer; |
| 437 Browser* new_browser = GetBrowserNotInSet(excluded_browsers); |
| 438 if (new_browser == NULL) { |
| 439 new_browser = WaitForNewBrowser(); |
| 440 // The new browser should never be in |excluded_browsers|. |
| 441 DCHECK(!ContainsKey(excluded_browsers, new_browser)); |
| 442 } |
| 443 return new_browser; |
| 444 } |
| 445 |
| 435 void OpenURLOffTheRecord(Profile* profile, const GURL& url) { | 446 void OpenURLOffTheRecord(Profile* profile, const GURL& url) { |
| 436 Browser::OpenURLOffTheRecord(profile, url); | 447 Browser::OpenURLOffTheRecord(profile, url); |
| 437 Browser* browser = BrowserList::FindBrowserWithType( | 448 Browser* browser = BrowserList::FindBrowserWithType( |
| 438 profile->GetOffTheRecordProfile(), Browser::TYPE_NORMAL, false); | 449 profile->GetOffTheRecordProfile(), Browser::TYPE_NORMAL, false); |
| 439 WaitForNavigations(&browser->GetSelectedTabContents()->controller(), 1); | 450 WaitForNavigations(&browser->GetSelectedTabContents()->controller(), 1); |
| 440 } | 451 } |
| 441 | 452 |
| 442 void NavigateToURL(Browser* browser, const GURL& url) { | 453 void NavigateToURL(Browser* browser, const GURL& url) { |
| 443 NavigateToURLBlockUntilNavigationsComplete(browser, url, 1); | 454 NavigateToURLWithDisposition(browser, url, CURRENT_TAB, |
| 455 BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 456 } |
| 457 |
| 458 // Navigates the specified tab (via |disposition|) of |browser| to |url|, |
| 459 // blocking until the |number_of_navigations| specified complete. |
| 460 // |disposition| indicates what tab the download occurs in, and |
| 461 // |browser_test_flags| controls what to wait for before continuing. |
| 462 static void NavigateToURLWithDispositionBlockUntilNavigationsComplete( |
| 463 Browser* browser, |
| 464 const GURL& url, |
| 465 int number_of_navigations, |
| 466 WindowOpenDisposition disposition, |
| 467 int browser_test_flags) { |
| 468 std::set<Browser*> initial_browsers; |
| 469 for (std::vector<Browser*>::const_iterator iter = BrowserList::begin(); |
| 470 iter != BrowserList::end(); |
| 471 ++iter) { |
| 472 initial_browsers.insert(*iter); |
| 473 } |
| 474 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED); |
| 475 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_BROWSER) |
| 476 browser = WaitForBrowserNotInSet(initial_browsers); |
| 477 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_TAB) |
| 478 WaitForNotification(NotificationType::TAB_ADDED); |
| 479 if (!(browser_test_flags & BROWSER_TEST_WAIT_FOR_NAVIGATION)) { |
| 480 // Some other flag caused the wait prior to this. |
| 481 return; |
| 482 } |
| 483 TabContents* tab_contents = NULL; |
| 484 if (disposition == NEW_BACKGROUND_TAB) { |
| 485 // We've opened up a new tab, but not selected it. |
| 486 tab_contents = browser->GetTabContentsAt(browser->selected_index() + 1); |
| 487 EXPECT_TRUE(tab_contents != NULL) |
| 488 << " Unable to wait for navigation to \"" << url.spec() |
| 489 << "\" because the new tab is not available yet"; |
| 490 return; |
| 491 } else if ((disposition == CURRENT_TAB) || |
| 492 (disposition == NEW_FOREGROUND_TAB) || |
| 493 (disposition == SINGLETON_TAB)) { |
| 494 // The currently selected tab is the right one. |
| 495 tab_contents = browser->GetSelectedTabContents(); |
| 496 } |
| 497 if (tab_contents) { |
| 498 NavigationController* controller = &tab_contents->controller(); |
| 499 WaitForNavigations(controller, number_of_navigations); |
| 500 return; |
| 501 } |
| 502 EXPECT_TRUE(NULL != tab_contents) << " Unable to wait for navigation to \"" |
| 503 << url.spec() << "\"" |
| 504 << " because we can't get the tab contents"; |
| 505 } |
| 506 |
| 507 void NavigateToURLWithDisposition(Browser* browser, |
| 508 const GURL& url, |
| 509 WindowOpenDisposition disposition, |
| 510 int browser_test_flags) { |
| 511 NavigateToURLWithDispositionBlockUntilNavigationsComplete( |
| 512 browser, |
| 513 url, |
| 514 1, |
| 515 disposition, |
| 516 browser_test_flags); |
| 444 } | 517 } |
| 445 | 518 |
| 446 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, | 519 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, |
| 447 const GURL& url, | 520 const GURL& url, |
| 448 int number_of_navigations) { | 521 int number_of_navigations) { |
| 449 NavigationController* controller = | 522 NavigateToURLWithDispositionBlockUntilNavigationsComplete( |
| 450 &browser->GetSelectedTabContents()->controller(); | 523 browser, |
| 451 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED); | 524 url, |
| 452 WaitForNavigations(controller, number_of_navigations); | 525 number_of_navigations, |
| 526 CURRENT_TAB, |
| 527 BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 453 } | 528 } |
| 454 | 529 |
| 455 DOMElementProxyRef GetActiveDOMDocument(Browser* browser) { | 530 DOMElementProxyRef GetActiveDOMDocument(Browser* browser) { |
| 456 JavaScriptExecutionController* executor = | 531 JavaScriptExecutionController* executor = |
| 457 new InProcessJavaScriptExecutionController( | 532 new InProcessJavaScriptExecutionController( |
| 458 browser->GetSelectedTabContents()->render_view_host()); | 533 browser->GetSelectedTabContents()->render_view_host()); |
| 459 int element_handle; | 534 int element_handle; |
| 460 executor->ExecuteJavaScriptAndGetReturn("document;", &element_handle); | 535 executor->ExecuteJavaScriptAndGetReturn("document;", &element_handle); |
| 461 return executor->GetObjectProxy<DOMElementProxy>(element_handle); | 536 return executor->GetObjectProxy<DOMElementProxy>(element_handle); |
| 462 } | 537 } |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 if (ordinal) | 631 if (ordinal) |
| 557 *ordinal = observer.active_match_ordinal(); | 632 *ordinal = observer.active_match_ordinal(); |
| 558 return observer.number_of_matches(); | 633 return observer.number_of_matches(); |
| 559 } | 634 } |
| 560 | 635 |
| 561 void WaitForNotification(NotificationType type) { | 636 void WaitForNotification(NotificationType type) { |
| 562 TestNotificationObserver observer; | 637 TestNotificationObserver observer; |
| 563 RegisterAndWait(&observer, type, NotificationService::AllSources()); | 638 RegisterAndWait(&observer, type, NotificationService::AllSources()); |
| 564 } | 639 } |
| 565 | 640 |
| 641 void WaitForNotificationFrom(NotificationType type, |
| 642 const NotificationSource& source) { |
| 643 TestNotificationObserver observer; |
| 644 RegisterAndWait(&observer, type, source); |
| 645 } |
| 646 |
| 566 void RegisterAndWait(NotificationObserver* observer, | 647 void RegisterAndWait(NotificationObserver* observer, |
| 567 NotificationType type, | 648 NotificationType type, |
| 568 const NotificationSource& source) { | 649 const NotificationSource& source) { |
| 569 NotificationRegistrar registrar; | 650 NotificationRegistrar registrar; |
| 570 registrar.Add(observer, type, source); | 651 registrar.Add(observer, type, source); |
| 571 RunMessageLoop(); | 652 RunMessageLoop(); |
| 572 } | 653 } |
| 573 | 654 |
| 574 void WaitForBookmarkModelToLoad(BookmarkModel* model) { | 655 void WaitForBookmarkModelToLoad(BookmarkModel* model) { |
| 575 if (model->IsLoaded()) | 656 if (model->IsLoaded()) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 599 | 680 |
| 600 bool BringBrowserWindowToFront(const Browser* browser) { | 681 bool BringBrowserWindowToFront(const Browser* browser) { |
| 601 gfx::NativeWindow window = NULL; | 682 gfx::NativeWindow window = NULL; |
| 602 if (!GetNativeWindow(browser, &window)) | 683 if (!GetNativeWindow(browser, &window)) |
| 603 return false; | 684 return false; |
| 604 | 685 |
| 605 ui_test_utils::ShowAndFocusNativeWindow(window); | 686 ui_test_utils::ShowAndFocusNativeWindow(window); |
| 606 return true; | 687 return true; |
| 607 } | 688 } |
| 608 | 689 |
| 690 Browser* GetBrowserNotInSet(std::set<Browser*> excluded_browsers) { |
| 691 for (BrowserList::const_iterator iter = BrowserList::begin(); |
| 692 iter != BrowserList::end(); |
| 693 ++iter) { |
| 694 if (excluded_browsers.find(*iter) == excluded_browsers.end()) |
| 695 return *iter; |
| 696 } |
| 697 |
| 698 return NULL; |
| 699 } |
| 700 |
| 609 bool SendKeyPressSync(const Browser* browser, | 701 bool SendKeyPressSync(const Browser* browser, |
| 610 app::KeyboardCode key, | 702 app::KeyboardCode key, |
| 611 bool control, | 703 bool control, |
| 612 bool shift, | 704 bool shift, |
| 613 bool alt, | 705 bool alt, |
| 614 bool command) { | 706 bool command) { |
| 615 base::TimeTicks start_time = base::TimeTicks::Now(); | 707 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 616 | 708 |
| 617 gfx::NativeWindow window = NULL; | 709 gfx::NativeWindow window = NULL; |
| 618 if (!GetNativeWindow(browser, &window)) | 710 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); | 1004 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); |
| 913 } | 1005 } |
| 914 | 1006 |
| 915 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { | 1007 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { |
| 916 DCHECK(bitmap); | 1008 DCHECK(bitmap); |
| 917 SnapshotTaker taker; | 1009 SnapshotTaker taker; |
| 918 return taker.TakeEntirePageSnapshot(rvh, bitmap); | 1010 return taker.TakeEntirePageSnapshot(rvh, bitmap); |
| 919 } | 1011 } |
| 920 | 1012 |
| 921 } // namespace ui_test_utils | 1013 } // namespace ui_test_utils |
| OLD | NEW |