summaryrefslogtreecommitdiffstats
path: root/tests/auto/quick/qmltests/data/tst_save.qml
blob: 3289dbd8b90608d25556bc84fccde12913056471 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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;
    property bool autoCancel: false

    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

            if (autoCancel)
                download.cancel()
        }
        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 = ""
            autoCancel = false
        }

        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_data() {
            return [
                   { tag: "Auto accept", autoCancel: false },
                   { tag: "Cancel", autoCancel: true },
            ];
        }

        function test_saveImage(row) {
            autoCancel = row.autoCancel

            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.DownloadInProgress)
            downloadFinishedSpy.wait()
            compare(downloadFinishedSpy.count, 1)
            if (autoCancel) {
                compare(receivedBytes, 0)
                compare(downloadState[1], WebEngineDownloadRequest.DownloadCancelled)
            } else {
                compare(totalBytes, receivedBytes)
                compare(downloadState[1], WebEngineDownloadRequest.DownloadCompleted)
            }
        }

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

            // Saving without specifying path shouldn't be auto accepted
            webEngineView.triggerWebAction(WebEngineView.SavePage)
            downLoadRequestedSpy.wait()
            compare(downLoadRequestedSpy.count, 1)
            compare(downloadUrl, webEngineView.url)
            compare(isSavePageDownload, true)
            // The initial download request starts from DownloadRequested state,
            // which means it wasn't automatically accepted.
            compare(downloadState[0], WebEngineDownloadRequest.DownloadRequested)
        }
    }
}