| OLD | NEW | 
|     1 #!/usr/bin/python |     1 #!/usr/bin/python | 
|     2  |     2  | 
|     3 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |     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 |     4 # Use of this source code is governed by a BSD-style license that can be | 
|     5 # found in the LICENSE file. |     5 # found in the LICENSE file. | 
|     6  |     6  | 
|     7 """PyAuto: Python Interface to Chromium's Automation Proxy. |     7 """PyAuto: Python Interface to Chromium's Automation Proxy. | 
|     8  |     8  | 
|     9 PyAuto uses swig to expose Automation Proxy interfaces to Python. |     9 PyAuto uses swig to expose Automation Proxy interfaces to Python. | 
|    10 For complete documentation on the functionality available, |    10 For complete documentation on the functionality available, | 
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|    79   raise |    79   raise | 
|    80  |    80  | 
|    81 # Should go after sys.path is set appropriately |    81 # Should go after sys.path is set appropriately | 
|    82 import bookmark_model |    82 import bookmark_model | 
|    83 import download_info |    83 import download_info | 
|    84 import history_info |    84 import history_info | 
|    85 import omnibox_info |    85 import omnibox_info | 
|    86 import plugins_info |    86 import plugins_info | 
|    87 import prefs_info |    87 import prefs_info | 
|    88 from pyauto_errors import JSONInterfaceError |    88 from pyauto_errors import JSONInterfaceError | 
 |    89 from pyauto_errors import NTPThumbnailNotShownError | 
