| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/python |
| 2 |
| 3 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 """NTPModel: python representation of the NTP. |
| 8 |
| 9 This does not refer to any one particular instance of the NTP, but the |
| 10 model behind all such pages. |
| 11 |
| 12 Obtain one of these from PyUITestSuite::GetNTPModel() call. |
| 13 |
| 14 Most actions with the NTPModel will raise a NTPThumbnailNotShownError |
| 15 if an attempt is made to manipulate a Most Visited site thumbnail that |
| 16 is not visible to the actual user. |
| 17 """ |
| 18 |
| 19 import exceptions |
| 20 |
| 21 |
| 22 class NTPThumbnailNotShownError(RuntimeError): |
| 23 """Represent an error from attempting to manipulate a NTP thumbnail that |
| 24 is not visible to a real user.""" |
| 25 pass |
| 26 |
| 27 class NTPModel(object): |
| 28 |
| 29 def __init__(self, sender): |
| 30 """Initialize the NTPModel with the channel for sending requests. |
| 31 |
| 32 Args: |
| 33 sender: a pyautolib.PyUITestBase which can send JSON requests |
| 34 """ |
| 35 self._sender = sender |
| 36 |
| 37 def GetThumbnails(self): |
| 38 """Return a list of info about the sites in the most visited section. |
| 39 SAMPLE: |
| 40 [{ u'title': u'Google', |
| 41 u'url': u'http://www.google.com', |
| 42 u'is_pinned': False}, |
| 43 { |
| 44 u'title': u'Yahoo', |
| 45 u'url': u'http://www.yahoo.com', |
| 46 u'is_pinned': True}] |
| 47 """ |
| 48 return self._GetInfo()['most_visited'] |
| 49 |
| 50 def GetThumbnailIndex(self, thumbnail): |
| 51 """Returns the index of the given thumbnail, or -1 if it is not shown. |
| 52 |
| 53 Args: |
| 54 thumbnail: a thumbnail dict received from |GetThumbnails| |
| 55 """ |
| 56 thumbnails = self.GetThumbnails() |
| 57 for i in range(len(thumbnails)): |
| 58 if thumbnails[i]['url'] == thumbnail['url']: |
| 59 return i |
| 60 return -1 |
| 61 |
| 62 def MoveThumbnail(self, thumbnail, new_index): |
| 63 """Moves the given thumbnail to a new index. The indices in the NTP Most |
| 64 Visited sites section look like: |
| 65 0 1 2 3 |
| 66 4 5 6 7 |
| 67 |
| 68 Args: |
| 69 thumbnail: a thumbnail dict received from |GetThumbnails| |
| 70 new_index: the index to be moved to in the Most Visited sites section |
| 71 |
| 72 Raises: |
| 73 exceptions.IndexError if there is no thumbnail at the index |
| 74 |
| 75 Note: |
| 76 When a thumbnail is moved, it is automatically pinned |
| 77 """ |
| 78 if new_index < 0 or new_index >= len(self.GetThumbnails()): |
| 79 raise exceptions.IndexError() |
| 80 self._CheckThumbnailShown(thumbnail) |
| 81 cmd_dict = { |
| 82 'command': 'MoveNTPMostVisitedThumbnail', |
| 83 'url': thumbnail['url'], |
| 84 'index': new_index, |
| 85 'old_index': self.GetThumbnailIndex(thumbnail) |
| 86 } |
| 87 self._sender._GetResultFromJSONRequest(cmd_dict) |
| 88 |
| 89 def RemoveThumbnail(self, thumbnail): |
| 90 """Removes the thumbnail and returns true on success. |
| 91 |
| 92 Args: |
| 93 thumbnail: a thumbnail dict received from |GetThumbnails| |
| 94 """ |
| 95 self._CheckThumbnailShown(thumbnail) |
| 96 cmd_dict = { |
| 97 'command': 'RemoveNTPMostVisitedThumbnail', |
| 98 'url': thumbnail['url'] |
| 99 } |
| 100 self._sender._GetResultFromJSONRequest(cmd_dict) |
| 101 |
| 102 def PinThumbnail(self, thumbnail): |
| 103 """Pins the thumbnail. |
| 104 |
| 105 Args: |
| 106 thumbnail: a thumbnail dict received from |GetThumbnails| |
| 107 """ |
| 108 self._CheckThumbnailShown(thumbnail) |
| 109 self.MoveThumbnail(thumbnail, self.GetThumbnailIndex(thumbnail)) |
| 110 |
| 111 def UnpinThumbnail(self, thumbnail): |
| 112 """Unpins the thumbnail and returns true on success. |
| 113 |
| 114 Args: |
| 115 thumbnail: a thumbnail dict received from |GetThumbnails| |
| 116 """ |
| 117 self._CheckThumbnailShown(thumbnail) |
| 118 cmd_dict = { |
| 119 'command': 'UnpinNTPMostVisitedThumbnail', |
| 120 'url': thumbnail['url'] |
| 121 } |
| 122 self._sender._GetResultFromJSONRequest(cmd_dict) |
| 123 |
| 124 def IsPinned(self, thumbnail): |
| 125 """Returns whether the thumbnail is pinned. |
| 126 |
| 127 Args: |
| 128 thumbnail: a thumbnail dict received from |GetThumbnails| |
| 129 """ |
| 130 self._CheckThumbnailShown(thumbnail) |
| 131 return self.GetThumbnails()[self.GetThumbnailIndex(thumbnail)]['is_pinned'] |
| 132 |
| 133 def RestoreAllThumbnails(self): |
| 134 """Restores all the removed thumbnails. |
| 135 Note: |
| 136 the default thumbnails may come back into the Most Visited sites |
| 137 section after doing this |
| 138 """ |
| 139 cmd_dict = { |
| 140 'command': 'RestoreAllNTPMostVisitedThumbnails' |
| 141 } |
| 142 self._sender._GetResultFromJSONRequest(cmd_dict) |
| 143 |
| 144 def CloseDefaultThumbnails(self): |
| 145 """Closes all the default thumbnails. These do not actually need to be |
| 146 showing to close. |
| 147 Note: |
| 148 using this method will make your test depend on the default thumbnail |
| 149 urls, which could easily change. Do not use this method unless |
| 150 necessary. |
| 151 """ |
| 152 cmd_dict = { 'command': 'RemoveNTPMostVisitedThumbnail' } |
| 153 cmd_dict['url'] = 'http://www.google.com/chrome/intl/en/welcome.html' |
| 154 self._sender._GetResultFromJSONRequest(cmd_dict) |
| 155 cmd_dict['url'] = ('https://tools.google.com/chrome/intl/en/themes' |
| 156 '/index.html') |
| 157 self._sender._GetResultFromJSONRequest(cmd_dict) |
| 158 |
| 159 def GetRecentlyClosed(self): |
| 160 """Return a list of info about the items in the recently closed section. |
| 161 SAMPLE: |
| 162 [{ |
| 163 u'type': u'tab', |
| 164 u'url': u'http://www.bing.com', |
| 165 u'title': u'Bing', |
| 166 u'timestamp': 2139082.03912, # Seconds since epoch (Jan 1, 1970) |
| 167 u'direction': u'ltr'}, |
| 168 { |
| 169 u'type': u'window', |
| 170 u'timestamp': 2130821.90812, |
| 171 u'tabs': [ |
| 172 { |
| 173 u'type': u'tab', |
| 174 u'url': u'http://www.cnn.com', |
| 175 u'title': u'CNN', |
| 176 u'timestamp': 2129082.12098, |
| 177 u'direction': u'ltr'}]}, |
| 178 { |
| 179 u'type': u'tab', |
| 180 u'url': u'http://www.altavista.com', |
| 181 u'title': u'Altavista', |
| 182 u'timestamp': 21390820.12903, |
| 183 u'direction': u'rtl'}] |
| 184 """ |
| 185 return self._GetInfo()['recently_closed'] |
| 186 |
| 187 def _GetInfo(self): |
| 188 """Get info about the NTP. This does not retrieve the actual info shown |
| 189 in a particular NTP, but the current data that would be used to display |
| 190 a NTP. |
| 191 |
| 192 This includes info about the most visited sites and the recently closed |
| 193 tabs and windows. |
| 194 |
| 195 Returns: |
| 196 a dictionary containing info about NTP info. See details about the |
| 197 sections in their respective methods. |
| 198 SAMPLE: |
| 199 { u'most_visited': [ ... ], |
| 200 u'recently_closed': [ ... ] |
| 201 } |
| 202 |
| 203 Raises: |
| 204 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 205 """ |
| 206 cmd_dict = { |
| 207 'command': 'GetNTPInfo', |
| 208 } |
| 209 return self._sender._GetResultFromJSONRequest(cmd_dict) |
| 210 |
| 211 def _CheckThumbnailShown(self, thumbnail): |
| 212 if self.GetThumbnailIndex(thumbnail) == -1: |
| 213 raise NTPThumbnailNotShownError() |
| OLD | NEW |