Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / data / notifications / notification_tester.html
blob20de190962ee4f1e2b6d12586a606e3223c07481
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. Immediately
43 // sends the result without waiting for callbacks to complete.
44 function requestPermissionAndRespond() {
45 Notification.requestPermission(permissionCallbackNoResponse);
46 sendResultToTest('requested');
49 // Requests permission for this origin to create notifications. The result will
50 // be sent from the callback (permissionCallbackWithResponse);
51 function requestPermission() {
52 Notification.requestPermission(permissionCallbackWithResponse);
55 // Callback for requestPermissionAndRespond. Will send the permission status to
56 // the test driver.
57 function permissionCallbackWithResponse(permissionStatus) {
58 if (permissionStatus === 'granted')
59 sendResultToTest('request-callback-granted');
60 else if (permissionStatus === 'denied')
61 sendResultToTest('request-callback-denied');
62 else if (permissionStatus === 'default')
63 sendResultToTest('request-callback-default');
66 // Callback for requesting notification privileges.
67 function permissionCallbackNoResponse() {
68 g_permissionGranted = true;
71 // Helper function that adds a notification to |g_notifications| and shows
72 // it. The index of the notification is sent back to the test, or -1 is sent
73 // back on error. If |waitForDisplay| is true, the response will not be sent
74 // until the notification is actually displayed.
75 function createNotificationHelper(note, waitForDisplay) {
76 function sendNotificationIdToTest() {
77 sendResultToTest(g_notifications.length - 1);
79 g_notifications.push(note);
80 if (waitForDisplay)
81 note.onshow = sendNotificationIdToTest;
82 note.onerror = function() {
83 sendResultToTest(-1);
86 if (!waitForDisplay)
87 sendNotificationIdToTest();
90 // Sends a result back to the main test logic.
91 function sendResultToTest(result) {
92 // Convert the result to a string.
93 var stringResult = "" + result;
94 if (typeof stringResult != "string")
95 stringResult = JSON.stringify(result);
96 window.domAutomationController.send(stringResult);
99 </script>
101 <body>
102 This page is used for testing HTML5 notifications.
103 </body>
104 </html>