1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
#!/usr/bin/python
# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
import urllib
import pyauto_functional
import pyauto
class NotificationsTest(pyauto.PyUITest):
"""Test of HTML5 desktop notifications."""
def __init__(self, methodName='runTest'):
super(NotificationsTest, self).__init__(methodName)
self.NO_SUCH_URL = 'http://no_such_url_exists/'
# Content settings for default notification permission.
self.ALLOW_ALL_SETTING = 1
self.DENY_ALL_SETTING = 2
self.ASK_SETTING = 3
# HTML page used for notification testing.
self.TEST_PAGE_URL = self.GetFileURLForDataPath(
os.path.join('notifications', 'notification_tester.html'))
def Debug(self):
"""Test method for experimentation.
This method will not run automatically.
"""
while True:
raw_input('Interact with the browser and hit <enter> to dump notification'
'state...')
print '*' * 20
import pprint
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(self.GetActiveNotifications())
def _SetDefaultPermissionSetting(self, setting):
"""Sets the default setting for whether sites are allowed to create
notifications.
"""
self.SetPrefs(pyauto.kDesktopNotificationDefaultContentSetting, setting)
def _GetDefaultPermissionSetting(self):
"""Gets the default setting for whether sites are allowed to create
notifications.
"""
return self.GetPrefsInfo().Prefs(
pyauto.kDesktopNotificationDefaultContentSetting)
def _GetDeniedOrigins(self):
"""Gets the list of origins that are explicitly denied to create
notifications.
"""
return (self.GetPrefsInfo().Prefs(pyauto.kDesktopNotificationDeniedOrigins)
or [])
def _GetAllowedOrigins(self):
"""Gets the list of origins that are explicitly allowed to create
notifications.
"""
return (self.GetPrefsInfo().Prefs(pyauto.kDesktopNotificationAllowedOrigins)
or [])
def _SetAllowedOrigins(self, origins):
"""Sets the list of allowed origins to the given list.
None of the items in the list should be explicitly denied.
"""
return self.SetPrefs(pyauto.kDesktopNotificationAllowedOrigins, origins)
def _SetDeniedOrigins(self, origins):
"""Sets the list of denied origins to the given list.
None of the items in the list should be explicitly allowed.
"""
return self.SetPrefs(pyauto.kDesktopNotificationDeniedOrigins, origins)
def _DenyOrigin(self, new_origin):
"""Denies the given origin to create notifications.
If it was explicitly allowed, that preference is dropped.
"""
self._DropOriginPreference(new_origin)
denied = self._GetDeniedOrigins()
if new_origin not in denied:
self._SetDeniedOrigins(denied + [new_origin])
def _AllowOrigin(self, new_origin):
"""Allows the given origin to create notifications. If it was explicitly
denied, that preference is dropped.
"""
self._DropOriginPreference(new_origin)
allowed = self._GetAllowedOrigins()
if new_origin not in allowed:
self._SetAllowedOrigins(allowed + [new_origin])
def _DropOriginPreference(self, new_origin):
"""Drops the preference as to whether this origin should be allowed to
create notifications. If it was explicitly allowed or explicitly denied,
that preference is removed.
"""
allowed = self._GetAllowedOrigins()
if allowed and new_origin in allowed:
allowed.remove(new_origin)
self._SetAllowedOrigins(allowed)
denied = self._GetDeniedOrigins()
if denied and new_origin in denied:
denied.remove(new_origin)
self._SetDeniedOrigins(denied)
def _AllowAllOrigins(self):
"""Allows any origin to create notifications."""
self._SetDefaultPermissionSetting(self.ALLOW_ALL_SETTING)
self._SetDeniedOrigins([])
def _VerifyInfobar(self, origin, tab_index=0, windex=0):
"""Helper to verify the notification infobar contents are correct.
Defaults to first tab in first window.
Args:
origin: origin of the notification, e.g., www.gmail.com
tab_index: index of the tab within the given window
windex: index of the window
"""
tab_info = self.GetBrowserInfo()['windows'][windex]['tabs'][tab_index]
self.assertEquals(1, len(tab_info['infobars']))
infobar = tab_info['infobars'][0]
text = 'Allow %s to show desktop notifications?' % origin
self.assertEqual(text, infobar['text'])
self.assertEqual(2, len(infobar['buttons']))
self.assertEqual('Allow', infobar['buttons'][0])
self.assertEqual('Deny', infobar['buttons'][1])
def _CreateSimpleNotification(self, img_url, title, text,
replace_id='', tab_index=0, windex=0):
"""Creates a simple notification.
Returns the id of the notification, which can be used to cancel it later.
This executes a script in the page which shows a notification.
This will only work if the page is navigated to |TEST_PAGE_URL|.
The page must also have permission to show notifications.
Args:
img_url: url of a image to use; can be a data url
title: title of the notification
text: text in the notification
replace_id: id string to be used for this notification. If another
notification is shown with the same replace_id, the former
will be replaced.
tab_index: index of the tab within the given window
windex: index of the window
"""
return self.CallJavascriptFunc('createNotification',
[img_url, title, text, replace_id],
tab_index,
windex);
def _CreateHTMLNotification(self, content_url, replace_id='', tab_index=0,
windex=0):
"""Creates an HTML notification.
Returns the id of the notification, which can be used to cancel it later.
This executes a script in the page which shows a notification.
This will only work if the page is navigated to |TEST_PAGE_URL|.
The page must also have permission to show notifications.
Args:
content_url: url of the page to show in the notification
replace_id: id string to be used for this notification. If another
notification is shown with the same replace_id, the former
will be replaced.
tab_index: index of the tab within the given window
windex: index of the window
"""
return self.CallJavascriptFunc('createHTMLNotification',
[content_url, replace_id],
tab_index,
windex)
def _RequestPermission(self, tab_index=0, windex=0):
"""Requests permission to create notifications.
This will only work if the current page is navigated to |TEST_PAGE_URL|.
Args:
tab_index: index of the tab within the given window
windex: index of the window
"""
self.CallJavascriptFunc('requestPermission', [], tab_index, windex)
def _CancelNotification(self, notification_id, tab_index=0, windex=0):
"""Cancels a notification with the given id.
This canceling is done in the page that showed that notification and so
follows a different path than closing a notification via the UI.
This function should NOT be called until |WaitForNotificationCount| has been
used to verify the notification is showing. This function cannot be used to
cancel a notification that is in the display queue.
This will only work if the page is navigated to |TEST_PAGE_URL|.
Args:
notification_id: id of the notification to cancel
tab_index: index of the tab within the given window that created the
notification
windex: index of the window
"""
msg = self.CallJavascriptFunc(
'cancelNotification', [notification_id], tab_index, windex)
# '1' signifies success.
self.assertEquals('1', msg)
def testCreateSimpleNotification(self):
"""Creates a simple notification."""
self._AllowAllOrigins()
self.NavigateToURL(self.TEST_PAGE_URL)
self._CreateSimpleNotification('no_such_file.png', 'My Title', 'My Body')
self.assertEquals(1, len(self.GetActiveNotifications()))
notification = self.GetActiveNotifications()[0]
html_data = urllib.unquote(notification['content_url'])
self.assertTrue('no_such_file.png' in html_data)
self.assertTrue('My Title' in html_data)
self.assertTrue('My Body' in html_data)
def testCreateHTMLNotification(self):
"""Creates an HTML notification using a fake url."""
self._AllowAllOrigins()
self.NavigateToURL(self.TEST_PAGE_URL)
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.assertEquals(1, len(self.GetActiveNotifications()))
notification = self.GetActiveNotifications()[0]
self.assertEquals(self.NO_SUCH_URL, notification['content_url'])
self.assertEquals('', notification['display_source'])
self.assertEquals('file:///', notification['origin_url'])
def testCloseNotification(self):
"""Creates a notification and closes it."""
self._AllowAllOrigins()
self.NavigateToURL(self.TEST_PAGE_URL)
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.CloseNotification(0)
self.assertFalse(self.GetActiveNotifications())
def testCancelNotification(self):
"""Creates a notification and cancels it in the origin page."""
self._AllowAllOrigins()
self.NavigateToURL(self.TEST_PAGE_URL)
note_id = self._CreateHTMLNotification(self.NO_SUCH_URL)
self.assertNotEquals(-1, note_id)
self.WaitForNotificationCount(1)
self._CancelNotification(note_id)
self.assertFalse(self.GetActiveNotifications())
def testPermissionInfobarAppears(self):
"""Requests notification privileges and verifies the infobar appears."""
self.NavigateToURL(self.TEST_PAGE_URL)
self._RequestPermission()
self.assertTrue(self.WaitForInfobarCount(1))
self.assertFalse(self.GetActiveNotifications())
self._VerifyInfobar('') # file:/// origins are blank
def testAllowOnPermissionInfobar(self):
"""Tries to create a notification and clicks allow on the infobar."""
self.NavigateToURL(self.TEST_PAGE_URL)
# This notification should not be shown because we do not have permission.
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.assertFalse(self.GetActiveNotifications())
self._RequestPermission()
self.assertTrue(self.WaitForInfobarCount(1))
self.PerformActionOnInfobar('accept', 0)
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.WaitForNotificationCount(1)
def testOriginPreferencesBasic(self):
"""Tests that we can allow and deny origins."""
altavista = 'www.altavista.com'
gmail = 'www.gmail.com'
yahoo = 'www.yahoo.com'
self._SetDeniedOrigins([altavista, gmail])
self.assertEquals(altavista, self._GetDeniedOrigins()[0])
self.assertEquals(gmail, self._GetDeniedOrigins()[1])
self._DenyOrigin(yahoo)
self.assertEquals(yahoo, self._GetDeniedOrigins()[2])
self.assertEquals(3, len(self._GetDeniedOrigins()))
self._DropOriginPreference(gmail)
self.assertEquals(2, len(self._GetDeniedOrigins()))
self.assertFalse(gmail in self._GetDeniedOrigins())
self._AllowOrigin(yahoo)
self.assertEquals(1, len(self._GetDeniedOrigins()))
self.assertFalse(yahoo in self._GetDeniedOrigins())
self.assertTrue(yahoo in self._GetAllowedOrigins())
self._SetAllowedOrigins([altavista, gmail])
self._SetDeniedOrigins([])
self.assertEquals(altavista, self._GetAllowedOrigins()[0])
self.assertEquals(gmail, self._GetAllowedOrigins()[1])
self._AllowOrigin(yahoo)
self.assertEquals(yahoo, self._GetAllowedOrigins()[2])
self.assertEquals(3, len(self._GetAllowedOrigins()))
self._DropOriginPreference(gmail)
self.assertEquals(2, len(self._GetAllowedOrigins()))
self.assertFalse(gmail in self._GetAllowedOrigins())
self._DenyOrigin(yahoo)
self.assertEquals(1, len(self._GetAllowedOrigins()))
self.assertTrue(yahoo in self._GetDeniedOrigins())
self.assertFalse(yahoo in self._GetAllowedOrigins())
def testDenyOnPermissionInfobar (self):
"""Test that no notification is created when Deny is chosen
from permission infobar."""
self.NavigateToURL(self.TEST_PAGE_URL)
self._RequestPermission()
self.assertTrue(self.WaitForInfobarCount(1))
self.PerformActionOnInfobar('cancel', 0)
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.assertFalse(self.GetActiveNotifications())
self.assertEquals(['file:///'], self._GetDeniedOrigins())
def testClosePermissionInfobar(self):
"""Test that no notification is created when permission
infobar is dismissed."""
self.NavigateToURL(self.TEST_PAGE_URL)
self._RequestPermission()
self.assertTrue(self.WaitForInfobarCount(1))
self.PerformActionOnInfobar('dismiss', 0)
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.assertFalse(self.GetActiveNotifications())
self.assertFalse(self._GetDeniedOrigins())
def testNotificationWithPropertyMissing(self):
"""Test that a notification can be created if one property is missing."""
self._AllowAllOrigins()
self.NavigateToURL(self.TEST_PAGE_URL)
self._CreateSimpleNotification('no_such_file.png', 'My Title', '')
self.assertEquals(1, len(self.GetActiveNotifications()))
html_data = urllib.unquote(self.GetActiveNotifications()[0]['content_url'])
self.assertTrue('no_such_file.png' in html_data)
self.assertTrue('My Title' in html_data)
def testAllowNotificationsFromAllSites(self):
"""Verify that all domains can be allowed to show notifications."""
self._SetDefaultPermissionSetting(self.ALLOW_ALL_SETTING)
self.NavigateToURL(self.TEST_PAGE_URL)
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.assertEquals(1, len(self.GetActiveNotifications()))
self.assertFalse(self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars'])
def testDenyNotificationsFromAllSites(self):
"""Verify that no domain can show notifications."""
self._SetDefaultPermissionSetting(self.DENY_ALL_SETTING)
self.NavigateToURL(self.TEST_PAGE_URL)
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.assertFalse(self.GetActiveNotifications())
def testDenyDomainAndAllowAll(self):
"""Verify that denying a domain and allowing all shouldn't show
notifications from the denied domain."""
self._DenyOrigin('file:///')
self._SetDefaultPermissionSetting(self.ALLOW_ALL_SETTING)
self.NavigateToURL(self.TEST_PAGE_URL)
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.assertFalse(self.GetActiveNotifications())
def testAllowDomainAndDenyAll(self):
"""Verify that allowing a domain and denying all others should show
notifications from the allowed domain."""
self._AllowOrigin('file:///')
self._SetDefaultPermissionSetting(self.DENY_ALL_SETTING)
self.NavigateToURL(self.TEST_PAGE_URL)
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.assertEquals(1, len(self.GetActiveNotifications()))
self.assertFalse(self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars'])
def testDenyAndThenAllowDomain(self):
"""Verify that denying and again allowing should show notifications."""
self._DenyOrigin('file:///')
self.NavigateToURL(self.TEST_PAGE_URL)
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.assertEquals(len(self.GetActiveNotifications()), 0)
self._AllowOrigin('file:///')
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.assertEquals(1, len(self.GetActiveNotifications()))
self.assertFalse(self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars'])
def testCreateDenyCloseNotifications(self):
"""Verify able to create, deny, and close the notification."""
self._AllowAllOrigins()
self.NavigateToURL(self.TEST_PAGE_URL)
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.assertEquals(1, len(self.GetActiveNotifications()))
origin = 'file:///'
self._DenyOrigin(origin)
self.assertTrue(origin in self._GetDeniedOrigins())
self.CloseNotification(0)
self.assertEquals(0, len(self.GetActiveNotifications()))
def testOriginPrefsNotSavedInIncognito(self):
"""Verify that allow/deny origin preferences are not saved in incognito."""
self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW)
self.NavigateToURL(self.TEST_PAGE_URL, 1, 0)
self._RequestPermission(windex=1)
self.assertTrue(self.WaitForInfobarCount(1, windex=1))
self.PerformActionOnInfobar('cancel', 0, windex=1)
self.NavigateToURL(self.TEST_PAGE_URL, 1, 0)
self._RequestPermission(windex=1)
self.assertTrue(self.WaitForInfobarCount(1, windex=1))
self.PerformActionOnInfobar('accept', 0, windex=1)
self._CreateHTMLNotification(self.NO_SUCH_URL, windex=1)
self.assertEquals(1, len(self.GetActiveNotifications()))
self.NavigateToURL(self.TEST_PAGE_URL, 1, 0)
self._RequestPermission(windex=1)
self.assertTrue(self.WaitForInfobarCount(1, windex=1))
self.assertFalse(self._GetDeniedOrigins())
self.assertFalse(self._GetAllowedOrigins())
def testExitBrowserWithInfobar(self):
"""Exit the browser window, when the infobar appears."""
self.NavigateToURL(self.TEST_PAGE_URL)
self._RequestPermission()
self.assertTrue(self.WaitForInfobarCount(1))
def testCrashTabWithPermissionInfobar(self):
"""Test crashing the tab with permission infobar doesn't crash Chrome."""
self.AppendTab(pyauto.GURL(self.NO_SUCH_URL))
self.assertTrue(self.ActivateTab(0))
self.NavigateToURL(self.TEST_PAGE_URL)
self._RequestPermission()
self.assertTrue(self.WaitForInfobarCount(1))
self.Kill(self.GetBrowserInfo()['windows'][0]['tabs'][0]['renderer_pid'])
self.assertTrue(self.IsBrowserRunning())
def testKillNotificationProcess(self):
"""Test killing a notification doesn't crash Chrome."""
self._AllowAllOrigins()
self.NavigateToURL(self.TEST_PAGE_URL)
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.Kill(self.GetActiveNotifications()[0]['pid'])
self.assertTrue(self.IsBrowserRunning())
self.WaitForNotificationCount(0)
def testIncognitoNotification(self):
"""Test notifications in incognito window."""
self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW)
self.NavigateToURL(self.TEST_PAGE_URL, 1, 0)
self.assertTrue(self.ActivateTab(0, 1))
self._RequestPermission(windex=1)
self.assertTrue(self.WaitForInfobarCount(1, windex=1))
self.PerformActionOnInfobar('accept', infobar_index=0, windex=1)
self._CreateHTMLNotification(self.NO_SUCH_URL, windex=1)
self.assertEquals(1, len(self.GetActiveNotifications()))
def testSpecialURLNotification(self):
"""Test a page cannot create a notification to a chrome: url."""
self._AllowAllOrigins()
self.NavigateToURL(self.TEST_PAGE_URL)
self._CreateHTMLNotification('chrome://extensions/')
self.assertFalse(self.GetActiveNotifications())
def testCloseTabWithPermissionInfobar(self):
"""Test that user can close tab when infobar present."""
self.AppendTab(pyauto.GURL('about:blank'))
self.ActivateTab(0)
self.NavigateToURL(self.TEST_PAGE_URL)
self._RequestPermission()
self.assertTrue(self.WaitForInfobarCount(1))
self.GetBrowserWindow(0).GetTab(0).Close(True)
self.assertTrue(self.IsBrowserRunning())
def testNavigateAwayWithPermissionInfobar(self):
"""Test navigating away when an infobar is present, then trying to create a
notification from the same page."""
self.AppendTab(pyauto.GURL('about:blank'))
self.assertTrue(self.ActivateTab(0))
self.NavigateToURL(self.TEST_PAGE_URL)
self._RequestPermission()
self.assertTrue(self.WaitForInfobarCount(1))
self.NavigateToURL(self.TEST_PAGE_URL)
self._RequestPermission()
self.assertTrue(self.WaitForInfobarCount(1))
self.PerformActionOnInfobar('accept', 0)
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.assertEquals(1, len(self.GetActiveNotifications()))
def testCrashRendererNotificationRemain(self):
"""Test crashing renderer does not close or crash notification."""
self._AllowAllOrigins()
self.AppendTab(pyauto.GURL('about:blank'))
self.ActivateTab(0)
self.NavigateToURL(self.TEST_PAGE_URL)
self._CreateHTMLNotification(self.NO_SUCH_URL)
self.assertEquals(1, len(self.GetActiveNotifications()))
self.Kill(self.GetBrowserInfo()['windows'][0]['tabs'][0]['renderer_pid'])
self.assertTrue(self.IsBrowserRunning())
self.assertEquals(1, len(self.GetActiveNotifications()))
def testNotificationOrderAfterClosingOne(self):
"""Tests that closing a notification leaves the rest
of the notifications in the correct order.
"""
self._AllowAllOrigins()
self.NavigateToURL(self.TEST_PAGE_URL)
self._CreateSimpleNotification('', 'Title1', '')
self._CreateSimpleNotification('', 'Title2', '')
self._CreateSimpleNotification('', 'Title3', '')
self.assertEquals(3, len(self.GetActiveNotifications()))
old_notifications = self.GetActiveNotifications()
self.CloseNotification(1)
self.assertEquals(2, len(self.GetActiveNotifications()))
new_notifications = self.GetActiveNotifications()
self.assertEquals(old_notifications[0], new_notifications[0])
self.assertEquals(old_notifications[2], new_notifications[1])
def testNotificationReplacement(self):
"""Test that we can replace a notification using the replaceId."""
self._AllowAllOrigins()
self.NavigateToURL(self.TEST_PAGE_URL)
self._CreateSimpleNotification('', 'Title2', '', 'chat')
self.WaitForNotificationCount(1)
# Since this notification has the same replaceId, 'chat', it should replace
# the first notification.
self._CreateHTMLNotification(self.NO_SUCH_URL, 'chat')
notifications = self.GetActiveNotifications()
self.assertEquals(1, len(notifications))
self.assertEquals(self.NO_SUCH_URL, notifications[0]['content_url'])
if __name__ == '__main__':
pyauto_functional.Main()
|