cros_test_case.py revision 010d83a9304c5a91596085d917d248abff47903a
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.backends.chrome 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 options.browser_options.create_browser_with_oobe = True 43 options.browser_options.auto_login = auto_login 44 options.browser_options.gaia_login = gaia_login 45 if username is not None: 46 options.browser_options.username = username 47 if password is not None: 48 options.browser_options.password = password 49 50 return browser_to_create.Create() 51 52 def _GetAutotestExtension(self, browser): 53 """Returns the autotest extension instance""" 54 extension = browser.extensions[self._load_extension] 55 self.assertTrue(extension) 56 return extension 57 58 def _IsCryptohomeMounted(self): 59 """Returns True if cryptohome is mounted. as determined by the cmd 60 cryptohome --action=is_mounted""" 61 return self._cri.RunCmdOnDevice( 62 ['/usr/sbin/cryptohome', '--action=is_mounted'])[0].strip() == 'true' 63 64 def _GetLoginStatus(self, browser): 65 extension = self._GetAutotestExtension(browser) 66 self.assertTrue(extension.EvaluateJavaScript( 67 "typeof('chrome.autotestPrivate') != 'undefined'")) 68 extension.ExecuteJavaScript(''' 69 window.__login_status = null; 70 chrome.autotestPrivate.loginStatus(function(s) { 71 window.__login_status = s; 72 }); 73 ''') 74 return util.WaitFor( 75 lambda: extension.EvaluateJavaScript('window.__login_status'), 10) 76 77 def _Credentials(self, credentials_path): 78 """Returns credentials from file.""" 79 credentials_path = os.path.join(os.path.dirname(__file__), 80 credentials_path) 81 if os.path.isfile(credentials_path): 82 with open(credentials_path) as f: 83 username, password = f.read().rstrip().split(':') 84 return (username, password) 85 return (None, None) 86