| 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 #include "chrome/test/ui/ui_test.h" | 5 #include "chrome/test/ui/ui_test.h" |
| 6 | 6 |
| 7 #if defined(OS_POSIX) | 7 #if defined(OS_POSIX) |
| 8 #include <signal.h> | 8 #include <signal.h> |
| 9 #include <sys/types.h> | 9 #include <sys/types.h> |
| 10 #endif | 10 #endif |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 scoped_refptr<BrowserProxy> window = | 290 scoped_refptr<BrowserProxy> window = |
| 291 automation()->GetBrowserWindow(window_index); | 291 automation()->GetBrowserWindow(window_index); |
| 292 ASSERT_TRUE(window.get()); | 292 ASSERT_TRUE(window.get()); |
| 293 scoped_refptr<TabProxy> tab_proxy(window->GetTab(tab_index)); | 293 scoped_refptr<TabProxy> tab_proxy(window->GetTab(tab_index)); |
| 294 ASSERT_TRUE(tab_proxy.get()); | 294 ASSERT_TRUE(tab_proxy.get()); |
| 295 EXPECT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, | 295 EXPECT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, |
| 296 tab_proxy->NavigateToURLBlockUntilNavigationsComplete( | 296 tab_proxy->NavigateToURLBlockUntilNavigationsComplete( |
| 297 url, number_of_navigations)) << url.spec(); | 297 url, number_of_navigations)) << url.spec(); |
| 298 } | 298 } |
| 299 | 299 |
| 300 bool UITestBase::WaitForBrowserProcessToQuit(int timeout) { | |
| 301 return launcher_->WaitForBrowserProcessToQuit(timeout); | |
| 302 } | |
| 303 | |
| 304 bool UITestBase::WaitForBookmarkBarVisibilityChange(BrowserProxy* browser, | 300 bool UITestBase::WaitForBookmarkBarVisibilityChange(BrowserProxy* browser, |
| 305 bool wait_for_open) { | 301 bool wait_for_open) { |
| 306 const int kCycles = 10; | 302 const int kCycles = 10; |
| 307 for (int i = 0; i < kCycles; i++) { | 303 for (int i = 0; i < kCycles; i++) { |
| 308 bool visible = false; | 304 bool visible = false; |
| 309 bool animating = true; | 305 bool animating = true; |
| 310 if (!browser->GetBookmarkBarVisibility(&visible, &animating)) | 306 if (!browser->GetBookmarkBarVisibility(&visible, &animating)) |
| 311 return false; // Some error. | 307 return false; // Some error. |
| 312 if (visible == wait_for_open && !animating) | 308 if (visible == wait_for_open && !animating) |
| 313 return true; // Bookmark bar visibility change complete. | 309 return true; // Bookmark bar visibility change complete. |
| 314 | 310 |
| 315 // Give it a chance to catch up. | 311 // Give it a chance to catch up. |
| 316 bool browser_survived = CrashAwareSleep( | 312 base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms() / kCycles); |
| 317 TestTimeouts::action_timeout_ms() / kCycles); | |
| 318 EXPECT_TRUE(browser_survived); | |
| 319 if (!browser_survived) | |
| 320 return false; | |
| 321 } | 313 } |
| 322 | 314 |
| 323 ADD_FAILURE() << "Timeout reached in WaitForBookmarkBarVisibilityChange"; | 315 ADD_FAILURE() << "Timeout reached in WaitForBookmarkBarVisibilityChange"; |
| 324 return false; | 316 return false; |
| 325 } | 317 } |
| 326 | 318 |
| 327 GURL UITestBase::GetActiveTabURL(int window_index) { | 319 GURL UITestBase::GetActiveTabURL(int window_index) { |
| 328 scoped_refptr<TabProxy> tab_proxy(GetActiveTab(window_index)); | 320 scoped_refptr<TabProxy> tab_proxy(GetActiveTab(window_index)); |
| 329 EXPECT_TRUE(tab_proxy.get()); | 321 EXPECT_TRUE(tab_proxy.get()); |
| 330 if (!tab_proxy.get()) | 322 if (!tab_proxy.get()) |
| (...skipping 27 matching lines...) Expand all Loading... |
| 358 | 350 |
| 359 int active_tab_index = -1; | 351 int active_tab_index = -1; |
| 360 EXPECT_TRUE(window_proxy->GetActiveTabIndex(&active_tab_index)); | 352 EXPECT_TRUE(window_proxy->GetActiveTabIndex(&active_tab_index)); |
| 361 return active_tab_index; | 353 return active_tab_index; |
| 362 } | 354 } |
| 363 | 355 |
| 364 bool UITestBase::IsBrowserRunning() { | 356 bool UITestBase::IsBrowserRunning() { |
| 365 return launcher_->IsBrowserRunning(); | 357 return launcher_->IsBrowserRunning(); |
| 366 } | 358 } |
| 367 | 359 |
| 368 bool UITestBase::CrashAwareSleep(int timeout_ms) { | |
| 369 return launcher_->CrashAwareSleep(timeout_ms); | |
| 370 } | |
| 371 | |
| 372 int UITestBase::GetTabCount() { | 360 int UITestBase::GetTabCount() { |
| 373 return GetTabCount(0); | 361 return GetTabCount(0); |
| 374 } | 362 } |
| 375 | 363 |
| 376 int UITestBase::GetTabCount(int window_index) { | 364 int UITestBase::GetTabCount(int window_index) { |
| 377 scoped_refptr<BrowserProxy> window( | 365 scoped_refptr<BrowserProxy> window( |
| 378 automation()->GetBrowserWindow(window_index)); | 366 automation()->GetBrowserWindow(window_index)); |
| 379 EXPECT_TRUE(window.get()); | 367 EXPECT_TRUE(window.get()); |
| 380 if (!window.get()) | 368 if (!window.get()) |
| 381 return 0; | 369 return 0; |
| 382 | 370 |
| 383 int result = 0; | 371 int result = 0; |
| 384 EXPECT_TRUE(window->GetTabCount(&result)); | 372 EXPECT_TRUE(window->GetTabCount(&result)); |
| 385 | 373 |
| 386 return result; | 374 return result; |
| 387 } | 375 } |
| 388 | 376 |
| 389 void UITestBase::WaitUntilTabCount(int tab_count) { | 377 void UITestBase::WaitUntilTabCount(int tab_count) { |
| 390 const int kMaxIntervals = 10; | 378 const int kMaxIntervals = 10; |
| 391 const int kIntervalMs = TestTimeouts::action_timeout_ms() / kMaxIntervals; | 379 const int kIntervalMs = TestTimeouts::action_timeout_ms() / kMaxIntervals; |
| 392 | 380 |
| 393 for (int i = 0; i < kMaxIntervals; ++i) { | 381 for (int i = 0; i < kMaxIntervals; ++i) { |
| 394 bool browser_survived = CrashAwareSleep(kIntervalMs); | |
| 395 EXPECT_TRUE(browser_survived); | |
| 396 if (!browser_survived) | |
| 397 return; | |
| 398 if (GetTabCount() == tab_count) | 382 if (GetTabCount() == tab_count) |
| 399 return; | 383 return; |
| 384 |
| 385 base::PlatformThread::Sleep(kIntervalMs); |
| 400 } | 386 } |
| 401 | 387 |
| 402 ADD_FAILURE() << "Timeout reached in WaitUntilTabCount"; | 388 ADD_FAILURE() << "Timeout reached in WaitUntilTabCount"; |
| 403 } | 389 } |
| 404 | 390 |
| 405 FilePath UITestBase::GetDownloadDirectory() { | 391 FilePath UITestBase::GetDownloadDirectory() { |
| 406 scoped_refptr<TabProxy> tab_proxy(GetActiveTab()); | 392 scoped_refptr<TabProxy> tab_proxy(GetActiveTab()); |
| 407 EXPECT_TRUE(tab_proxy.get()); | 393 EXPECT_TRUE(tab_proxy.get()); |
| 408 if (!tab_proxy.get()) | 394 if (!tab_proxy.get()) |
| 409 return FilePath(); | 395 return FilePath(); |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 | 706 |
| 721 bool UITest::WaitUntilJavaScriptCondition(TabProxy* tab, | 707 bool UITest::WaitUntilJavaScriptCondition(TabProxy* tab, |
| 722 const std::wstring& frame_xpath, | 708 const std::wstring& frame_xpath, |
| 723 const std::wstring& jscript, | 709 const std::wstring& jscript, |
| 724 int timeout_ms) { | 710 int timeout_ms) { |
| 725 const int kIntervalMs = 250; | 711 const int kIntervalMs = 250; |
| 726 const int kMaxIntervals = timeout_ms / kIntervalMs; | 712 const int kMaxIntervals = timeout_ms / kIntervalMs; |
| 727 | 713 |
| 728 // Wait until the test signals it has completed. | 714 // Wait until the test signals it has completed. |
| 729 for (int i = 0; i < kMaxIntervals; ++i) { | 715 for (int i = 0; i < kMaxIntervals; ++i) { |
| 730 bool browser_survived = CrashAwareSleep(kIntervalMs); | |
| 731 EXPECT_TRUE(browser_survived); | |
| 732 if (!browser_survived) | |
| 733 return false; | |
| 734 | |
| 735 bool done_value = false; | 716 bool done_value = false; |
| 736 bool success = tab->ExecuteAndExtractBool(frame_xpath, jscript, | 717 bool success = tab->ExecuteAndExtractBool(frame_xpath, jscript, |
| 737 &done_value); | 718 &done_value); |
| 738 EXPECT_TRUE(success); | 719 EXPECT_TRUE(success); |
| 739 if (!success) | 720 if (!success) |
| 740 return false; | 721 return false; |
| 741 if (done_value) | 722 if (done_value) |
| 742 return true; | 723 return true; |
| 724 |
| 725 base::PlatformThread::Sleep(kIntervalMs); |
| 743 } | 726 } |
| 744 | 727 |
| 745 ADD_FAILURE() << "Timeout reached in WaitUntilJavaScriptCondition"; | 728 ADD_FAILURE() << "Timeout reached in WaitUntilJavaScriptCondition"; |
| 746 return false; | 729 return false; |
| 747 } | 730 } |
| 748 | 731 |
| 749 bool UITest::WaitUntilCookieValue(TabProxy* tab, | 732 bool UITest::WaitUntilCookieValue(TabProxy* tab, |
| 750 const GURL& url, | 733 const GURL& url, |
| 751 const char* cookie_name, | 734 const char* cookie_name, |
| 752 int timeout_ms, | 735 int timeout_ms, |
| 753 const char* expected_value) { | 736 const char* expected_value) { |
| 754 const int kIntervalMs = 250; | 737 const int kIntervalMs = 250; |
| 755 const int kMaxIntervals = timeout_ms / kIntervalMs; | 738 const int kMaxIntervals = timeout_ms / kIntervalMs; |
| 756 | 739 |
| 757 std::string cookie_value; | 740 std::string cookie_value; |
| 758 for (int i = 0; i < kMaxIntervals; ++i) { | 741 for (int i = 0; i < kMaxIntervals; ++i) { |
| 759 bool browser_survived = CrashAwareSleep(kIntervalMs); | |
| 760 EXPECT_TRUE(browser_survived); | |
| 761 if (!browser_survived) | |
| 762 return false; | |
| 763 | |
| 764 EXPECT_TRUE(tab->GetCookieByName(url, cookie_name, &cookie_value)); | 742 EXPECT_TRUE(tab->GetCookieByName(url, cookie_name, &cookie_value)); |
| 765 if (cookie_value == expected_value) | 743 if (cookie_value == expected_value) |
| 766 return true; | 744 return true; |
| 745 |
| 746 base::PlatformThread::Sleep(kIntervalMs); |
| 767 } | 747 } |
| 768 | 748 |
| 769 ADD_FAILURE() << "Timeout reached in WaitUntilCookieValue"; | 749 ADD_FAILURE() << "Timeout reached in WaitUntilCookieValue"; |
| 770 return false; | 750 return false; |
| 771 } | 751 } |
| 772 | 752 |
| 773 std::string UITest::WaitUntilCookieNonEmpty(TabProxy* tab, | 753 std::string UITest::WaitUntilCookieNonEmpty(TabProxy* tab, |
| 774 const GURL& url, | 754 const GURL& url, |
| 775 const char* cookie_name, | 755 const char* cookie_name, |
| 776 int timeout_ms) { | 756 int timeout_ms) { |
| 777 const int kIntervalMs = 250; | 757 const int kIntervalMs = 250; |
| 778 const int kMaxIntervals = timeout_ms / kIntervalMs; | 758 const int kMaxIntervals = timeout_ms / kIntervalMs; |
| 779 | 759 |
| 780 for (int i = 0; i < kMaxIntervals; ++i) { | 760 for (int i = 0; i < kMaxIntervals; ++i) { |
| 781 bool browser_survived = CrashAwareSleep(kIntervalMs); | |
| 782 EXPECT_TRUE(browser_survived); | |
| 783 if (!browser_survived) | |
| 784 return std::string(); | |
| 785 | |
| 786 std::string cookie_value; | 761 std::string cookie_value; |
| 787 EXPECT_TRUE(tab->GetCookieByName(url, cookie_name, &cookie_value)); | 762 EXPECT_TRUE(tab->GetCookieByName(url, cookie_name, &cookie_value)); |
| 788 if (!cookie_value.empty()) | 763 if (!cookie_value.empty()) |
| 789 return cookie_value; | 764 return cookie_value; |
| 765 |
| 766 base::PlatformThread::Sleep(kIntervalMs); |
| 790 } | 767 } |
| 791 | 768 |
| 792 ADD_FAILURE() << "Timeout reached in WaitUntilCookieNonEmpty"; | 769 ADD_FAILURE() << "Timeout reached in WaitUntilCookieNonEmpty"; |
| 793 return std::string(); | 770 return std::string(); |
| 794 } | 771 } |
| 795 | 772 |
| 796 bool UITest::WaitForDownloadShelfVisible(BrowserProxy* browser) { | 773 bool UITest::WaitForDownloadShelfVisible(BrowserProxy* browser) { |
| 797 return WaitForDownloadShelfVisibilityChange(browser, true); | 774 return WaitForDownloadShelfVisibilityChange(browser, true); |
| 798 } | 775 } |
| 799 | 776 |
| 800 bool UITest::WaitForDownloadShelfInvisible(BrowserProxy* browser) { | 777 bool UITest::WaitForDownloadShelfInvisible(BrowserProxy* browser) { |
| 801 return WaitForDownloadShelfVisibilityChange(browser, false); | 778 return WaitForDownloadShelfVisibilityChange(browser, false); |
| 802 } | 779 } |
| 803 | 780 |
| 804 bool UITest::WaitForFindWindowVisibilityChange(BrowserProxy* browser, | 781 bool UITest::WaitForFindWindowVisibilityChange(BrowserProxy* browser, |
| 805 bool wait_for_open) { | 782 bool wait_for_open) { |
| 806 const int kCycles = 10; | 783 const int kCycles = 10; |
| 807 for (int i = 0; i < kCycles; i++) { | 784 for (int i = 0; i < kCycles; i++) { |
| 808 bool visible = false; | 785 bool visible = false; |
| 809 if (!browser->IsFindWindowFullyVisible(&visible)) | 786 if (!browser->IsFindWindowFullyVisible(&visible)) |
| 810 return false; // Some error. | 787 return false; // Some error. |
| 811 if (visible == wait_for_open) | 788 if (visible == wait_for_open) |
| 812 return true; // Find window visibility change complete. | 789 return true; // Find window visibility change complete. |
| 813 | 790 |
| 814 // Give it a chance to catch up. | 791 // Give it a chance to catch up. |
| 815 bool browser_survived = CrashAwareSleep( | 792 base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms() / kCycles); |
| 816 TestTimeouts::action_timeout_ms() / kCycles); | |
| 817 EXPECT_TRUE(browser_survived); | |
| 818 if (!browser_survived) | |
| 819 return false; | |
| 820 } | 793 } |
| 821 | 794 |
| 822 ADD_FAILURE() << "Timeout reached in WaitForFindWindowVisibilityChange"; | 795 ADD_FAILURE() << "Timeout reached in WaitForFindWindowVisibilityChange"; |
| 823 return false; | 796 return false; |
| 824 } | 797 } |
| 825 | 798 |
| 826 void UITest::TerminateBrowser() { | 799 void UITest::TerminateBrowser() { |
| 827 launcher_->TerminateBrowser(); | 800 launcher_->TerminateBrowser(); |
| 828 } | 801 } |
| 829 | 802 |
| 830 void UITest::NavigateToURLAsync(const GURL& url) { | 803 void UITest::NavigateToURLAsync(const GURL& url) { |
| 831 scoped_refptr<TabProxy> tab_proxy(GetActiveTab()); | 804 scoped_refptr<TabProxy> tab_proxy(GetActiveTab()); |
| 832 ASSERT_TRUE(tab_proxy.get()); | 805 ASSERT_TRUE(tab_proxy.get()); |
| 833 ASSERT_TRUE(tab_proxy->NavigateToURLAsync(url)); | 806 ASSERT_TRUE(tab_proxy->NavigateToURLAsync(url)); |
| 834 } | 807 } |
| 835 | 808 |
| 836 bool UITest::WaitForDownloadShelfVisibilityChange(BrowserProxy* browser, | 809 bool UITest::WaitForDownloadShelfVisibilityChange(BrowserProxy* browser, |
| 837 bool wait_for_open) { | 810 bool wait_for_open) { |
| 838 const int kCycles = 10; | 811 const int kCycles = 10; |
| 839 int fail_count = 0; | 812 int fail_count = 0; |
| 840 int incorrect_state_count = 0; | 813 int incorrect_state_count = 0; |
| 841 base::Time start = base::Time::Now(); | 814 base::Time start = base::Time::Now(); |
| 842 for (int i = 0; i < kCycles; i++) { | 815 for (int i = 0; i < kCycles; i++) { |
| 843 // Give it a chance to catch up. | |
| 844 bool browser_survived = CrashAwareSleep( | |
| 845 TestTimeouts::action_timeout_ms() / kCycles); | |
| 846 EXPECT_TRUE(browser_survived); | |
| 847 if (!browser_survived) { | |
| 848 LOG(INFO) << "Elapsed time: " << (base::Time::Now() - start).InSecondsF() | |
| 849 << " seconds" | |
| 850 << " call failed " << fail_count << " times" | |
| 851 << " state was incorrect " << incorrect_state_count << " times"; | |
| 852 ADD_FAILURE() << "Browser failed in " << __FUNCTION__; | |
| 853 return false; | |
| 854 } | |
| 855 | |
| 856 bool visible = !wait_for_open; | 816 bool visible = !wait_for_open; |
| 857 if (!browser->IsShelfVisible(&visible)) { | 817 if (!browser->IsShelfVisible(&visible)) { |
| 858 fail_count++; | 818 fail_count++; |
| 859 continue; | 819 continue; |
| 860 } | 820 } |
| 861 if (visible == wait_for_open) { | 821 if (visible == wait_for_open) { |
| 862 LOG(INFO) << "Elapsed time: " << (base::Time::Now() - start).InSecondsF() | 822 LOG(INFO) << "Elapsed time: " << (base::Time::Now() - start).InSecondsF() |
| 863 << " seconds" | 823 << " seconds" |
| 864 << " call failed " << fail_count << " times" | 824 << " call failed " << fail_count << " times" |
| 865 << " state was incorrect " << incorrect_state_count << " times"; | 825 << " state was incorrect " << incorrect_state_count << " times"; |
| 866 return true; // Got the download shelf. | 826 return true; // Got the download shelf. |
| 867 } | 827 } |
| 868 incorrect_state_count++; | 828 incorrect_state_count++; |
| 829 |
| 830 // Give it a chance to catch up. |
| 831 base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms() / kCycles); |
| 869 } | 832 } |
| 870 | 833 |
| 871 LOG(INFO) << "Elapsed time: " << (base::Time::Now() - start).InSecondsF() | 834 LOG(INFO) << "Elapsed time: " << (base::Time::Now() - start).InSecondsF() |
| 872 << " seconds" | 835 << " seconds" |
| 873 << " call failed " << fail_count << " times" | 836 << " call failed " << fail_count << " times" |
| 874 << " state was incorrect " << incorrect_state_count << " times"; | 837 << " state was incorrect " << incorrect_state_count << " times"; |
| 875 ADD_FAILURE() << "Timeout reached in " << __FUNCTION__; | 838 ADD_FAILURE() << "Timeout reached in " << __FUNCTION__; |
| 876 return false; | 839 return false; |
| 877 } | 840 } |
| OLD | NEW |