Home | History | Annotate | Download | only in functional
      1 #!/usr/bin/env python
      2 
      3 # Copyright (c) 2011 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 import logging
      8 import time
      9 
     10 import pyauto_functional
     11 import pyauto
     12 import pyauto_utils
     13 import timer_queue
     14 
     15 
     16 class ChromeOSLongTerm(pyauto.PyUITest):
     17   """Set of long running tests for ChromeOS.
     18 
     19   This class is comprised of several tests that perform long term tests.
     20   """
     21 
     22   def _ActivateTabWithURL(self, url):
     23     """Activates the window that has the given tab url.
     24 
     25     Args:
     26       url: The url of the tab to find.
     27 
     28     Returns:
     29       An array of the index values of the tab and window.  Returns None if the
     30       tab connot be found.
     31     """
     32     info = self.GetBrowserInfo()
     33     windows = info['windows']
     34     for window_index, window in enumerate(windows):
     35       tabs = window['tabs']
     36       for tab_index, tab in enumerate(tabs):
     37         tab['url'] = tab['url'].strip('/')
     38         if tab['url'] == url:
     39           self.ActivateTab(tab_index, window_index)
     40           return [tab_index, window_index]
     41     return None
     42 
     43   def _SetupLongTermWindow(self, long_term_pages):
     44     """Appends a list of tab to the current active window.
     45 
     46     Args:
     47       long_term_pages: The list of urls to open.
     48     """
     49     for url in long_term_pages:
     50       self.AppendTab(pyauto.GURL(url))
     51 
     52   def _RefreshLongTermWindow(self, long_term_pages):
     53     """ Refreshes all of the tabs from the given list.
     54 
     55     Args:
     56       long_term_pages: The list of urls to refresh.
     57     """
     58     for page in long_term_pages:
     59       long_index = self._ActivateTabWithURL(page)
     60       if not long_index:
     61         logging.info('Unable to find page with url: %s.')
     62       else:
     63         self.ActivateTab(long_index[0], long_index[1])
     64         self.ReloadActiveTab(long_index[1])
     65 
     66   def _ConfigureNewWindow(self, pages, incognito=False):
     67     """Setups a windows with multiple tabs running.
     68 
     69     This method acts as a state machine.  If a window containing a tab with the
     70     url of the first item of pages it closes that window.  If that window
     71     cannot be found then a new window with the urls in pages is opened.
     72 
     73     Args:
     74       pages: The list of urls to load.
     75     """
     76     page_index = self._ActivateTabWithURL(pages[0])
     77     if not page_index:
     78       # This means the pages do not exist, load them
     79       if incognito:
     80         self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW)
     81       else:
     82         self.OpenNewBrowserWindow(True)
     83       for url in pages:
     84         self.AppendTab(pyauto.GURL(url), self.GetBrowserWindowCount() - 1)
     85       # Cycle through the pages to make sure they render
     86       win = self.GetBrowserInfo()['windows'][self.GetBrowserWindowCount() - 1]
     87       for tab in win['tabs']:
     88         self.ActivateTab(tab['index'], self.GetBrowserWindowCount() - 1)
     89         # Give the plugin time to activate
     90         time.sleep(1.5)
     91     else:
     92       self.CloseBrowserWindow(page_index[1])
     93 
     94   def testLongTerm(self):
     95     """Main entry point for the long term tests.
     96 
     97     This method will spin in a while loop forever until it encounters a keyboard
     98     interrupt.  Other worker methods will be managed by the TimerQueue.
     99     """
    100     long_term_pages = ['http://news.google.com', 'http://www.engadget.com',
    101                        'http://www.washingtonpost.com']
    102 
    103     flash_pages = [
    104        'http://www.craftymind.com/factory/guimark2/FlashChartingTest.swf',
    105        'http://www.craftymind.com/factory/guimark2/FlashGamingTest.swf',
    106        'http://www.craftymind.com/factory/guimark2/FlashTextTest.swf']
    107 
    108     incognito_pages = ['http://www.msn.com', 'http://www.ebay.com',
    109                        'http://www.bu.edu', 'http://www.youtube.com']
    110 
    111     start_time = time.time()
    112     self._SetupLongTermWindow(long_term_pages)
    113     timers = timer_queue.TimerQueue()
    114     timers.AddTimer(self._ConfigureNewWindow, 90, args=(flash_pages,))
    115     timers.AddTimer(self._RefreshLongTermWindow, 30, args=(long_term_pages,))
    116     timers.AddTimer(self._ConfigureNewWindow, 15, args=(incognito_pages, True))
    117     timers.start()
    118     try:
    119       while True:
    120         if not timers.is_alive():
    121           logging.error('Timer queue died, shutting down.')
    122           return
    123         time.sleep(1)
    124 
    125     except KeyboardInterrupt:
    126       # Kill the timers
    127       timers.Stop()
    128 
    129 
    130 if __name__ == '__main__':
    131   pyauto_functional.Main()
    132