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 // Waits for a new tab to be added anywhere. | |
| 52 Browser* WaitForAnyNewTab(); | |
| 53 | |
| 54 // Navigates the specified tab (via |disposition|) of |browser| to |url|, | |
| 55 // blocking until the |number_of_navigations| specified complete. | |
| 56 // |disposition| indicates what tab the download occurs in, and | |
| 57 // |browser_test_flags| controls what to wait for before continuing. | |
| 58 void NavigateToURLWithDispositionBlockUntilNavigationsComplete( | |
| 59 Browser* browser, | |
| 60 const GURL& url, | |
| 61 int number_of_navigations, | |
| 62 WindowOpenDisposition disposition, | |
| 63 int browser_test_flags); | |
| 64 | |
| 51 namespace { | 65 namespace { |
| 52 | 66 |
| 53 // Used to block until a navigation completes. | 67 // Used to block until a navigation completes. |
| 54 class NavigationNotificationObserver : public NotificationObserver { | 68 class NavigationNotificationObserver : public NotificationObserver { |
| 55 public: | 69 public: |
| 56 NavigationNotificationObserver(NavigationController* controller, | 70 NavigationNotificationObserver(NavigationController* controller, |
| 57 int number_of_navigations) | 71 int number_of_navigations) |
| 58 : navigation_started_(false), | 72 : navigation_started_(false), |
| 59 navigations_completed_(0), | 73 navigations_completed_(0), |
| 60 number_of_navigations_(number_of_navigations) { | 74 number_of_navigations_(number_of_navigations) { |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 400 | 414 |
| 401 void WaitForNavigation(NavigationController* controller) { | 415 void WaitForNavigation(NavigationController* controller) { |
| 402 WaitForNavigations(controller, 1); | 416 WaitForNavigations(controller, 1); |
| 403 } | 417 } |
| 404 | 418 |
| 405 void WaitForNavigations(NavigationController* controller, | 419 void WaitForNavigations(NavigationController* controller, |
| 406 int number_of_navigations) { | 420 int number_of_navigations) { |
| 407 NavigationNotificationObserver observer(controller, number_of_navigations); | 421 NavigationNotificationObserver observer(controller, number_of_navigations); |
| 408 } | 422 } |
| 409 | 423 |
| 424 void WaitForWindowClosed(Browser* browser) { | |
|
Paweł Hajdan Jr.
2011/01/03 09:06:47
I'd strongly prefer to add another variant of Wait
ahendrickson
2011/01/05 00:32:09
Done.
| |
| 425 TestNotificationObserver observer; | |
| 426 RegisterAndWait(&observer, | |
| 427 NotificationType::BROWSER_CLOSED, | |
| 428 Source<Browser>(browser)); | |
| 429 } | |
| 430 | |
| 431 Browser* WaitForAnyNewTab() { | |
| 432 TestNotificationObserver observer; | |
| 433 RegisterAndWait(&observer, | |
| 434 NotificationType::TAB_ADDED, | |
| 435 NotificationService::AllSources()); | |
| 436 return Source<Browser>(observer.source()).ptr(); | |
| 437 } | |
| 438 | |
| 410 void WaitForNewTab(Browser* browser) { | 439 void WaitForNewTab(Browser* browser) { |
| 411 TestNotificationObserver observer; | 440 TestNotificationObserver observer; |
| 412 RegisterAndWait(&observer, NotificationType::TAB_ADDED, | 441 RegisterAndWait(&observer, NotificationType::TAB_ADDED, |
| 413 Source<Browser>(browser)); | 442 Source<Browser>(browser)); |
| 414 } | 443 } |
| 415 | 444 |
| 416 void WaitForBrowserActionUpdated(ExtensionAction* browser_action) { | 445 void WaitForBrowserActionUpdated(ExtensionAction* browser_action) { |
| 417 TestNotificationObserver observer; | 446 TestNotificationObserver observer; |
| 418 RegisterAndWait(&observer, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | 447 RegisterAndWait(&observer, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, |
| 419 Source<ExtensionAction>(browser_action)); | 448 Source<ExtensionAction>(browser_action)); |
| 420 } | 449 } |
| 421 | 450 |
| 422 void WaitForLoadStop(NavigationController* controller) { | 451 void WaitForLoadStop(NavigationController* controller) { |
| 423 TestNotificationObserver observer; | 452 TestNotificationObserver observer; |
| 424 RegisterAndWait(&observer, NotificationType::LOAD_STOP, | 453 RegisterAndWait(&observer, NotificationType::LOAD_STOP, |
| 425 Source<NavigationController>(controller)); | 454 Source<NavigationController>(controller)); |
| 426 } | 455 } |
| 427 | 456 |
| 428 Browser* WaitForNewBrowser() { | 457 Browser* WaitForNewBrowser() { |
| 429 TestNotificationObserver observer; | 458 TestNotificationObserver observer; |
| 430 RegisterAndWait(&observer, NotificationType::BROWSER_WINDOW_READY, | 459 RegisterAndWait(&observer, NotificationType::BROWSER_WINDOW_READY, |
| 431 NotificationService::AllSources()); | 460 NotificationService::AllSources()); |
| 432 return Source<Browser>(observer.source()).ptr(); | 461 Browser* new_browser = Source<Browser>(observer.source()).ptr(); |
| 462 return new_browser; | |
| 463 } | |
| 464 | |
| 465 Browser* WaitForNewBrowserWithCount(size_t start_count) { | |
|
Paweł Hajdan Jr.
2011/01/03 09:06:47
This function seems quite confusing. Can we avoid
ahendrickson
2011/01/05 00:32:09
Replaced the function.
| |
| 466 TestNotificationObserver observer; | |
| 467 size_t cur_count = BrowserList::size(); | |
| 468 Browser* new_browser = NULL; | |
| 469 if (cur_count <= start_count) { | |
| 470 new_browser = WaitForNewBrowser(); | |
| 471 } else { | |
| 472 int index = start_count; | |
| 473 new_browser = GetBrowser(index); | |
| 474 } | |
| 475 return new_browser; | |
| 433 } | 476 } |
| 434 | 477 |
| 435 void OpenURLOffTheRecord(Profile* profile, const GURL& url) { | 478 void OpenURLOffTheRecord(Profile* profile, const GURL& url) { |
| 436 Browser::OpenURLOffTheRecord(profile, url); | 479 Browser::OpenURLOffTheRecord(profile, url); |
| 437 Browser* browser = BrowserList::FindBrowserWithType( | 480 Browser* browser = BrowserList::FindBrowserWithType( |
| 438 profile->GetOffTheRecordProfile(), Browser::TYPE_NORMAL, false); | 481 profile->GetOffTheRecordProfile(), Browser::TYPE_NORMAL, false); |
| 439 WaitForNavigations(&browser->GetSelectedTabContents()->controller(), 1); | 482 WaitForNavigations(&browser->GetSelectedTabContents()->controller(), 1); |
| 440 } | 483 } |
| 441 | 484 |
| 442 void NavigateToURL(Browser* browser, const GURL& url) { | 485 void NavigateToURL(Browser* browser, const GURL& url) { |
| 443 NavigateToURLBlockUntilNavigationsComplete(browser, url, 1); | 486 NavigateToURLWithDisposition(browser, url, CURRENT_TAB, |
| 487 BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 488 } | |
| 489 | |
| 490 void NavigateToURLWithDisposition(Browser* browser, | |
| 491 const GURL& url, | |
| 492 WindowOpenDisposition disposition, | |
| 493 int browser_test_flags) { | |
| 494 NavigateToURLWithDispositionBlockUntilNavigationsComplete( | |
| 495 browser, | |
| 496 url, | |
| 497 1, | |
| 498 disposition, | |
| 499 browser_test_flags); | |
| 444 } | 500 } |
| 445 | 501 |
| 446 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, | 502 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, |
| 447 const GURL& url, | 503 const GURL& url, |
| 448 int number_of_navigations) { | 504 int number_of_navigations) { |
| 449 NavigationController* controller = | 505 NavigateToURLWithDispositionBlockUntilNavigationsComplete( |
| 450 &browser->GetSelectedTabContents()->controller(); | 506 browser, |
| 451 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED); | 507 url, |
| 452 WaitForNavigations(controller, number_of_navigations); | 508 number_of_navigations, |
| 509 CURRENT_TAB, | |
| 510 BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 511 } | |
| 512 | |
| 513 void NavigateToURLWithDispositionBlockUntilNavigationsComplete( | |
| 514 Browser* browser, | |
| 515 const GURL& url, | |
| 516 int number_of_navigations, | |
| 517 WindowOpenDisposition disposition, | |
| 518 int browser_test_flags) { | |
| 519 size_t browser_count = BrowserList::size(); | |
| 520 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED); | |
| 521 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_BROWSER) | |
| 522 browser = WaitForNewBrowserWithCount(browser_count); | |
| 523 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_TAB) | |
| 524 WaitForAnyNewTab(); | |
| 525 if (!(browser_test_flags & BROWSER_TEST_WAIT_FOR_NAVIGATION)) | |
|
Paweł Hajdan Jr.
2011/01/03 09:06:47
This seems to contradict the function's name. :-(
ahendrickson
2011/01/05 00:32:09
Added a comment explaining this.
| |
| 526 return; | |
| 527 TabContents* tab_contents = NULL; | |
| 528 if (disposition == NEW_BACKGROUND_TAB) { | |
| 529 // We've opened up a new tab, but not selected it. | |
| 530 tab_contents = browser->GetTabContentsAt(browser->selected_index() + 1); | |
| 531 EXPECT_TRUE(tab_contents != NULL) | |
| 532 << " Unable to wait for navigation to \"" << url.spec() | |
| 533 << "\" because the new tab is not available yet"; | |
| 534 return; | |
| 535 } else if ((disposition == CURRENT_TAB) || | |
| 536 (disposition == NEW_FOREGROUND_TAB) || | |
| 537 (disposition == SINGLETON_TAB)) { | |
| 538 // The currently selected tab is the right one. | |
| 539 tab_contents = browser->GetSelectedTabContents(); | |
| 540 } | |
| 541 if (tab_contents) { | |
| 542 NavigationController* controller = &tab_contents->controller(); | |
| 543 WaitForNavigations(controller, number_of_navigations); | |
| 544 return; | |
| 545 } | |
| 546 EXPECT_TRUE(NULL != tab_contents) << " Unable to wait for navigation to \"" | |
| 547 << url.spec() << "\"" | |
| 548 << " because we can't get the tab contents"; | |
| 453 } | 549 } |
| 454 | 550 |
| 455 DOMElementProxyRef GetActiveDOMDocument(Browser* browser) { | 551 DOMElementProxyRef GetActiveDOMDocument(Browser* browser) { |
| 456 JavaScriptExecutionController* executor = | 552 JavaScriptExecutionController* executor = |
| 457 new InProcessJavaScriptExecutionController( | 553 new InProcessJavaScriptExecutionController( |
| 458 browser->GetSelectedTabContents()->render_view_host()); | 554 browser->GetSelectedTabContents()->render_view_host()); |
| 459 int element_handle; | 555 int element_handle; |
| 460 executor->ExecuteJavaScriptAndGetReturn("document;", &element_handle); | 556 executor->ExecuteJavaScriptAndGetReturn("document;", &element_handle); |
| 461 return executor->GetObjectProxy<DOMElementProxy>(element_handle); | 557 return executor->GetObjectProxy<DOMElementProxy>(element_handle); |
| 462 } | 558 } |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 599 | 695 |
| 600 bool BringBrowserWindowToFront(const Browser* browser) { | 696 bool BringBrowserWindowToFront(const Browser* browser) { |
| 601 gfx::NativeWindow window = NULL; | 697 gfx::NativeWindow window = NULL; |
| 602 if (!GetNativeWindow(browser, &window)) | 698 if (!GetNativeWindow(browser, &window)) |
| 603 return false; | 699 return false; |
| 604 | 700 |
| 605 ui_test_utils::ShowAndFocusNativeWindow(window); | 701 ui_test_utils::ShowAndFocusNativeWindow(window); |
| 606 return true; | 702 return true; |
| 607 } | 703 } |
| 608 | 704 |
| 705 Browser* GetBrowser(int index) { | |
|
Paweł Hajdan Jr.
2011/01/03 09:06:47
This seems more like something to be added to Brow
ahendrickson
2011/01/05 00:32:09
No longer using this -- replaced with a more appro
| |
| 706 if (index >= 0) { | |
| 707 BrowserList::const_iterator iter = BrowserList::begin(); | |
| 708 for ( ; (iter != BrowserList::end()) && (index > 0); ++iter, --index) {} | |
| 709 if (iter != BrowserList::end()) { | |
| 710 return *iter; | |
| 711 } | |
| 712 } | |
| 713 return NULL; | |
| 714 } | |
| 715 | |
| 609 bool SendKeyPressSync(const Browser* browser, | 716 bool SendKeyPressSync(const Browser* browser, |
| 610 app::KeyboardCode key, | 717 app::KeyboardCode key, |
| 611 bool control, | 718 bool control, |
| 612 bool shift, | 719 bool shift, |
| 613 bool alt, | 720 bool alt, |
| 614 bool command) { | 721 bool command) { |
| 615 base::TimeTicks start_time = base::TimeTicks::Now(); | 722 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 616 | 723 |
| 617 gfx::NativeWindow window = NULL; | 724 gfx::NativeWindow window = NULL; |
| 618 if (!GetNativeWindow(browser, &window)) | 725 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); | 1019 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); |
| 913 } | 1020 } |
| 914 | 1021 |
| 915 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { | 1022 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { |
| 916 DCHECK(bitmap); | 1023 DCHECK(bitmap); |
| 917 SnapshotTaker taker; | 1024 SnapshotTaker taker; |
| 918 return taker.TakeEntirePageSnapshot(rvh, bitmap); | 1025 return taker.TakeEntirePageSnapshot(rvh, bitmap); |
| 919 } | 1026 } |
| 920 | 1027 |
| 921 } // namespace ui_test_utils | 1028 } // namespace ui_test_utils |
| OLD | NEW |