diff options
author | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-10 19:33:18 +0000 |
---|---|---|
committer | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-10 19:33:18 +0000 |
commit | a1d5396ed9f2101d021031675eab55aafb3a2894 (patch) | |
tree | 256d3e518348bbc962075591a9922720422f3dd8 | |
parent | 7a2a9ce8a5fd79c2fbb5bf3db7a7d58bbd73ce80 (diff) | |
download | chromium_src-a1d5396ed9f2101d021031675eab55aafb3a2894.zip chromium_src-a1d5396ed9f2101d021031675eab55aafb3a2894.tar.gz chromium_src-a1d5396ed9f2101d021031675eab55aafb3a2894.tar.bz2 |
Convert JavascriptAlertActivatesTab to browser_tests framework.
This should make it non-flaky, so I un-disabled the test. I also added necessary plumbing so we can
wait for AppModalDialog to appear and close it.
TEST=Covered by browser_tests.
http://crbug.com/16062
Review URL: http://codereview.chromium.org/149460
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20400 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/app_modal_dialog.cc | 24 | ||||
-rw-r--r-- | chrome/browser/app_modal_dialog.h | 3 | ||||
-rw-r--r-- | chrome/browser/browser_browsertest.cc | 19 | ||||
-rw-r--r-- | chrome/browser/browser_uitest.cc | 19 | ||||
-rw-r--r-- | chrome/common/notification_type.h | 10 | ||||
-rw-r--r-- | chrome/test/ui_test_utils.cc | 40 | ||||
-rw-r--r-- | chrome/test/ui_test_utils.h | 5 |
7 files changed, 98 insertions, 22 deletions
diff --git a/chrome/browser/app_modal_dialog.cc b/chrome/browser/app_modal_dialog.cc index afabff8..aa876cc 100644 --- a/chrome/browser/app_modal_dialog.cc +++ b/chrome/browser/app_modal_dialog.cc @@ -50,6 +50,13 @@ void AppModalDialog::Observe(NotificationType type, CloseModalDialog(); } +void AppModalDialog::SendCloseNotification() { + NotificationService::current()->Notify( + NotificationType::APP_MODAL_DIALOG_CLOSED, + Source<AppModalDialog>(this), + NotificationService::NoDetails()); +} + void AppModalDialog::InitNotifications() { // Make sure we get navigation notifications so we know when our parent // contents will disappear or navigate to a different page. @@ -70,6 +77,11 @@ void AppModalDialog::ShowModalDialog() { tab_contents_->Activate(); CreateAndShowDialog(); + + NotificationService::current()->Notify( + NotificationType::APP_MODAL_DIALOG_SHOWN, + Source<AppModalDialog>(this), + NotificationService::NoDetails()); } void AppModalDialog::OnCancel() { @@ -85,6 +97,8 @@ void AppModalDialog::OnCancel() { tab_contents_->OnJavaScriptMessageBoxClosed(reply_msg_, false, std::wstring()); } + + SendCloseNotification(); } void AppModalDialog::OnAccept(const std::wstring& prompt_text, @@ -98,10 +112,14 @@ void AppModalDialog::OnAccept(const std::wstring& prompt_text, if (suppress_js_messages) tab_contents()->set_suppress_javascript_messages(true); } + + SendCloseNotification(); } void AppModalDialog::OnClose() { - if (tab_contents_) { - tab_contents_->OnJavaScriptMessageBoxWindowDestroyed(); - } + if (tab_contents_) { + tab_contents_->OnJavaScriptMessageBoxWindowDestroyed(); + } + + SendCloseNotification(); } diff --git a/chrome/browser/app_modal_dialog.h b/chrome/browser/app_modal_dialog.h index 325c222..8d44d64 100644 --- a/chrome/browser/app_modal_dialog.h +++ b/chrome/browser/app_modal_dialog.h @@ -95,6 +95,9 @@ class AppModalDialog : public NotificationObserver { const NotificationSource& source, const NotificationDetails& details); + // Sends APP_MODAL_DIALOG_CLOSED notification. + void SendCloseNotification(); + NotificationRegistrar registrar_; // A reference to the platform native dialog box. diff --git a/chrome/browser/browser_browsertest.cc b/chrome/browser/browser_browsertest.cc index 69ef1b8..f209837 100644 --- a/chrome/browser/browser_browsertest.cc +++ b/chrome/browser/browser_browsertest.cc @@ -5,9 +5,11 @@ #include <string> #include "app/l10n_util.h" +#include "chrome/browser/app_modal_dialog.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/tab_contents/tab_contents.h" +#include "chrome/common/page_transition_types.h" #include "chrome/test/in_process_browser_test.h" #include "chrome/test/ui_test_utils.h" #include "testing/gtest/include/gtest/gtest.h" @@ -79,3 +81,20 @@ IN_PROC_BROWSER_TEST_F(BrowserTest, Title) { ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &tab_title)); EXPECT_EQ(WideToUTF16(test_title), tab_title); } + +IN_PROC_BROWSER_TEST_F(BrowserTest, JavascriptAlertActivatesTab) { + GURL url(ui_test_utils::GetTestUrl(L".", L"title1.html")); + ui_test_utils::NavigateToURL(browser(), url); + browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, + true, 0, false, NULL); + EXPECT_EQ(2, browser()->tab_count()); + EXPECT_EQ(0, browser()->selected_index()); + TabContents* second_tab = browser()->GetTabContentsAt(1); + ASSERT_TRUE(second_tab); + second_tab->render_view_host()->ExecuteJavascriptInWebFrame(L"", + L"alert('Activate!');"); + AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog(); + alert->CloseModalDialog(); + EXPECT_EQ(2, browser()->tab_count()); + EXPECT_EQ(1, browser()->selected_index()); +} diff --git a/chrome/browser/browser_uitest.cc b/chrome/browser/browser_uitest.cc index a65499f..57fc6b5 100644 --- a/chrome/browser/browser_uitest.cc +++ b/chrome/browser/browser_uitest.cc @@ -113,25 +113,6 @@ TEST_F(BrowserTest, WindowsSessionEnd) { } #endif -// This test is flakey, see bug 5668 for details. -TEST_F(BrowserTest, DISABLED_JavascriptAlertActivatesTab) { - scoped_refptr<BrowserProxy> window(automation()->GetBrowserWindow(0)); - int start_index; - ASSERT_TRUE(window->GetActiveTabIndex(&start_index)); - ASSERT_TRUE(window->AppendTab(GURL("about:blank"))); - int javascript_tab_index; - ASSERT_TRUE(window->GetActiveTabIndex(&javascript_tab_index)); - scoped_refptr<TabProxy> javascript_tab = window->GetActiveTab(); - ASSERT_TRUE(javascript_tab.get()); - // Switch back to the starting tab, then send the second tab a javascript - // alert, which should force it to become active. - ASSERT_TRUE(window->ActivateTab(start_index)); - ASSERT_TRUE( - javascript_tab->NavigateToURLAsync(GURL("javascript:alert('Alert!')"))); - ASSERT_TRUE(window->WaitForTabToBecomeActive(javascript_tab_index, - action_max_timeout_ms())); -} - // Test that scripts can fork a new renderer process for a tab in a particular // case (which matches following a link in Gmail). The script must open a new // tab, set its window.opener to null, and redirect it to a cross-site URL. diff --git a/chrome/common/notification_type.h b/chrome/common/notification_type.h index 93cbbaf..d56a0b0 100644 --- a/chrome/common/notification_type.h +++ b/chrome/common/notification_type.h @@ -215,6 +215,16 @@ class NotificationType { // Linux. ACTIVE_WINDOW_CHANGED, + // Application-modal dialogs ----------------------------------------------- + + // Sent after an application-modal dialog has been shown. The source + // is the dialog. + APP_MODAL_DIALOG_SHOWN, + + // Sent after an application-modal dialog has been closed. The source + // is the dialog. + APP_MODAL_DIALOG_CLOSED, + // Tabs -------------------------------------------------------------------- // This notification is sent after a tab has been appended to the diff --git a/chrome/test/ui_test_utils.cc b/chrome/test/ui_test_utils.cc index 4f65c35..8167968 100644 --- a/chrome/test/ui_test_utils.cc +++ b/chrome/test/ui_test_utils.cc @@ -194,6 +194,41 @@ class DownloadsCompleteObserver : public DownloadManager::Observer, DISALLOW_COPY_AND_ASSIGN(DownloadsCompleteObserver); }; +// Used to block until an application modal dialog is shown. +class AppModalDialogObserver : public NotificationObserver { + public: + AppModalDialogObserver() {} + + AppModalDialog* WaitForAppModalDialog() { + registrar_.Add(this, NotificationType::APP_MODAL_DIALOG_SHOWN, + NotificationService::AllSources()); + dialog_ = NULL; + ui_test_utils::RunMessageLoop(); + DCHECK(dialog_); + return dialog_; + } + + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + if (type == NotificationType::APP_MODAL_DIALOG_SHOWN) { + registrar_.Remove(this, NotificationType::APP_MODAL_DIALOG_SHOWN, + NotificationService::AllSources()); + dialog_ = Source<AppModalDialog>(source).ptr(); + MessageLoopForUI::current()->Quit(); + } else { + NOTREACHED(); + } + } + + private: + NotificationRegistrar registrar_; + + AppModalDialog* dialog_; + + DISALLOW_COPY_AND_ASSIGN(AppModalDialogObserver); +}; + } // namespace void RunMessageLoop() { @@ -330,4 +365,9 @@ void WaitForDownloadCount(DownloadManager* download_manager, size_t count) { DownloadsCompleteObserver download_observer(download_manager, count); } +AppModalDialog* WaitForAppModalDialog() { + AppModalDialogObserver observer; + return observer.WaitForAppModalDialog(); +} + } // namespace ui_test_utils diff --git a/chrome/test/ui_test_utils.h b/chrome/test/ui_test_utils.h index 34c8545..3b2aef9 100644 --- a/chrome/test/ui_test_utils.h +++ b/chrome/test/ui_test_utils.h @@ -11,6 +11,7 @@ #include "base/string16.h" #include "chrome/common/notification_observer.h" +class AppModalDialog; class Browser; class DownloadManager; class GURL; @@ -85,6 +86,10 @@ GURL GetTestUrl(const std::wstring& dir, const std::wstring file); // Creates an observer that waits for |download_manager| to report that it // has a total of |count| downloads that have been handles void WaitForDownloadCount(DownloadManager* download_manager, size_t count); + +// Blocks until an application modal dialog is showns and returns it. +AppModalDialog* WaitForAppModalDialog(); + } #endif // CHROME_TEST_UI_TEST_UTILS_H_ |