1# Copyright 2017 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 gobject 6import logging 7import os 8import time 9 10from autotest_lib.client.bin import test 11from autotest_lib.client.common_lib import error 12from autotest_lib.client.common_lib.cros import chrome, session_manager 13from autotest_lib.client.cros import cryptohome 14from autotest_lib.client.cros.graphics import graphics_utils 15 16from dbus.mainloop.glib import DBusGMainLoop 17 18 19class desktopui_ChromeSanity(test.test): 20 """Performs basic integration testing for Chrome. 21 22 This test performs very basic tests to verify that Chrome is somewhat 23 usable in conjunction with the rest of the system. 24 """ 25 version = 1 26 27 _CHECK_CHROME_TIMEOUT_SEC = 30 28 _SESSION_START_TIMEOUT_SEC = 20 29 30 _TEST_FILENAME = 'test.html' 31 _TEST_CONTENT = 'Page loaded successfully.' 32 33 _SCREENSHOT_DIR = '/usr/local/autotest/results/default/' \ 34 'desktopui_ChromeSanity/results/' 35 36 37 def initialize(self): 38 super(desktopui_ChromeSanity, self).initialize() 39 40 41 def run_once(self): 42 """ 43 Runs the test. 44 """ 45 dbus_loop = DBusGMainLoop(set_as_default=True) 46 listener = session_manager.SessionSignalListener(gobject.MainLoop()) 47 listener.listen_for_session_state_change('started') 48 49 logging.info('Logging in...') 50 with chrome.Chrome(init_network_controller=True) as cr: 51 # Check that Chrome asks session_manager to start a session. 52 listener.wait_for_signals( 53 desc=('SessionStateChanged "started" D-Bus signal from ' 54 'session_manager'), 55 timeout=self._SESSION_START_TIMEOUT_SEC) 56 logging.info('Successfully logged in as "%s"', cr.username) 57 58 # Check that the user's encrypted home directory was mounted. 59 if not cryptohome.is_vault_mounted(user=cr.username, 60 allow_fail=False): 61 raise error.TestFail( 62 'Didn\'t find mounted cryptohome for "%s"' % 63 cr.username) 64 65 # Check that Chrome is able to load a web page. 66 cr.browser.platform.SetHTTPServerDirectories(self.bindir) 67 url = cr.browser.platform.http_server.UrlOf( 68 os.path.join(self.bindir, self._TEST_FILENAME)) 69 logging.info('Loading %s...', url) 70 71 try: 72 tab = cr.browser.tabs.New() 73 tab.Navigate(url) 74 tab.WaitForDocumentReadyStateToBeComplete() 75 content = tab.EvaluateJavaScript( 76 'document.documentElement.innerText') 77 if content != self._TEST_CONTENT: 78 raise error.TestFail( 79 'Expected page content "%s" but got "%s"' % 80 (self._TEST_CONTENT, content)) 81 logging.info('Saw expected content') 82 except Exception as e: 83 prefix = 'screenshot-%s' % time.strftime('%Y%m%d-%H%M%S') 84 logging.info( 85 'Got exception; saving screenshot to %s/%s', 86 self._SCREENSHOT_DIR, prefix) 87 if not os.path.exists(self._SCREENSHOT_DIR): 88 os.makedirs(self._SCREENSHOT_DIR) 89 graphics_utils.take_screenshot(self._SCREENSHOT_DIR, prefix) 90 91 if isinstance(e, error.TestFail): 92 raise e 93 else: 94 raise error.TestFail(str(e)) 95