summaryrefslogtreecommitdiffstats
path: root/tests/auto/quick/qmltests/data/tst_save.qml
blob: 08d99022ae299f03fb792b0b6bf5823b0ef28b8c (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import QtQuick
import QtTest
import QtWebEngine
import Test.util

TestWebEngineView {
    id: webEngineView
    width: 200
    height: 200
    profile: testSaveProfile

    property url downloadUrl: ""
    property int totalBytes: 0
    property int receivedBytes: 0
    property string downloadDir: ""
    property string downloadFileName: ""
    property bool isSavePageDownload: false
    property var downloadState: []
    property int savePageFormat: WebEngineDownloadRequest.MimeHtmlSaveFormat;

    TempDir {
        id: tempDir
    }

    SignalSpy {
        id: downLoadRequestedSpy
        target: testSaveProfile
        signalName: "downloadRequested"
    }

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

    WebEngineProfile {
        id: testSaveProfile

        onDownloadRequested: function(download) {
            downloadState.push(download.state)
            downloadUrl = download.url
            savePageFormat = download.savePageFormat
            downloadDir = download.downloadDirectory;
            downloadFileName = download.downloadFileName
            isSavePageDownload = download.isSavePageDownload
        }
        onDownloadFinished: function(download) {
            receivedBytes = download.receivedBytes
            totalBytes = download.totalBytes
            downloadState.push(download.state)
        }
    }

    TestCase {
        name: "WebEngineViewSave"

        function verifyData() {
            var isDataValid = false
            webEngineView.runJavaScript("(function() {" +
                                        "var title = document.title.toString();" +
                                        "var body = document.body.innerText;" +
                                        " return title === \"Test page 1\" && body.includes(\"Hello.\")" +
                                        "})();", function(result) {
                isDataValid = result;
            });
            tryVerify(function() { return isDataValid });
            return isDataValid;
        }

        function init() {
            downLoadRequestedSpy.clear()
            downloadFinishedSpy.clear()
            totalBytes = 0
            receivedBytes = 0
            downloadDir = ""
            downloadFileName = ""
            isSavePageDownload = false
            downloadState = []
            downloadUrl = ""
        }

        function test_savePage_data() {
            return [
                   { tag: "SingleHtmlSaveFormat", savePageFormat: WebEngineDownloadRequest.SingleHtmlSaveFormat },
                   { tag: "CompleteHtmlSaveFormat", savePageFormat: WebEngineDownloadRequest.CompleteHtmlSaveFormat },
                   { tag: "MimeHtmlSaveFormat", savePageFormat: WebEngineDownloadRequest.MimeHtmlSaveFormat },
            ];
        }

        function test_savePage(row) {
            var saveFormat = row.savePageFormat

            var fileDir = tempDir.path()
            var fileName = "saved_page.html"
            var filePath = fileDir + "/"+ fileName

            // load data to view
            webEngineView.url = Qt.resolvedUrl("test1.html")
            verify(webEngineView.waitForLoadSucceeded())
            verify(verifyData())

            webEngineView.save(filePath, saveFormat)
            downLoadRequestedSpy.wait()
            compare(downLoadRequestedSpy.count, 1)
            compare(downloadUrl, webEngineView.url)
            compare(savePageFormat, saveFormat)
            compare(downloadDir, fileDir)
            compare(downloadFileName, fileName)
            compare(isSavePageDownload, true)
            compare(downloadState[0], WebEngineDownloadRequest.DownloadInProgress)
            downloadFinishedSpy.wait()
            compare(downloadFinishedSpy.count, 1)
            compare(totalBytes, receivedBytes)
            compare(downloadState[1], WebEngineDownloadRequest.DownloadCompleted)

            // load some other data
            webEngineView.url = Qt.resolvedUrl("about:blank")
            verify(webEngineView.waitForLoadSucceeded())

            // load save file to view
            webEngineView.url = Qt.resolvedUrl(filePath)
            verify(webEngineView.waitForLoadSucceeded())
            verify(verifyData())
        }

        function test_saveImage() {
            var fileDir = tempDir.path()
            var fileName = "favicon.png"
            var filePath = fileDir + "/"+ fileName

            // Load an image
            webEngineView.url = Qt.resolvedUrl("icons/favicon.png")
            verify(webEngineView.waitForLoadSucceeded())

            webEngineView.save(filePath)
            downLoadRequestedSpy.wait()
            compare(downLoadRequestedSpy.count, 1)
            compare(downloadUrl, webEngineView.url)
            compare(downloadDir, fileDir)
            compare(downloadFileName, fileName)
            compare(isSavePageDownload, true)
            compare(downloadState[0], WebEngineDownloadRequest.DownloadRequested)
        }
    }
}