chromium/third_party/blink/web_tests/screen_orientation/page-visibility.html

<!DOCTYPE html>
<html>
<body>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/visibility.js"></script>
<iframe src='about:blank'></iframe>
<script>

var eventVisibleTest = async_test("Test that a change event is fired when the page is visible.");
var noEventHiddenTest = async_test("Test that change event is not fired when the page is not visible.");
var orientationUnchangeHiddenTest = async_test("Test that screen.orientation keeps returning the same orientation when the page is not visible.");
var orientationUpdateVisibleTest = async_test("Test that screen.orientation is updated once the page is visible again.");
var frameEventsTest = async_test("Test that the iframe got as many events as the main frame.");

var orientationChangeContinuation = null;
var orientationChangeEventListenerCalls = 0;
var orientationChangeEventListenerCallsForFrame = 0;

screen.orientation.addEventListener('change', function() {
    orientationChangeEventListenerCalls++;
    if (orientationChangeEventContinuation) {
        setTimeout(orientationChangeEventContinuation);
        orientationChangeEventContinuation = null;
    }
});

window.frames[0].screen.orientation.addEventListener('change', function() {
    orientationChangeEventListenerCallsForFrame++;
});

function runEventVisibleTest() {
    eventVisibleTest.step(function() {
        assert_false(document.hidden);
    });

    orientationChangeEventContinuation = function() {
        eventVisibleTest.step(function() {
            assert_equals(orientationChangeEventListenerCalls, 1);
            assert_equals(screen.orientation.type, "landscape-primary");
        });
        eventVisibleTest.done();

        setMainWindowHidden(true).then(runNoEventHiddenTest);
    };

    if (window.testRunner)
        testRunner.setMockScreenOrientation("landscape-primary");
}

  function runNoEventHiddenTest() {
    noEventHiddenTest.step(function() {
        assert_true(document.hidden);
    });

    noEventHiddenTest.step(function() {
        assert_equals(orientationChangeEventListenerCalls, 1);
    });

    if (window.testRunner)
        testRunner.setMockScreenOrientation("portrait-primary");

    noEventHiddenTest.done();

    runOrientationUnchangeHiddenTest();
}

function runOrientationUnchangeHiddenTest() {
    orientationUnchangeHiddenTest.step(function() {
        assert_equals(screen.orientation.type, "landscape-primary");
    });
    orientationUnchangeHiddenTest.done();

    runOrientationUpdateVisibleTest();
}

function runOrientationUpdateVisibleTest() {
    orientationChangeEventContinuation = function() {
        orientationUpdateVisibleTest.step(function() {
            assert_false(document.hidden);
            // A change event should have been fired.
            assert_equals(orientationChangeEventListenerCalls, 2);
            // Should keep returning the start returning the orientation value.
            assert_equals(screen.orientation.type, "portrait-primary");
        });

        orientationUpdateVisibleTest.done();

        runFrameEventsTest();
    };

    setMainWindowHidden(false);
}

function runFrameEventsTest() {
    frameEventsTest.step(function() {
        assert_equals(orientationChangeEventListenerCallsForFrame, orientationChangeEventListenerCalls);
    });
    frameEventsTest.done();
}

runEventVisibleTest();

</script>
</body>
</html>