1# Copyright (c) 2013 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 tempfile
6from autotest_lib.client.bin import test
7from autotest_lib.client.cros import cryptohome
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib import file_utils
10from autotest_lib.client.common_lib.cros import chrome
11
12
13class login_GaiaLogin(test.test):
14    """Sign into production gaia using Telemetry."""
15    version = 1
16
17
18    _USERNAME = 'powerloadtest@gmail.com'
19    # TODO(achuith): Get rid of this when crbug.com/358427 is fixed.
20    _USERNAME_DISPLAY = 'power.loadtest@gmail.com'
21    _PLTP_URL = 'https://sites.google.com/a/chromium.org/dev/chromium-os' \
22                '/testing/power-testing/pltp/pltp'
23
24    def run_once(self):
25        with tempfile.NamedTemporaryFile() as pltp:
26            file_utils.download_file(self._PLTP_URL, pltp.name)
27            self._password = pltp.read().rstrip()
28
29        with chrome.Chrome(gaia_login=True, username=self._USERNAME,
30                                            password=self._password) as cr:
31            if not cryptohome.is_vault_mounted(user=self._USERNAME):
32                raise error.TestFail('Expected to find a mounted vault for %s'
33                                     % self._USERNAME)
34            tab = cr.browser.tabs.New()
35            # TODO(achuith): Use a better signal of being logged in, instead of
36            # parsing accounts.google.com.
37            tab.Navigate('http://accounts.google.com')
38            tab.WaitForDocumentReadyStateToBeComplete()
39            res = tab.EvaluateJavaScript('''
40                    var res = '',
41                        divs = document.getElementsByTagName('div');
42                    for (var i = 0; i < divs.length; i++) {
43                        res = divs[i].textContent;
44                        if (res.search('%s') > 1) {
45                            break;
46                        }
47                    }
48                    res;
49            ''' % self._USERNAME_DISPLAY)
50            if not res:
51                raise error.TestFail('No references to %s on accounts page.'
52                                     % self._USERNAME_DISPLAY)
53            tab.Close()
54