blob: 427ca5f10481b5ff272a404d1ca91ef16f52c48f [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 Bhandarkara2df47b2013-10-09 21:23:15 -07005import os
6
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -07007from telemetry.core import browser_finder, browser_options, exceptions
8from telemetry.core import extension_to_load, util
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -07009
10
Achuith Bhandarkared498932013-07-16 17:01:40 -070011# Name of the logged-in user specified by the telemetry login extension.
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -070012LOGIN_USER = 'test@test.test'
13
14
Achuith Bhandarkared498932013-07-16 17:01:40 -070015class Chrome(object):
16 """Wrapper for creating a telemetry browser instance with extensions."""
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070017
18
Achuith Bhandarkared498932013-07-16 17:01:40 -070019 BROWSER_TYPE_LOGIN = 'system'
20 BROWSER_TYPE_GUEST = 'system-guest'
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070021
22
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070023 def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False):
24 self._autotest_ext_path = None
25 if autotest_ext:
26 self._autotest_ext_path = os.path.join(os.path.dirname(__file__),
27 'autotest_private_ext')
28 extension_paths.append(self._autotest_ext_path)
29
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070030 finder_options = browser_options.BrowserFinderOptions()
Achuith Bhandarkared498932013-07-16 17:01:40 -070031 self._browser_type = (self.BROWSER_TYPE_LOGIN
32 if logged_in else self.BROWSER_TYPE_GUEST)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070033 finder_options.browser_type = self.browser_type
34
Achuith Bhandarkared498932013-07-16 17:01:40 -070035 if logged_in:
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070036 extensions_to_load = finder_options.extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070037 for path in extension_paths:
38 extension = extension_to_load.ExtensionToLoad(
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070039 path, self.browser_type, is_component=True)
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070040 extensions_to_load.append(extension)
41 self._extensions_to_load = extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070042
Achuith Bhandarkardafc9a52013-09-24 15:26:33 +020043 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070044 b_options = finder_options.browser_options
45 b_options.disable_component_extensions_with_background_pages = False
46 b_options.create_browser_with_oobe = True
47
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070048 browser_to_create = browser_finder.FindBrowser(finder_options)
Achuith Bhandarkared498932013-07-16 17:01:40 -070049 self._browser = browser_to_create.Create()
Achuith Bhandarkarc8496852013-08-02 13:57:02 -070050 self._browser.Start()
Achuith Bhandarkared498932013-07-16 17:01:40 -070051
52
53 def __enter__(self):
54 return self
55
56
57 def __exit__(self, *args):
58 self.browser.Close()
59
60
61 @property
62 def browser(self):
63 """Returns a telemetry browser instance."""
64 return self._browser
65
66
67 def get_extension(self, extension_path):
68 """Fetches a telemetry extension instance given the extension path."""
69 for ext in self._extensions_to_load:
70 if extension_path == ext.path:
71 return self.browser.extensions[ext]
72 return None
73
74
75 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070076 def autotest_ext(self):
77 """Returns the autotest extension."""
78 return self.get_extension(self._autotest_ext_path)
79
80
81 @property
82 def login_status(self):
83 """Returns login status."""
84 ext = self.autotest_ext
85 if not ext:
86 return None
87
88 ext.ExecuteJavaScript('''
89 window.__login_status = null;
90 chrome.autotestPrivate.loginStatus(function(s) {
91 window.__login_status = s;
92 });
93 ''')
94 return ext.EvaluateJavaScript('window.__login_status')
95
96
97 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -070098 def browser_type(self):
99 """Returns the browser_type."""
100 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700101
102
103 def wait_for_browser_to_come_up(self):
Achuith Bhandarkara5bb3f62013-10-10 10:39:57 -0700104 """Waits for the browser to come up. This should only be called after a
105 browser crash.
106 """
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700107 def _BrowserReady(cr):
108 try:
109 tab = cr.browser.tabs.New()
110 except (exceptions.BrowserGoneException,
111 exceptions.BrowserConnectionGoneException):
112 return False
113 tab.Close()
114 return True
115 util.WaitFor(lambda: _BrowserReady(self), poll_interval=1, timeout=10)
116
117
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700118 def did_browser_crash(self, func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700119 """Runs func, returns True if the browser crashed, False otherwise.
120
121 @param func: function to run.
122
123 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700124 try:
125 func()
126 except (exceptions.BrowserGoneException,
127 exceptions.BrowserConnectionGoneException):
128 return True
129 return False
130