blob: 0084edf225fe13c6f9d2e88e1e9c9ff03db181fb [file] [log] [blame]
<html>
<body onload='startTest()'>
<script src="../../resources/js-test.js"></script>
<script>
description("This test checks that Page Visibility state values are correct and the event changes are fired correctly.");
var jsTestIsAsync = true;
function makePageVisible() {
if (window.testRunner)
testRunner.setPageVisibility("visible");
}
function makePageHidden() {
if (window.testRunner)
testRunner.setPageVisibility("hidden");
}
function makePagePrerender() {
if (window.testRunner)
testRunner.setPageVisibility("prerender");
}
function checkIsPageVisible() {
shouldBeEqualToString("document.visibilityState", "visible");
shouldBeFalse("document.hidden");
}
function checkIsPageHidden() {
shouldBeEqualToString("document.visibilityState", "hidden");
shouldBeTrue("document.hidden");
}
function checkIsPagePrerendering() {
shouldBeEqualToString("document.visibilityState", "prerender");
shouldBeTrue("document.hidden");
}
// We will try to change the visibility states as:
// 0 - visible. (Initial - i.e. on load).
// 1 - hidden (should fire event).
// 2 - hidden (no event).
// 3 - prerender (should fire event).
// 4 - visible (should fire event).
var numVisibilityChanges = 0;
function startTest() {
document.addEventListener(
"visibilitychange", onVisibilityChange, false);
checkIsPageVisible();
numVisibilityChanges++;
makePageHidden();
}
function onVisibilityChange() {
if (numVisibilityChanges == 1) {
checkIsPageHidden();
numVisibilityChanges++;
makePageHidden();
checkIsPageHidden();
numVisibilityChanges++;
makePagePrerender();
return;
} else if (numVisibilityChanges == 2) {
testFailed("Invalid event fired on same state change.");
finishJSTest();
return;
} else if (numVisibilityChanges == 3) {
checkIsPagePrerendering();
numVisibilityChanges++;
makePageVisible();
return;
} else if (numVisibilityChanges == 4) {
checkIsPageVisible();
numVisibilityChanges++;
finishJSTest();
return;
} else {
testFailed("Too many visibility transitions");
finishJSTest();
return;
}
}
</script>
</body>
</html>