OLD | NEW |
1 <html> | 1 <html> |
2 <!--notification_tester.html | 2 <!--notification_tester.html |
3 Script with javascript functions for creating and canceling notifications. | 3 Script with javascript functions for creating and canceling notifications. |
4 Also can be used to request permission for notifications. | 4 Also can be used to request permission for notifications. |
5 --> | 5 --> |
6 <script> | 6 <script> |
7 | 7 |
8 // Array of all notifications this page has created. | 8 // Array of all notifications this page has created. |
9 var g_notifications = []; | 9 var g_notifications = []; |
10 // Object of notifications which have been closed. | |
11 var g_closed_notifications = {}; | |
12 // Whether the site has requested and been granted permission. | 10 // Whether the site has requested and been granted permission. |
13 var g_permissionGranted = false; | 11 var g_permissionGranted = false; |
14 | 12 |
15 // Creates a notification with a iconUrl, title, text, and tag. | 13 // Creates a notification with a iconUrl, title, text, and tag. |
16 // Returns an id for the notification, which can be used to cancel it with | 14 // Returns an id for the notification, which can be used to cancel it with |
17 // |cancelNotification|. If two notifications are created with the same | 15 // |cancelNotification|. If two notifications are created with the same |
18 // tag, the second one should replace the first. | 16 // tag, the second one should replace the first. |
19 function createNotification(iconUrl, title, text, tag) { | 17 function createNotification(iconUrl, title, text, tag) { |
20 var notification = new Notification(title, { | 18 var notification = new Notification(title, { |
21 icon: iconUrl, | 19 icon: iconUrl, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 // Callback for requesting notification privileges. | 57 // Callback for requesting notification privileges. |
60 function onPermissionGranted() { | 58 function onPermissionGranted() { |
61 g_permissionGranted = true; | 59 g_permissionGranted = true; |
62 } | 60 } |
63 | 61 |
64 // Helper function that adds a notification to |g_notifications| and shows | 62 // Helper function that adds a notification to |g_notifications| and shows |
65 // it. The index of the notification is sent back to the test, or -1 is sent | 63 // it. The index of the notification is sent back to the test, or -1 is sent |
66 // back on error. If |waitForDisplay| is true, the response will not be sent | 64 // back on error. If |waitForDisplay| is true, the response will not be sent |
67 // until the notification is actually displayed. | 65 // until the notification is actually displayed. |
68 function createNotificationHelper(note, waitForDisplay) { | 66 function createNotificationHelper(note, waitForDisplay) { |
69 var notification_id = g_notifications.length; | 67 function sendNotificationIdToTest() { |
70 | 68 sendResultToTest(g_notifications.length - 1); |
| 69 } |
71 g_notifications.push(note); | 70 g_notifications.push(note); |
72 g_closed_notifications[notification_id] = false; | |
73 | |
74 function sendNotificationIdToTest() { | |
75 sendResultToTest(notification_id); | |
76 } | |
77 | |
78 if (waitForDisplay) | 71 if (waitForDisplay) |
79 note.onshow = sendNotificationIdToTest; | 72 note.onshow = sendNotificationIdToTest; |
80 note.onerror = function() { | 73 note.onerror = function() { |
81 sendResultToTest(-1); | 74 sendResultToTest(-1); |
82 } | 75 } |
83 note.onclose = function() { | |
84 g_closed_notifications[notification_id] = true; | |
85 } | |
86 | 76 |
87 if (!waitForDisplay) | 77 if (!waitForDisplay) |
88 sendNotificationIdToTest(); | 78 sendNotificationIdToTest(); |
89 } | 79 } |
90 | 80 |
91 // Returns whether the notification with Id has been closed. | |
92 function hasBeenClosed(notification_id) { | |
93 sendResultToTest(g_closed_notifications[notification_id]); | |
94 } | |
95 | |
96 // Sends a result back to the main test logic. | 81 // Sends a result back to the main test logic. |
97 function sendResultToTest(result) { | 82 function sendResultToTest(result) { |
98 // Convert the result to a string. | 83 // Convert the result to a string. |
99 var stringResult = "" + result; | 84 var stringResult = "" + result; |
100 if (typeof stringResult != "string") | 85 if (typeof stringResult != "string") |
101 stringResult = JSON.stringify(result); | 86 stringResult = JSON.stringify(result); |
102 window.domAutomationController.send(stringResult); | 87 window.domAutomationController.send(stringResult); |
103 } | 88 } |
104 | 89 |
105 </script> | 90 </script> |
106 | 91 |
107 <body> | 92 <body> |
108 This page is used for testing HTML5 notifications. | 93 This page is used for testing HTML5 notifications. |
109 </body> | 94 </body> |
110 </html> | 95 </html> |
OLD | NEW |