blob: 6280e2d751afd5fdee3218ca8142b8f98835f08a [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 Bhandarkar3e5051d2013-10-15 15:20:12 -070043 # finder options must be set before parse_args(), browser options must
44 # be set before Create().
45 finder_options.verbosity = 1 # info logging for telemetry.
Achuith Bhandarkardafc9a52013-09-24 15:26:33 +020046 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070047 b_options = finder_options.browser_options
48 b_options.disable_component_extensions_with_background_pages = False
49 b_options.create_browser_with_oobe = True
50
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070051 browser_to_create = browser_finder.FindBrowser(finder_options)
Achuith Bhandarkared498932013-07-16 17:01:40 -070052 self._browser = browser_to_create.Create()
Achuith Bhandarkarc8496852013-08-02 13:57:02 -070053 self._browser.Start()
Achuith Bhandarkared498932013-07-16 17:01:40 -070054
55
56 def __enter__(self):
57 return self
58
59
60 def __exit__(self, *args):
61 self.browser.Close()
62
63
64 @property
65 def browser(self):
66 """Returns a telemetry browser instance."""
67 return self._browser
68
69
70 def get_extension(self, extension_path):
71 """Fetches a telemetry extension instance given the extension path."""
72 for ext in self._extensions_to_load:
73 if extension_path == ext.path:
74 return self.browser.extensions[ext]
75 return None
76
77
78 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070079 def autotest_ext(self):
80 """Returns the autotest extension."""
81 return self.get_extension(self._autotest_ext_path)
82
83
84 @property
85 def login_status(self):
86 """Returns login status."""
87 ext = self.autotest_ext
88 if not ext:
89 return None
90
91 ext.ExecuteJavaScript('''
92 window.__login_status = null;
93 chrome.autotestPrivate.loginStatus(function(s) {
94 window.__login_status = s;
95 });
96 ''')
97 return ext.EvaluateJavaScript('window.__login_status')
98
99
100 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -0700101 def browser_type(self):
102 """Returns the browser_type."""
103 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700104
105
106 def wait_for_browser_to_come_up(self):
Achuith Bhandarkara5bb3f62013-10-10 10:39:57 -0700107 """Waits for the browser to come up. This should only be called after a
108 browser crash.
109 """
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700110 def _BrowserReady(cr):
111 try:
112 tab = cr.browser.tabs.New()
113 except (exceptions.BrowserGoneException,
114 exceptions.BrowserConnectionGoneException):
115 return False
116 tab.Close()
117 return True
118 util.WaitFor(lambda: _BrowserReady(self), poll_interval=1, timeout=10)
119
120
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700121 def did_browser_crash(self, func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700122 """Runs func, returns True if the browser crashed, False otherwise.
123
124 @param func: function to run.
125
126 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700127 try:
128 func()
129 except (exceptions.BrowserGoneException,
130 exceptions.BrowserConnectionGoneException):
131 return True
132 return False
133