diff options
author | nirnimesh@chromium.org <nirnimesh@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-22 04:55:28 +0000 |
---|---|---|
committer | nirnimesh@chromium.org <nirnimesh@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-22 04:55:28 +0000 |
commit | e004a2d0d8d5f3e67483f52a48db29998882d7ff (patch) | |
tree | 1c5d4eeaa1734a026c0a1a5070dc3af6dfb85d7e /chrome | |
parent | 8e8efbd197406eaf4b4e458c32b158f360ba9307 (diff) | |
download | chromium_src-e004a2d0d8d5f3e67483f52a48db29998882d7ff.zip chromium_src-e004a2d0d8d5f3e67483f52a48db29998882d7ff.tar.gz chromium_src-e004a2d0d8d5f3e67483f52a48db29998882d7ff.tar.bz2 |
Add PyAuto automation hooks to perform actions on infobars
Actions include: dismissing an infobar, accept/close-ing it
Also, enable infobars tests.
Review URL: http://codereview.chromium.org/3042012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@53304 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/automation/automation_provider.cc | 57 | ||||
-rw-r--r-- | chrome/browser/automation/automation_provider.h | 6 | ||||
-rw-r--r-- | chrome/test/functional/PYAUTO_TESTS | 1 | ||||
-rw-r--r-- | chrome/test/functional/infobars.py | 56 | ||||
-rw-r--r-- | chrome/test/pyautolib/pyauto.py | 28 |
5 files changed, 147 insertions, 1 deletions
diff --git a/chrome/browser/automation/automation_provider.cc b/chrome/browser/automation/automation_provider.cc index 6c66931..7160a3d 100644 --- a/chrome/browser/automation/automation_provider.cc +++ b/chrome/browser/automation/automation_provider.cc @@ -1817,6 +1817,61 @@ void AutomationProvider::WaitForInfobarCount(Browser* browser, new WaitForInfobarCountObserver(this, reply_message, tab_contents, count); } +// Sample json input: { "command": "PerformActionOnInfobar", +// "action": "dismiss", +// "infobar_index": 0, +// "tab_index": 0 } +// Sample output: {} +void AutomationProvider::PerformActionOnInfobar(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message) { + AutomationJSONReply reply(this, reply_message); + int tab_index; + int infobar_index; + std::string action; + if (!args->GetInteger(L"tab_index", &tab_index) || + !args->GetInteger(L"infobar_index", &infobar_index) || + !args->GetString(L"action", &action)) { + reply.SendError("Invalid or missing args"); + return; + } + TabContents* tab_contents = browser->GetTabContentsAt(tab_index); + if (!tab_contents) { + reply.SendError(StringPrintf("No such tab at index %d", tab_index)); + return; + } + InfoBarDelegate* infobar = NULL; + if (infobar_index < 0 || + infobar_index >= tab_contents->infobar_delegate_count() || + !(infobar = tab_contents->GetInfoBarDelegateAt(infobar_index))) { + reply.SendError(StringPrintf("No such infobar at index %d", infobar_index)); + return; + } + if ("dismiss" == action) { + infobar->InfoBarDismissed(); + tab_contents->RemoveInfoBar(infobar); + reply.SendSuccess(NULL); + return; + } + if ("accept" == action || "cancel" == action) { + ConfirmInfoBarDelegate* confirm_infobar; + if (!(confirm_infobar = infobar->AsConfirmInfoBarDelegate())) { + reply.SendError("Not a confirm infobar"); + return; + } + if ("accept" == action) { + if (confirm_infobar->Accept()) + tab_contents->RemoveInfoBar(infobar); + } else if ("cancel" == action) { + if (confirm_infobar->Cancel()) + tab_contents->RemoveInfoBar(infobar); + } + reply.SendSuccess(NULL); + return; + } + reply.SendError("Invalid action"); +} + namespace { // Task to get info about BrowserChildProcessHost. Must run on IO thread to @@ -2915,6 +2970,8 @@ void AutomationProvider::SendJSONRequest(int handle, handler_map["GetBrowserInfo"] = &AutomationProvider::GetBrowserInfo; handler_map["WaitForInfobarCount"] = &AutomationProvider::WaitForInfobarCount; + handler_map["PerformActionOnInfobar"] = + &AutomationProvider::PerformActionOnInfobar; handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo; handler_map["AddHistoryItem"] = &AutomationProvider::AddHistoryItem; diff --git a/chrome/browser/automation/automation_provider.h b/chrome/browser/automation/automation_provider.h index f166771..0bc30fcc 100644 --- a/chrome/browser/automation/automation_provider.h +++ b/chrome/browser/automation/automation_provider.h @@ -350,6 +350,12 @@ class AutomationProvider : public base::RefCounted<AutomationProvider>, DictionaryValue* args, IPC::Message* reply_message); + // Perform actions on an infobar like dismiss, accept, cancel. + // Uses the JSON interface for input/output. + void PerformActionOnInfobar(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); + // Get info about the chromium/chrome in use. // This includes things like version, executable name, executable path. // Uses the JSON interface for input/output. diff --git a/chrome/test/functional/PYAUTO_TESTS b/chrome/test/functional/PYAUTO_TESTS index 952eb98..1e16df7 100644 --- a/chrome/test/functional/PYAUTO_TESTS +++ b/chrome/test/functional/PYAUTO_TESTS @@ -31,6 +31,7 @@ 'cookies', 'downloads', 'history', + 'infobars', 'navigation', 'omnibox', 'passwords', diff --git a/chrome/test/functional/infobars.py b/chrome/test/functional/infobars.py index f5d48e3..b4f080b 100644 --- a/chrome/test/functional/infobars.py +++ b/chrome/test/functional/infobars.py @@ -31,6 +31,13 @@ class InfobarTest(pyauto.PyUITest): print 'Window', window['index'], 'tab', tab['index'] pp.pprint(tab['infobars']) + def _GetTabInfo(self, windex=0, tab_index=0): + """Helper to return info for the given tab in the given window. + + Defaults to first tab in first window. + """ + return self.GetBrowserInfo()['windows'][windex]['tabs'][tab_index] + def testPluginCrashInfobar(self): """Verify the "plugin crashed" infobar.""" flash_url = self.GetFileURLForPath(os.path.join(self.DataDir(), @@ -44,11 +51,58 @@ class InfobarTest(pyauto.PyUITest): logging.info('Killing flash plugin. pid %d' % flash['pid']) self.Kill(flash['pid']) self.WaitForInfobarCount(1) - crash_infobar = self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars'] + crash_infobar = self._GetTabInfo()['infobars'] self.assertTrue(crash_infobar) self.assertEqual(1, len(crash_infobar)) self.assertTrue(re.match('The following plug-in has crashed:', crash_infobar[0]['text'])) + self.assertEqual('alert_infobar', crash_infobar[0]['type']) + # Dismiss the infobar + self.PerformActionOnInfobar('dismiss', infobar_index=0) + self.assertFalse(self._GetTabInfo()['infobars']) + + def _VerifyGeolocationInfobar(self, match_text, windex, tab_index): + """Verify geolocation infobar and match given text. + + Assumes that geolocation infobar is showing up in the given tab in the + given window. + """ + tab_info = self._GetTabInfo(windex, tab_index) + geolocation_infobar = tab_info['infobars'] + self.assertTrue(geolocation_infobar) + self.assertEqual(1, len(geolocation_infobar)) + self.assertEqual(match_text, geolocation_infobar[0]['text']) + self.assertEqual('Learn more', geolocation_infobar[0]['link_text']) + self.assertEqual(2, len(geolocation_infobar[0]['buttons'])) + self.assertEqual('Allow', geolocation_infobar[0]['buttons'][0]) + self.assertEqual('Deny', geolocation_infobar[0]['buttons'][1]) + + def testGeolocationInfobar(self): + """Verify geoLocation infobar.""" + url = 'http://m.flickr.com/#/nearby/' # automatically triggers geolocation + match_text='m.flickr.com wants to track your physical location' + self.NavigateToURL(url) + self._VerifyGeolocationInfobar(windex=0, tab_index=0, match_text=match_text) + # Accept, and verify that the infobar went away + self.PerformActionOnInfobar('accept', infobar_index=0) + self.assertFalse(self._GetTabInfo()['infobars']) + + def testGeolocationInfobarInMultipleTabsAndWindows(self): + """Verify GeoLocation inforbar in multiple tabs.""" + url = 'http://m.flickr.com/#/nearby/' # automatically triggers geolocation + match_text='m.flickr.com wants to track your physical location' + for tab_index in range(1, 2): + self.AppendTab(pyauto.GURL(url)) + self._VerifyGeolocationInfobar(windex=0, tab_index=tab_index, + match_text=match_text) + # Try in a new window + self.OpenNewBrowserWindow(True) + self.NavigateToURL(url, 1, 0) + self._VerifyGeolocationInfobar(windex=1, tab_index=0, match_text=match_text) + # Incognito window + self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW) + self.NavigateToURL(url, 2, 0) + self._VerifyGeolocationInfobar(windex=2, tab_index=0, match_text=match_text) if __name__ == '__main__': diff --git a/chrome/test/pyautolib/pyauto.py b/chrome/test/pyautolib/pyauto.py index fbd7ee3..5394752 100644 --- a/chrome/test/pyautolib/pyauto.py +++ b/chrome/test/pyautolib/pyauto.py @@ -510,6 +510,34 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): } self._GetResultFromJSONRequest(cmd_dict, windex=windex) + def PerformActionOnInfobar( + self, action, infobar_index, windex=0, tab_index=0): + """Perform actions on an infobar. + + Args: + action: the action to be performed. + Actions depend on the type of the infobar. The user needs to + call the right action for the right infobar. + Valid inputs are: + - "dismiss": closes the infobar (for all infobars) + - "accept", "cancel": click accept / cancel (for confirm infobars) + infobar_index: 0-based index of the infobar on which to perform the action + windex: 0-based window index Defaults to 0 (first window) + tab_index: 0-based tab index. Defaults to 0 (first tab) + + Raises: + pyauto_errors.JSONInterfaceError if the automation call returns an error. + """ + cmd_dict = { + 'command': 'PerformActionOnInfobar', + 'action': action, + 'infobar_index': infobar_index, + 'tab_index': tab_index, + } + if action not in ('dismiss', 'accept', 'cancel'): + raise JSONInterfaceError('Invalid action %s' % action) + self._GetResultFromJSONRequest(cmd_dict, windex=windex) + def GetBrowserInfo(self): """Return info about the browser. |