<!DOCTYPE html>
<script src="/resources/js-test-pre.js"></script>
<script src="/resources/notifications-test-pre.js"></script>
<p id="description"></p>
<div id="console"></div>
<script>
if (window.testRunner)
	testRunner.waitUntilDone();

description("This test looks for the dispatch of the onshow event handler.");

var n = null;
function part1() {
	shouldBeEqualToString("Notification.permission", "default");

	n = new Notification("title 1");
	n.onshow = function() {
		testFailed("onshow event handler should not be called");
		setTimeout(part2, 0);
	};
	n.onerror = function() {
		testPassed("onerror event handler was called");
		setTimeout(part2, 0);
	};
}

function part2() {
	debug("Granting permission to " + testURL);
	testRunner.grantWebNotificationPermission(testURL);
	shouldBeEqualToString("Notification.permission", "granted");

	n = new Notification("title 2");
	n.onshow = function() {
		testPassed("onshow event handler was called");
		setTimeout(part3, 0);
	};
	n.onerror = function() {
		testFailed("onerror event handler should not be called");
		setTimeout(part3, 0);
	};
}

function part3() {
	debug("Denying permission to " + testURL);
	testRunner.denyWebNotificationPermission(testURL);
	// To reduce fingerprinting, Notification.permission returns "prompt" instead of "deny" 
	// since the test has not tried to request permission to use the Notifications API.
	shouldBeEqualToString("Notification.permission", "default");

	n = new Notification("title 3");
	n.onshow = function() {
		testFailed("onshow event handler should not be called");
		setTimeout(part4, 0);
	};
	n.onerror = function() {
		testPassed("onerror event handler was called");
		setTimeout(part4, 0);
	};
}

function part4() {
	debug("Denying permission to " + testURL);
	testRunner.denyWebNotificationPermission(testURL);
	internals.withUserGesture(() => {
		window.Notification.requestPermission().then((result)=> {
			shouldBeEqualToString("Notification.permission", "denied");

			n = new Notification("title 3");
			n.onshow = function() {
				testFailed("onshow event handler should not be called");
				setTimeout(part5, 0);
			};
			n.onerror = function() {
				testPassed("onerror event handler was called");
				setTimeout(part5, 0);
			};
		});
	});
}

function part5() {
	debug("Resetting all permissions");
	testRunner.removeAllWebNotificationPermissions();
	shouldBeEqualToString("Notification.permission", "default");
	testCompleted();
}

part1();
</script>
