summaryrefslogtreecommitdiff
path: root/Source/WebKit2/UIProcess/API/qt/tests/qmltests/WebView/tst_fitToView.qml
blob: 01192a8af388d5173948ca950fd88dd371a1d426 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import QtQuick 2.0
import QtTest 1.0
import QtWebKit 3.0
import QtWebKit.experimental 1.0
import Test 1.0
import "../common"

Item {
    TestWebView {
        id: webView
        width: 480
        height: 720

        property variant result

        property variant content: "data:text/html," +
            "<head>" +
            "    <meta name='viewport' content='width=device-width'>" +
            "</head>" +
            "<body style='margin: 0px'>" +
            "    <div id='target' style='display:none; width:960px; height:1440px;'></div>" +
            "</body>"

        signal resultReceived
    }

    SignalSpy {
        id: resultSpy
        target: webView
        signalName: "resultReceived"
    }

    SignalSpy {
        id: scaleSpy
        target: webView.experimental.test
        signalName: "contentsScaleCommitted"
    }

    TestCase {
        name: "FitToView"
        when: windowShown

        property variant test: webView.experimental.test

        function init() {
            resultSpy.clear()
            scaleSpy.clear()
        }

        function run(signalSpy, script) {
            signalSpy.clear();
            var result;
             webView.experimental.evaluateJavaScript(
                script, function(value) { webView.resultReceived(); result = value });
            signalSpy.wait();
            return result;
        }

        function documentSize() {
            return run(resultSpy, "document.width + 'x' + document.height");
        }

        function setDisplay(id, value) {
            // When changing to/from 'none' to 'block', this will result in a
            // contentsScaleCommitted scale, even if it results in  the same
            // scale, making it possible to check whether user interaction
            // blocks fit-to-view or not.
            run(scaleSpy, "document.getElementById('" + id + "').style.display = '" + value + "';");
        }

        function test_basic() {
            webView.url = webView.content
            verify(webView.waitForViewportReady())

            compare(documentSize(), "480x720")
            compare(test.contentsScale, 1.0)

            setDisplay("target", "block")
            compare(documentSize(), "960x1440")
            compare(test.contentsScale, 0.5)

            // Add user interaction.
            test.touchTap(webView, 10, 10)

            // We are no longer within valid bounds after this change
            // so we have to change our scale back to 1.0.
            setDisplay("target", "none")
            compare(documentSize(), "480x720")
            compare(test.contentsScale, 1.0)

            // We had user interaction, size should change but not scale.
            setDisplay("target", "block")
            compare(documentSize(), "960x1440")
            compare(test.contentsScale, 1.0)
        }

        function test_localPageDeviceWidth() {
            webView.url = "about:blank"
            verify(webView.waitForLoadSucceeded())

            webView.url = "../common/test5.html"
            verify(webView.waitForLoadSucceeded())
            compare(test.contentsScale, 0.5)

            // Add user interaction.
            test.touchTap(webView, 10, 10)

            webView.reload()
            verify(webView.waitForLoadSucceeded())
            // The page should still fit to view after a reload
            compare(test.contentsScale, 0.5)
        }

        function test_localPageInitialScale() {
            webView.url = "about:blank"
            verify(webView.waitForLoadSucceeded())

            webView.url = "../common/test4.html"
            verify(webView.waitForLoadSucceeded())

            compare(test.contentsScale, 2.0)

            // Add user interaction.
            test.touchTap(webView, 10, 10)

            webView.reload()
            verify(webView.waitForLoadSucceeded())
            compare(test.contentsScale, 2.0)
        }
    }
}