Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / chrome / test / data / notifications / notification_tester.html
blobabc17fd0721e85051f25c805db92022fb236b890
1 <html>
2 <!--notification_tester.html
3 Script with javascript functions for creating and canceling notifications.
4 Also can be used to request permission for notifications.
5 -->
6 <script>
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 tag.
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 // tag, the second one should replace the first.
17 function createNotification(iconUrl, title, text, tag) {
18 var notification = new Notification(title, {
19 icon: iconUrl,
20 body: text,
21 tag: tag
22 });
24 createNotificationHelper(notification, true);
27 // Cancels a notification with the given id. The notification must be showing,
28 // as opposed to waiting to be shown in the display queue.
29 // Returns '1' on success.
30 function cancelNotification(id) {
31 if (id < 0 || id > g_notifications.length) {
32 var errorMsg = "Attempted to cancel notification with invalid ID.\n" +
33 "ID: " + id + "\n# of notifications: " + g_notifications.length;
34 sendResultToTest(errorMsg);
36 g_notifications[id].onclose = function() {
37 sendResultToTest(1);
39 g_notifications[id].close();
42 // Requests permission for this origin to create notifications.
43 function requestPermission() {
44 Notification.requestPermission(onPermissionGranted);
45 sendResultToTest(1);
48 // Waits for the permission to create notifications to be granted.
49 function waitForPermissionGranted() {
50 if (g_permissionGranted) {
51 sendResultToTest(1);
52 } else {
53 setTimeout(waitForPermissionGranted, 50);
57 // Callback for requesting notification privileges.
58 function onPermissionGranted() {
59 g_permissionGranted = true;
62 // Helper function that adds a notification to |g_notifications| and shows
63 // it. The index of the notification is sent back to the test, or -1 is sent
64 // back on error. If |waitForDisplay| is true, the response will not be sent
65 // until the notification is actually displayed.
66 function createNotificationHelper(note, waitForDisplay) {
67 function sendNotificationIdToTest() {
68 sendResultToTest(g_notifications.length - 1);
70 g_notifications.push(note);
71 if (waitForDisplay)
72 note.onshow = sendNotificationIdToTest;
73 note.onerror = function() {
74 sendResultToTest(-1);
77 if (!waitForDisplay)
78 sendNotificationIdToTest();
81 // Sends a result back to the main test logic.
82 function sendResultToTest(result) {
83 // Convert the result to a string.
84 var stringResult = "" + result;
85 if (typeof stringResult != "string")
86 stringResult = JSON.stringify(result);
87 window.domAutomationController.send(stringResult);
90 </script>
92 <body>
93 This page is used for testing HTML5 notifications.
94 </body>
95 </html>