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 browser_finder
9from telemetry.core import extension_to_load
10from telemetry.core import util
11from telemetry.core.platform import cros_interface
12from telemetry.unittest 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_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
24
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()
30
31    if autotest_ext:
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(
35          path=extension_path,
36          browser_type=options.browser_type,
37          is_component=True)
38      options.extensions_to_load = [self._load_extension]
39
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
50
51    return browser_to_create.Create()
52
53  def _GetAutotestExtension(self, browser):
54    """Returns the autotest extension instance"""
55    extension = browser.extensions[self._load_extension]
56    self.assertTrue(extension)
57    return extension
58
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'
64
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;
73        });
74    ''')
75    return util.WaitFor(
76        lambda: extension.EvaluateJavaScript('window.__login_status'), 10)
77
78  def _Credentials(self, credentials_path):
79    """Returns credentials from file."""
80    credentials_path = os.path.join(os.path.dirname(__file__),
81                                    credentials_path)
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)
86    return (None, None)
87