summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/screen-orientation/page-visibility-manual.html
blob: 95633ba1240c23f32e3bed55a8f1ded13ce09709 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
<!DOCTYPE html>
<html>
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<meta name='flags' content='interact'>
<meta name="timeout" content="long">
<p>Switch the page to background, then switch back in a minute.</p>
<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++;
});

document.addEventListener("visibilitychange", function () {
    if(document.hidden)
        runNoEventHiddenTest();
    else
        runOrientationUpdateVisibleTest();
});

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

    screen.orientation.lock("landscape-primary").then(function() {}, function() {});

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

    };
}

function runNoEventHiddenTest() {

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

    screen.orientation.lock("portrait-primary").then(function() {}, function() {});

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

    runOrientationUnchangeHiddenTest();
}

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

}

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();
    };
}

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

runEventVisibleTest();

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