Change copyrights from Nokia to Digia
[profile/ivi/qtdeclarative.git] / tests / auto / qmltest / image / tst_image.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 **     of its contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 import QtQuick 2.0
42 import QtTest 1.0
43
44 Item {
45     id: top
46
47     property string srcImage: "logo.png"
48     property bool canconnect
49     property bool checkfinished: false
50
51     Component.onCompleted: {
52         var check = new XMLHttpRequest;
53         check.open("GET", "http://127.0.0.1:14445/logo.png");
54         check.onreadystatechange = function() {
55             if (check.readyState == XMLHttpRequest.DONE) {
56                 if (check.status == 404) {
57                     top.canconnect = false;
58                 } else {
59                     top.canconnect = true;
60                 }
61                 top.checkfinished = true;
62             }
63         }
64         check.send();
65     }
66
67     Image {
68         id: noSource
69         source: ""
70     }
71
72     Image {
73         id: clearSource
74         source: srcImage
75     }
76
77     Image {
78         id: resized
79         source: srcImage
80         width: 300
81         height: 300
82     }
83
84     Image {
85         id: smooth
86         source: srcImage
87         smooth: true
88         width: 300
89         height: 300
90     }
91
92     Image {
93         id: tileModes1
94         source: srcImage
95         width: 100
96         height: 300
97         fillMode: Image.Tile
98     }
99
100     Image {
101         id: tileModes2
102         source: srcImage
103         width: 300
104         height: 150
105         fillMode: Image.TileVertically
106     }
107     Image {
108         id: tileModes3
109         source: srcImage
110         width: 300
111         height: 150
112         fillMode: Image.TileHorizontally
113     }
114
115     TestCase {
116         name: "Image"
117
118         function test_noSource() {
119             compare(noSource.source, "")
120             compare(noSource.width, 0)
121             compare(noSource.height, 0)
122             compare(noSource.fillMode, Image.Stretch)
123         }
124
125         function test_imageSource_data() {
126             return [
127                 {
128                     tag: "local",
129                     source: "logo.png",
130                     remote: false,
131                     error: ""
132                 },
133                 {
134                     tag: "local not found",
135                     source: "no-such-file.png",
136                     remote: false,
137                     error: "SUBinline:1:21: QML Image: Cannot open: SUBno-such-file.png"
138                 },
139                 {
140                     tag: "remote",
141                     source: "http://127.0.0.1:14445/logo.png",
142                     remote: true,
143                     error: ""
144                 }
145             ]
146         }
147
148         function test_imageSource(row) {
149             var expectError = (row.error.length != 0)
150             if (expectError) {
151                 var parentUrl = Qt.resolvedUrl(".")
152                 ignoreWarning(row.error.replace(/SUB/g, parentUrl))
153             }
154
155             var img = Qt.createQmlObject('import QtQuick 2.0; Image { source: "'+row.source+'" }', top)
156
157             if (row.remote) {
158                 skip("Remote solution not yet complete")
159                 tryCompare(img, "status", Image.Loading)
160                 tryCompare(top, "checkfinished", true, 10000)
161                 if (top.canconnect == false)
162                     skip("Cannot access remote")
163             }
164
165             if (!expectError) {
166                 tryCompare(img, "status", Image.Ready, 10000)
167                 compare(img.width, 59)
168                 compare(img.height, 71)
169                 compare(img.fillMode, Image.Stretch)
170             } else {
171                 tryCompare(img, "status", Image.Error)
172             }
173
174             img.destroy()
175         }
176
177         function test_clearSource() {
178             compare(clearSource.source, Qt.resolvedUrl(srcImage))
179             compare(clearSource.width, 59)
180             compare(clearSource.height, 71)
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.fillMode, Image.Stretch)
192         }
193
194         function test_smooth() {
195             compare(smooth.smooth, true)
196             compare(smooth.width, 300)
197             compare(smooth.height, 300)
198             compare(smooth.fillMode, Image.Stretch)
199         }
200
201         function test_tileModes() {
202             compare(tileModes1.width, 100)
203             compare(tileModes1.height, 300)
204             compare(tileModes1.fillMode, Image.Tile)
205
206             compare(tileModes2.width, 300)
207             compare(tileModes2.height, 150)
208             compare(tileModes2.fillMode, Image.TileVertically)
209
210             compare(tileModes3.width, 300)
211             compare(tileModes3.height, 150)
212             compare(tileModes3.fillMode, Image.TileHorizontally)
213         }
214
215     }
216 }