OLD | NEW |
1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
| 5 from autotest_lib.client.bin import chromeos_constants |
5 from autotest_lib.client.bin import site_login, test as bin_test | 6 from autotest_lib.client.bin import site_login, test as bin_test |
6 from autotest_lib.client.common_lib import error | 7 from autotest_lib.client.common_lib import error, site_ui |
7 | 8 |
8 | 9 |
9 class UITest(bin_test.test): | 10 class UITest(bin_test.test): |
10 """ | 11 """Base class for tests that drive some portion of the user interface. |
11 Tests that require the user to be logged in should subclass this test | 12 |
12 This script by default logs in using the default remote account, however, | 13 By default subclasses will use the default remote credentials before |
13 tests can override this by setting script="your_script" in the control | 14 the run_once method is invoked, and will log out at the completion |
14 file running the test | 15 of the test case even if an exception is thrown. |
| 16 |
| 17 Subclasses can opt out of the automatic login by setting the member |
| 18 variable 'auto_login' to False. |
| 19 |
| 20 Subclasses can log in with arbitrary credentials by passing |
| 21 the 'creds' parameter in their control file. See the documentation of |
| 22 UITest.initialize for more details. |
| 23 |
| 24 If your subclass overrides the initialize() or cleanup() methods, it |
| 25 should make sure to invoke this class' version of those methods as well. |
| 26 The standard super(...) function cannot be used for this, since the base |
| 27 test class is not a 'new style' Python class. |
15 """ | 28 """ |
16 version = 1 | 29 version = 1 |
17 | 30 |
18 | 31 auto_login = True |
19 def setup(self): | 32 username = None |
20 site_login.setup_autox(self) | 33 password = None |
21 | 34 |
22 | 35 |
23 def initialize(self, script='autox_script.json'): | 36 def __is_screensaver(self, status): |
24 # Clean up past state and assume logged out before logging in. | 37 """Returns True if xscreensaver reports a matching status. |
| 38 |
| 39 This function matches the output of `xscreensaver -time` against the |
| 40 specified status. It does no sanity checking or framing of the status |
| 41 value, so use with caution. |
| 42 |
| 43 Args: |
| 44 status: String representing the status to match against. |
| 45 """ |
| 46 return self.xsystem('xscreensaver-command -time | ' + |
| 47 'egrep -q "%s"' % status, ignore_status=True) == 0 |
| 48 |
| 49 |
| 50 def is_screensaver_locked(self): |
| 51 """Returns True if the screensaver is locked, false otherwise. |
| 52 |
| 53 The screensaver has more than two potential states, do not assume |
| 54 that the screensaver is completely deactivated if this returns False, |
| 55 use is_screensaver_unlocked() for that. |
| 56 """ |
| 57 return self.__is_screensaver('locked|no saver status') |
| 58 |
| 59 |
| 60 def is_screensaver_unlocked(self): |
| 61 """Returns True if the screensaver is unlocked, false otherwise. |
| 62 |
| 63 The screensaver has more than two potential states, do not assume |
| 64 that the screensaver is completely locked if this returns False, |
| 65 use is_screensaver_locked() for that. |
| 66 """ |
| 67 return self.__is_screensaver('non-blanked') |
| 68 |
| 69 |
| 70 def xsystem(self, cmd, timeout=None, ignore_status=False): |
| 71 """Convenience wrapper around site_ui.xsystem, to save you an import. |
| 72 """ |
| 73 return site_ui.xsystem(cmd, timeout, ignore_status) |
| 74 |
| 75 |
| 76 def wait_for_screensaver(self, timeout=10): |
| 77 """Convenience wrapper around site_login.wait_for_screensaver, to save |
| 78 you an import. |
| 79 """ |
| 80 site_login.wait_for_screensaver(timeout=timeout) |
| 81 |
| 82 |
| 83 def initialize(self, creds='$default'): |
| 84 """Overridden from test.initialize() to log out and (maybe) log in. |
| 85 |
| 86 If self.auto_login is True, this will automatically log in using the |
| 87 credentials specified by 'creds' at startup, otherwise login will not |
| 88 happen. |
| 89 |
| 90 Regardless of the state of self.auto_login, the self.username and |
| 91 self.password properties will be set to the credentials specified |
| 92 by 'creds'. |
| 93 |
| 94 Args: |
| 95 creds: String specifying the credentials for this test case. Can |
| 96 be a named set of credentials as defined by |
| 97 chromeos_constants.CREDENTIALS, or a 'username:password' pair. |
| 98 Defaults to '$default'. |
| 99 """ |
25 if site_login.logged_in(): | 100 if site_login.logged_in(): |
26 site_login.attempt_logout() | 101 site_login.attempt_logout() |
27 | 102 |
28 # Test account information embedded into json file. | 103 (self.username, self.password) = self.__resolve_creds(creds) |
29 site_login.attempt_login(self, script) | 104 |
30 site_login.wait_for_initial_chrome_window() | 105 if self.auto_login: |
| 106 self.login(self.username, self.password) |
| 107 |
| 108 |
| 109 def __resolve_creds(self, creds): |
| 110 if creds[0] == '$': |
| 111 if creds not in chromeos_constants.CREDENTIALS: |
| 112 raise error.TestFail('Unknown credentials: %s' % creds) |
| 113 |
| 114 return chromeos_constants.CREDENTIALS[creds] |
| 115 |
| 116 return creds.split(':') |
| 117 |
| 118 |
| 119 def login(self, username=None, password=None): |
| 120 """Log in with a set of credentials. |
| 121 |
| 122 Args: |
| 123 username: String representing the username to log in as, defaults |
| 124 to self.username. |
| 125 password: String representing the password to log in with, defaults |
| 126 to self.password. |
| 127 |
| 128 This method is called from UITest.initialize(), so you won't need it |
| 129 unless your testcase has cause to log in multiple times. This |
| 130 DOES NOT affect self.username or self.password. |
| 131 |
| 132 Forces a log out if the test is already logged in. |
| 133 |
| 134 Raises: |
| 135 Exceptions raised by site_login.attempt_login |
| 136 """ |
| 137 |
| 138 if site_login.logged_in(): |
| 139 site_login.attempt_logout(timeout=10) |
| 140 |
| 141 site_login.attempt_login(username or self.username, |
| 142 password or self.password) |
| 143 |
| 144 |
| 145 def logout(self): |
| 146 """Log out. |
| 147 |
| 148 This method is called from UITest.cleanup(), so you won't need it |
| 149 unless your testcase needs to test functionality while logged out. |
| 150 """ |
| 151 site_login.attempt_logout() |
| 152 |
| 153 |
| 154 def get_autox(self): |
| 155 """Return a new autox instance. |
| 156 |
| 157 Explicitly cache this in your testcase if you want to reuse the |
| 158 object, but beware that logging out will invalidate any existing |
| 159 sessions. |
| 160 """ |
| 161 return site_ui.get_autox() |
31 | 162 |
32 def cleanup(self): | 163 def cleanup(self): |
33 """Logs out when object is deleted""" | 164 """Overridden from test.cleanup() to log out when the test is complete. |
34 site_login.attempt_logout() | 165 """ |
| 166 if site_login.logged_in(): |
| 167 self.logout() |
OLD | NEW |