diff options
Diffstat (limited to 'chrome/test')
-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 |
4 files changed, 309 insertions, 0 deletions
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. |