Chromium Code Reviews| Index: chrome/test/pyautolib/pyauto.py |
| diff --git a/chrome/test/pyautolib/pyauto.py b/chrome/test/pyautolib/pyauto.py |
| index 8545f65370ef27ffa0e6a2e6652bcf7c3353f5c6..a2a7544faba10288349479d533a24456c51b4ac8 100644 |
| --- a/chrome/test/pyautolib/pyauto.py |
| +++ b/chrome/test/pyautolib/pyauto.py |
| @@ -25,6 +25,7 @@ to unittest.py |
| python pyauto.py test_script |
| """ |
| +import exceptions |
|
Nirnimesh
2010/11/17 01:25:49
not needed. 'IndexError' is already in scope
kkania
2010/11/17 22:16:08
Done.
|
| import logging |
| import optparse |
| import os |
| @@ -86,6 +87,7 @@ import omnibox_info |
| import plugins_info |
| import prefs_info |
| from pyauto_errors import JSONInterfaceError |
| +from pyauto_errors import NTPThumbnailNotShownError |
| import simplejson as json # found in third_party |
| @@ -197,6 +199,15 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): |
| os.path.join(PyUITest.DataDir(), relative_path)) |
| @staticmethod |
| + def GetFileURLForDataPath(relative_path): |
|
Nirnimesh
2010/11/17 01:25:49
this is still duplicated
kkania
2010/11/17 22:16:08
Done.
|
| + """Get file:// url for the given path relative to the chrome test data dir. |
| + |
| + Also quotes the url using urllib.quote(). |
| + """ |
| + return PyUITest.GetFileURLForPath( |
| + os.path.join(PyUITest.DataDir(), relative_path)) |
| + |
| + @staticmethod |
| def IsMac(): |
| """Are we on Mac?""" |
| return 'darwin' == sys.platform |
| @@ -1776,6 +1787,189 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): |
| } |
| return self._GetResultFromJSONRequest(cmd_dict)['success'] |
| + def GetNTPThumbnails(self): |
| + """Return a list of info about the sites in the NTP most visited section. |
| + SAMPLE: |
| + [{ u'title': u'Google', |
| + u'url': u'http://www.google.com', |
| + u'is_pinned': False}, |
| + { |
| + u'title': u'Yahoo', |
| + u'url': u'http://www.yahoo.com', |
| + u'is_pinned': True}] |
| + """ |
| + return self._GetNTPInfo()['most_visited'] |
| + |
| + def GetNTPThumbnailIndex(self, thumbnail): |
| + """Returns the index of the given NTP thumbnail, or -1 if it is not shown. |
| + |
| + Args: |
| + thumbnail: a thumbnail dict received from |GetNTPThumbnails| |
| + """ |
| + thumbnails = self.GetNTPThumbnails() |
| + for i in range(len(thumbnails)): |
| + if thumbnails[i]['url'] == thumbnail['url']: |
| + return i |
| + return -1 |
| + |
| + def MoveNTPThumbnail(self, thumbnail, new_index): |
| + """Moves the given thumbnail to a new index. The indices in the NTP Most |
| + Visited sites section look like: |
| + 0 1 2 3 |
| + 4 5 6 7 |
| + |
| + Args: |
| + thumbnail: a thumbnail dict received from |GetNTPThumbnails| |
| + new_index: the index to be moved to in the Most Visited sites section |
| + |
| + Raises: |
| + exceptions.IndexError if there is no thumbnail at the index |
| + |
| + Note: |
|
Nirnimesh
2010/11/17 01:25:49
Move the note before Args:
kkania
2010/11/17 22:16:08
Done.
|
| + When a thumbnail is moved, it is automatically pinned |
| + """ |
| + if new_index < 0 or new_index >= len(self.GetNTPThumbnails()): |
| + raise exceptions.IndexError() |
| + self._CheckNTPThumbnailShown(thumbnail) |
| + cmd_dict = { |
| + 'command': 'MoveNTPMostVisitedThumbnail', |
| + 'url': thumbnail['url'], |
| + 'index': new_index, |
| + 'old_index': self.GetNTPThumbnailIndex(thumbnail) |
| + } |
| + self._GetResultFromJSONRequest(cmd_dict) |
| + |
| + def RemoveNTPThumbnail(self, thumbnail): |
| + """Removes the NTP thumbnail and returns true on success. |
| + |
| + Args: |
| + thumbnail: a thumbnail dict received from |GetNTPThumbnails| |
| + """ |
| + self._CheckNTPThumbnailShown(thumbnail) |
| + cmd_dict = { |
| + 'command': 'RemoveNTPMostVisitedThumbnail', |
| + 'url': thumbnail['url'] |
| + } |
| + self._GetResultFromJSONRequest(cmd_dict) |
| + |
| + def PinNTPThumbnail(self, thumbnail): |
| + """Pins the NTP thumbnail. |
| + |
| + Args: |
| + thumbnail: a thumbnail dict received from |GetNTPThumbnails| |
| + """ |
| + self._CheckNTPThumbnailShown(thumbnail) |
| + self.MoveNTPThumbnail(thumbnail, self.GetNTPThumbnailIndex(thumbnail)) |
| + |
| + def UnpinNTPThumbnail(self, thumbnail): |
| + """Unpins the NTP thumbnail and returns true on success. |
| + |
| + Args: |
| + thumbnail: a thumbnail dict received from |GetNTPThumbnails| |
| + """ |
| + self._CheckNTPThumbnailShown(thumbnail) |
| + cmd_dict = { |
| + 'command': 'UnpinNTPMostVisitedThumbnail', |
| + 'url': thumbnail['url'] |
| + } |
| + self._GetResultFromJSONRequest(cmd_dict) |
| + |
| + def IsNTPThumbnailPinned(self, thumbnail): |
| + """Returns whether the NTP thumbnail is pinned. |
| + |
| + Args: |
| + thumbnail: a thumbnail dict received from |GetNTPThumbnails| |
| + """ |
| + self._CheckNTPThumbnailShown(thumbnail) |
| + index = self.GetNTPThumbnailIndex(thumbnail) |
| + return self.GetNTPThumbnails()[index]['is_pinned'] |
| + |
| + def RestoreAllNTPThumbnails(self): |
| + """Restores all the removed NTP thumbnails. |
| + Note: |
| + the default thumbnails may come back into the Most Visited sites |
| + section after doing this |
| + """ |
| + cmd_dict = { |
| + 'command': 'RestoreAllNTPMostVisitedThumbnails' |
| + } |
| + self._GetResultFromJSONRequest(cmd_dict) |
| + |
| + def CloseDefaultNTPThumbnails(self): |
|
Nirnimesh
2010/11/17 01:25:49
s/Close/Remove/ ?
Where is it used?
kkania
2010/11/17 22:16:08
Renamed it, use it in a new test
|
| + """Closes all the default NTP thumbnails. These do not actually need to be |
| + showing to close. |
| + Note: |
| + using this method will make your test depend on the default thumbnail |
| + urls, which could easily change. Do not use this method unless |
| + necessary. |
| + """ |
| + cmd_dict = { 'command': 'RemoveNTPMostVisitedThumbnail' } |
| + cmd_dict['url'] = 'http://www.google.com/chrome/intl/en/welcome.html' |
| + self._GetResultFromJSONRequest(cmd_dict) |
| + cmd_dict['url'] = ('https://tools.google.com/chrome/intl/en/themes' |
| + '/index.html') |
| + self._GetResultFromJSONRequest(cmd_dict) |
| + |
| + def GetNTPRecentlyClosed(self): |
| + """Return a list of info about the items in the NTP recently closed section. |
| + SAMPLE: |
| + [{ |
| + u'type': u'tab', |
| + u'url': u'http://www.bing.com', |
| + u'title': u'Bing', |
| + u'timestamp': 2139082.03912, # Seconds since epoch (Jan 1, 1970) |
| + u'direction': u'ltr'}, |
| + { |
| + u'type': u'window', |
| + u'timestamp': 2130821.90812, |
| + u'tabs': [ |
| + { |
| + u'type': u'tab', |
| + u'url': u'http://www.cnn.com', |
| + u'title': u'CNN', |
| + u'timestamp': 2129082.12098, |
| + u'direction': u'ltr'}]}, |
| + { |
| + u'type': u'tab', |
| + u'url': u'http://www.altavista.com', |
| + u'title': u'Altavista', |
| + u'timestamp': 21390820.12903, |
| + u'direction': u'rtl'}] |
| + """ |
| + return self._GetNTPInfo()['recently_closed'] |
| + |
| + def _GetNTPInfo(self): |
| + """Get info about the NTP. This does not retrieve the actual info shown |
| + in a particular NTP, but the current data that would be used to display |
| + a NTP. |
| + |
| + This includes info about the most visited sites and the recently closed |
| + tabs and windows. |
| + |
| + TODO(kkania): Add info about apps. |
| + |
| + Returns: |
| + a dictionary containing info about NTP info. See details about the |
| + sections in their respective methods. |
| + |
| + SAMPLE: |
| + { u'most_visited': [ ... ], |
| + u'recently_closed': [ ... ] |
| + } |
| + |
| + Raises: |
| + pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| + """ |
| + cmd_dict = { |
| + 'command': 'GetNTPInfo', |
| + } |
| + return self._GetResultFromJSONRequest(cmd_dict) |
| + |
| + def _CheckNTPThumbnailShown(self, thumbnail): |
| + if self.GetNTPThumbnailIndex(thumbnail) == -1: |
| + raise NTPThumbnailNotShownError() |
| + |
| + |
| class PyUITestSuite(pyautolib.PyUITestSuiteBase, unittest.TestSuite): |
| """Base TestSuite for PyAuto UI tests.""" |