Chromium Code Reviews| Index: chrome/test/pyautolib/ntp_model.py |
| diff --git a/chrome/test/pyautolib/ntp_model.py b/chrome/test/pyautolib/ntp_model.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c93b3b0889bdd5df79c65a66bc6986bcb49a84de |
| --- /dev/null |
| +++ b/chrome/test/pyautolib/ntp_model.py |
| @@ -0,0 +1,213 @@ |
| +#!/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. |
| + |
| +"""NTPModel: python representation of the NTP. |
|
Nirnimesh
2010/11/16 22:25:05
A "model" should not have methods to manipulate it
kkania
2010/11/17 01:11:34
Done.
|
| + |
| +This does not refer to any one particular instance of the NTP, but the |
| +model behind all such pages. |
| + |
| +Obtain one of these from PyUITestSuite::GetNTPModel() call. |
| + |
| +Most actions with the NTPModel will raise a NTPThumbnailNotShownError |
| +if an attempt is made to manipulate a Most Visited site thumbnail that |
| +is not visible to the actual user. |
| +""" |
| + |
| +import exceptions |
| + |
| + |
| +class NTPThumbnailNotShownError(RuntimeError): |
| + """Represent an error from attempting to manipulate a NTP thumbnail that |
| + is not visible to a real user.""" |
| + pass |
| + |
| +class NTPModel(object): |
| + |
| + def __init__(self, sender): |
| + """Initialize the NTPModel with the channel for sending requests. |
| + |
| + Args: |
| + sender: a pyautolib.PyUITestBase which can send JSON requests |
|
Nirnimesh
2010/11/16 22:25:05
not true. _GetResultFromJSONRequest lives in PyUIT
kkania
2010/11/17 01:11:34
Done.
|
| + """ |
| + self._sender = sender |
| + |
| + def GetThumbnails(self): |
| + """Return a list of info about the sites in the 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._GetInfo()['most_visited'] |
| + |
| + def GetThumbnailIndex(self, thumbnail): |
| + """Returns the index of the given thumbnail, or -1 if it is not shown. |
| + |
| + Args: |
| + thumbnail: a thumbnail dict received from |GetThumbnails| |
| + """ |
| + thumbnails = self.GetThumbnails() |
| + for i in range(len(thumbnails)): |
| + if thumbnails[i]['url'] == thumbnail['url']: |
| + return i |
| + return -1 |
| + |
| + def MoveThumbnail(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 |GetThumbnails| |
| + 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: |
| + When a thumbnail is moved, it is automatically pinned |
| + """ |
| + if new_index < 0 or new_index >= len(self.GetThumbnails()): |
| + raise exceptions.IndexError() |
| + self._CheckThumbnailShown(thumbnail) |
| + cmd_dict = { |
| + 'command': 'MoveNTPMostVisitedThumbnail', |
| + 'url': thumbnail['url'], |
| + 'index': new_index, |
| + 'old_index': self.GetThumbnailIndex(thumbnail) |
| + } |
| + self._sender._GetResultFromJSONRequest(cmd_dict) |
| + |
| + def RemoveThumbnail(self, thumbnail): |
| + """Removes the thumbnail and returns true on success. |
| + |
| + Args: |
| + thumbnail: a thumbnail dict received from |GetThumbnails| |
| + """ |
| + self._CheckThumbnailShown(thumbnail) |
| + cmd_dict = { |
| + 'command': 'RemoveNTPMostVisitedThumbnail', |
| + 'url': thumbnail['url'] |
| + } |
| + self._sender._GetResultFromJSONRequest(cmd_dict) |
| + |
| + def PinThumbnail(self, thumbnail): |
| + """Pins the thumbnail. |
| + |
| + Args: |
| + thumbnail: a thumbnail dict received from |GetThumbnails| |
| + """ |
| + self._CheckThumbnailShown(thumbnail) |
| + self.MoveThumbnail(thumbnail, self.GetThumbnailIndex(thumbnail)) |
| + |
| + def UnpinThumbnail(self, thumbnail): |
| + """Unpins the thumbnail and returns true on success. |
| + |
| + Args: |
| + thumbnail: a thumbnail dict received from |GetThumbnails| |
| + """ |
| + self._CheckThumbnailShown(thumbnail) |
| + cmd_dict = { |
| + 'command': 'UnpinNTPMostVisitedThumbnail', |
| + 'url': thumbnail['url'] |
| + } |
| + self._sender._GetResultFromJSONRequest(cmd_dict) |
| + |
| + def IsPinned(self, thumbnail): |
| + """Returns whether the thumbnail is pinned. |
| + |
| + Args: |
| + thumbnail: a thumbnail dict received from |GetThumbnails| |
| + """ |
| + self._CheckThumbnailShown(thumbnail) |
| + return self.GetThumbnails()[self.GetThumbnailIndex(thumbnail)]['is_pinned'] |
| + |
| + def RestoreAllThumbnails(self): |
| + """Restores all the removed thumbnails. |
| + Note: |
| + the default thumbnails may come back into the Most Visited sites |
| + section after doing this |
| + """ |
| + cmd_dict = { |
| + 'command': 'RestoreAllNTPMostVisitedThumbnails' |
| + } |
| + self._sender._GetResultFromJSONRequest(cmd_dict) |
| + |
| + def CloseDefaultThumbnails(self): |
| + """Closes all the default 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._sender._GetResultFromJSONRequest(cmd_dict) |
| + cmd_dict['url'] = ('https://tools.google.com/chrome/intl/en/themes' |
| + '/index.html') |
| + self._sender._GetResultFromJSONRequest(cmd_dict) |
| + |
| + def GetRecentlyClosed(self): |
| + """Return a list of info about the items in the 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._GetInfo()['recently_closed'] |
| + |
| + def _GetInfo(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. |
| + |
|
Nirnimesh
2010/11/16 22:25:05
please add a TODO about adding apps info
kkania
2010/11/17 01:11:34
Done.
|
| + This includes info about the most visited sites and the recently closed |
| + tabs and windows. |
| + |
| + 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._sender._GetResultFromJSONRequest(cmd_dict) |
| + |
| + def _CheckThumbnailShown(self, thumbnail): |
| + if self.GetThumbnailIndex(thumbnail) == -1: |
| + raise NTPThumbnailNotShownError() |