Home | History | Annotate | Download | only in functional
      1 #!/usr/bin/env python
      2 # Copyright 2013 The Chromium Authors. All rights reserved.
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 
      6 import logging
      7 import os
      8 import re
      9 
     10 import pyauto_functional  # Must be imported before pyauto
     11 import pyauto
     12 import test_utils
     13 
     14 
     15 class InfobarTest(pyauto.PyUITest):
     16   """TestCase for Infobars."""
     17 
     18   def Debug(self):
     19     """Test method for experimentation.
     20 
     21     This method will not run automatically.
     22     To run:
     23       python chrome/test/functional/infobars.py infobars.InfobarTest.Debug
     24     """
     25     while True:
     26       raw_input('Hit <enter> to dump info.. ')
     27       info = self.GetBrowserInfo()
     28       for window in info['windows']:
     29         for tab in window['tabs']:
     30           print 'Window', window['index'], 'tab', tab['index']
     31           self.pprint(tab['infobars'])
     32 
     33   def setUp(self):
     34     pyauto.PyUITest.setUp(self)
     35     self._flash_plugin_type = 'Plug-in'
     36     if self.GetBrowserInfo()['properties']['branding'] == 'Google Chrome':
     37       self._flash_plugin_type = 'Pepper Plugin'
     38     # Forcibly trigger all plugins to get registered.  crbug.com/94123
     39     # Sometimes flash files loaded too quickly after firing browser
     40     # ends up getting downloaded, which seems to indicate that the plugin
     41     # hasn't been registered yet.
     42     self.GetPluginsInfo()
     43 
     44   def _GetTabInfo(self, windex=0, tab_index=0):
     45     """Helper to return info for the given tab in the given window.
     46 
     47     Defaults to first tab in first window.
     48     """
     49     return self.GetBrowserInfo()['windows'][windex]['tabs'][tab_index]
     50 
     51   def testPluginCrashInfobar(self):
     52     """Verify the "plugin crashed" infobar."""
     53     flash_url = self.GetFileURLForContentDataPath('plugin', 'flash.swf')
     54     # Trigger flash plugin
     55     self.NavigateToURL(flash_url)
     56     child_processes = self.GetBrowserInfo()['child_processes']
     57     flash = [x for x in child_processes if
     58              x['type'] == self._flash_plugin_type and
     59              x['name'] == 'Shockwave Flash'][0]
     60     self.assertTrue(flash)
     61     logging.info('Killing flash plugin. pid %d' % flash['pid'])
     62     self.Kill(flash['pid'])
     63     self.assertTrue(self.WaitForInfobarCount(1))
     64     crash_infobar = self._GetTabInfo()['infobars']
     65     self.assertTrue(crash_infobar)
     66     self.assertEqual(1, len(crash_infobar))
     67     self.assertTrue('crashed' in crash_infobar[0]['text'])
     68     self.assertEqual('confirm_infobar', crash_infobar[0]['type'])
     69     # Dismiss the infobar
     70     self.PerformActionOnInfobar('dismiss', infobar_index=0)
     71     self.assertFalse(self._GetTabInfo()['infobars'])
     72 
     73   def _VerifyGeolocationInfobar(self, windex, tab_index):
     74     """Verify geolocation infobar properties.
     75 
     76     Assumes that geolocation infobar is showing up in the given tab in the
     77     given window.
     78     """
     79     # TODO(dyu): Remove this helper function when a function to identify
     80     # infobar_type and index of the type is implemented.
     81     tab_info = self._GetTabInfo(windex, tab_index)
     82     geolocation_infobar = tab_info['infobars']
     83     self.assertTrue(geolocation_infobar)
     84     self.assertEqual(1, len(geolocation_infobar))
     85     self.assertEqual('Learn more', geolocation_infobar[0]['link_text'])
     86     self.assertEqual(2, len(geolocation_infobar[0]['buttons']))
     87     self.assertEqual('Allow', geolocation_infobar[0]['buttons'][0])
     88     self.assertEqual('Deny', geolocation_infobar[0]['buttons'][1])
     89 
     90   def testGeolocationInfobar(self):
     91     """Verify geoLocation infobar."""
     92     url = self.GetHttpURLForDataPath('geolocation', 'geolocation_on_load.html')
     93     self.NavigateToURL(url)
     94     self.assertTrue(self.WaitForInfobarCount(1))
     95     self._VerifyGeolocationInfobar(windex=0, tab_index=0)
     96     # Accept, and verify that the infobar went away
     97     self.PerformActionOnInfobar('accept', infobar_index=0)
     98     self.assertFalse(self._GetTabInfo()['infobars'])
     99 
    100   def testGeolocationInfobarInMultipleTabsAndWindows(self):
    101     """Verify GeoLocation inforbar in multiple tabs."""
    102     url = self.GetFileURLForDataPath(  # triggers geolocation
    103         'geolocation', 'geolocation_on_load.html')
    104     for tab_index in range(1, 2):
    105       self.AppendTab(pyauto.GURL(url))
    106       self.assertTrue(
    107           self.WaitForInfobarCount(1, windex=0, tab_index=tab_index))
    108       self._VerifyGeolocationInfobar(windex=0, tab_index=tab_index)
    109     # Try in a new window
    110     self.OpenNewBrowserWindow(True)
    111     self.NavigateToURL(url, 1, 0)
    112     self.assertTrue(self.WaitForInfobarCount(1, windex=1, tab_index=0))
    113     self._VerifyGeolocationInfobar(windex=1, tab_index=0)
    114     # Incognito window
    115     self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW)
    116     self.NavigateToURL(url, 2, 0)
    117     self.assertTrue(self.WaitForInfobarCount(1, windex=2, tab_index=0))
    118     self._VerifyGeolocationInfobar(windex=2, tab_index=0)
    119 
    120   def _GetFlashCrashInfobarCount(self, windex=0, tab_index=0):
    121     """Returns the count of 'Shockwave Flash has crashed' infobars."""
    122     browser_window = self.GetBrowserInfo()['windows'][windex]
    123     infobars = browser_window['tabs'][tab_index]['infobars']
    124     flash_crash_infobar_count = 0
    125     for infobar in infobars:
    126       if (('text' in infobar) and
    127           infobar['text'].startswith('Shockwave Flash has crashed')):
    128         flash_crash_infobar_count += 1
    129     return flash_crash_infobar_count
    130 
    131   def testPluginCrashForMultiTabs(self):
    132     """Verify plugin crash infobar shows up only on the tabs using plugin."""
    133     non_flash_url = self.GetFileURLForDataPath('english_page.html')
    134     flash_url = self.GetFileURLForContentDataPath('plugin', 'FlashSpin.swf')
    135     # False = Non flash url, True = Flash url
    136     # We have set of these values to compare a flash page and a non-flash page
    137     urls_type = [False, True, False, True, False]
    138     for _ in range(2):
    139       self.AppendTab(pyauto.GURL(flash_url))
    140       self.AppendTab(pyauto.GURL(non_flash_url))
    141     # Killing flash process
    142     child_processes = self.GetBrowserInfo()['child_processes']
    143     flash = [x for x in child_processes if
    144              x['type'] == self._flash_plugin_type and
    145              x['name'] == 'Shockwave Flash'][0]
    146     self.assertTrue(flash)
    147     self.Kill(flash['pid'])
    148     # Crash plugin infobar should show up in the second tab of this window
    149     # so passing window and tab argument in the wait for an infobar.
    150     self.assertTrue(self.WaitForInfobarCount(1, windex=0, tab_index=1))
    151     for i in range(len(urls_type)):
    152       # Verify that if page doesn't have flash plugin,
    153       # it should not have infobar popped-up
    154       self.ActivateTab(i)
    155       if not urls_type[i]:
    156         self.assertEqual(
    157             self._GetFlashCrashInfobarCount(0, i), 0,
    158             msg='Did not expect crash infobar in tab at index %d' % i)
    159       elif urls_type[i]:
    160         self.assertEqual(
    161             self._GetFlashCrashInfobarCount(0, i), 1,
    162             msg='Expected crash infobar in tab at index %d' % i)
    163         infobar = self.GetBrowserInfo()['windows'][0]['tabs'][i]['infobars']
    164         self.assertEqual(infobar[0]['type'], 'confirm_infobar')
    165         self.assertEqual(len(infobar), 1)
    166 
    167 
    168 class OneClickInfobarTest(pyauto.PyUITest):
    169   """Tests for one-click sign in infobar."""
    170 
    171   BLOCK_COOKIE_PATTERN = {'https://accounts.google.com/': {'cookies': 2}}
    172   OC_INFOBAR_TYPE = 'oneclicklogin_infobar'
    173   PW_INFOBAR_TYPE = 'password_infobar'
    174   URL = 'https://www.google.com/accounts/ServiceLogin'
    175   URL_LOGIN = 'https://www.google.com/accounts/Login'
    176   URL_LOGOUT = 'https://www.google.com/accounts/Logout'
    177 
    178   def setUp(self):
    179     pyauto.PyUITest.setUp(self)
    180     self._driver = self.NewWebDriver()
    181 
    182   def _LogIntoGoogleAccount(self, tab_index=0, windex=0):
    183     """Log into Google account.
    184 
    185     Args:
    186       tab_index: The tab index, default is 0.
    187       windex: The window index, default is 0.
    188     """
    189     creds = self.GetPrivateInfo()['test_google_account']
    190     username = creds['username']
    191     password = creds['password']
    192     test_utils.GoogleAccountsLogin(self, username, password, tab_index, windex)
    193     # TODO(dyu): Use WaitUntilNavigationCompletes after investigating
    194     # crbug.com/124877
    195     self.WaitUntil(
    196         lambda: self.GetDOMValue('document.readyState'),
    197         expect_retval='complete')
    198 
    199   def _PerformActionOnInfobar(self, action):
    200     """Perform an action on the infobar: accept, cancel, or dismiss.
    201 
    202     The one-click sign in infobar must show in the first tab of the first
    203     window. If action is 'accept' then the account is synced. If the action is
    204     'cancel' then the infobar should be dismissed and never shown again. The
    205     account will not be synced. If the action is 'dismiss' then the infobar will
    206     shown again after the next login.
    207 
    208     Args:
    209       action: The action to perform on the infobar.
    210     """
    211     infobar_index = test_utils.WaitForInfobarTypeAndGetIndex(
    212         self, self.OC_INFOBAR_TYPE)
    213     self.PerformActionOnInfobar(action, infobar_index)
    214 
    215   def _DisplayOneClickInfobar(self, tab_index=0, windex=0):
    216     """One-click sign in infobar appears after logging into google account.
    217 
    218     Args:
    219       tab_index: The tab index, default is 0.
    220       windex: The window index, default is 0.
    221     """
    222     self._LogIntoGoogleAccount(tab_index=tab_index, windex=windex)
    223     self.assertTrue(self.WaitUntil(
    224         lambda: test_utils.GetInfobarIndexByType(
    225             self, self.OC_INFOBAR_TYPE,
    226             tab_index=tab_index, windex=windex) is not None),
    227                     msg='The one-click login infobar did not appear.')
    228 
    229   def testDisplayOneClickInfobar(self):
    230     """Verify one-click infobar appears after login into google account.
    231 
    232     One-click infobar should appear after signing into a google account
    233     for the first time using a clean profile.
    234     """
    235     self._DisplayOneClickInfobar()
    236 
    237   def testNoOneClickInfobarAfterCancel(self):
    238     """Verify one-click infobar does not appear again after clicking cancel.
    239 
    240     The one-click infobar should not display again after logging into an
    241     account and selecting to reject sync the first time. The test covers
    242     restarting the browser with the same profile and verifying the one-click
    243     infobar does not show after login.
    244 
    245     This test also verifies that the password infobar displays.
    246     """
    247     self._DisplayOneClickInfobar()
    248     self._PerformActionOnInfobar(action='cancel')  # Click 'No thanks' button.
    249     self.NavigateToURL(self.URL_LOGOUT)
    250     self._LogIntoGoogleAccount()
    251     test_utils.WaitForInfobarTypeAndGetIndex(self, self.PW_INFOBAR_TYPE)
    252     test_utils.AssertInfobarTypeDoesNotAppear(self, self.OC_INFOBAR_TYPE)
    253     # Restart browser with the same profile.
    254     self.RestartBrowser(clear_profile=False)
    255     self.NavigateToURL(self.URL_LOGOUT)
    256     self._LogIntoGoogleAccount()
    257     test_utils.AssertInfobarTypeDoesNotAppear(self, self.OC_INFOBAR_TYPE)
    258 
    259   def testDisplayOneClickInfobarAfterDismiss(self):
    260     """Verify one-click infobar appears again after clicking dismiss button.
    261 
    262     The one-click infobar should display again after logging into an
    263     account and clicking to dismiss the infobar the first time.
    264 
    265     This test also verifies that the password infobar does not display.
    266     The one-click infobar should supersede the password infobar.
    267     """
    268     self._DisplayOneClickInfobar()
    269     self._PerformActionOnInfobar(action='dismiss')  # Click 'x' button.
    270     self.NavigateToURL(self.URL_LOGOUT)
    271     self._LogIntoGoogleAccount()
    272     test_utils.WaitForInfobarTypeAndGetIndex(self, self.OC_INFOBAR_TYPE)
    273     test_utils.AssertInfobarTypeDoesNotAppear(self, self.PW_INFOBAR_TYPE)
    274 
    275   def _OpenSecondProfile(self):
    276     """Create a second profile."""
    277     self.OpenNewBrowserWindowWithNewProfile()
    278     self.assertEqual(2, len(self.GetMultiProfileInfo()['profiles']),
    279         msg='The second profile was not created.')
    280 
    281   def testDisplayOneClickInfobarPerProfile(self):
    282     """Verify one-click infobar appears for each profile after sign-in."""
    283     # Default profile.
    284     self._DisplayOneClickInfobar()
    285     self._OpenSecondProfile()
    286     self._DisplayOneClickInfobar(windex=1)
    287 
    288   def testNoOneClickInfobarWhenCookiesBlocked(self):
    289     """Verify one-click infobar does not show when cookies are blocked.
    290 
    291     One-click sign in should not be enabled if cookies are blocked for Google
    292     accounts domain.
    293 
    294     This test verifies the following bug: crbug.com/117841
    295     """
    296     # Block cookies for Google accounts domain.
    297     self.SetPrefs(pyauto.kContentSettingsPatternPairs,
    298                   self.BLOCK_COOKIE_PATTERN)
    299     self._LogIntoGoogleAccount()
    300     test_utils.AssertInfobarTypeDoesNotAppear(self, self.OC_INFOBAR_TYPE)
    301 
    302   def testOneClickInfobarShownWhenWinLoseFocus(self):
    303     """Verify one-click infobar still shows when window loses focus.
    304 
    305     This test verifies the following bug: crbug.com/121739
    306     """
    307     self._LogIntoGoogleAccount()
    308     test_utils.WaitForInfobarTypeAndGetIndex(self, self.OC_INFOBAR_TYPE)
    309     # Open new window to shift focus away.
    310     self.OpenNewBrowserWindow(True)
    311     test_utils.GetInfobarIndexByType(self, self.OC_INFOBAR_TYPE)
    312 
    313   def testNoOneClickInfobarInIncognito(self):
    314     """Verify that one-click infobar does not show up in incognito mode."""
    315     self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW)
    316     self._LogIntoGoogleAccount(windex=1)
    317     test_utils.AssertInfobarTypeDoesNotAppear(
    318         self, self.OC_INFOBAR_TYPE, windex=1)
    319 
    320 
    321 if __name__ == '__main__':
    322   pyauto_functional.Main()
    323