summaryrefslogtreecommitdiff
path: root/Source/WebKit2/UIProcess/API/qt/tests/qmltests/WebView/tst_download.qml
blob: f8324a6067bd6b62b3074f4ff920bcb4987d3bb8 (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
import QtQuick 2.0
import QtTest 1.0
import QtWebKit 3.0
import QtWebKit.experimental 1.0

WebView {
    id: webView
    width: 200
    height: 200

    property int expectedLength: 0
    property int totalBytes: 0

    signal downloadFinished()

    SignalSpy {
        id: spy
        target: experimental
        signalName: "downloadRequested"
    }

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

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

    TestCase {
        name: "WebViewDownload"

        // 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() {
            spy.clear()
            downloadFinishedSpy.clear()
            expectedLength = 0
        }

        function test_downloadRequest() {
            compare(spy.count, 0)
            webView.load(Qt.resolvedUrl("../common/download.zip"))
            spy.wait()
            compare(spy.count, 1)
        }

        function test_expectedLength() {
            compare(spy.count, 0)
            webView.load(Qt.resolvedUrl("../common/download.zip"))
            spy.wait()
            compare(spy.count, 1)
            compare(expectedLength, 325)
        }

        function test_succeeded() {
            compare(spy.count, 0)
            webView.load(Qt.resolvedUrl("../common/download.zip"))
            spy.wait()
            compare(spy.count, 1)
            downloadFinishedSpy.wait()
            compare(totalBytes, expectedLength)
        }
    }
}