Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / chrome / test / data / notifications / notification_tester.html
blob5bbe0f74126f36f9419a48e776fd7111e99380eb
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 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;
26 createNotificationHelper(note, replaceId, true);
29 // Creates an HTML notification with a given content url.
30 // Returns an id for the notification, which can be used to cancel it with
31 // |cancelNotification|. If two notifications are created with the same
32 // replaceId, the second one should replace the first. If the notification
33 // cannot be created, this returns -1.
34 // If |waitForDisplay| is true, a response will not be sent until the
35 // notification is actually displayed.
36 function createHTMLNotification(contentUrl, replaceId, waitForDisplay) {
37 try {
38 var note = webkitNotifications.createHTMLNotification(contentUrl);
39 } catch (exception) {
40 sendResultToTest(-1);
41 return;
43 createNotificationHelper(note, replaceId, waitForDisplay);
46 // Cancels a notification with the given id. The notification must be showing,
47 // as opposed to waiting to be shown in the display queue.
48 // Returns '1' on success.
49 function cancelNotification(id) {
50 if (id < 0 || id > g_notifications.length) {
51 var errorMsg = "Attempted to cancel notification with invalid ID.\n" +
52 "ID: " + id + "\n# of notifications: " + g_notifications.length;
53 sendResultToTest(errorMsg);
55 g_notifications[id].onclose = function() {
56 sendResultToTest(1);
58 g_notifications[id].cancel();
61 // Requests permission for this origin to create notifications.
62 function requestPermission() {
63 window.webkitNotifications.requestPermission(onPermissionGranted);
64 sendResultToTest(1);
67 // Waits for the permission to create notifications to be granted.
68 function waitForPermissionGranted() {
69 if (g_permissionGranted) {
70 sendResultToTest(1);
71 } else {
72 setTimeout(waitForPermissionGranted, 50);
76 // Callback for requesting notification privileges.
77 function onPermissionGranted() {
78 g_permissionGranted = true;
81 // Helper function that adds a notification to |g_notifications| and shows
82 // it. The index of the notification is sent back to the test, or -1 is sent
83 // back on error. If |waitForDisplay| is true, the response will not be sent
84 // until the notification is actually displayed.
85 function createNotificationHelper(note, replaceId, waitForDisplay) {
86 function sendNotificationIdToTest() {
87 sendResultToTest(g_notifications.length - 1);
89 g_notifications.push(note);
90 note.replaceId = replaceId;
91 if (waitForDisplay)
92 note.ondisplay = sendNotificationIdToTest;
93 note.onerror = function() {
94 sendResultToTest(-1);
97 note.show();
98 if (!waitForDisplay)
99 sendNotificationIdToTest();
102 // Sends a result back to the main test logic.
103 function sendResultToTest(result) {
104 // Convert the result to a string.
105 var stringResult = "" + result;
106 if (typeof stringResult != "string")
107 stringResult = JSON.stringify(result);
108 window.domAutomationController.send(stringResult);
111 </script>
113 <body>
114 This page is used for testing HTML5 notifications.
115 </body>
116 </html>