1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
<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 replaceId.
// Returns an id for the notification, which can be used to cancel it with
// |cancelNotification|. If two notifications are created with the same
// replaceId, the second one should replace the first.
function createNotification(iconUrl, title, text, replaceId) {
try {
var note = webkitNotifications.createNotification(iconUrl,
title,
text);
} catch (exception) {
sendResultToTest(-1);
return;
}
createNotificationHelper(note, replaceId, 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].cancel();
}
// Requests permission for this origin to create notifications.
function requestPermission() {
window.webkitNotifications.requestPermission(onPermissionGranted);
sendResultToTest(1);
}
// Waits for the permission to create notifications to be granted.
function waitForPermissionGranted() {
if (g_permissionGranted) {
sendResultToTest(1);
} else {
setTimeout(waitForPermissionGranted, 50);
}
}
// Callback for requesting notification privileges.
function onPermissionGranted() {
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, replaceId, waitForDisplay) {
function sendNotificationIdToTest() {
sendResultToTest(g_notifications.length - 1);
}
g_notifications.push(note);
note.replaceId = replaceId;
if (waitForDisplay)
note.ondisplay = sendNotificationIdToTest;
note.onerror = function() {
sendResultToTest(-1);
}
note.show();
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>
|