2 <!--notification_tester.html
3 Script with javascript functions for creating and canceling notifications.
4 Also can be used to request permission for notifications.
8 // Array of all notifications this page has created.
9 var g_notifications
= [];
10 // Whether the site has requested and been granted permission.
11 var g_permissionGranted
= false;
13 // Creates a notification with a iconUrl, title, text, and replaceId.
14 // Returns an id for the notification, which can be used to cancel it with
15 // |cancelNotification|. If two notifications are created with the same
16 // replaceId, the second one should replace the first.
17 function createNotification(iconUrl
, title
, text
, replaceId
) {
19 var note
= webkitNotifications
.createNotification(iconUrl
,
26 createNotificationHelper(note
, replaceId
, true);
29 // Creates an HTML notification with a given content url.
30 // Returns an id for the notification, which can be used to cancel it with
31 // |cancelNotification|. If two notifications are created with the same
32 // replaceId, the second one should replace the first. If the notification
33 // cannot be created, this returns -1.
34 // If |waitForDisplay| is true, a response will not be sent until the
35 // notification is actually displayed.
36 function createHTMLNotification(contentUrl
, replaceId
, waitForDisplay
) {
38 var note
= webkitNotifications
.createHTMLNotification(contentUrl
);
43 createNotificationHelper(note
, replaceId
, waitForDisplay
);
46 // Cancels a notification with the given id. The notification must be showing,
47 // as opposed to waiting to be shown in the display queue.
48 // Returns '1' on success.
49 function cancelNotification(id
) {
50 if (id
< 0 || id
> g_notifications
.length
) {
51 var errorMsg
= "Attempted to cancel notification with invalid ID.\n" +
52 "ID: " + id
+ "\n# of notifications: " + g_notifications
.length
;
53 sendResultToTest(errorMsg
);
55 g_notifications
[id
].onclose = function() {
58 g_notifications
[id
].cancel();
61 // Requests permission for this origin to create notifications.
62 function requestPermission() {
63 window
.webkitNotifications
.requestPermission(onPermissionGranted
);
67 // Waits for the permission to create notifications to be granted.
68 function waitForPermissionGranted() {
69 if (g_permissionGranted
) {
72 setTimeout(waitForPermissionGranted
, 50);
76 // Callback for requesting notification privileges.
77 function onPermissionGranted() {
78 g_permissionGranted
= true;
81 // Helper function that adds a notification to |g_notifications| and shows
82 // it. The index of the notification is sent back to the test, or -1 is sent
83 // back on error. If |waitForDisplay| is true, the response will not be sent
84 // until the notification is actually displayed.
85 function createNotificationHelper(note
, replaceId
, waitForDisplay
) {
86 function sendNotificationIdToTest() {
87 sendResultToTest(g_notifications
.length
- 1);
89 g_notifications
.push(note
);
90 note
.replaceId
= replaceId
;
92 note
.ondisplay
= sendNotificationIdToTest
;
93 note
.onerror = function() {
99 sendNotificationIdToTest();
102 // Sends a result back to the main test logic.
103 function sendResultToTest(result
) {
104 // Convert the result to a string.
105 var stringResult
= "" + result
;
106 if (typeof stringResult
!= "string")
107 stringResult
= JSON
.stringify(result
);
108 window
.domAutomationController
.send(stringResult
);
114 This page is used for testing HTML5 notifications.