blob: 980a0633a7277dfb368587a243436f8da532c848 [file] [log] [blame]
David Haddockdd35fb5e2018-05-03 14:52:33 -07001# 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
5import json
6import logging
7import time
8
9from autotest_lib.client.bin import test
10from autotest_lib.client.bin import utils
David Haddock7f262882018-11-08 11:53:52 -080011from autotest_lib.client.common_lib import error
David Haddockdd35fb5e2018-05-03 14:52:33 -070012from autotest_lib.client.common_lib.cros import chrome
sdantuluri49749772019-02-01 16:52:49 -080013from autotest_lib.client.cros import cryptohome
David Haddockdd35fb5e2018-05-03 14:52:33 -070014from autotest_lib.client.cros.input_playback import input_playback
15
16
17class 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():
David Haddock7f262882018-11-08 11:53:52 -080026 """Check rlz data exists."""
David Haddockdd35fb5e2018-05-03 14:52:33 -070027 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 """
David Haddock7f262882018-11-08 11:53:52 -080044 timeout_minutes = 2
45 timeout = time.time() + 60 * timeout_minutes
46
David Haddockdd35fb5e2018-05-03 14:52:33 -070047 # Setup a keyboard emulator to open new tabs and type a search.
Daniel Erata87122c2018-05-19 07:43:59 -070048 with input_playback.InputPlayback() as player:
49 player.emulate(input_type='keyboard')
50 player.find_connected_inputs()
David Haddockdd35fb5e2018-05-03 14:52:33 -070051
Daniel Erata87122c2018-05-19 07:43:59 -070052 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:
David Haddock7f262882018-11-08 11:53:52 -080062 if time.time() > timeout:
63 raise error.TestFail('RLZ ping did not send in %d '
David Haddocke2eb20f2018-12-11 08:28:39 +000064 'minutes.' % timeout_minutes)
Daniel Erata87122c2018-05-19 07:43:59 -070065 time.sleep(10)
David Haddockdd35fb5e2018-05-03 14:52:33 -070066
67
sdantuluri49749772019-02-01 16:52:49 -080068 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
sdantuluri3029e9d2019-02-20 14:47:38 -080074 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')
sdantuluri49749772019-02-01 16:52:49 -080081
82
Kyle Shimabukurob3014bc2020-01-24 13:26:40 -080083 def run_once(self, ping_timeout=30, logged_in=True):
David Haddock7f262882018-11-08 11:53:52 -080084 """
85 Main entry to the test.
86
Kyle Shimabukurob3014bc2020-01-24 13:26:40 -080087 @param logged_in: True for real login or False for guest mode.
88 @param ping_timeout: Delay time (seconds) before rlz ping is sent.
David Haddock7f262882018-11-08 11:53:52 -080089
90 """
Kyle Shimabukurob3014bc2020-01-24 13:26:40 -080091 # Browser arg to make DUT send rlz ping after a short delay.
92 rlz_flag = '--rlz-ping-delay=%d' % ping_timeout
93
David Haddock7f262882018-11-08 11:53:52 -080094 # If we are testing the ping is sent in guest mode (logged_in=False),
95 # we need to first do a real login and wait for the DUT to become
96 # 'locked' for rlz. Then logout and enter guest mode.
97 if not logged_in:
Kyle Shimabukurob3014bc2020-01-24 13:26:40 -080098 with chrome.Chrome(logged_in=True, extra_browser_args=rlz_flag):
sdantuluri49749772019-02-01 16:52:49 -080099 self._wait_for_rlz_lock()
David Haddock7f262882018-11-08 11:53:52 -0800100
Kyle Shimabukurob3014bc2020-01-24 13:26:40 -0800101 with chrome.Chrome(logged_in=logged_in,
102 extra_browser_args=rlz_flag) as cr:
David Haddockdd35fb5e2018-05-03 14:52:33 -0700103 self._check_url_for_rlz(cr)
104 self._verify_rlz_data()