Home | History | Annotate | Download | only in desktopui_CheckRlzPingSent
      1 # Copyright 2018 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import json
      6 import logging
      7 import time
      8 
      9 from autotest_lib.client.bin import test
     10 from autotest_lib.client.bin import utils
     11 from autotest_lib.client.common_lib import error
     12 from autotest_lib.client.common_lib.cros import chrome
     13 from autotest_lib.client.cros import cryptohome
     14 from autotest_lib.client.cros.input_playback import input_playback
     15 
     16 
     17 class desktopui_CheckRlzPingSent(test.test):
     18     """Tests creating a new user, doing a google search, checking RLZ Data."""
     19     version = 1
     20 
     21     _RLZ_DATA_FILE = "/home/chronos/user/RLZ Data"
     22 
     23     def _verify_rlz_data(self):
     24         """Checks that the CAF event is in the RLZ Data file."""
     25         def rlz_data_exists():
     26             """Check rlz data exists."""
     27             rlz_data = json.loads(utils.run('cat "%s"' %
     28                                             self._RLZ_DATA_FILE).stdout)
     29             logging.debug('rlz data: %s', rlz_data)
     30             if 'stateful_events' in rlz_data:
     31                 return 'CAF' in rlz_data['stateful_events']['C']['_']
     32             return False
     33 
     34         utils.poll_for_condition(rlz_data_exists, timeout=120)
     35 
     36 
     37     def _check_url_for_rlz(self, cr):
     38         """
     39         Does a Google search and ensures there is an rlz parameter.
     40 
     41         @param cr: Chrome instance.
     42 
     43         """
     44         timeout_minutes = 2
     45         timeout = time.time() + 60 * timeout_minutes
     46 
     47         # Setup a keyboard emulator to open new tabs and type a search.
     48         with input_playback.InputPlayback() as player:
     49             player.emulate(input_type='keyboard')
     50             player.find_connected_inputs()
     51 
     52             while True:
     53                 # Open a new tab, search in the omnibox.
     54                 player.blocking_playback_of_default_file(
     55                     input_type='keyboard', filename='keyboard_ctrl+t')
     56                 player.blocking_playback_of_default_file(
     57                     input_type='keyboard', filename='keyboard_b+a+d+enter')
     58                 logging.info(cr.browser.tabs[-1].url)
     59                 if 'rlz=' in cr.browser.tabs[-1].url:
     60                     break
     61                 else:
     62                     if time.time() > timeout:
     63                         raise error.TestFail('RLZ ping did not send in %d '
     64                                              'minutes.' % timeout_minutes)
     65                     time.sleep(10)
     66 
     67 
     68     def _wait_for_rlz_lock(self):
     69         """Waits for the DUT to get into locked state after login."""
     70         def get_install_lockbox_finalized_status():
     71             status = cryptohome.get_tpm_more_status()
     72             return status.get('install_lockbox_finalized')
     73 
     74         try:
     75             utils.poll_for_condition(
     76                 lambda: get_install_lockbox_finalized_status(),
     77                 exception=utils.TimeoutError(),
     78                 timeout=120)
     79         except utils.TimeoutError:
     80             raise error.TestFail('Timed out trying to lock the device')
     81 
     82 
     83     def run_once(self, logged_in=True):
     84         """
     85         Main entry to the test.
     86 
     87         @param logged_in: True for real login or guest mode.
     88 
     89         """
     90         # If we are testing the ping is sent in guest mode (logged_in=False),
     91         # we need to first do a real login and wait for the DUT to become
     92         # 'locked' for rlz. Then logout and enter guest mode.
     93         if not logged_in:
     94             with chrome.Chrome(logged_in=True):
     95                 self._wait_for_rlz_lock()
     96 
     97         with chrome.Chrome(logged_in=logged_in) as cr:
     98             self._check_url_for_rlz(cr)
     99             self._verify_rlz_data()
    100