113c0de77d8ef1f11b60ad043c253a2a5d7330ba
[profile/ivi/qtdeclarative.git] / tests / auto / qmltest / borderimage / tst_borderimage.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 import QtQuick 2.0
43 import QtTest 1.0
44
45 Item {
46     id: top
47     property bool canconnect
48     property bool checkfinished: false
49
50     Component.onCompleted: {
51         var check = new XMLHttpRequest;
52         check.open("GET", "http://127.0.0.1:14445/colors.png");
53         check.onreadystatechange = function() {
54             if (check.readyState == XMLHttpRequest.DONE) {
55                 if (check.status == 404) {
56                     top.canconnect = false;
57                 }else{
58                     top.canconnect = true;
59                 }
60                 top.checkfinished = true;
61             }
62         }
63         check.send();
64     }
65
66     BorderImage {
67         id: noSource
68         source: ""
69     }
70
71     property string srcImage: "colors.png"
72
73     BorderImage {
74         id: clearSource
75         source: srcImage
76     }
77
78     BorderImage {
79         id: resized
80         source: srcImage
81         width: 300
82         height: 300
83     }
84
85     BorderImage {
86         id: smooth
87         source: srcImage
88         smooth: true
89         width: 300
90         height: 300
91     }
92
93     BorderImage {
94         id: tileModes1
95         source: srcImage
96         width: 100
97         height: 300
98         horizontalTileMode: BorderImage.Repeat
99         verticalTileMode: BorderImage.Repeat
100     }
101
102     BorderImage {
103         id: tileModes2
104         source: srcImage
105         width: 300
106         height: 150
107         horizontalTileMode: BorderImage.Round
108         verticalTileMode: BorderImage.Round
109     }
110
111     TestCase {
112         name: "BorderImage"
113
114         function test_noSource() {
115             compare(noSource.source, "")
116             compare(noSource.width, 0)
117             compare(noSource.height, 0)
118             compare(noSource.horizontalTileMode, BorderImage.Stretch)
119             compare(noSource.verticalTileMode, BorderImage.Stretch)
120         }
121
122         function test_imageSource_data() {
123             return [
124                 {
125                     tag: "local",
126                     source: "colors.png",
127                     remote: false,
128                     error: ""
129                 },
130                 {
131                     tag: "local not found",
132                     source: "no-such-file.png",
133                     remote: false,
134                     error: "SUBinline:1:21: QML BorderImage: Cannot open: SUBno-such-file.png"
135                 },
136                 {
137                     tag: "remote",
138                     source: "http://127.0.0.1:14445/colors.png",
139                     remote: true,
140                     error: ""
141                 }
142             ]
143         }
144
145         function test_imageSource(row) {
146             var expectError = (row.error.length != 0)
147             if (expectError) {
148                 var parentUrl = Qt.resolvedUrl(".")
149                 ignoreWarning(row.error.replace(/SUB/g, parentUrl))
150             }
151
152             var img = Qt.createQmlObject
153                 ('import QtQuick 2.0; BorderImage { source: "' +
154                     row.source + '" }', top)
155
156             if (row.remote) {
157                 skip("Remote solution not yet complete")
158                 tryCompare(img, "status", BorderImage.Loading)
159                 tryCompare(top, "checkfinished", true, 10000)
160                 if (top.canconnect == false)
161                     skip("Cannot access remote")
162             }
163
164             if (!expectError) {
165                 tryCompare(img, "status", BorderImage.Ready, 10000)
166                 compare(img.width, 120)
167                 compare(img.height, 120)
168                 compare(img.horizontalTileMode, BorderImage.Stretch)
169                 compare(img.verticalTileMode, BorderImage.Stretch)
170             } else {
171                 tryCompare(img, "status", BorderImage.Error)
172             }
173
174             img.destroy()
175         }
176
177         function test_clearSource() {
178             compare(clearSource.source, Qt.resolvedUrl("colors.png"))
179             compare(clearSource.width, 120)
180             compare(clearSource.height, 120)
181
182             srcImage = ""
183             compare(clearSource.source, "")
184             compare(clearSource.width, 0)
185             compare(clearSource.height, 0)
186         }
187
188         function test_resized() {
189             compare(resized.width, 300)
190             compare(resized.height, 300)
191             compare(resized.horizontalTileMode, BorderImage.Stretch)
192             compare(resized.verticalTileMode, BorderImage.Stretch)
193         }
194
195         function test_smooth() {
196             compare(smooth.smooth, true)
197             compare(smooth.width, 300)
198             compare(smooth.height, 300)
199             compare(smooth.horizontalTileMode, BorderImage.Stretch)
200             compare(smooth.verticalTileMode, BorderImage.Stretch)
201         }
202
203         function test_tileModes() {
204             compare(tileModes1.width, 100)
205             compare(tileModes1.height, 300)
206             compare(tileModes1.horizontalTileMode, BorderImage.Repeat)
207             compare(tileModes1.verticalTileMode, BorderImage.Repeat)
208
209             compare(tileModes2.width, 300)
210             compare(tileModes2.height, 150)
211             compare(tileModes2.horizontalTileMode, BorderImage.Round)
212             compare(tileModes2.verticalTileMode, BorderImage.Round)
213         }
214
215         function test_sciSource_data() {
216             return [
217                 {
218                     tag: "local",
219                     source: "colors-round.sci",
220                     remote: false,
221                     valid: true
222                 },
223                 {
224                     tag: "local not found",
225                     source: "no-such-file.sci",
226                     remote: false,
227                     valid: false
228                 },
229                 {
230                     tag: "remote",
231                     source: "remote.sci",
232                     remote: true,
233                     valid: true
234                 }
235             ]
236         }
237
238         function test_sciSource(row) {
239             var img = Qt.createQmlObject('import QtQuick 2.0; BorderImage { height: 300; width: 300 }', top)
240
241             if (row.remote) {
242                 skip("Remote solution not yet complete")
243                 img.source = row.source;
244                 tryCompare(top, "checkfinished", true, 10000)
245                 if (top.canconnect == false)
246                     skip("Cannot access remote")
247             }else{
248                 img.source = row.source;
249             }
250
251             compare(img.source, Qt.resolvedUrl(row.source))
252             compare(img.width, 300)
253             compare(img.height, 300)
254
255             if (row.valid) {
256                 tryCompare(img, "status", BorderImage.Ready, 10000)
257                 compare(img.border.left, 10)
258                 compare(img.border.top, 20)
259                 compare(img.border.right, 30)
260                 compare(img.border.bottom, 40)
261                 compare(img.horizontalTileMode, BorderImage.Round)
262                 compare(img.verticalTileMode, BorderImage.Repeat)
263             } else {
264                 tryCompare(img, "status", BorderImage.Error)
265             }
266
267             img.destroy()
268         }
269
270
271         function test_invalidSciFile() {
272             ignoreWarning("QQuickGridScaledImage: Invalid tile rule specified. Using Stretch.") // for "Roun"
273             ignoreWarning("QQuickGridScaledImage: Invalid tile rule specified. Using Stretch.") // for "Repea"
274
275             var component = Qt.createComponent("InvalidSciFile.qml")
276             var invalidSciFile = component.createObject(top)
277
278             compare(invalidSciFile.status, Image.Error)
279             compare(invalidSciFile.width, 300)
280             compare(invalidSciFile.height, 300)
281             compare(invalidSciFile.horizontalTileMode, BorderImage.Stretch)
282             compare(invalidSciFile.verticalTileMode, BorderImage.Stretch)
283         }
284     }
285 }