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.
8 from telemetry.core import browser_finder
9 from telemetry.core import extension_to_load
10 from telemetry.core import util
11 from telemetry.core.platform import cros_interface
12 from telemetry.unittest import options_for_unittests
15 class CrOSTestCase(unittest.TestCase):
17 options = options_for_unittests.GetCopy()
18 self._cri = cros_interface.CrOSInterface(options.cros_remote,
19 options.cros_ssh_identity)
20 self._is_guest = options.browser_type == 'cros-chrome-guest'
21 self._username = options.browser_options.username
22 self._password = options.browser_options.password
23 self._load_extension = None
25 def _CreateBrowser(self, autotest_ext=False, auto_login=True,
26 gaia_login=False, username=None, password=None):
27 """Finds and creates a browser for tests. if autotest_ext is True,
28 also loads the autotest extension"""
29 options = options_for_unittests.GetCopy()
32 extension_path = os.path.join(util.GetUnittestDataDir(), 'autotest_ext')
33 assert os.path.isdir(extension_path)
34 self._load_extension = extension_to_load.ExtensionToLoad(
36 browser_type=options.browser_type,
38 options.extensions_to_load = [self._load_extension]
40 browser_to_create = browser_finder.FindBrowser(options)
41 self.assertTrue(browser_to_create)
42 browser_options = browser_to_create.finder_options.browser_options
43 browser_options.create_browser_with_oobe = True
44 browser_options.auto_login = auto_login
45 browser_options.gaia_login = gaia_login
46 if username is not None:
47 browser_options.username = username
48 if password is not None:
49 browser_options.password = password
51 return browser_to_create.Create(options)
53 def _GetAutotestExtension(self, browser):
54 """Returns the autotest extension instance"""
55 extension = browser.extensions[self._load_extension]
56 self.assertTrue(extension)
59 def _IsCryptohomeMounted(self):
60 """Returns True if cryptohome is mounted. as determined by the cmd
61 cryptohome --action=is_mounted"""
62 return self._cri.RunCmdOnDevice(
63 ['/usr/sbin/cryptohome', '--action=is_mounted'])[0].strip() == 'true'
65 def _GetLoginStatus(self, browser):
66 extension = self._GetAutotestExtension(browser)
67 self.assertTrue(extension.EvaluateJavaScript(
68 "typeof('chrome.autotestPrivate') != 'undefined'"))
69 extension.ExecuteJavaScript('''
70 window.__login_status = null;
71 chrome.autotestPrivate.loginStatus(function(s) {
72 window.__login_status = s;
76 lambda: extension.EvaluateJavaScript('window.__login_status'), 10)
78 def _Credentials(self, credentials_path):
79 """Returns credentials from file."""
80 credentials_path = os.path.join(os.path.dirname(__file__),
82 if os.path.isfile(credentials_path):
83 with open(credentials_path) as f:
84 username, password = f.read().rstrip().split(':')
85 return (username, password)