1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/test/reliability/automated_ui_test_base.h" 6 7 #include "base/test/test_timeouts.h" 8 #include "chrome/app/chrome_command_ids.h" 9 #include "chrome/browser/ui/view_ids.h" 10 #include "chrome/test/automation/automation_proxy.h" 11 #include "chrome/test/automation/browser_proxy.h" 12 #include "chrome/test/automation/tab_proxy.h" 13 #include "chrome/test/automation/window_proxy.h" 14 #include "chrome/test/ui/ui_test.h" 15 #include "ui/base/events/event_constants.h" 16 #include "ui/gfx/point.h" 17 #include "ui/gfx/rect.h" 18 19 AutomatedUITestBase::AutomatedUITestBase() {} 20 21 AutomatedUITestBase::~AutomatedUITestBase() {} 22 23 void AutomatedUITestBase::LogErrorMessage(const std::string& error) { 24 } 25 26 void AutomatedUITestBase::LogWarningMessage(const std::string& warning) { 27 } 28 29 void AutomatedUITestBase::LogInfoMessage(const std::string& info) { 30 } 31 32 void AutomatedUITestBase::SetUp() { 33 UITest::SetUp(); 34 set_active_browser(automation()->GetBrowserWindow(0).get()); 35 } 36 37 bool AutomatedUITestBase::BackButton() { 38 return RunCommand(IDC_BACK); 39 } 40 41 bool AutomatedUITestBase::CloseActiveTab() { 42 BrowserProxy* browser = active_browser(); 43 int tab_count; 44 if (!browser->GetTabCount(&tab_count)) { 45 LogErrorMessage("get_tab_count_failed"); 46 return false; 47 } 48 49 if (tab_count > 1) { 50 return RunCommand(IDC_CLOSE_TAB); 51 } else if (tab_count == 1) { 52 // Synchronously close the window if it is not the last window. 53 return CloseActiveWindow(); 54 } else { 55 LogInfoMessage("invalid_tab_count"); 56 return false; 57 } 58 } 59 60 bool AutomatedUITestBase::CloseActiveWindow() { 61 int browser_windows_count = 0; 62 if (!automation()->GetNormalBrowserWindowCount(&browser_windows_count)) 63 return false; 64 // Avoid quitting the application by not closing the last window. 65 if (browser_windows_count < 2) 66 return false; 67 bool application_closed; 68 CloseBrowser(active_browser(), &application_closed); 69 if (application_closed) { 70 LogErrorMessage("Application closed unexpectedly."); 71 return false; 72 } 73 for (int i = 0; i < browser_windows_count - 1; ++i) { 74 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(i)); 75 Browser::Type type; 76 if (browser->GetType(&type) && type == Browser::TYPE_TABBED) { 77 set_active_browser(browser.get()); 78 return true; 79 } 80 } 81 82 LogErrorMessage("Can't find browser window."); 83 return false; 84 } 85 86 bool AutomatedUITestBase::DuplicateTab() { 87 return RunCommand(IDC_DUPLICATE_TAB); 88 } 89 90 bool AutomatedUITestBase::FindInPage() { 91 if (!RunCommandAsync(IDC_FIND)) 92 return false; 93 94 return WaitForFindWindowVisibilityChange(active_browser(), true); 95 } 96 97 bool AutomatedUITestBase::ForwardButton() { 98 return RunCommand(IDC_FORWARD); 99 } 100 101 bool AutomatedUITestBase::GoOffTheRecord() { 102 return RunCommand(IDC_NEW_INCOGNITO_WINDOW); 103 } 104 105 bool AutomatedUITestBase::Home() { 106 return RunCommand(IDC_HOME); 107 } 108 109 bool AutomatedUITestBase::OpenAndActivateNewBrowserWindow( 110 scoped_refptr<BrowserProxy>* previous_browser) { 111 if (!automation()->OpenNewBrowserWindow(Browser::TYPE_TABBED, 112 true /* SW_SHOWNORMAL */)) { 113 LogWarningMessage("failed_to_open_new_browser_window"); 114 return false; 115 } 116 int num_browser_windows; 117 if (!automation()->GetBrowserWindowCount(&num_browser_windows)) { 118 LogErrorMessage("failed_to_get_browser_window_count"); 119 return false; 120 } 121 // Get the most recently opened browser window and activate the tab 122 // in order to activate this browser window. 123 scoped_refptr<BrowserProxy> browser( 124 automation()->GetBrowserWindow(num_browser_windows - 1)); 125 if (browser.get() == NULL) { 126 LogErrorMessage("browser_window_not_found"); 127 return false; 128 } 129 if (!browser->ActivateTab(0)) { 130 LogWarningMessage("failed_to_activate_tab"); 131 return false; 132 } 133 134 if (previous_browser) { 135 DCHECK(previous_browser->get() == NULL); 136 active_browser_.swap(*previous_browser); 137 } 138 139 active_browser_.swap(browser); 140 return true; 141 } 142 143 bool AutomatedUITestBase::Navigate(const GURL& url) { 144 scoped_refptr<TabProxy> tab(GetActiveTab()); 145 if (tab.get() == NULL) { 146 LogErrorMessage("active_tab_not_found"); 147 return false; 148 } 149 AutomationMsg_NavigationResponseValues result = tab->NavigateToURL(url); 150 if (result != AUTOMATION_MSG_NAVIGATION_SUCCESS) { 151 LogErrorMessage("navigation_failed"); 152 return false; 153 } 154 155 return true; 156 } 157 158 bool AutomatedUITestBase::NewTab() { 159 // Apply accelerator and wait for a new tab to open, if either 160 // fails, return false. Apply Accelerator takes care of logging its failure. 161 return RunCommand(IDC_NEW_TAB); 162 } 163 164 bool AutomatedUITestBase::ReloadPage() { 165 return RunCommand(IDC_RELOAD); 166 } 167 168 bool AutomatedUITestBase::RestoreTab() { 169 return RunCommand(IDC_RESTORE_TAB); 170 } 171 172 bool AutomatedUITestBase::SelectNextTab() { 173 return RunCommand(IDC_SELECT_NEXT_TAB); 174 } 175 176 bool AutomatedUITestBase::SelectPreviousTab() { 177 return RunCommand(IDC_SELECT_PREVIOUS_TAB); 178 } 179 180 bool AutomatedUITestBase::ShowDownloads() { 181 return RunCommand(IDC_SHOW_DOWNLOADS); 182 } 183 184 bool AutomatedUITestBase::ShowHistory() { 185 return RunCommand(IDC_SHOW_HISTORY); 186 } 187 188 bool AutomatedUITestBase::RunCommandAsync(int browser_command) { 189 BrowserProxy* browser = active_browser(); 190 if (NULL == browser) { 191 LogErrorMessage("browser_window_not_found"); 192 return false; 193 } 194 195 if (!browser->RunCommandAsync(browser_command)) { 196 LogWarningMessage("failure_running_browser_command"); 197 return false; 198 } 199 return true; 200 } 201 202 bool AutomatedUITestBase::RunCommand(int browser_command) { 203 BrowserProxy* browser = active_browser(); 204 if (NULL == browser) { 205 LogErrorMessage("browser_window_not_found"); 206 return false; 207 } 208 209 if (!browser->RunCommand(browser_command)) { 210 LogWarningMessage("failure_running_browser_command"); 211 return false; 212 } 213 return true; 214 } 215 216 scoped_refptr<TabProxy> AutomatedUITestBase::GetActiveTab() { 217 BrowserProxy* browser = active_browser(); 218 if (browser == NULL) { 219 LogErrorMessage("browser_window_not_found"); 220 return NULL; 221 } 222 223 return browser->GetActiveTab(); 224 } 225 226 scoped_refptr<WindowProxy> AutomatedUITestBase::GetAndActivateWindowForBrowser( 227 BrowserProxy* browser) { 228 if (!browser->BringToFront()) { 229 LogWarningMessage("failed_to_bring_window_to_front"); 230 return NULL; 231 } 232 233 return browser->GetWindow(); 234 } 235