tenfourfox/dom/tests/mochitest/notification/test_notification_resend.html
Cameron Kaiser c9b2922b70 hello FPR
2017-04-19 00:56:45 -07:00

209 lines
7.2 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<title>Testing mozResendAllNotifications() resend behavior for Apps</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="MockServices.js"></script>
<script type="text/javascript" src="NotificationTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=967475">Bug 967475</a>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
<script type="text/javascript">
var info = NotificationTest.info;
var now = new Date().getTime();
var notifications = [];
var manifestURL;
SimpleTest.requestFlakyTimeout("untriaged");
var steps = [
function (done) {
info("Set manifestURL");
var request = window.navigator.mozApps.getSelf();
request.onsuccess = function() {
if (request.result) {
manifestURL = request.result.manifestURL;
} else {
manifestURL = window.location.origin;
}
info("Value of manifestURL is: " + manifestURL);
done();
};
},
function (done) {
if (window.Notification) {
NotificationTest.fakeApp(manifestURL);
SpecialPowers.pushPrefEnv({"set": [
["dom.ignore_webidl_scope_checks", true],
]}, done);
} else {
ok(true, "Notifications are not enabled on the platform.");
done();
}
},
function (done) {
info("Test that we have mozChromeNotifications API");
ok(('mozChromeNotifications' in navigator), "should have mozChromeNotifications API");
ok(('mozResendAllNotifications' in navigator.mozChromeNotifications), "should have mozResendAllNotifications()");
done();
},
function (done) {
info("Making sure we have no previous notification pending");
var promise = Notification.get();
promise.then(function (notifications) {
is(notifications.length, 0, "notifications are all cleaned");
done();
});
},
function (done) {
info("Sending one notification");
var notif = new Notification("title");
ok(notif, "Notification object is valid");
notifications.push(notif);
var promise = Notification.get();
promise.then(function (notifications) {
is(notifications.length, 1, "one notification has been sent");
done();
});
},
function (done) {
info("Trying to resend the notification");
var notif = notifications.pop();
notif.onclose = function() {
done();
};
navigator.mozChromeNotifications.mozResendAllNotifications(function(number) {
is(number, 1, "One notification resent");
notif.close();
});
},
function (done) {
info("Sending one non-resendable notification");
var behavior = {
showOnlyOnce: true
};
var notif = new Notification("title", { mozbehavior: behavior });
ok(notif, "Notification object is valid");
notifications.push(notif);
var promise = Notification.get();
promise.then(function (notifications) {
is(notifications.length, 1, "one notification has been sent");
done();
});
},
function (done) {
info("Trying to resend non-resendable notification");
var notif = notifications.pop();
notif.onclose = function() {
done();
};
navigator.mozChromeNotifications.mozResendAllNotifications(function(number) {
is(number, 0, "One notification not resent");
notif.close();
});
},
function (done) {
info("Sending two notifications, closing one");
var notif1 = new Notification("title1");
ok(notif1, "Notification object is valid");
notif1.onclose = function() {
done();
};
var payload = NotificationTest.payload;
var notif2 = new Notification("Title2", payload);
ok(notif2, "Notification object is valid");
notifications.push(notif2);
var promise = Notification.get();
promise.then(function (notifications) {
is(notifications.length, 2, "two notifications have been sent");
notif1.close();
});
},
function (done) {
info("Checking if only notif2 is resent");
navigator.mozChromeNotifications.mozResendAllNotifications(function(number) {
is(number, 1, "One notification resent");
var promise = Notification.get();
promise.then(function (notifications) {
is(notifications.length, 1, "one notification still available");
is(notifications[0].title, "Title2", "notification title is 'Title2'");
done();
});
});
},
function (done) {
info("Checking ShowAppNotification behavior");
var notif2 = notifications.pop();
notif2.onclose = function() {
done();
};
navigator.mozChromeNotifications.mozResendAllNotifications(function(number) {
is(number, 1, "One notification resent");
var appNotifs = MockServices.activeAppNotifications;
var alertNotifs = MockServices.activeAlertNotifications;
var nbAppNotifs = Object.keys(appNotifs).length;
var nbAlertNotifs = Object.keys(alertNotifs).length;
is(nbAppNotifs, 1, "AlertsServices has one app notification");
is(nbAlertNotifs, 1, "AlertsServices has one alert notification");
var uuidRegEx = /[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}/;
var notif = appNotifs[Object.keys(appNotifs)[0]];
ok(notif, "Notification object is valid");
ok((typeof notif.observer === "object"), "Notification observer is valid");
is(notif.title, "Title2", "Notification title is valid: " + notif.title);
is(notif.text, "Body", "Notification body is valid: " + notif.text);
is(notif.manifestURL, manifestURL, "Notification manifest URL is valid: " + notif.manifestURL);
is(notif.imageURL, "icon.jpg", "Notification icon URL is valid: " + notif.imageURL);
is(notif.lang, "en-US", "Notification lang is valid: " + notif.lang);
is(notif.id, notif.manifestURL + "#tag:" + notif.tag, "Notification id is valid: " + notif.id);
ok(notif.dbId.match(uuidRegEx), "Notification dbId is valid: " + notif.dbId);
is(notif.dir, "ltr", "Notification dir is valid: " + notif.dir);
is(notif.tag, "fakeTag", "Notification tag is valid: " + notif.tag);
ok((notif.timestamp >= now), "Notification timestamp is valid: (" + notif.timestamp + " >= " + now + ")");
var scContainer =
SpecialPowers.Cc["@mozilla.org/docshell/structured-clone-container;1"].
createInstance(SpecialPowers.Ci.nsIStructuredCloneContainer);
scContainer.initFromBase64(notif.data, 4);
var dataObj = scContainer.deserializeToVariant().SpecialPowers_wrappedObject;
ok(NotificationTest.customDataMatches(dataObj),
"Notification data is valid");
notif2.close();
});
},
];
MockServices.register();
NotificationTest.run(steps, function () {
NotificationTest.unfakeApp();
MockServices.unregister();
});
</script>
</body>
</html>