1# Copyright 2014 The Chromium 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 os
6import unittest
7
8from telemetry.core import cros_interface
9from telemetry.core import util
10from telemetry.internal.browser import browser_finder
11from telemetry.internal.browser import extension_to_load
12from telemetry.testing import options_for_unittests
13
14
15class CrOSTestCase(unittest.TestCase):
16  def setUp(self):
17    options = options_for_unittests.GetCopy()
18    self._cri = cros_interface.CrOSInterface(options.cros_remote,
19                                             options.cros_remote_ssh_port,
20                                             options.cros_ssh_identity)
21    self._is_guest = options.browser_type == 'cros-chrome-guest'
22    self._username = options.browser_options.username
23    self._password = options.browser_options.password
24    self._gaia_id = options.browser_options.gaia_id
25    self._load_extension = None
26
27  def _CreateBrowser(self, autotest_ext=False, auto_login=True,
28                     gaia_login=False, username=None, password=None,
29                     gaia_id=None, dont_override_profile=False):
30    """Finds and creates a browser for tests. if autotest_ext is True,
31    also loads the autotest extension"""
32    options = options_for_unittests.GetCopy()
33
34    if autotest_ext:
35      extension_path = os.path.join(util.GetUnittestDataDir(), 'autotest_ext')
36      assert os.path.isdir(extension_path)
37      self._load_extension = extension_to_load.ExtensionToLoad(
38          path=extension_path,
39          browser_type=options.browser_type,
40          is_component=True)
41      options.extensions_to_load = [self._load_extension]
42
43    browser_to_create = browser_finder.FindBrowser(options)
44    self.assertTrue(browser_to_create)
45    browser_options = options.browser_options
46    browser_options.create_browser_with_oobe = True
47    browser_options.auto_login = auto_login
48    browser_options.gaia_login = gaia_login
49    browser_options.dont_override_profile = dont_override_profile
50    if username is not None:
51      browser_options.username = username
52    if password is not None:
53      browser_options.password = password
54    if gaia_id is not None:
55      browser_options.gaia_id = gaia_id
56
57    return browser_to_create.Create(options)
58
59  def _GetAutotestExtension(self, browser):
60    """Returns the autotest extension instance"""
61    extension = browser.extensions[self._load_extension]
62    self.assertTrue(extension)
63    return extension
64
65  def _IsCryptohomeMounted(self):
66    """Returns True if cryptohome is mounted. as determined by the cmd
67    cryptohome --action=is_mounted"""
68    return self._cri.RunCmdOnDevice(
69        ['/usr/sbin/cryptohome', '--action=is_mounted'])[0].strip() == 'true'
70
71  def _GetLoginStatus(self, browser):
72    extension = self._GetAutotestExtension(browser)
73    self.assertTrue(extension.EvaluateJavaScript(
74        "typeof('chrome.autotestPrivate') != 'undefined'"))
75    extension.ExecuteJavaScript('''
76        window.__login_status = null;
77        chrome.autotestPrivate.loginStatus(function(s) {
78          window.__login_status = s;
79        });
80    ''')
81    return util.WaitFor(
82        lambda: extension.EvaluateJavaScript('window.__login_status'), 10)
83