|
OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
Nirnimesh
2012/02/10 01:36:51
All the files still have execute permissions.
jqian1
2012/02/15 18:41:27
I ran "chmod -x" against those files and run "gcl
| |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """The base test class for GTalk tests. | |
Nirnimesh
2012/02/10 01:36:51
Base of GTalk tests.
jqian1
2012/02/15 18:41:27
Done.
| |
7 | |
8 This module contains a set of common utilities used by unittest for querying | |
Nirnimesh
2012/02/10 01:36:51
remove 'used by unittest'
jqian1
2012/02/15 18:41:27
Done.
| |
9 and manipulating the Google Talk Chrome Extension (http//go/quasar). | |
10 """ | |
11 | |
12 import logging | |
13 import re | |
14 import os | |
15 | |
16 import pyauto_gtalk # must preceed pyauto | |
17 import pyauto | |
18 import pyauto_errors | |
19 | |
20 | |
21 class GTalkBaseTest(pyauto.PyUITest): | |
22 """Base test class for testing GTalk.""" | |
23 | |
24 _injected_js = None | |
25 | |
26 def ExtraChromeFlags(self): | |
27 return pyauto.PyUITest.ExtraChromeFlags(self) + ['--no-sandbox'] | |
Nirnimesh
2012/02/10 01:36:51
why --no-sandbox?
The behavior without the sandbox
jqian1
2012/02/15 18:41:27
I will remove "--no-sandbox" when I check in the c
| |
28 | |
29 def Prompt(self, text): | |
30 """Pause execution with debug output. | |
31 | |
32 Args: | |
33 text: The debug output. | |
34 """ | |
35 text = str(text) | |
36 raw_input('--------------------> ' + text.encode('utf-8')) | |
Nirnimesh
2012/02/10 01:36:51
why encode to utf-8? you're just going to print
jqian1
2012/02/15 18:41:27
Removed "utf8"
On 2012/02/10 01:36:51, Nirnimesh w
| |
37 | |
38 def InstallGTalkExtension(self): | |
39 """Download and install the GTalk extension.""" | |
40 self.UninstallGTalkExtension() | |
Nirnimesh
2012/02/10 01:36:51
why uninstall? This is unexpected. Neither the fun
jqian1
2012/02/15 18:41:27
I removed the uninstall part.thanks.
On 2012/02/10
| |
41 | |
42 extension_path = os.path.abspath( | |
43 os.path.join(self.DataDir(), 'extensions', 'gtalk', 'gtalk.crx')) | |
44 self.assertTrue(os.path.exists(extension_path), | |
45 msg='Failed to find GTalk extension: ' + extension_path) | |
46 | |
47 self.InstallExtension(extension_path, False) | |
frankf
2012/02/10 03:09:36
nit, False is the default value.
| |
48 extension = self.GetGTalkExtensionInfo() | |
49 self.assertTrue(extension, msg='Failed to install GTalk extension.') | |
50 self.assertTrue(extension['is_enabled'], msg='GTalk extension is disabled.') | |
51 | |
52 def UninstallGTalkExtension(self): | |
53 """Uninstall the GTalk extension (if present)""" | |
54 extension = self.GetGTalkExtensionInfo() | |
55 if extension: | |
56 self.UninstallExtensionById(extension['id']) | |
57 | |
58 def GetGTalkExtensionInfo(self): | |
59 """Get the data object about the GTalk extension.""" | |
60 extensions = [x for x in self.GetExtensionsInfo() | |
61 if x['name'] == 'Google Talk'] | |
62 return extensions[0] if len(extensions) == 1 else None | |
63 | |
64 def RunInMole(self, js, mole_index=0): | |
65 """Execute javascript in a chat mole. | |
66 | |
67 Args: | |
68 js: The javascript to run. | |
69 mole_index: The index of the mole in which to run the JS. | |
70 | |
71 Returns: | |
72 The resulting value from executing the javascript. | |
73 """ | |
74 return self._RunInTab(self.GetMoleInfo(mole_index), js, '//iframe[1]') | |
75 | |
76 def RunInRoster(self, js): | |
77 """Execute javascript in the chat roster. | |
78 | |
79 Args: | |
80 js: The javascript to run. | |
81 | |
82 Returns: | |
83 The resulting value from executing the javascript. | |
84 """ | |
85 return self._RunInTab(self.GetViewerInfo(), js, '//iframe[1]\n//iframe[1]') | |
86 | |
87 def RunInLoginPage(self, js, xpath=''): | |
88 """Execute javascript in the gaia login popup. | |
89 | |
90 Args: | |
91 js: The javascript to run. | |
92 xpath: The xpath to the frame in which to execute the javascript. | |
93 | |
94 Returns: | |
95 The resulting value from executing the javascript. | |
96 """ | |
97 return self._RunInTab(self.GetLoginPageInfo(), js, xpath) | |
98 | |
99 def RunInViewer(self, js, xpath=''): | |
100 """Execute javascript in the GTalk viewer window. | |
101 | |
102 Args: | |
103 js: The javascript to run. | |
104 xpath: The xpath to the frame in which to execute the javascript. | |
105 | |
106 Returns: | |
107 The resulting value from executing the javascript. | |
108 """ | |
109 return self._RunInTab(self.GetViewerInfo(), js, xpath) | |
110 | |
111 def RunInBackground(self, js, xpath=''): | |
112 """Execute javascript in the GTalk viewer window. | |
113 | |
114 Args: | |
115 js: The javascript to run. | |
116 xpath: The xpath to the frame in which to execute the javascript. | |
117 | |
118 Returns: | |
119 The resulting value from executing the javascript. | |
120 """ | |
121 background_view = self.GetBackgroundInfo() | |
122 value = self.ExecuteJavascriptInRenderView( | |
123 self._WrapJs(js), background_view['view']) | |
124 self._LogRun(js, value) | |
125 return value | |
126 | |
127 def GetMoleInfo(self, mole_index=0): | |
128 """Get the data object about a given chat mole. | |
129 | |
130 Args: | |
131 mole_index: The index of the mole to retrieve. | |
132 | |
133 Returns: | |
134 Data object describing mole. | |
135 """ | |
136 extension = self.GetGTalkExtensionInfo() | |
137 return self._GetTabInfo( | |
138 'chrome-extension://' + extension['id'] + '/panel.html', mole_index) | |
frankf
2012/02/10 03:09:36
'chrome-extension://%s/panel.html' % extension['id
jqian1
2012/02/15 18:41:27
Done.
| |
139 | |
140 def GetViewerInfo(self): | |
141 """Get the data object about the GTalk viewer dialog.""" | |
142 extension = self.GetGTalkExtensionInfo() | |
143 return self._GetTabInfo( | |
144 'chrome-extension://' + extension['id'] + '/viewer.html') | |
145 | |
146 def GetLoginPageInfo(self): | |
147 """Get the data object about the gaia login popup.""" | |
148 return self._GetTabInfo('https://accounts.google.com/ServiceLogin?') | |
149 | |
150 def GetBackgroundInfo(self): | |
151 """Get the data object about the GTalk background page.""" | |
152 extension_views = self.GetBrowserInfo()['extension_views'] | |
153 for extension_view in extension_views: | |
154 if 'Google Talk' in extension_view['name'] and \ | |
155 'EXTENSION_BACKGROUND_PAGE' == extension_view['view_type']: | |
156 return extension_view | |
157 return None | |
158 | |
159 def WaitUntilResult(self, result, func, msg): | |
Nirnimesh
2012/02/10 01:36:51
Do you really need this helper method? It's a one-
jqian1
2012/02/15 18:41:27
I would prefer to having this helper method for no
| |
160 """Loop func until a condition matches is satified. | |
161 | |
162 Args: | |
163 result: Value of func() at which to stop. | |
164 func: Function to run at each iteration. | |
165 msg: Error to print upon timing out. | |
166 """ | |
167 assert callable(func) | |
168 self.assertTrue(self.WaitUntil( | |
169 lambda: func(), expect_retval=result), msg=msg) | |
170 | |
171 def WaitUntilCondition(self, func, matches, msg): | |
172 """Loop func until condition matches is satified. | |
173 | |
174 Args: | |
175 func: Function to run at each iteration. | |
176 matches: Funtion to evalute output and determine whether to stop. | |
177 msg: Error to print upon timing out. | |
178 """ | |
179 assert callable(func) | |
180 assert callable(matches) | |
181 self.assertTrue(self.WaitUntil( | |
182 lambda: matches(func())), msg=msg) | |
183 | |
184 def _WrapJs(self, statement): | |
185 """Wrap the javascript to be executed. | |
186 | |
187 Args: | |
188 statement: The piece of javascript to wrap. | |
189 | |
190 Returns: | |
191 The wrapped javascript. | |
192 """ | |
193 return """ | |
194 window.domAutomationController.send( | |
195 (function(){ | |
196 %s | |
197 try{return %s} | |
198 catch(e){return "JS_ERROR: " + e}})()) | |
199 """ % (self._GetInjectedJs(), statement) | |
Nirnimesh
2012/02/10 01:36:51
align """ under return
jqian1
2012/02/15 18:41:27
Done.
| |
200 | |
201 def _RunInTab(self, tab, js, xpath=''): | |
202 """Execute javascript in a given tab. | |
203 | |
204 Args: | |
205 tab: The data object for the Chrome window tab returned by | |
206 _GetTabInfo. | |
207 js: The javascript to run. | |
208 xpath: The xpath to the frame in which to execute the javascript. | |
209 | |
210 Returns: | |
211 The resulting value from executing the javascript. | |
212 """ | |
213 if not tab: | |
214 logging.debug('Tab not found: %s' % tab) | |
215 return False | |
216 logging.info('Run in tab: %s' % js) | |
217 | |
218 value = self.ExecuteJavascript(self._WrapJs(js), | |
219 windex = tab['windex'], | |
220 frame_xpath = xpath) | |
frankf
2012/02/10 03:09:36
http://google-styleguide.googlecode.com/svn/trunk/
jqian1
2012/02/15 18:41:27
Done.
| |
221 self._LogRun(js, value) | |
222 return value | |
223 | |
224 def _LogRun(self, js, value): | |
225 """Log a particular run. | |
226 | |
227 Args: | |
228 js: The javascript statement executed. | |
229 value: The return value for the execution. | |
230 """ | |
231 # works around UnicodeEncodeError: 'ascii' codec can't encode... | |
232 out = value | |
233 if not isinstance(value, basestring): | |
frankf
2012/02/10 03:09:36
you can probably get rid of the if and do str(valu
| |
234 out = str(value) | |
235 out = re.sub('\s', ';', out[:300]) | |
236 logging.info(js + ' ===> ' + out.encode('utf-8')) | |
237 | |
238 def _GetTabInfo(self, url_query, index=0): | |
239 """Get the data object for a given tab. | |
240 | |
241 Args: | |
242 url_query: The substring of the URL to search for. | |
243 index: The index within the list of matches to return. | |
244 | |
245 Returns: | |
246 The data object for the tab. | |
247 """ | |
248 windows = self.GetBrowserInfo()['windows'] | |
249 i = 0 | |
250 for win in windows: | |
251 for tab in win['tabs']: | |
252 if tab['url'] and url_query in tab['url']: | |
253 # Store reference to windex used in _RunInTab. | |
254 tab['windex'] = win['index'] | |
255 if i == index: | |
256 return tab | |
257 i = i + 1 | |
258 return None | |
259 | |
260 def _GetInjectedJs(self): | |
261 """Get the javascript to inject in the execution environment.""" | |
262 if self._injected_js is None: | |
263 self._injected_js = open(os.path.dirname(__file__) + '/jsutils.js').read() | |
frankf
2012/02/10 03:09:36
Use os.path.join
jqian1
2012/02/15 18:41:27
Done.
| |
264 return self._injected_js | |
OLD | NEW |