| 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 // Whether the site has requested and been granted permission. | 10 // Whether the site has requested and been granted permission. |
| 11 var g_permissionGranted = false; | 11 var g_permissionGranted = false; |
| 12 | 12 |
| 13 // Creates a notification with a iconUrl, title, text, and replaceId. | 13 // Creates a notification with a iconUrl, title, text, and tag. |
| 14 // 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 |
| 15 // |cancelNotification|. If two notifications are created with the same | 15 // |cancelNotification|. If two notifications are created with the same |
| 16 // replaceId, the second one should replace the first. | 16 // tag, the second one should replace the first. |
| 17 function createNotification(iconUrl, title, text, replaceId) { | 17 function createNotification(iconUrl, title, text, tag) { |
| 18 try { | 18 var notification = new Notification(title, { |
| 19 var note = webkitNotifications.createNotification(iconUrl, | 19 icon: iconUrl, |
| 20 title, | 20 body: text, |
| 21 text); | 21 tag: tag |
| 22 } catch (exception) { | 22 }); |
| 23 sendResultToTest(-1); | 23 |
| 24 return; | 24 createNotificationHelper(notification, true); |
| 25 } | |
| 26 createNotificationHelper(note, replaceId, true); | |
| 27 } | 25 } |
| 28 | 26 |
| 29 // Cancels a notification with the given id. The notification must be showing, | 27 // Cancels a notification with the given id. The notification must be showing, |
| 30 // as opposed to waiting to be shown in the display queue. | 28 // as opposed to waiting to be shown in the display queue. |
| 31 // Returns '1' on success. | 29 // Returns '1' on success. |
| 32 function cancelNotification(id) { | 30 function cancelNotification(id) { |
| 33 if (id < 0 || id > g_notifications.length) { | 31 if (id < 0 || id > g_notifications.length) { |
| 34 var errorMsg = "Attempted to cancel notification with invalid ID.\n" + | 32 var errorMsg = "Attempted to cancel notification with invalid ID.\n" + |
| 35 "ID: " + id + "\n# of notifications: " + g_notifications.length; | 33 "ID: " + id + "\n# of notifications: " + g_notifications.length; |
| 36 sendResultToTest(errorMsg); | 34 sendResultToTest(errorMsg); |
| 37 } | 35 } |
| 38 g_notifications[id].onclose = function() { | 36 g_notifications[id].onclose = function() { |
| 39 sendResultToTest(1); | 37 sendResultToTest(1); |
| 40 } | 38 } |
| 41 g_notifications[id].cancel(); | 39 g_notifications[id].close(); |
| 42 } | 40 } |
| 43 | 41 |
| 44 // Requests permission for this origin to create notifications. | 42 // Requests permission for this origin to create notifications. |
| 45 function requestPermission() { | 43 function requestPermission() { |
| 46 window.webkitNotifications.requestPermission(onPermissionGranted); | 44 Notification.requestPermission(onPermissionGranted); |
| 47 sendResultToTest(1); | 45 sendResultToTest(1); |
| 48 } | 46 } |
| 49 | 47 |
| 50 // Waits for the permission to create notifications to be granted. | 48 // Waits for the permission to create notifications to be granted. |
| 51 function waitForPermissionGranted() { | 49 function waitForPermissionGranted() { |
| 52 if (g_permissionGranted) { | 50 if (g_permissionGranted) { |
| 53 sendResultToTest(1); | 51 sendResultToTest(1); |
| 54 } else { | 52 } else { |
| 55 setTimeout(waitForPermissionGranted, 50); | 53 setTimeout(waitForPermissionGranted, 50); |
| 56 } | 54 } |
| 57 } | 55 } |
| 58 | 56 |
| 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, replaceId, waitForDisplay) { | 66 function createNotificationHelper(note, waitForDisplay) { |
| 69 function sendNotificationIdToTest() { | 67 function sendNotificationIdToTest() { |
| 70 sendResultToTest(g_notifications.length - 1); | 68 sendResultToTest(g_notifications.length - 1); |
| 71 } | 69 } |
| 72 g_notifications.push(note); | 70 g_notifications.push(note); |
| 73 note.replaceId = replaceId; | |
| 74 if (waitForDisplay) | 71 if (waitForDisplay) |
| 75 note.ondisplay = sendNotificationIdToTest; | 72 note.onshow = sendNotificationIdToTest; |
| 76 note.onerror = function() { | 73 note.onerror = function() { |
| 77 sendResultToTest(-1); | 74 sendResultToTest(-1); |
| 78 } | 75 } |
| 79 | 76 |
| 80 note.show(); | |
| 81 if (!waitForDisplay) | 77 if (!waitForDisplay) |
| 82 sendNotificationIdToTest(); | 78 sendNotificationIdToTest(); |
| 83 } | 79 } |
| 84 | 80 |
| 85 // Sends a result back to the main test logic. | 81 // Sends a result back to the main test logic. |
| 86 function sendResultToTest(result) { | 82 function sendResultToTest(result) { |
| 87 // Convert the result to a string. | 83 // Convert the result to a string. |
| 88 var stringResult = "" + result; | 84 var stringResult = "" + result; |
| 89 if (typeof stringResult != "string") | 85 if (typeof stringResult != "string") |
| 90 stringResult = JSON.stringify(result); | 86 stringResult = JSON.stringify(result); |
| 91 window.domAutomationController.send(stringResult); | 87 window.domAutomationController.send(stringResult); |
| 92 } | 88 } |
| 93 | 89 |
| 94 </script> | 90 </script> |
| 95 | 91 |
| 96 <body> | 92 <body> |
| 97 This page is used for testing HTML5 notifications. | 93 This page is used for testing HTML5 notifications. |
| 98 </body> | 94 </body> |
| 99 </html> | 95 </html> |
| OLD | NEW |