- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / notifications / notification_tester.html
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>
7
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;
12
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) {
18   try {
19     var note = webkitNotifications.createNotification(iconUrl,
20                                                       title,
21                                                       text);
22   } catch (exception)  {
23     sendResultToTest(-1);
24     return;
25   }
26   createNotificationHelper(note, replaceId, true);
27 }
28
29 // Cancels a notification with the given id. The notification must be showing,
30 // as opposed to waiting to be shown in the display queue.
31 // Returns '1' on success.
32 function cancelNotification(id) {
33   if (id < 0 || id > g_notifications.length) {
34     var errorMsg = "Attempted to cancel notification with invalid ID.\n" +
35         "ID: " + id + "\n# of notifications: " + g_notifications.length;
36     sendResultToTest(errorMsg);
37   }
38   g_notifications[id].onclose = function() {
39     sendResultToTest(1);
40   }
41   g_notifications[id].cancel();
42 }
43
44 // Requests permission for this origin to create notifications.
45 function requestPermission() {
46   window.webkitNotifications.requestPermission(onPermissionGranted);
47   sendResultToTest(1);
48 }
49
50 // Waits for the permission to create notifications to be granted.
51 function waitForPermissionGranted() {
52   if (g_permissionGranted) {
53     sendResultToTest(1);
54   } else {
55     setTimeout(waitForPermissionGranted, 50);
56   }
57 }
58
59 // Callback for requesting notification privileges.
60 function onPermissionGranted() {
61   g_permissionGranted = true;
62 }
63
64 // 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
66 // back on error. If |waitForDisplay| is true, the response will not be sent
67 // until the notification is actually displayed.
68 function createNotificationHelper(note, replaceId, waitForDisplay) {
69   function sendNotificationIdToTest() {
70     sendResultToTest(g_notifications.length - 1);
71   }
72   g_notifications.push(note);
73   note.replaceId = replaceId;
74   if (waitForDisplay)
75     note.ondisplay = sendNotificationIdToTest;
76   note.onerror = function() {
77     sendResultToTest(-1);
78   }
79
80   note.show();
81   if (!waitForDisplay)
82     sendNotificationIdToTest();
83 }
84
85 // Sends a result back to the main test logic.
86 function sendResultToTest(result) {
87   // Convert the result to a string.
88   var stringResult = "" + result;
89   if (typeof stringResult != "string")
90     stringResult = JSON.stringify(result);
91   window.domAutomationController.send(stringResult);
92 }
93
94 </script>
95
96 <body>
97 This page is used for testing HTML5 notifications.
98 </body>
99 </html>