diff options
author | nirnimesh@chromium.org <nirnimesh@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-14 21:10:58 +0000 |
---|---|---|
committer | nirnimesh@chromium.org <nirnimesh@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-14 21:10:58 +0000 |
commit | 53329586a1a78bbe8052f172dea5f865dfa78c60 (patch) | |
tree | b56a208a8c88f92040392e07aafb9fab20ad066d | |
parent | 72682af437bf85a6f15c052e2c574a3fcf4433ae (diff) | |
download | chromium_src-53329586a1a78bbe8052f172dea5f865dfa78c60.zip chromium_src-53329586a1a78bbe8052f172dea5f865dfa78c60.tar.gz chromium_src-53329586a1a78bbe8052f172dea5f865dfa78c60.tar.bz2 |
Add automation hooks for fetching info about omnibox
1. Make the use of browser explicit for each json handler so that handlers
that need access to Browser* can have it. Omnibox needs it.
2. Add a simple test
TEST=python chrome/test/functional/omnibox.py
Review URL: http://codereview.chromium.org/2015010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@47311 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/automation/automation_provider.cc | 162 | ||||
-rw-r--r-- | chrome/browser/automation/automation_provider.h | 78 | ||||
-rw-r--r-- | chrome/test/functional/PYAUTO_TESTS | 3 | ||||
-rw-r--r-- | chrome/test/functional/omnibox.py | 72 | ||||
-rw-r--r-- | chrome/test/pyautolib/omnibox_info.py | 144 | ||||
-rw-r--r-- | chrome/test/pyautolib/pyauto.py | 90 |
6 files changed, 513 insertions, 36 deletions
diff --git a/chrome/browser/automation/automation_provider.cc b/chrome/browser/automation/automation_provider.cc index f73024f..e146f1b 100644 --- a/chrome/browser/automation/automation_provider.cc +++ b/chrome/browser/automation/automation_provider.cc @@ -1595,7 +1595,8 @@ void AutomationProvider::RemoveBookmark(int handle, // Sample json input: { "command": "GetBrowserInfo" } // Refer to GetBrowserInfo() in chrome/test/pyautolib/pyauto.py for // sample json output. -void AutomationProvider::GetBrowserInfo(DictionaryValue* args, +void AutomationProvider::GetBrowserInfo(Browser* browser, + DictionaryValue* args, IPC::Message* reply_message) { std::string json_return; bool reply_return = true; @@ -1634,9 +1635,9 @@ void AutomationProvider::GetBrowserInfo(DictionaryValue* args, // Sample json input: { "command": "GetHistoryInfo", // "search_text": "some text" } // Refer chrome/test/pyautolib/history_info.py for sample json output. -void AutomationProvider::GetHistoryInfo( - DictionaryValue* args, - IPC::Message* reply_message) { +void AutomationProvider::GetHistoryInfo(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message) { consumer_.CancelAllRequests(); std::wstring search_text; @@ -1662,9 +1663,9 @@ void AutomationProvider::GetHistoryInfo( // "time": 12345 # optional (time_t) // } } // Refer chrome/test/pyautolib/pyauto.py for details on input. -void AutomationProvider::AddHistoryItem( - DictionaryValue* args, - IPC::Message* reply_message) { +void AutomationProvider::AddHistoryItem(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message) { bool reply_return = true; std::string json_return = "{}"; @@ -1712,9 +1713,9 @@ void AutomationProvider::AddHistoryItem( // Sample json input: { "command": "GetDownloadsInfo" } // Refer chrome/test/pyautolib/download_info.py for sample json output. -void AutomationProvider::GetDownloadsInfo( - DictionaryValue* args, - IPC::Message* reply_message) { +void AutomationProvider::GetDownloadsInfo(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message) { std::string json_return; bool reply_return = true; AutomationProviderDownloadManagerObserver observer; @@ -1778,6 +1779,7 @@ void AutomationProvider::GetDownloadsInfo( } void AutomationProvider::WaitForDownloadsToComplete( + Browser* browser, DictionaryValue* args, IPC::Message* reply_message) { std::string json_return; @@ -1817,7 +1819,8 @@ void AutomationProvider::WaitForDownloadsToComplete( // Sample json input: { "command": "GetPrefsInfo" } // Refer chrome/test/pyautolib/prefs_info.py for sample json output. -void AutomationProvider::GetPrefsInfo(DictionaryValue* args, +void AutomationProvider::GetPrefsInfo(Browser* browser, + DictionaryValue* args, IPC::Message* reply_message) { std::string json_return; bool reply_return = true; @@ -1839,7 +1842,8 @@ void AutomationProvider::GetPrefsInfo(DictionaryValue* args, } // Sample json input: { "command": "SetPrefs", "path": path, "value": value } -void AutomationProvider::SetPrefs(DictionaryValue* args, +void AutomationProvider::SetPrefs(Browser* browser, + DictionaryValue* args, IPC::Message* reply_message) { bool reply_return = true; std::string json_return = "{}"; @@ -1868,9 +1872,118 @@ void AutomationProvider::SetPrefs(DictionaryValue* args, Send(reply_message); } +// Sample json input: { "command": "GetOmniboxInfo" } +// Refer chrome/test/pyautolib/omnibox_info.py for sample json output. +void AutomationProvider::GetOmniboxInfo(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message) { + std::string json_return; + bool reply_return = true; + scoped_ptr<DictionaryValue> return_value(new DictionaryValue); + + LocationBar* loc_bar = browser->window()->GetLocationBar(); + AutocompleteEditView* edit_view = loc_bar->location_entry(); + AutocompleteEditModel* model = edit_view->model(); + + // Fill up matches. + ListValue* matches = new ListValue; + const AutocompleteResult& result = model->result(); + for (AutocompleteResult::const_iterator i = result.begin(); + i != result.end(); ++i) { + const AutocompleteMatch& match = *i; + DictionaryValue* item = new DictionaryValue; // owned by return_value + item->SetString(L"type", AutocompleteMatch::TypeToString(match.type)); + item->SetBoolean(L"starred", match.starred); + item->SetString(L"destination_url", match.destination_url.spec()); + item->SetString(L"contents", match.contents); + item->SetString(L"description", match.description); + matches->Append(item); + } + return_value->Set(L"matches", matches); + + // Fill up other properties. + DictionaryValue* properties = new DictionaryValue; // owned by return_value + properties->SetBoolean(L"has_focus", model->has_focus()); + properties->SetBoolean(L"query_in_progress", model->query_in_progress()); + properties->SetString(L"keyword", model->keyword()); + properties->SetString(L"text", edit_view->GetText()); + return_value->Set(L"properties", properties); + + base::JSONWriter::Write(return_value.get(), false, &json_return); + AutomationMsg_SendJSONRequest::WriteReplyParams( + reply_message, json_return, reply_return); + Send(reply_message); +} + +// Sample json input: { "command": "SetOmniboxText", +// "text": "goog" } +void AutomationProvider::SetOmniboxText(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message) { + std::string json_return = "{}"; + bool reply_return = true; + std::wstring text; + + if (!args->GetString(L"text", &text)) { + json_return = "{\"error\": \"text missing\"}"; + reply_return = false; + } else { + browser->FocusLocationBar(); + LocationBar* loc_bar = browser->window()->GetLocationBar(); + AutocompleteEditView* edit_view = loc_bar->location_entry(); + edit_view->model()->OnSetFocus(false); + edit_view->SetUserText(text); + } + + AutomationMsg_SendJSONRequest::WriteReplyParams( + reply_message, json_return, reply_return); + Send(reply_message); +} + +// Sample json input: { "command": "OmniboxMovePopupSelection", +// "count": 1 } +// Negative count implies up, positive implies down. Count values will be +// capped by the size of the popup list. +void AutomationProvider::OmniboxMovePopupSelection( + Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message) { + std::string json_return = "{}"; + bool reply_return = true; + int count; + + if (!args->GetInteger(L"count", &count)) { + json_return = "{\"error\": \"count missing\"}"; + reply_return = false; + } else { + LocationBar* loc_bar = browser->window()->GetLocationBar(); + AutocompleteEditModel* model = loc_bar->location_entry()->model(); + model->OnUpOrDownKeyPressed(count); + } + + AutomationMsg_SendJSONRequest::WriteReplyParams( + reply_message, json_return, reply_return); + Send(reply_message); +} + +// Sample json input: { "command": "OmniboxAcceptInput" } +void AutomationProvider::OmniboxAcceptInput(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message) { + std::string json_return = "{}"; + bool reply_return = true; + + browser->window()->GetLocationBar()->AcceptInput(); + + AutomationMsg_SendJSONRequest::WriteReplyParams( + reply_message, json_return, reply_return); + Send(reply_message); +} + // Sample json input: { "command": "GetPluginsInfo" } // Refer chrome/test/pyautolib/plugins_info.py for sample json output. -void AutomationProvider::GetPluginsInfo(DictionaryValue* args, +void AutomationProvider::GetPluginsInfo(Browser* browser, + DictionaryValue* args, IPC::Message* reply_message) { std::string json_return; bool reply_return = true; @@ -1923,7 +2036,8 @@ void AutomationProvider::GetPluginsInfo(DictionaryValue* args, // Sample json input: // { "command": "EnablePlugin", // "path": "/Library/Internet Plug-Ins/Flash Player.plugin" } -void AutomationProvider::EnablePlugin(DictionaryValue* args, +void AutomationProvider::EnablePlugin(Browser* browser, + DictionaryValue* args, IPC::Message* reply_message) { std::string json_return = "{}"; bool reply_return = true; @@ -1945,8 +2059,9 @@ void AutomationProvider::EnablePlugin(DictionaryValue* args, // Sample json input: // { "command": "DisablePlugin", // "path": "/Library/Internet Plug-Ins/Flash Player.plugin" } -void AutomationProvider::DisablePlugin(DictionaryValue* args, - IPC::Message* reply_message) { +void AutomationProvider::DisablePlugin(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message) { std::string json_return = "{}"; bool reply_return = true; FilePath::StringType path; @@ -1964,10 +2079,9 @@ void AutomationProvider::DisablePlugin(DictionaryValue* args, Send(reply_message); } -void AutomationProvider::SendJSONRequest( - int handle, - std::string json_request, - IPC::Message* reply_message) { +void AutomationProvider::SendJSONRequest(int handle, + std::string json_request, + IPC::Message* reply_message) { Browser* browser = NULL; std::string error_string; scoped_ptr<Value> values; @@ -2013,6 +2127,12 @@ void AutomationProvider::SendJSONRequest( handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo; handler_map["AddHistoryItem"] = &AutomationProvider::AddHistoryItem; + handler_map["GetOmniboxInfo"] = &AutomationProvider::GetOmniboxInfo; + handler_map["SetOmniboxText"] = &AutomationProvider::SetOmniboxText; + handler_map["OmniboxAcceptInput"] = &AutomationProvider::OmniboxAcceptInput; + handler_map["OmniboxMovePopupSelection"] = + &AutomationProvider::OmniboxMovePopupSelection; + handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo; handler_map["SetPrefs"] = &AutomationProvider::SetPrefs; @@ -2022,7 +2142,7 @@ void AutomationProvider::SendJSONRequest( if (error_string.empty()) { if (handler_map.find(std::string(command)) != handler_map.end()) { - (this->*handler_map[command])(dict_value, reply_message); + (this->*handler_map[command])(browser, dict_value, reply_message); return; } else { error_string = "Unknown command. Options: "; diff --git a/chrome/browser/automation/automation_provider.h b/chrome/browser/automation/automation_provider.h index b4a7c76..0b5e425 100644 --- a/chrome/browser/automation/automation_provider.h +++ b/chrome/browser/automation/automation_provider.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -337,54 +337,102 @@ class AutomationProvider : public base::RefCounted<AutomationProvider>, // Get info about the chromium/chrome in use. // This includes things like version, executable name, executable path. // Uses the JSON interface for input/output. - void GetBrowserInfo(DictionaryValue* args, IPC::Message* reply_message); + void GetBrowserInfo(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); // Get info about downloads. This includes only ones that have been // registered by the history system. // Uses the JSON interface for input/output. - void GetDownloadsInfo(DictionaryValue* args, IPC::Message* reply_message); + void GetDownloadsInfo(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); // Wait for all downloads to complete. // Uses the JSON interface for input/output. - void WaitForDownloadsToComplete( - DictionaryValue* args, - IPC::Message* reply_message); + void WaitForDownloadsToComplete(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); // Get info about history. // Uses the JSON interface for input/output. - void GetHistoryInfo(DictionaryValue* args, IPC::Message* reply_message); + void GetHistoryInfo(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); // Add an item to the history service. // Uses the JSON interface for input/output. - void AddHistoryItem(DictionaryValue* args, IPC::Message* reply_message); + void AddHistoryItem(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); // Get info about preferences. // Uses the JSON interface for input/output. - void GetPrefsInfo(DictionaryValue* args, IPC::Message* reply_message); + void GetPrefsInfo(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); + + // Set prefs. + // Uses the JSON interface for input/output. + void SetPrefs(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); // Get info about plugins. // Uses the JSON interface for input/output. - void GetPluginsInfo(DictionaryValue* args, IPC::Message* reply_message); + void GetPluginsInfo(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); // Enable a plugin. // Uses the JSON interface for input/output. - void EnablePlugin(DictionaryValue* args, IPC::Message* reply_message); + void EnablePlugin(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); // Disable a plugin. // Uses the JSON interface for input/output. - void DisablePlugin(DictionaryValue* args, IPC::Message* reply_message); + void DisablePlugin(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); - // Set prefs. + // Get info about omnibox. + // Contains data about the matches (url, content, description) + // in the omnibox popup, the text in the omnibox. + // Uses the JSON interface for input/output. + void GetOmniboxInfo(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); + + // Set text in the omnibox. This sets focus to the omnibox. + // Uses the JSON interface for input/output. + void SetOmniboxText(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); + + // Move omnibox popup selection up or down. + // Uses the JSON interface for input/output. + void OmniboxMovePopupSelection(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); + + // Accept the current string of text in the omnibox. + // This is equivalent to clicking or hiting enter on a popup selection. // Uses the JSON interface for input/output. - void SetPrefs(DictionaryValue* args, IPC::Message* reply_message); + void OmniboxAcceptInput(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); // Generic pattern for pyautolib + // Uses the JSON interface for input/output. void SendJSONRequest(int handle, std::string json_request, IPC::Message* reply_message); // Method ptr for json handlers. - typedef void (AutomationProvider::*JsonHandler)(DictionaryValue*, + // Uses the JSON interface for input/output. + typedef void (AutomationProvider::*JsonHandler)(Browser* browser, + DictionaryValue*, IPC::Message*); // Responds to InspectElement request diff --git a/chrome/test/functional/PYAUTO_TESTS b/chrome/test/functional/PYAUTO_TESTS index e90655e..ba63abd 100644 --- a/chrome/test/functional/PYAUTO_TESTS +++ b/chrome/test/functional/PYAUTO_TESTS @@ -33,16 +33,19 @@ # testBookmarkBarVisible fails on windows. crbug.com/42823 # 'bookmark_bar.BookmarkBarTest.testBookmarkBarVisible', 'bookmark_bar.BookmarkBarTest.testBookmarkBarVisibleWait', + 'omnibox', ], 'mac': [ 'bookmark_bar.BookmarkBarTest.testBookmarkBarVisibleWait', 'bookmark_bar.BookmarkBarTest.testBookmarkBarVisibleWait', + 'omnibox', ], 'linux': [ 'bookmark_bar.BookmarkBarTest.testBookmarkBarVisibleWait', 'bookmark_bar.BookmarkBarTest.testBookmarkBarVisibleWait', + # 'omnibox', # http://crbug.com/44203 ], # TODO(nirnimesh): Add a ChromeOS section diff --git a/chrome/test/functional/omnibox.py b/chrome/test/functional/omnibox.py new file mode 100644 index 0000000..b7797c5 --- /dev/null +++ b/chrome/test/functional/omnibox.py @@ -0,0 +1,72 @@ +#!/usr/bin/python +# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import os + +import pyauto_functional # Must be imported before pyauto +import pyauto + + +class OmniboxTest(pyauto.PyUITest): + """TestCase for Omnibox.""" + + def Debug(self): + """Test method for experimentation. + + This method will not run automatically. + """ + import pprint + import time + pp = pprint.PrettyPrinter(indent=2) + while True: + pp.pprint(self.GetOmniboxInfo().omniboxdict) + time.sleep(1) + + def testHistoryResult(self): + """Verify that omnibox can fetch items from history.""" + url = self.GetFileURLForPath(os.path.join(self.DataDir(), 'title2.html')) + title = 'Title Of Awesomeness' + self.AppendTab(pyauto.GURL(url)) + def _VerifyResult(query_text, description): + """Verify result matching given description for given query_text.""" + self.SetOmniboxText(query_text) + self.WaitUntilOmniboxQueryDone() + info = self.GetOmniboxInfo() + self.assertTrue(info.Matches()) + matches = info.MatchesWithAttributes({'description': description}) + self.assertTrue(matches) + self.assertEqual(1, len(matches)) + item = matches[0] + self.assertEqual(url, item['destination_url']) + # Query using URL + _VerifyResult(url, title) + # Query using title + _VerifyResult(title, title) + + def testSelect(self): + """Verify omnibox popup selection.""" + url1 = self.GetFileURLForPath(os.path.join(self.DataDir(), 'title2.html')) + url2 = self.GetFileURLForPath(os.path.join(self.DataDir(), 'title1.html')) + title1 = 'Title Of Awesomeness' + self.NavigateToURL(url1) + self.NavigateToURL(url2) + self.SetOmniboxText('file://') + self.WaitUntilOmniboxQueryDone() + matches = self.GetOmniboxInfo().Matches() + # Find the index of match for url1 + index = None + for i, match in enumerate(matches): + if match['description'] == title1: + index = i + self.assertTrue(index is not None) + self.OmniboxMovePopupSelection(index) # Select url1 line in popup + self.assertEqual(url1, self.GetOmniboxInfo().Text()) + self.OmniboxAcceptInput() + self.assertEqual(title1, self.GetActiveTabTitle()) + + +if __name__ == '__main__': + pyauto_functional.Main() + diff --git a/chrome/test/pyautolib/omnibox_info.py b/chrome/test/pyautolib/omnibox_info.py new file mode 100644 index 0000000..ac6b16a --- /dev/null +++ b/chrome/test/pyautolib/omnibox_info.py @@ -0,0 +1,144 @@ +#!/usr/bin/python + +# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""Python representation for Chromium Omnibox. + +Obtain one of these from PyUITestSuite::GetOmniboxInfo() call. + +Example: +class MyTest(pyauto.PyUITest): + def testBasic(self): + info = self.OmniboxInfo() # fetch omnibox snapshot + print info.Matches() + +See more tests in chrome/test/functional/omnibox.py. +""" + +import simplejson as json + +from pyauto_errors import JSONInterfaceError + + +class OmniboxInfo(object): + """Represent info for Chromium Omnibox. + + Info contains: + - a list of matches in the same order as you'd see in the omnibox, + - a dictionary of properties related to the omnibox. + + Sample info text: + + { u'matches': [ + { + u'contents': u'google', + u'description': u'Google Search', + u'destination_url': u'http://www.google.com/search?aq=f&' + 'sourceid=chrome&ie=UTF-8&q=google', + u'starred': False, + u'type': u'search-what-you-typed'}, + { + u'contents': u'maps.google.com/', + u'description': u'Google Maps', + u'destination_url': u'http://maps.google.com/', + u'starred': False, + u'type': u'navsuggest'}, + { u'contents': u'google maps', + u'description': u'', + u'destination_url': u'http://www.google.com/search?aq=0&oq=google&' + 'sourceid=chrome&ie=UTF-8&q=google+maps', + u'starred': False, + u'type': u'search-suggest'}, + { u'contents': u'google earth', + u'description': u'', + u'destination_url': u'http://www.google.com/search?aq=1&oq=google&' + 'sourceid=chrome&ie=UTF-8&q=google+earth', + u'starred': False, + u'type': u'search-suggest'}, + { u'contents': u'Search Google for <enter query>', + u'description': u'(Keyword: google.com)', + u'destination_url': u'', + u'starred': False, + u'type': u'search-other-engine'}], + + u'properties': { u'has_focus': True, + u'keyword': u'', + u'query_in_progress': False, + u'text': u'google'}} + """ + def __init__(self, json_string): + """Initialize a OmniboxInfo from a json string. + + Args: + json_string: a json string, as returned by a json ipc call for the + command 'GetOmniboxInfo' + + Raises: + pyauto_errors.JSONInterfaceError if the automation call returns an error. + """ + # JSON string prepared in GetOmniboxInfo() in automation_provider.cc + self.omniboxdict = json.loads(json_string) + if self.omniboxdict.has_key('error'): + raise JSONInterfaceError(self.omniboxdict['error']) + + def Matches(self): + """Get omnibox matches. + + Returns: + a list of omnibox match items. + """ + return self.omniboxdict.get('matches', []) + + def MatchesWithAttributes(self, attr_dict): + """Find all omnibox matches which match the attributes in |attr_dict|. + + Args: + attr_dict: a dictionary of attributes to be satisfied. + All attributes in the given dictionary should be satisfied. + example: + { 'destiantion_url': 'http://www.google.com/', + 'description': 'Google' } + + Returns: + a list of omnibox match items. + """ + out = [] + for item in self.Matches(): + matched = True + for key, val in attr_dict.iteritems(): + if not item.has_key(key) or item[key] != val: + matched = False + if matched: + out.append(item) + return out + + def Properties(self, key=None): + """Get the properties + + Args: + key: if specified, value for the given property is returned. + + Returns: + a dictionary of properties if no key is given, OR + value corresponding to a particular property if key is given + """ + all = self.omniboxdict.get('properties') + if not key: + return all + return all.get(key) + + def Text(self): + """Get the text in the omnibox. + + This need not be the same as the user-inputted text, since omnibox may + autocomplete some URLs, or the user may move omnibox popup selection + up/down. + """ + return self.Properties('text') + + def IsQueryInProgress(self): + """Determine if a query is in progress.""" + return self.Properties('query_in_progress') + diff --git a/chrome/test/pyautolib/pyauto.py b/chrome/test/pyautolib/pyauto.py index 21d4408..a2b7166 100644 --- a/chrome/test/pyautolib/pyauto.py +++ b/chrome/test/pyautolib/pyauto.py @@ -74,6 +74,7 @@ except ImportError: import bookmark_model import download_info import history_info +import omnibox_info import plugins_info import prefs_info from pyauto_errors import JSONInterfaceError @@ -241,6 +242,95 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): return download_info.DownloadInfo( self._SendJSONRequest(0, json.dumps({'command': 'GetDownloadsInfo'}))) + def GetOmniboxInfo(self, windex=0): + """Return info about Omnibox. + + This represents a snapshot of the omnibox. If you expect changes + you need to call this method again to get a fresh snapshot. + Note that this DOES NOT shift focus to the omnibox; you've to ensure that + the omnibox is in focus or else you won't get any interesting info. + + It's OK to call this even when the omnibox popup is not showing. In this + case however, there won't be any matches, but other properties (like the + current text in the omnibox) will still be fetched. + + Due to the nature of the omnibox, this function is sensitive to mouse + focus. DO NOT HOVER MOUSE OVER OMNIBOX OR CHANGE WINDOW FOCUS WHEN USING + THIS METHOD. + + Args: + windex: the index of the browser window to work on. + Default: 0 (first window) + + Returns: + an instance of omnibox_info.OmniboxInfo + """ + return omnibox_info.OmniboxInfo( + self._SendJSONRequest(windex, + json.dumps({'command': 'GetOmniboxInfo'}))) + + def SetOmniboxText(self, text, windex=0): + """Enter text into the omnibox. This shifts focus to the omnibox. + + Args: + text: the text to be set. + windex: the index of the browser window to work on. + Default: 0 (first window) + """ + cmd_dict = { + 'command': 'SetOmniboxText', + 'text': text, + } + ret_dict = json.loads(self._SendJSONRequest(windex, json.dumps(cmd_dict))) + if ret_dict.has_key('error'): + raise JSONInterfaceError(ret_dict['error']) + + def WaitUntilOmniboxQueryDone(self, windex=0): + """Wait until omnibox has finished populating results. + + Uses WaitUntil() so the wait duration is capped by the timeout values + used by automation, which WaitUntil() uses. + + Args: + windex: the index of the browser window to work on. + Default: 0 (first window) + """ + return self.WaitUntil( + lambda : not self.GetOmniboxInfo(windex).IsQueryInProgress()) + + def OmniboxMovePopupSelection(self, count, windex=0): + """Move omnibox popup selection up or down. + + Args: + count: number of rows by which to move. + -ve implies down, +ve implies up + windex: the index of the browser window to work on. + Default: 0 (first window) + """ + cmd_dict = { + 'command': 'OmniboxMovePopupSelection', + 'count': count, + } + ret_dict = json.loads(self._SendJSONRequest(windex, json.dumps(cmd_dict))) + if ret_dict.has_key('error'): + raise JSONInterfaceError(ret_dict['error']) + + def OmniboxAcceptInput(self, windex=0): + """Accepts the current string of text in the omnibox. + + This is equivalent to clicking or hiting enter on a popup selection. + + Args: + windex: the index of the browser window to work on. + Default: 0 (first window) + """ + cmd_dict = { + 'command': 'OmniboxAcceptInput', + } + ret_dict = json.loads(self._SendJSONRequest(windex, json.dumps(cmd_dict))) + if ret_dict.has_key('error'): + raise JSONInterfaceError(ret_dict['error']) + def GetPrefsInfo(self): """Return info about preferences. |