| <html> |
| <!--notification_tester.html |
| Script with javascript functions for creating and canceling notifications. |
| Also can be used to request permission for notifications. |
| --> |
| <script> |
| |
| // Array of all notifications this page has created. |
| var g_notifications = []; |
| // Whether the site has requested and been granted permission. |
| var g_permissionGranted = false; |
| |
| // Creates a notification with a iconUrl, title, text, and tag. |
| // Returns an id for the notification, which can be used to cancel it with |
| // |cancelNotification|. If two notifications are created with the same |
| // tag, the second one should replace the first. |
| function createNotification(iconUrl, title, text, tag, onclick) { |
| var notification = new Notification(title, { |
| icon: iconUrl, |
| body: text, |
| tag: tag |
| }); |
| notification.onclick = onclick; |
| |
| createNotificationHelper(notification, true); |
| } |
| |
| // Cancels a notification with the given id. The notification must be showing, |
| // as opposed to waiting to be shown in the display queue. |
| // Returns '1' on success. |
| function cancelNotification(id) { |
| if (id < 0 || id > g_notifications.length) { |
| var errorMsg = "Attempted to cancel notification with invalid ID.\n" + |
| "ID: " + id + "\n# of notifications: " + g_notifications.length; |
| sendResultToTest(errorMsg); |
| } |
| g_notifications[id].onclose = function() { |
| sendResultToTest(1); |
| } |
| g_notifications[id].close(); |
| } |
| |
| // Requests permission for this origin to create notifications. Immediately |
| // sends the result without waiting for callbacks to complete. |
| function requestPermissionAndRespond() { |
| Notification.requestPermission(permissionCallbackNoResponse); |
| sendResultToTest('requested'); |
| } |
| |
| // Requests permission for this origin to create notifications. The result will |
| // be sent from the callback (permissionCallbackWithResponse); |
| function requestPermission() { |
| Notification.requestPermission(permissionCallbackWithResponse); |
| } |
| |
| // Checks the notification permission status. The result will be sent directly |
| // via permissionCallbackWithResponse. |
| function queryPermissionStatus() { |
| sendResultToTest(Notification.permission); |
| } |
| |
| // Callback for requestPermissionAndRespond. Will send the permission status to |
| // the test driver. |
| function permissionCallbackWithResponse(permissionStatus) { |
| if (permissionStatus === 'granted') |
| sendResultToTest('request-callback-granted'); |
| else if (permissionStatus === 'denied') |
| sendResultToTest('request-callback-denied'); |
| else if (permissionStatus === 'default') |
| sendResultToTest('request-callback-default'); |
| } |
| |
| // Callback for requesting notification privileges. |
| function permissionCallbackNoResponse() { |
| g_permissionGranted = true; |
| } |
| |
| // Helper function that adds a notification to |g_notifications| and shows |
| // it. The index of the notification is sent back to the test, or -1 is sent |
| // back on error. If |waitForDisplay| is true, the response will not be sent |
| // until the notification is actually displayed. |
| function createNotificationHelper(note, waitForDisplay) { |
| function sendNotificationIdToTest() { |
| sendResultToTest(g_notifications.length - 1); |
| } |
| g_notifications.push(note); |
| if (waitForDisplay) |
| note.onshow = sendNotificationIdToTest; |
| note.onerror = function() { |
| sendResultToTest(-1); |
| } |
| |
| if (!waitForDisplay) |
| sendNotificationIdToTest(); |
| } |
| |
| // Sends a result back to the main test logic. |
| function sendResultToTest(result) { |
| // Convert the result to a string. |
| var stringResult = "" + result; |
| if (typeof stringResult != "string") |
| stringResult = JSON.stringify(result); |
| window.domAutomationController.send(stringResult); |
| } |
| |
| </script> |
| |
| <body> |
| This page is used for testing HTML5 notifications. |
| </body> |
| </html> |