blob: 33737cbaeb79ea3b0c8b82494532fce874865e52 [file] [log] [blame]
Chris Sosad6d09422010-03-25 17:17:46 -07001# 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
3# found in the LICENSE file.
4
rgindaf25f73f2010-04-07 14:55:25 -07005from autotest_lib.client.bin import chromeos_constants
Chris Sosad6d09422010-03-25 17:17:46 -07006from autotest_lib.client.bin import site_login, test as bin_test
rgindaf25f73f2010-04-07 14:55:25 -07007from autotest_lib.client.common_lib import error, site_ui
Ken Mixter2e32fd42010-03-28 19:25:51 -07008
Chris Sosad6d09422010-03-25 17:17:46 -07009
10class UITest(bin_test.test):
rgindaf25f73f2010-04-07 14:55:25 -070011 """Base class for tests that drive some portion of the user interface.
12
13 By default subclasses will use the default remote credentials before
14 the run_once method is invoked, and will log out at the completion
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.
Chris Sosad6d09422010-03-25 17:17:46 -070028 """
29 version = 1
Ken Mixter2e32fd42010-03-28 19:25:51 -070030
rgindaf25f73f2010-04-07 14:55:25 -070031 auto_login = True
32 username = None
33 password = None
Ken Mixter2e32fd42010-03-28 19:25:51 -070034
35
rgindaf25f73f2010-04-07 14:55:25 -070036 def __is_screensaver(self, status):
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 """
Chris Sosad6d09422010-03-25 17:17:46 -0700100 if site_login.logged_in():
Daniel Erat3e3f7f42010-03-29 17:19:14 -0700101 site_login.attempt_logout()
Chris Sosad6d09422010-03-25 17:17:46 -0700102
rgindaf25f73f2010-04-07 14:55:25 -0700103 (self.username, self.password) = self.__resolve_creds(creds)
104
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()
Chris Sosad6d09422010-03-25 17:17:46 -0700162
Chris Sosad6d09422010-03-25 17:17:46 -0700163 def cleanup(self):
rgindaf25f73f2010-04-07 14:55:25 -0700164 """Overridden from test.cleanup() to log out when the test is complete.
165 """
166 if site_login.logged_in():
167 self.logout()