1 # Copyright (c) 2012 The Chromium 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.
7 This module contains a set of common utilities for querying
8 and manipulating the Google Talk Chrome Extension.
20 class GTalkBaseTest(pyauto.PyUITest):
21 """Base test class for testing GTalk."""
25 def Prompt(self, text):
26 """Pause execution with debug output.
29 text: The debug output.
32 raw_input('--------------------> ' + text)
34 def InstallGTalkExtension(self, gtalk_version):
35 """Download and install the GTalk extension."""
36 extension_path = os.path.abspath(
37 os.path.join(self.DataDir(), 'extensions', 'gtalk',
38 gtalk_version + '.crx'))
40 os.path.exists(extension_path),
41 msg='Failed to find GTalk extension: ' + extension_path)
43 extension = self.GetGTalkExtensionInfo()
45 logging.info('Extension already installed. Skipping install...\n')
48 self.InstallExtension(extension_path, False)
49 extension = self.GetGTalkExtensionInfo()
50 self.assertTrue(extension, msg='Failed to install GTalk extension.')
51 self.assertTrue(extension['is_enabled'], msg='GTalk extension is disabled.')
53 def UninstallGTalkExtension(self):
54 """Uninstall the GTalk extension (if present)"""
55 extension = self.GetGTalkExtensionInfo()
57 self.UninstallExtensionById(extension['id'])
59 def GetGTalkExtensionInfo(self):
60 """Get the data object about the GTalk extension."""
61 extensions = [x for x in self.GetExtensionsInfo()
62 if x['name'] == 'Chat for Google']
63 return extensions[0] if len(extensions) == 1 else None
65 def RunInMole(self, js, mole_index=0):
66 """Execute javascript in a chat mole.
69 js: The javascript to run.
70 mole_index: The index of the mole in which to run the JS.
73 The resulting value from executing the javascript.
75 return self._RunInRenderView(self.GetMoleInfo(mole_index), js,
78 def RunInAllMoles(self, js):
79 """Execute javascript in all chat moles.
82 js: The javascript to run.
84 moles = self.GetMolesInfo()
86 self._RunInRenderView(mole, js, '//iframe[1]')
88 def RunInRoster(self, js):
89 """Execute javascript in the chat roster.
92 js: The javascript to run.
95 The resulting value from executing the javascript.
97 return self._RunInRenderView(self.GetViewerInfo(), js,
98 '//iframe[1]\n//iframe[1]')
100 def RunInLoginPage(self, js, xpath=''):
101 """Execute javascript in the gaia login popup.
104 js: The javascript to run.
105 xpath: The xpath to the frame in which to execute the javascript.
108 The resulting value from executing the javascript.
110 return self._RunInTab(self.GetLoginPageInfo(), js, xpath)
112 def RunInViewer(self, js, xpath=''):
113 """Execute javascript in the GTalk viewer window.
116 js: The javascript to run.
117 xpath: The xpath to the frame in which to execute the javascript.
120 The resulting value from executing the javascript.
122 return self._RunInRenderView(self.GetViewerInfo(), js, xpath)
124 def RunInBackground(self, js, xpath=''):
125 """Execute javascript in the GTalk viewer window.
128 js: The javascript to run.
129 xpath: The xpath to the frame in which to execute the javascript.
132 The resulting value from executing the javascript.
134 background_view = self.GetBackgroundInfo()
135 return self._RunInRenderView(background_view['view'], js, xpath)
137 def GetMoleInfo(self, mole_index=0):
138 """Get the data object about a given chat mole.
141 mole_index: The index of the mole to retrieve.
144 Data object describing mole.
146 extension = self.GetGTalkExtensionInfo()
147 return self._GetExtensionViewInfo(
148 'chrome-extension://%s/panel.html' % extension['id'],
151 def GetMolesInfo(self):
152 """Get the data objects for all of the chat moles.
155 Set of data objects describing moles.
157 extension = self.GetGTalkExtensionInfo()
158 return self._GetMatchingExtensionViews(
159 'chrome-extension://%s/panel.html' % extension['id'])
161 def GetViewerInfo(self):
162 """Get the data object about the GTalk viewer dialog."""
163 extension = self.GetGTalkExtensionInfo()
164 return self._GetExtensionViewInfo(
165 'chrome-extension://%s/viewer.html' % extension['id'])
167 def GetLoginPageInfo(self):
168 """Get the data object about the gaia login popup."""
169 return self._GetTabInfo('https://accounts.google.com/ServiceLogin?')
171 def GetBackgroundInfo(self):
172 """Get the data object about the GTalk background page."""
173 extension_views = self.GetBrowserInfo()['extension_views']
174 for extension_view in extension_views:
175 if 'Google Talk' in extension_view['name'] and \
176 'EXTENSION_BACKGROUND_PAGE' == extension_view['view_type']:
177 return extension_view
180 def WaitUntilResult(self, result, func, msg):
181 """Loop func until a condition matches is satified.
184 result: Value of func() at which to stop.
185 func: Function to run at each iteration.
186 msg: Error to print upon timing out.
188 assert callable(func)
189 self.assertTrue(self.WaitUntil(
190 lambda: func(), expect_retval=result), msg=msg)
192 def WaitUntilCondition(self, func, matches, msg):
193 """Loop func until condition matches is satified.
196 func: Function to run at each iteration.
197 matches: Funtion to evalute output and determine whether to stop.
198 msg: Error to print upon timing out.
200 assert callable(func)
201 assert callable(matches)
202 self.assertTrue(self.WaitUntil(
203 lambda: matches(func())), msg=msg)
205 def _WrapJs(self, statement):
206 """Wrap the javascript to be executed.
209 statement: The piece of javascript to wrap.
212 The wrapped javascript.
215 window.domAutomationController.send(
219 catch(e){return "JS_ERROR: " + e}})())
220 """ % (self._GetInjectedJs(), statement)
222 def _RunInTab(self, tab, js, xpath=''):
223 """Execute javascript in a given tab.
226 tab: The data object for the Chrome window tab returned by
228 js: The javascript to run.
229 xpath: The xpath to the frame in which to execute the javascript.
232 The resulting value from executing the javascript.
235 logging.debug('Tab not found: %s' % tab)
237 logging.info('Run in tab: %s' % js)
239 value = self.ExecuteJavascript(
241 tab_index = tab['index'],
242 windex = tab['windex'],
244 self._LogRun(js, value)
247 def _RunInRenderView(self, view, js, xpath=''):
248 """Execute javascript in a given render view.
251 view: The data object for the Chrome render view returned by
252 _GetExtensionViewInfo.
253 js: The javascript to run.
254 xpath: The xpath to the frame in which to execute the javascript.
257 The resulting value from executing the javascript.
260 logging.debug('View not found: %s' % view)
262 logging.info('Run in view: %s' % js)
264 value = self.ExecuteJavascriptInRenderView(
268 self._LogRun(js, value)
271 def _LogRun(self, js, value):
272 """Log a particular run.
275 js: The javascript statement executed.
276 value: The return value for the execution.
278 # works around UnicodeEncodeError: 'ascii' codec can't encode...
280 if not isinstance(value, basestring):
282 out = re.sub('\s', ';', out[:300])
283 logging.info(js + ' ===> ' + out.encode('utf-8'))
285 def _GetTabInfo(self, url_query, index=0):
286 """Get the data object for a given tab.
289 url_query: The substring of the URL to search for.
290 index: The index within the list of matches to return.
293 The data object for the tab.
295 windows = self.GetBrowserInfo()['windows']
298 for tab in win['tabs']:
299 if tab['url'] and url_query in tab['url']:
300 # Store reference to windex used in _RunInTab.
301 tab['windex'] = win['index']
307 def _GetExtensionViewInfo(self, url_query, index=0):
308 """Get the data object for a given extension view.
311 url_query: The substring of the URL to search for.
312 index: The index within the list of matches to return.
315 The data object for the tab.
318 candidate_views = self._GetMatchingExtensionViews(url_query)
319 if len(candidate_views) > index:
320 return candidate_views[index]
323 def _GetMatchingExtensionViews(self, url_query):
324 """Gets the data objects for the extension views matching the url_query.
327 url_query: The substring of the URL to search for.
330 An array of matching data objects.
332 extension_views = self.GetBrowserInfo()['extension_views']
333 candidate_views = list()
334 for extension_view in extension_views:
335 if extension_view['url'] and url_query in extension_view['url']:
336 candidate_views.append(extension_view['view'])
338 # No guarantee on view order, so sort the views to get the correct one for
340 candidate_views.sort()
341 return candidate_views
343 def _GetInjectedJs(self):
344 """Get the javascript to inject in the execution environment."""
345 if self._injected_js is None:
346 self._injected_js = open(
347 os.path.join(os.path.dirname(__file__), 'jsutils.js')).read()
348 return self._injected_js