summaryrefslogtreecommitdiff
path: root/tests/webkitqml/qmltests/DesktopBehavior/tst_navigationRequested.qml
blob: 2aefce53730812dd87b67629c8e392638d1b75b4 (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
import QtQuick 2.0
import QtTest 1.0
import QtWebKit 3.0
import QtWebKit.experimental 1.0
import "../common"

Item {
    property int expectedLength: 0
    property int totalBytes: 0
    property bool shouldDownload: false
    property url beginUrl: Qt.resolvedUrl("../common/test2.html")
    property url endUrl: Qt.resolvedUrl("../common/test1.html")

    TestWebView {
        id: webView
        width: 200
        height: 200

        signal downloadFinished()

        onNavigationRequested: {
            if (shouldDownload)
                request.action = WebViewExperimental.DownloadRequest
            else if (request.mouseButton == Qt.MiddleButton && request.keyboardModifiers & Qt.ControlModifier) {
                otherWebView.url = request.url
                request.action = WebView.IgnoreRequest
            }
        }

        experimental.onDownloadRequested: {
            download.target = downloadItem
            expectedLength = downloadItem.expectedContentLength
            downloadItem.destinationPath = downloadItem.suggestedFilename
            downloadItem.start()
        }

        Connections {
            id: download
            ignoreUnknownSignals: true
            onSucceeded: {
                totalBytes = download.target.totalBytesReceived
                webView.downloadFinished()
            }
        }
    }

    TestWebView {
        id: otherWebView
    }

    SignalSpy {
        id: downloadSpy
        target: webView.experimental
        signalName: "downloadRequested"
    }

    SignalSpy {
        id: downloadFinishedSpy
        target: webView
        signalName: "downloadFinished"
    }

    TestCase {
        name: "DesktopWebViewNavigationRequested"

        // Delayed windowShown to workaround problems with Qt5 in debug mode.
        when: false
        Timer {
            running: parent.windowShown
            repeat: false
            interval: 1
            onTriggered: parent.when = true
        }

        function init() {
            downloadSpy.clear()
            downloadFinishedSpy.clear()
            shouldDownload = false
        }

        function test_usePolicy() {
            webView.url = beginUrl
            verify(webView.waitForLoadSucceeded())
            mouseClick(webView, 100, 100, Qt.LeftButton)
            verify(webView.waitForLoadSucceeded())
            compare(webView.title, "Test page 1")
            compare(webView.url, endUrl)
        }

        function test_ignorePolicy() {
            webView.url = beginUrl
            verify(webView.waitForLoadSucceeded())
            mouseClick(webView, 100, 100, Qt.MiddleButton, Qt.ControlModifier)
            verify(otherWebView.waitForLoadSucceeded())
            verify(webView.loadStatus == null)
            compare(webView.url, beginUrl)
            compare(otherWebView.title, "Test page 1")
            compare(otherWebView.url, endUrl)
        }

        function test_downloadPolicy() {
            webView.url = beginUrl
            verify(webView.waitForLoadSucceeded())
            downloadSpy.clear()
            downloadFinishedSpy.clear()
            expectedLength = 0
            shouldDownload = true
            mouseClick(webView, 100, 100, Qt.LeftButton)
            downloadSpy.wait()
            compare(downloadSpy.count, 1)
            downloadFinishedSpy.wait()
            compare(downloadFinishedSpy.count, 1)
            compare(totalBytes, expectedLength)
        }
    }
}