blob: 5e1cf2978153b9a4066833ed39ea92bb3017e4b6 [file] [log] [blame]
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -07001# Copyright (c) 2013 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
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -08005import logging, os
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -07006
Don Garrett11b13972015-09-11 17:37:53 +00007from autotest_lib.client.cros import constants
Achuith Bhandarkar88a2bab2015-07-09 17:36:25 -07008from autotest_lib.client.bin import utils
Prathmesh Prabhuc3961df2015-07-21 10:44:54 -07009from telemetry.core import cros_interface, exceptions, util
Achuith Bhandarkarb058a8f2015-07-02 01:29:05 -070010from telemetry.internal.browser import browser_finder, browser_options
11from telemetry.internal.browser import extension_to_load
Achuith Bhandarkar704134d2015-03-05 17:31:57 -080012
13Error = exceptions.Error
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -070014
15
Victor Hsieh000240b2016-04-11 16:04:31 -070016def is_arc_available():
17 """Returns true if ARC is available on current device."""
18 with open('/etc/lsb-release') as f:
19 for line in f:
20 if line.startswith('CHROMEOS_ARC_VERSION='):
21 return True
22 return False
23
24
Achuith Bhandarkared498932013-07-16 17:01:40 -070025class Chrome(object):
26 """Wrapper for creating a telemetry browser instance with extensions."""
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070027
28
Victor Hsieh000240b2016-04-11 16:04:31 -070029 # Chrome will start ARC instance and the script will block until ARC's boot
30 # completed event.
31 ARC_MODE_ENABLED = "enabled"
32 # Similar to "enabled", except that it will not block.
33 ARC_MODE_ENABLED_ASYNC = "enabled_async"
34 # Chrome will not start ARC instance.
35 ARC_MODE_DISABLED = "disabled"
36 # All available ARC options.
37 ARC_MODES = [ARC_MODE_ENABLED, ARC_MODE_ENABLED_ASYNC, ARC_MODE_DISABLED]
38
Achuith Bhandarkared498932013-07-16 17:01:40 -070039 BROWSER_TYPE_LOGIN = 'system'
40 BROWSER_TYPE_GUEST = 'system-guest'
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070041
42
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080043 def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False,
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070044 is_component=True, num_tries=3, extra_browser_args=None,
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070045 clear_enterprise_policy=True, dont_override_profile=False,
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080046 disable_gaia_services=True, disable_default_apps = True,
47 auto_login=True, gaia_login=False,
Victor Hsieh000240b2016-04-11 16:04:31 -070048 username=None, password=None, gaia_id=None,
49 arc_mode=ARC_MODE_DISABLED):
Dean Liaob12e2ee2013-11-19 16:49:12 +080050 """
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080051 Constructor of telemetry wrapper.
52
53 @param logged_in: Regular user (True) or guest user (False).
54 @param extension_paths: path of unpacked extension to install.
55 @param autotest_ext: Load a component extension with privileges to
56 invoke chrome.autotestPrivate.
57 @param is_component: Whether extensions should be loaded as component
58 extensions.
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070059 @param num_tries: Number of attempts to log in.
Dean Liaob12e2ee2013-11-19 16:49:12 +080060 @param extra_browser_args: Additional argument(s) to pass to the
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070061 browser. It can be a string or a list.
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -070062 @param clear_enterprise_policy: Clear enterprise policy before
63 logging in.
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070064 @param dont_override_profile: Don't delete cryptohome before login.
65 Telemetry will output a warning with this
66 option.
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -070067 @param disable_gaia_services: For enterprise autotests, this option may
68 be used to enable policy fetch.
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080069 @param disable_default_apps: For tests that exercise default apps.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080070 @param auto_login: Does not login automatically if this is False.
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070071 Useful if you need to examine oobe.
72 @param gaia_login: Logs in to real gaia.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080073 @param username: Log in using this username instead of the default.
Alexander Alekseevd9682862015-10-01 22:42:49 -070074 @param password: Log in using this password instead of the default.
75 @param gaia_id: Log in using this gaia_id instead of the default.
Victor Hsieh000240b2016-04-11 16:04:31 -070076 @param arc_mode: How ARC instance should be started.
Dean Liaob12e2ee2013-11-19 16:49:12 +080077 """
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070078 self._autotest_ext_path = None
79 if autotest_ext:
80 self._autotest_ext_path = os.path.join(os.path.dirname(__file__),
81 'autotest_private_ext')
82 extension_paths.append(self._autotest_ext_path)
83
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070084 finder_options = browser_options.BrowserFinderOptions()
Victor Hsieh000240b2016-04-11 16:04:31 -070085 assert arc_mode in self.ARC_MODES
86 if is_arc_available():
87 if arc_mode in [self.ARC_MODE_ENABLED, self.ARC_MODE_ENABLED_ASYNC]:
88 logging.debug('ARC is enabled in mode ' + arc_mode)
89 from autotest_lib.client.common_lib.cros import arc_util
90 extra_browser_args = arc_util.append_extra_args(extra_browser_args)
91 logged_in = True
92 else:
93 assert arc_mode == self.ARC_MODE_DISABLED
Achuith Bhandarkared498932013-07-16 17:01:40 -070094 self._browser_type = (self.BROWSER_TYPE_LOGIN
95 if logged_in else self.BROWSER_TYPE_GUEST)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070096 finder_options.browser_type = self.browser_type
Dean Liaob12e2ee2013-11-19 16:49:12 +080097 if extra_browser_args:
98 finder_options.browser_options.AppendExtraBrowserArgs(
99 extra_browser_args)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -0700100
Achuith Bhandarkar3e5051d2013-10-15 15:20:12 -0700101 # finder options must be set before parse_args(), browser options must
102 # be set before Create().
Todd Brocheb6f4812014-04-29 16:04:28 -0700103 # TODO(crbug.com/360890) Below MUST be '2' so that it doesn't inhibit
104 # autotest debug logs
105 finder_options.verbosity = 2
Achuith Bhandarkardafc9a52013-09-24 15:26:33 +0200106 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700107 b_options = finder_options.browser_options
108 b_options.disable_component_extensions_with_background_pages = False
109 b_options.create_browser_with_oobe = True
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -0700110 b_options.clear_enterprise_policy = clear_enterprise_policy
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -0700111 b_options.dont_override_profile = dont_override_profile
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -0700112 b_options.disable_gaia_services = disable_gaia_services
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -0800113 b_options.disable_default_apps = disable_default_apps
114 b_options.disable_component_extensions_with_background_pages = disable_default_apps
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -0800115
116 b_options.auto_login = auto_login
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -0700117 b_options.gaia_login = gaia_login
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -0800118 self.username = b_options.username if username is None else username
119 self.password = b_options.password if password is None else password
120 b_options.username = self.username
121 b_options.password = self.password
Achuith Bhandarkar2d706d82016-04-15 12:53:17 -0700122 if gaia_id is not None:
123 b_options.gaia_id = gaia_id
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700124
Achuith Bhandarkar531a62a2016-04-13 11:09:43 -0700125 if logged_in:
Achuith Bhandarkar2d706d82016-04-15 12:53:17 -0700126 extensions_to_load = b_options.extensions_to_load
127 for path in extension_paths:
128 extension = extension_to_load.ExtensionToLoad(
129 path, self.browser_type, is_component=is_component)
130 extensions_to_load.append(extension)
131 self._extensions_to_load = extensions_to_load
Achuith Bhandarkar531a62a2016-04-13 11:09:43 -0700132
David James54a830e2015-05-06 17:21:47 -0700133 # Turn on collection of Chrome coredumps via creation of a magic file.
134 # (Without this, Chrome coredumps are trashed.)
Don Garrett11b13972015-09-11 17:37:53 +0000135 open(constants.CHROME_CORE_MAGIC_FILE, 'w').close()
David James54a830e2015-05-06 17:21:47 -0700136
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800137 for i in range(num_tries):
138 try:
139 browser_to_create = browser_finder.FindBrowser(finder_options)
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -0800140 self._browser = browser_to_create.Create(finder_options)
Victor Hsieh000240b2016-04-11 16:04:31 -0700141 if (is_arc_available() and arc_mode == self.ARC_MODE_ENABLED):
142 from autotest_lib.client.common_lib.cros import arc_util
143 arc_util.post_processing_after_browser()
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800144 break
Achuith Bhandarkarfab82d12015-02-26 11:05:35 -0800145 except (exceptions.LoginException) as e:
Achuith Bhandarkarf800edf2013-12-06 13:37:52 -0800146 logging.error('Timed out logging in, tries=%d, error=%s',
147 i, repr(e))
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800148 if i == num_tries-1:
149 raise
Achuith Bhandarkared498932013-07-16 17:01:40 -0700150
151
152 def __enter__(self):
153 return self
154
155
156 def __exit__(self, *args):
Derek Basehore41acbf82014-07-11 18:34:30 -0700157 self.close()
Achuith Bhandarkared498932013-07-16 17:01:40 -0700158
159
160 @property
161 def browser(self):
162 """Returns a telemetry browser instance."""
163 return self._browser
164
165
166 def get_extension(self, extension_path):
167 """Fetches a telemetry extension instance given the extension path."""
168 for ext in self._extensions_to_load:
169 if extension_path == ext.path:
170 return self.browser.extensions[ext]
171 return None
172
173
174 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700175 def autotest_ext(self):
176 """Returns the autotest extension."""
177 return self.get_extension(self._autotest_ext_path)
178
179
180 @property
181 def login_status(self):
182 """Returns login status."""
183 ext = self.autotest_ext
184 if not ext:
185 return None
186
187 ext.ExecuteJavaScript('''
188 window.__login_status = null;
189 chrome.autotestPrivate.loginStatus(function(s) {
190 window.__login_status = s;
191 });
192 ''')
193 return ext.EvaluateJavaScript('window.__login_status')
194
195
Victor Hsiehfc3417e2016-03-25 16:49:59 -0700196 def get_visible_notifications(self):
197 """Returns an array of visible notifications of Chrome.
198
199 For specific type of each notification, please refer to Chromium's
200 chrome/common/extensions/api/autotest_private.idl.
201 """
202 ext = self.autotest_ext
203 if not ext:
204 return None
205
206 ext.ExecuteJavaScript('''
207 window.__items = null;
208 chrome.autotestPrivate.getVisibleNotifications(function(items) {
209 window.__items = items;
210 });
211 ''')
212 if ext.EvaluateJavaScript('window.__items') is None:
213 return None
214 return ext.EvaluateJavaScript('window.__items')
215
216
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700217 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -0700218 def browser_type(self):
219 """Returns the browser_type."""
220 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700221
222
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800223 @staticmethod
224 def did_browser_crash(func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700225 """Runs func, returns True if the browser crashed, False otherwise.
226
227 @param func: function to run.
228
229 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700230 try:
231 func()
Achuith Bhandarkar704134d2015-03-05 17:31:57 -0800232 except (Error):
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700233 return True
234 return False
Derek Basehore41acbf82014-07-11 18:34:30 -0700235
236
Achuith Bhandarkar88a2bab2015-07-09 17:36:25 -0700237 @staticmethod
238 def wait_for_browser_restart(func):
239 """Runs func, and waits for a browser restart.
240
241 @param func: function to run.
242
243 """
244 _cri = cros_interface.CrOSInterface()
245 pid = _cri.GetChromePid()
246 Chrome.did_browser_crash(func)
247 utils.poll_for_condition(lambda: pid != _cri.GetChromePid(), timeout=60)
248
249
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800250 def wait_for_browser_to_come_up(self):
251 """Waits for the browser to come up. This should only be called after a
252 browser crash.
253 """
254 def _BrowserReady(cr):
255 tabs = [] # Wrapper for pass by reference.
256 if self.did_browser_crash(
257 lambda: tabs.append(cr.browser.tabs.New())):
258 return False
259 try:
260 tabs[0].Close()
Achuith Bhandarkarfab82d12015-02-26 11:05:35 -0800261 except:
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800262 # crbug.com/350941
263 logging.error('Timed out closing tab')
264 return True
265 util.WaitFor(lambda: _BrowserReady(self), timeout=10)
266
267
Derek Basehore41acbf82014-07-11 18:34:30 -0700268 def close(self):
269 """Closes the browser."""
Hidehiko Abe202ed322016-04-14 15:08:43 +0900270 try:
Victor Hsieh000240b2016-04-11 16:04:31 -0700271 if is_arc_available():
272 from autotest_lib.client.common_lib.cros import arc_util
273 arc_util.pre_processing_before_close()
Hidehiko Abe202ed322016-04-14 15:08:43 +0900274 finally:
275 self._browser.Close()