OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
| 6 import os |
6 import urllib | 7 import urllib |
7 | 8 |
8 import pyauto_functional | 9 import pyauto_functional |
9 import pyauto | 10 import pyauto |
10 | 11 |
11 | 12 |
12 class NotificationsTest(pyauto.PyUITest): | 13 class NotificationsTest(pyauto.PyUITest): |
13 """Test of HTML5 desktop notifications.""" | 14 """Test of HTML5 desktop notifications.""" |
14 def __init__(self, methodName='runTest'): | 15 def __init__(self, methodName='runTest'): |
15 super(NotificationsTest, self).__init__(methodName) | 16 super(NotificationsTest, self).__init__(methodName) |
16 self.NO_SUCH_URL = 'http://no_such_url_exists/' | 17 self.NO_SUCH_URL = 'http://no_such_url_exists/' |
17 # Content settings for default notification permission. | 18 # Content settings for default notification permission. |
18 self.ALLOW_ALL_SETTING = 1 | 19 self.ALLOW_ALL_SETTING = 1 |
19 self.DENY_ALL_SETTING = 2 | 20 self.DENY_ALL_SETTING = 2 |
20 self.ASK_SETTING = 3 | 21 self.ASK_SETTING = 3 |
21 | 22 |
22 # HTML page used for notification testing. | 23 # HTML page used for notification testing. |
23 self.TEST_PAGE_URL = ( | 24 self.TEST_PAGE_URL = self.GetFileURLForDataPath( |
24 self.GetFileURLForDataPath('notifications/notification_tester.html')) | 25 os.path.join('notifications', 'notification_tester.html')) |
25 | 26 |
26 def Debug(self): | 27 def Debug(self): |
27 """Test method for experimentation. | 28 """Test method for experimentation. |
28 | 29 |
29 This method will not run automatically. | 30 This method will not run automatically. |
30 """ | 31 """ |
31 while True: | 32 while True: |
32 raw_input('Interact with the browser and hit <enter> to dump notification' | 33 raw_input('Interact with the browser and hit <enter> to dump notification' |
33 'state...') | 34 'state...') |
34 print '*' * 20 | 35 print '*' * 20 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 """ | 126 """ |
126 tab_info = self.GetBrowserInfo()['windows'][windex]['tabs'][tab_index] | 127 tab_info = self.GetBrowserInfo()['windows'][windex]['tabs'][tab_index] |
127 self.assertEquals(1, len(tab_info['infobars'])) | 128 self.assertEquals(1, len(tab_info['infobars'])) |
128 infobar = tab_info['infobars'][0] | 129 infobar = tab_info['infobars'][0] |
129 text = 'Allow %s to show desktop notifications?' % origin | 130 text = 'Allow %s to show desktop notifications?' % origin |
130 self.assertEqual(text, infobar['text']) | 131 self.assertEqual(text, infobar['text']) |
131 self.assertEqual(2, len(infobar['buttons'])) | 132 self.assertEqual(2, len(infobar['buttons'])) |
132 self.assertEqual('Allow', infobar['buttons'][0]) | 133 self.assertEqual('Allow', infobar['buttons'][0]) |
133 self.assertEqual('Deny', infobar['buttons'][1]) | 134 self.assertEqual('Deny', infobar['buttons'][1]) |
134 | 135 |
135 def _CallJavascriptFunc(self, function, args=[], tab_index=0, windex=0): | |
136 """Helper function to execute a script that calls a given function. | |
137 | |
138 Defaults to first tab in first window. | |
139 | |
140 Args: | |
141 function: name of the function | |
142 args: list of all the arguments to pass into the called function. These | |
143 should be able to be converted to a string using the |str| function. | |
144 tab_index: index of the tab within the given window | |
145 windex: index of the window | |
146 """ | |
147 # Convert the given arguments for evaluation in a javascript statement. | |
148 converted_args = [] | |
149 for arg in args: | |
150 # If it is a string argument, we need to quote and escape it properly. | |
151 if type(arg) == type('string') or type(arg) == type(u'unicode'): | |
152 # We must convert all " in the string to \", so that we don't try | |
153 # to evaluate invalid javascript like ""arg"". | |
154 converted_arg = '"' + arg.replace('"', '\\"') + '"' | |
155 else: | |
156 # Convert it to a string so that we can use |join| later. | |
157 converted_arg = str(arg) | |
158 converted_args += [converted_arg] | |
159 js = '%s(%s)' % (function, ', '.join(converted_args)) | |
160 return self.ExecuteJavascript(js, windex, tab_index) | |
161 | |
162 def _CreateSimpleNotification(self, img_url, title, text, | 136 def _CreateSimpleNotification(self, img_url, title, text, |
163 replace_id='', tab_index=0, windex=0): | 137 replace_id='', tab_index=0, windex=0): |
164 """Creates a simple notification. | 138 """Creates a simple notification. |
165 | 139 |
166 Returns the id of the notification, which can be used to cancel it later. | 140 Returns the id of the notification, which can be used to cancel it later. |
167 | 141 |
168 This executes a script in the page which shows a notification. | 142 This executes a script in the page which shows a notification. |
169 This will only work if the page is navigated to |TEST_PAGE_URL|. | 143 This will only work if the page is navigated to |TEST_PAGE_URL|. |
170 The page must also have permission to show notifications. | 144 The page must also have permission to show notifications. |
171 | 145 |
172 Args: | 146 Args: |
173 img_url: url of a image to use; can be a data url | 147 img_url: url of a image to use; can be a data url |
174 title: title of the notification | 148 title: title of the notification |
175 text: text in the notification | 149 text: text in the notification |
176 replace_id: id string to be used for this notification. If another | 150 replace_id: id string to be used for this notification. If another |
177 notification is shown with the same replace_id, the former | 151 notification is shown with the same replace_id, the former |
178 will be replaced. | 152 will be replaced. |
179 tab_index: index of the tab within the given window | 153 tab_index: index of the tab within the given window |
180 windex: index of the window | 154 windex: index of the window |
181 """ | 155 """ |
182 return self._CallJavascriptFunc('createNotification', | 156 return self.CallJavascriptFunc('createNotification', |
183 [img_url, title, text, replace_id], | 157 [img_url, title, text, replace_id], |
184 tab_index, | 158 tab_index, |
185 windex); | 159 windex); |
186 | 160 |
187 def _CreateHTMLNotification(self, content_url, replace_id='', tab_index=0, | 161 def _CreateHTMLNotification(self, content_url, replace_id='', tab_index=0, |
188 windex=0): | 162 windex=0): |
189 """Creates an HTML notification. | 163 """Creates an HTML notification. |
190 | 164 |
191 Returns the id of the notification, which can be used to cancel it later. | 165 Returns the id of the notification, which can be used to cancel it later. |
192 | 166 |
193 This executes a script in the page which shows a notification. | 167 This executes a script in the page which shows a notification. |
194 This will only work if the page is navigated to |TEST_PAGE_URL|. | 168 This will only work if the page is navigated to |TEST_PAGE_URL|. |
195 The page must also have permission to show notifications. | 169 The page must also have permission to show notifications. |
196 | 170 |
197 Args: | 171 Args: |
198 content_url: url of the page to show in the notification | 172 content_url: url of the page to show in the notification |
199 replace_id: id string to be used for this notification. If another | 173 replace_id: id string to be used for this notification. If another |
200 notification is shown with the same replace_id, the former | 174 notification is shown with the same replace_id, the former |
201 will be replaced. | 175 will be replaced. |
202 tab_index: index of the tab within the given window | 176 tab_index: index of the tab within the given window |
203 windex: index of the window | 177 windex: index of the window |
204 """ | 178 """ |
205 return self._CallJavascriptFunc('createHTMLNotification', | 179 return self.CallJavascriptFunc('createHTMLNotification', |
206 [content_url, replace_id], | 180 [content_url, replace_id], |
207 tab_index, | 181 tab_index, |
208 windex) | 182 windex) |
209 | 183 |
210 def _RequestPermission(self, tab_index=0, windex=0): | 184 def _RequestPermission(self, tab_index=0, windex=0): |
211 """Requests permission to create notifications. | 185 """Requests permission to create notifications. |
212 | 186 |
213 This will only work if the current page is navigated to |TEST_PAGE_URL|. | 187 This will only work if the current page is navigated to |TEST_PAGE_URL|. |
214 | 188 |
215 Args: | 189 Args: |
216 tab_index: index of the tab within the given window | 190 tab_index: index of the tab within the given window |
217 windex: index of the window | 191 windex: index of the window |
218 """ | 192 """ |
219 self._CallJavascriptFunc('requestPermission', [], windex, tab_index) | 193 self.CallJavascriptFunc('requestPermission', [], windex, tab_index) |
220 | 194 |
221 def _CancelNotification(self, notification_id, tab_index=0, windex=0): | 195 def _CancelNotification(self, notification_id, tab_index=0, windex=0): |
222 """Cancels a notification with the given id. | 196 """Cancels a notification with the given id. |
223 | 197 |
224 This canceling is done in the page that showed that notification and so | 198 This canceling is done in the page that showed that notification and so |
225 follows a different path than closing a notification via the UI. | 199 follows a different path than closing a notification via the UI. |
226 | 200 |
227 A notification can be canceled even if it has not been shown yet. | 201 A notification can be canceled even if it has not been shown yet. |
228 This will only work if the page is navigated to |TEST_PAGE_URL|. | 202 This will only work if the page is navigated to |TEST_PAGE_URL|. |
229 | 203 |
230 Args: | 204 Args: |
231 tab_index: index of the tab within the given window that created the | 205 tab_index: index of the tab within the given window that created the |
232 notification | 206 notification |
233 windex: index of the window | 207 windex: index of the window |
234 """ | 208 """ |
235 self._CallJavascriptFunc( | 209 self.CallJavascriptFunc( |
236 'cancelNotification', [notification_id], tab_index, windex) | 210 'cancelNotification', [notification_id], tab_index, windex) |
237 | 211 |
238 def testCreateSimpleNotification(self): | 212 def testCreateSimpleNotification(self): |
239 """Creates a simple notification.""" | 213 """Creates a simple notification.""" |
240 self._AllowAllOrigins() | 214 self._AllowAllOrigins() |
241 self.NavigateToURL(self.TEST_PAGE_URL) | 215 self.NavigateToURL(self.TEST_PAGE_URL) |
242 self._CreateSimpleNotification('no_such_file.png', 'My Title', 'My Body') | 216 self._CreateSimpleNotification('no_such_file.png', 'My Title', 'My Body') |
243 self.assertEquals(len(self.GetActiveNotifications()), 1) | 217 self.assertEquals(len(self.GetActiveNotifications()), 1) |
244 notification = self.GetActiveNotifications()[0] | 218 notification = self.GetActiveNotifications()[0] |
245 html_data = urllib.unquote(notification['content_url']) | 219 html_data = urllib.unquote(notification['content_url']) |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 self.assertFalse(gmail in self._GetAllowedOrigins()) | 302 self.assertFalse(gmail in self._GetAllowedOrigins()) |
329 | 303 |
330 self._DenyOrigin(yahoo) | 304 self._DenyOrigin(yahoo) |
331 self.assertEquals(1, len(self._GetAllowedOrigins())) | 305 self.assertEquals(1, len(self._GetAllowedOrigins())) |
332 self.assertTrue(yahoo in self._GetDeniedOrigins()) | 306 self.assertTrue(yahoo in self._GetDeniedOrigins()) |
333 self.assertFalse(yahoo in self._GetAllowedOrigins()) | 307 self.assertFalse(yahoo in self._GetAllowedOrigins()) |
334 | 308 |
335 | 309 |
336 if __name__ == '__main__': | 310 if __name__ == '__main__': |
337 pyauto_functional.Main() | 311 pyauto_functional.Main() |
OLD | NEW |