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