diff options
Diffstat (limited to 'chrome/browser/automation/testing_automation_provider.cc')
-rw-r--r-- | chrome/browser/automation/testing_automation_provider.cc | 289 |
1 files changed, 289 insertions, 0 deletions
diff --git a/chrome/browser/automation/testing_automation_provider.cc b/chrome/browser/automation/testing_automation_provider.cc index 5ec755b..e7abd68 100644 --- a/chrome/browser/automation/testing_automation_provider.cc +++ b/chrome/browser/automation/testing_automation_provider.cc @@ -4,9 +4,13 @@ #include "chrome/browser/automation/testing_automation_provider.h" +#include "app/message_box_flags.h" #include "base/command_line.h" +#include "base/time.h" #include "base/utf_string_conversions.h" #include "chrome/app/chrome_dll_resource.h" +#include "chrome/browser/app_modal_dialog.h" +#include "chrome/browser/app_modal_dialog_queue.h" #include "chrome/browser/autocomplete/autocomplete_edit.h" #include "chrome/browser/automation/automation_autocomplete_edit_tracker.h" #include "chrome/browser/automation/automation_browser_tracker.h" @@ -17,15 +21,19 @@ #include "chrome/browser/automation/ui_controls.h" #include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/bookmarks/bookmark_storage.h" +#include "chrome/browser/blocked_popup_container.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/browser_window.h" #include "chrome/browser/debugger/devtools_manager.h" +#include "chrome/browser/download/download_shelf.h" #include "chrome/browser/find_bar.h" #include "chrome/browser/location_bar.h" #include "chrome/browser/login_prompt.h" #include "chrome/browser/platform_util.h" +#include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/renderer_host/render_process_host.h" #include "chrome/browser/renderer_host/render_view_host.h" +#include "chrome/browser/tab_contents/infobar_delegate.h" #include "chrome/browser/tab_contents/interstitial_page.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/net/url_request_context_getter.h" @@ -343,6 +351,40 @@ void TestingAutomationProvider::OnMessageReceived( SetBookmarkURL) IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBookmark, RemoveBookmark) + IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount) + IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept, + ClickInfoBarAccept) + IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime, + GetLastNavigationTime) + IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation, + WaitForNavigation) + IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference) + IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog, + GetShowingAppModalDialog) + IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton, + ClickAppModalDialogButton) + IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference) + IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference, + GetBooleanPreference) + IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference, + SetBooleanPreference) + IPC_MESSAGE_HANDLER_DELAY_REPLY( + AutomationMsg_WaitForBrowserWindowCountToBecome, + WaitForBrowserWindowCountToBecome) + IPC_MESSAGE_HANDLER_DELAY_REPLY( + AutomationMsg_WaitForAppModalDialogToBeShown, + WaitForAppModalDialogToBeShown) + IPC_MESSAGE_HANDLER_DELAY_REPLY( + AutomationMsg_GoBackBlockUntilNavigationsComplete, + GoBackBlockUntilNavigationsComplete) + IPC_MESSAGE_HANDLER_DELAY_REPLY( + AutomationMsg_GoForwardBlockUntilNavigationsComplete, + GoForwardBlockUntilNavigationsComplete) + IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser, + SavePackageShouldPromptUser) + IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle) + IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility) + IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount) IPC_MESSAGE_UNHANDLED(AutomationProvider::OnMessageReceived(message)); IPC_END_MESSAGE_MAP() @@ -1627,6 +1669,253 @@ void TestingAutomationProvider::RemoveBookmark(int handle, *success = false; } +void TestingAutomationProvider::GetInfoBarCount(int handle, int* count) { + *count = -1; // -1 means error. + if (tab_tracker_->ContainsHandle(handle)) { + NavigationController* nav_controller = tab_tracker_->GetResource(handle); + if (nav_controller) + *count = nav_controller->tab_contents()->infobar_delegate_count(); + } +} + +void TestingAutomationProvider::ClickInfoBarAccept( + int handle, + int info_bar_index, + bool wait_for_navigation, + IPC::Message* reply_message) { + bool success = false; + if (tab_tracker_->ContainsHandle(handle)) { + NavigationController* nav_controller = tab_tracker_->GetResource(handle); + if (nav_controller) { + int count = nav_controller->tab_contents()->infobar_delegate_count(); + if (info_bar_index >= 0 && info_bar_index < count) { + if (wait_for_navigation) { + AddNavigationStatusListener(nav_controller, reply_message, 1, false); + } + InfoBarDelegate* delegate = + nav_controller->tab_contents()->GetInfoBarDelegateAt( + info_bar_index); + if (delegate->AsConfirmInfoBarDelegate()) + delegate->AsConfirmInfoBarDelegate()->Accept(); + success = true; + } + } + } + + // This "!wait_for_navigation || !success condition" logic looks suspicious. + // It will send a failure message when success is true but + // |wait_for_navigation| is false. + // TODO(phajdan.jr): investgate whether the reply param (currently + // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success. + if (!wait_for_navigation || !success) + AutomationMsg_ClickInfoBarAccept::WriteReplyParams( + reply_message, AUTOMATION_MSG_NAVIGATION_ERROR); +} + +void TestingAutomationProvider::GetLastNavigationTime( + int handle, + int64* last_navigation_time) { + base::Time time(tab_tracker_->GetLastNavigationTime(handle)); + *last_navigation_time = time.ToInternalValue(); +} + +void TestingAutomationProvider::WaitForNavigation(int handle, + int64 last_navigation_time, + IPC::Message* reply_message) { + NavigationController* controller = tab_tracker_->GetResource(handle); + base::Time time(tab_tracker_->GetLastNavigationTime(handle)); + + if (time.ToInternalValue() > last_navigation_time || !controller) { + AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message, + controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR : + AUTOMATION_MSG_NAVIGATION_SUCCESS); + Send(reply_message); + return; + } + + AddNavigationStatusListener(controller, reply_message, 1, true); +} + +void TestingAutomationProvider::SetIntPreference(int handle, + const std::string& name, + int value, + bool* success) { + *success = false; + if (browser_tracker_->ContainsHandle(handle)) { + Browser* browser = browser_tracker_->GetResource(handle); + browser->profile()->GetPrefs()->SetInteger(name.c_str(), value); + *success = true; + } +} + +void TestingAutomationProvider::SetStringPreference(int handle, + const std::string& name, + const std::string& value, + bool* success) { + *success = false; + if (browser_tracker_->ContainsHandle(handle)) { + Browser* browser = browser_tracker_->GetResource(handle); + browser->profile()->GetPrefs()->SetString(name.c_str(), value); + *success = true; + } +} + +void TestingAutomationProvider::GetBooleanPreference(int handle, + const std::string& name, + bool* success, + bool* value) { + *success = false; + *value = false; + if (browser_tracker_->ContainsHandle(handle)) { + Browser* browser = browser_tracker_->GetResource(handle); + *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str()); + *success = true; + } +} + +void TestingAutomationProvider::SetBooleanPreference(int handle, + const std::string& name, + bool value, + bool* success) { + *success = false; + if (browser_tracker_->ContainsHandle(handle)) { + Browser* browser = browser_tracker_->GetResource(handle); + browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value); + *success = true; + } +} + +void TestingAutomationProvider::GetShowingAppModalDialog(bool* showing_dialog, + int* dialog_button) { + AppModalDialog* dialog_delegate = + Singleton<AppModalDialogQueue>()->active_dialog(); + *showing_dialog = (dialog_delegate != NULL); + if (*showing_dialog) + *dialog_button = dialog_delegate->GetDialogButtons(); + else + *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE; +} + +void TestingAutomationProvider::ClickAppModalDialogButton(int button, + bool* success) { + *success = false; + + AppModalDialog* dialog_delegate = + Singleton<AppModalDialogQueue>()->active_dialog(); + if (dialog_delegate && + (dialog_delegate->GetDialogButtons() & button) == button) { + if ((button & MessageBoxFlags::DIALOGBUTTON_OK) == + MessageBoxFlags::DIALOGBUTTON_OK) { + dialog_delegate->AcceptWindow(); + *success = true; + } + if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) == + MessageBoxFlags::DIALOGBUTTON_CANCEL) { + DCHECK(!*success) << "invalid param, OK and CANCEL specified"; + dialog_delegate->CancelWindow(); + *success = true; + } + } +} + +void TestingAutomationProvider::WaitForBrowserWindowCountToBecome( + int target_count, IPC::Message* reply_message) { + if (static_cast<int>(BrowserList::size()) == target_count) { + AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams( + reply_message, true); + Send(reply_message); + return; + } + + // Set up an observer (it will delete itself). + new BrowserCountChangeNotificationObserver(target_count, this, reply_message); +} + +void TestingAutomationProvider::WaitForAppModalDialogToBeShown( + IPC::Message* reply_message) { + if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) { + AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams( + reply_message, true); + Send(reply_message); + return; + } + + // Set up an observer (it will delete itself). + new AppModalDialogShownObserver(this, reply_message); +} + +void TestingAutomationProvider::GoBackBlockUntilNavigationsComplete( + int handle, int number_of_navigations, IPC::Message* reply_message) { + if (tab_tracker_->ContainsHandle(handle)) { + NavigationController* tab = tab_tracker_->GetResource(handle); + Browser* browser = FindAndActivateTab(tab); + if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) { + AddNavigationStatusListener(tab, reply_message, number_of_navigations, + false); + browser->GoBack(CURRENT_TAB); + return; + } + } + + AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams( + reply_message, AUTOMATION_MSG_NAVIGATION_ERROR); + Send(reply_message); +} + +void TestingAutomationProvider::GoForwardBlockUntilNavigationsComplete( + int handle, int number_of_navigations, IPC::Message* reply_message) { + if (tab_tracker_->ContainsHandle(handle)) { + NavigationController* tab = tab_tracker_->GetResource(handle); + Browser* browser = FindAndActivateTab(tab); + if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) { + AddNavigationStatusListener(tab, reply_message, number_of_navigations, + false); + browser->GoForward(CURRENT_TAB); + return; + } + } + + AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams( + reply_message, AUTOMATION_MSG_NAVIGATION_ERROR); + Send(reply_message); +} + +void TestingAutomationProvider::SavePackageShouldPromptUser( + bool should_prompt) { + SavePackage::SetShouldPromptUser(should_prompt); +} + +void TestingAutomationProvider::SetShelfVisibility(int handle, bool visible) { + if (browser_tracker_->ContainsHandle(handle)) { + Browser* browser = browser_tracker_->GetResource(handle); + if (browser) { + if (visible) + browser->window()->GetDownloadShelf()->Show(); + else + browser->window()->GetDownloadShelf()->Close(); + } + } +} + +void TestingAutomationProvider::GetBlockedPopupCount(int handle, int* count) { + *count = -1; // -1 is the error code + if (tab_tracker_->ContainsHandle(handle)) { + NavigationController* nav_controller = tab_tracker_->GetResource(handle); + TabContents* tab_contents = nav_controller->tab_contents(); + if (tab_contents) { + BlockedPopupContainer* container = + tab_contents->blocked_popup_container(); + if (container) { + *count = static_cast<int>(container->GetBlockedPopupCount()); + } else { + // If we don't have a container, we don't have any blocked popups to + // contain! + *count = 0; + } + } + } +} + // TODO(brettw) change this to accept GURLs when history supports it void TestingAutomationProvider::OnRedirectQueryComplete( HistoryService::Handle request_handle, |