|    89 import simplejson as json  # found in third_party |    90 import simplejson as json  # found in third_party | 
|    90  |    91  | 
|    91  |    92  | 
|    92 class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): |    93 class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): | 
|    93   """Base class for UI Test Cases in Python. |    94   """Base class for UI Test Cases in Python. | 
|    94  |    95  | 
|    95   A browser is created before executing each test, and is destroyed after |    96   A browser is created before executing each test, and is destroyed after | 
|    96   each test irrespective of whether the test passed or failed. |    97   each test irrespective of whether the test passed or failed. | 
|    97  |    98  | 
|    98   You should derive from this class and create methods with 'test' prefix, |    99   You should derive from this class and create methods with 'test' prefix, | 
| (...skipping 1670 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1769  |  1770  | 
|  1770     Raises: |  1771     Raises: | 
|  1771       pyauto_errors.JSONInterfaceError if the automation call returns an error. |  1772       pyauto_errors.JSONInterfaceError if the automation call returns an error. | 
|  1772     """ |  1773     """ | 
|  1773     cmd_dict = { |  1774     cmd_dict = { | 
|  1774       'command': 'DisableSyncForDatatypes', |  1775       'command': 'DisableSyncForDatatypes', | 
|  1775       'datatypes': datatypes, |  1776       'datatypes': datatypes, | 
|  1776     } |  1777     } | 
|  1777     return self._GetResultFromJSONRequest(cmd_dict)['success'] |  1778     return self._GetResultFromJSONRequest(cmd_dict)['success'] | 
|  1778  |  1779  | 
 |  1780   def GetNTPThumbnails(self): | 
 |  1781     """Return a list of info about the sites in the NTP most visited section. | 
 |  1782     SAMPLE: | 
 |  1783       [{ u'title': u'Google', | 
 |  1784          u'url': u'http://www.google.com', | 
 |  1785          u'is_pinned': False}, | 
 |  1786        { | 
 |  1787          u'title': u'Yahoo', | 
 |  1788          u'url': u'http://www.yahoo.com', | 
 |  1789          u'is_pinned': True}] | 
 |  1790     """ | 
 |  1791     return self._GetNTPInfo()['most_visited'] | 
 |  1792  | 
 |  1793   def GetNTPThumbnailIndex(self, thumbnail): | 
 |  1794     """Returns the index of the given NTP thumbnail, or -1 if it is not shown. | 
 |  1795  | 
 |  1796     Args: | 
 |  1797       thumbnail: a thumbnail dict received from |GetNTPThumbnails| | 
 |  1798     """ | 
 |  1799     thumbnails = self.GetNTPThumbnails() | 
 |  1800     for i in range(len(thumbnails)): | 
 |  1801       if thumbnails[i]['url'] == thumbnail['url']: | 
 |  1802         return i | 
 |  1803     return -1 | 
 |  1804  | 
 |  1805   def MoveNTPThumbnail(self, thumbnail, new_index): | 
 |  1806     """Moves the given thumbnail to a new index. The indices in the NTP Most | 
 |  1807     Visited sites section look like: | 
 |  1808       0  1  2  3 | 
 |  1809       4  5  6  7 | 
 |  1810  | 
 |  1811     When a thumbnail is moved, it is automatically pinned. | 
 |  1812  | 
 |  1813     Args: | 
 |  1814       thumbnail: a thumbnail dict received from |GetNTPThumbnails| | 
 |  1815       new_index: the index to be moved to in the Most Visited sites section | 
 |  1816  | 
 |  1817     Raises: | 
 |  1818       IndexError if there is no thumbnail at the index | 
 |  1819     """ | 
 |  1820     if new_index < 0 or new_index >= len(self.GetNTPThumbnails()): | 
 |  1821       raise IndexError() | 
 |  1822     self._CheckNTPThumbnailShown(thumbnail) | 
 |  1823     cmd_dict = { | 
 |  1824       'command': 'MoveNTPMostVisitedThumbnail', | 
 |  1825       'url': thumbnail['url'], | 
 |  1826       'index': new_index, | 
 |  1827       'old_index': self.GetNTPThumbnailIndex(thumbnail) | 
 |  1828     } | 
 |  1829     self._GetResultFromJSONRequest(cmd_dict) | 
 |  1830  | 
 |  1831   def RemoveNTPThumbnail(self, thumbnail): | 
 |  1832     """Removes the NTP thumbnail and returns true on success. | 
 |  1833  | 
 |  1834     Args: | 
 |  1835       thumbnail: a thumbnail dict received from |GetNTPThumbnails| | 
 |  1836     """ | 
 |  1837     self._CheckNTPThumbnailShown(thumbnail) | 
 |  1838     cmd_dict = { | 
 |  1839       'command': 'RemoveNTPMostVisitedThumbnail', | 
 |  1840       'url': thumbnail['url'] | 
 |  1841     } | 
 |  1842     self._GetResultFromJSONRequest(cmd_dict) | 
 |  1843  | 
 |  1844   def PinNTPThumbnail(self, thumbnail): | 
 |  1845     """Pins the NTP thumbnail. | 
 |  1846  | 
 |  1847     Args: | 
 |  1848       thumbnail: a thumbnail dict received from |GetNTPThumbnails| | 
 |  1849     """ | 
 |  1850     self._CheckNTPThumbnailShown(thumbnail) | 
 |  1851     self.MoveNTPThumbnail(thumbnail, self.GetNTPThumbnailIndex(thumbnail)) | 
 |  1852  | 
 |  1853   def UnpinNTPThumbnail(self, thumbnail): | 
 |  1854     """Unpins the NTP thumbnail and returns true on success. | 
 |  1855  | 
 |  1856     Args: | 
 |  1857       thumbnail: a thumbnail dict received from |GetNTPThumbnails| | 
 |  1858     """ | 
 |  1859     self._CheckNTPThumbnailShown(thumbnail) | 
 |  1860     cmd_dict = { | 
 |  1861       'command': 'UnpinNTPMostVisitedThumbnail', | 
 |  1862       'url': thumbnail['url'] | 
 |  1863     } | 
 |  1864     self._GetResultFromJSONRequest(cmd_dict) | 
 |  1865  | 
 |  1866   def IsNTPThumbnailPinned(self, thumbnail): | 
 |  1867     """Returns whether the NTP thumbnail is pinned. | 
 |  1868  | 
 |  1869     Args: | 
 |  1870       thumbnail: a thumbnail dict received from |GetNTPThumbnails| | 
 |  1871     """ | 
 |  1872     self._CheckNTPThumbnailShown(thumbnail) | 
 |  1873     index = self.GetNTPThumbnailIndex(thumbnail) | 
 |  1874     return self.GetNTPThumbnails()[index]['is_pinned'] | 
 |  1875  | 
 |  1876   def RestoreAllNTPThumbnails(self): | 
 |  1877     """Restores all the removed NTP thumbnails. | 
 |  1878     Note: | 
 |  1879       the default thumbnails may come back into the Most Visited sites | 
 |  1880       section after doing this | 
 |  1881     """ | 
 |  1882     cmd_dict = { | 
 |  1883       'command': 'RestoreAllNTPMostVisitedThumbnails' | 
 |  1884     } | 
 |  1885     self._GetResultFromJSONRequest(cmd_dict) | 
 |  1886  | 
 |  1887   def GetNTPDefaultSites(self): | 
 |  1888     """Returns a list of URLs for all the default NTP sites, regardless of | 
 |  1889     whether they are showing or not. | 
 |  1890  | 
 |  1891     These sites are the ones present in the NTP on a fresh install of Chrome. | 
 |  1892     """ | 
 |  1893     return self._GetNTPInfo()['default_sites'] | 
 |  1894  | 
 |  1895   def RemoveNTPDefaultThumbnails(self): | 
 |  1896     """Removes all thumbnails for default NTP sites, regardless of whether they | 
 |  1897     are showing or not.""" | 
 |  1898     cmd_dict = { 'command': 'RemoveNTPMostVisitedThumbnail' } | 
 |  1899     for site in self.GetNTPDefaultSites(): | 
 |  1900       cmd_dict['url'] = site | 
 |  1901       self._GetResultFromJSONRequest(cmd_dict) | 
 |  1902  | 
 |  1903   def GetNTPRecentlyClosed(self): | 
 |  1904     """Return a list of info about the items in the NTP recently closed section. | 
 |  1905     SAMPLE: | 
 |  1906       [{ | 
 |  1907          u'type': u'tab', | 
 |  1908          u'url': u'http://www.bing.com', | 
 |  1909          u'title': u'Bing', | 
 |  1910          u'timestamp': 2139082.03912,  # Seconds since epoch (Jan 1, 1970) | 
 |  1911          u'direction': u'ltr'}, | 
 |  1912        { | 
 |  1913          u'type': u'window', | 
 |  1914          u'timestamp': 2130821.90812, | 
 |  1915          u'tabs': [ | 
 |  1916          { | 
 |  1917            u'type': u'tab', | 
 |  1918            u'url': u'http://www.cnn.com', | 
 |  1919            u'title': u'CNN', | 
 |  1920            u'timestamp': 2129082.12098, | 
 |  1921            u'direction': u'ltr'}]}, | 
 |  1922        { | 
 |  1923          u'type': u'tab', | 
 |  1924          u'url': u'http://www.altavista.com', | 
 |  1925          u'title': u'Altavista', | 
 |  1926          u'timestamp': 21390820.12903, | 
 |  1927          u'direction': u'rtl'}] | 
 |  1928     """ | 
 |  1929     return self._GetNTPInfo()['recently_closed'] | 
 |  1930  | 
 |  1931   def _GetNTPInfo(self): | 
 |  1932     """Get info about the NTP. This does not retrieve the actual info shown | 
 |  1933     in a particular NTP, but the current data that would be used to display | 
 |  1934     a NTP. | 
 |  1935  | 
 |  1936     This includes info about the most visited sites, the recently closed | 
 |  1937     tabs and windows, and the default NTP sites. | 
 |  1938  | 
 |  1939     TODO(kkania): Add info about apps. | 
 |  1940  | 
 |  1941     Returns: | 
 |  1942       a dictionary containing info about NTP info. See details about the | 
 |  1943       sections in their respective methods. | 
 |  1944  | 
 |  1945     SAMPLE: | 
 |  1946     { u'most_visited': [ ... ], | 
 |  1947       u'recently_closed': [ ... ] | 
 |  1948       u'default_sites': [ ... ] | 
 |  1949     } | 
 |  1950  | 
 |  1951     Raises: | 
 |  1952       pyauto_errors.JSONInterfaceError if the automation call returns an error. | 
 |  1953     """ | 
 |  1954     cmd_dict = { | 
 |  1955       'command': 'GetNTPInfo', | 
 |  1956     } | 
 |  1957     return self._GetResultFromJSONRequest(cmd_dict) | 
 |  1958  | 
 |  1959   def _CheckNTPThumbnailShown(self, thumbnail): | 
 |  1960     if self.GetNTPThumbnailIndex(thumbnail) == -1: | 
 |  1961       raise NTPThumbnailNotShownError() | 
 |  1962  | 
 |  1963  | 
|  1779 class PyUITestSuite(pyautolib.PyUITestSuiteBase, unittest.TestSuite): |  1964 class PyUITestSuite(pyautolib.PyUITestSuiteBase, unittest.TestSuite): | 
|  1780   """Base TestSuite for PyAuto UI tests.""" |  1965   """Base TestSuite for PyAuto UI tests.""" | 
|  1781  |  1966  | 
|  1782   def __init__(self, args): |  1967   def __init__(self, args): | 
|  1783     pyautolib.PyUITestSuiteBase.__init__(self, args) |  1968     pyautolib.PyUITestSuiteBase.__init__(self, args) | 
|  1784  |  1969  | 
|  1785     # Figure out path to chromium binaries |  1970     # Figure out path to chromium binaries | 
|  1786     browser_dir = os.path.normpath(os.path.dirname(pyautolib.__file__)) |  1971     browser_dir = os.path.normpath(os.path.dirname(pyautolib.__file__)) | 
|  1787     logging.debug('Loading pyauto libs from %s', browser_dir) |  1972     logging.debug('Loading pyauto libs from %s', browser_dir) | 
|  1788     self.Initialize(pyautolib.FilePath(browser_dir)) |  1973     self.Initialize(pyautolib.FilePath(browser_dir)) | 
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  2077     if self._options.verbose: |  2262     if self._options.verbose: | 
|  2078       verbosity = 2 |  2263       verbosity = 2 | 
|  2079     result = PyAutoTextTestRuner(verbosity=verbosity).run(pyauto_suite) |  2264     result = PyAutoTextTestRuner(verbosity=verbosity).run(pyauto_suite) | 
|  2080     del loaded_tests  # Need to destroy test cases before the suite |  2265     del loaded_tests  # Need to destroy test cases before the suite | 
|  2081     del pyauto_suite |  2266     del pyauto_suite | 
|  2082     sys.exit(not result.wasSuccessful()) |  2267     sys.exit(not result.wasSuccessful()) | 
|  2083  |  2268  | 
|  2084  |  2269  | 
|  2085 if __name__ == '__main__': |  2270 if __name__ == '__main__': | 
|  2086   Main() |  2271   Main() | 
| OLD | NEW |