e917ba892dcb2db76fab75357237a3eeb98e16d4
[profile/ivi/qtdeclarative.git] / tests / auto / qmltest / animatedimage / tst_animatedimage.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     property string srcImage: "stickman.gif"
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/stickman.gif");
53         check.onreadystatechange = function() {
54
55             console.log("Status: ", check.status)
56             console.log("Readystate", check.readyState)
57             if (check.readyState == XMLHttpRequest.DONE) {
58                 if (check.status == 404) {
59                     top.canconnect = false;
60                 }else{
61                     top.canconnect = true;
62                 }
63                 top.checkfinished = true;
64             }
65         }
66         check.send();
67     }
68
69     AnimatedImage {
70         id: noSource
71         source: ""
72     }
73
74     AnimatedImage {
75         id: clearSource
76         source: srcImage
77     }
78
79     AnimatedImage {
80         id: resized
81         source: srcImage
82         width: 300
83         height: 300
84     }
85
86     AnimatedImage {
87         id: smooth
88         source: srcImage
89         smooth: true
90         width: 300
91         height: 300
92     }
93
94     AnimatedImage {
95         id: tileModes1
96         source: srcImage
97         width: 100
98         height: 300
99         fillMode: AnimatedImage.Tile
100     }
101
102     AnimatedImage {
103         id: tileModes2
104         source: srcImage
105         width: 300
106         height: 150
107         fillMode: AnimatedImage.TileVertically
108     }
109     AnimatedImage {
110         id: tileModes3
111         source: srcImage
112         width: 300
113         height: 150
114         fillMode: AnimatedImage.TileHorizontally
115     }
116
117     TestCase {
118         name: "AnimatedImage"
119
120         function test_noSource() {
121             compare(noSource.source, "")
122             compare(noSource.width, 0)
123             compare(noSource.height, 0)
124             compare(noSource.fillMode, AnimatedImage.Stretch)
125         }
126
127         function test_imageSource_data() {
128             return [
129                 {
130                     tag: "local",
131                     source: "stickman.gif",
132                     remote: false,
133                     error: ""
134                 },
135                 {
136                     tag: "local not found",
137                     source: "no-such-file.png",
138                     remote: false,
139                     error: "SUBinline:1:21: QML AnimatedImage: Error Reading Animated Image File SUBno-such-file.png"
140                 },
141                 {
142                     tag: "remote",
143                     source: "http://127.0.0.1:14445/stickman.gif",
144                     remote: true,
145                     error: ""
146                 }
147             ]
148         }
149
150         function test_imageSource(row) {
151             var expectError = (row.error.length != 0)
152             var canconnect = false;
153
154             if (expectError) {
155                 var parentUrl = Qt.resolvedUrl(".")
156                 ignoreWarning(row.error.replace(/SUB/g, parentUrl))
157             }
158
159             var img = Qt.createQmlObject('import QtQuick 2.0; AnimatedImage { source: "'+row.source+'" }', top)
160
161             if (row.remote) {
162                 skip("Remote solution not yet complete")
163                 tryCompare(img, "status", AnimatedImage.Loading)
164                 tryCompare(top, "checkfinished", true, 10000)
165                 if (top.canconnect == false)
166                     skip("Cannot access remote")
167             }
168
169             if (!expectError) {
170                 tryCompare(img, "status", AnimatedImage.Ready, 10000)
171                 compare(img.width, 160)
172                 compare(img.height, 120)
173                 compare(img.fillMode, AnimatedImage.Stretch)
174             } else {
175                 tryCompare(img, "status", AnimatedImage.Error)
176             }
177
178             img.destroy()
179         }
180
181         function test_clearSource() {
182             compare(clearSource.source, Qt.resolvedUrl(srcImage))
183             compare(clearSource.width, 160)
184             compare(clearSource.height, 120)
185
186             srcImage = ""
187             compare(clearSource.source, "")
188             compare(clearSource.width, 0)
189             compare(clearSource.height, 0)
190         }
191
192         function test_resized() {
193             compare(resized.width, 300)
194             compare(resized.height, 300)
195             compare(resized.fillMode, AnimatedImage.Stretch)
196         }
197
198         function test_smooth() {
199             compare(smooth.smooth, true)
200             compare(smooth.width, 300)
201             compare(smooth.height, 300)
202             compare(smooth.fillMode, AnimatedImage.Stretch)
203         }
204
205         function test_tileModes() {
206             compare(tileModes1.width, 100)
207             compare(tileModes1.height, 300)
208             compare(tileModes1.fillMode, AnimatedImage.Tile)
209
210             compare(tileModes2.width, 300)
211             compare(tileModes2.height, 150)
212             compare(tileModes2.fillMode, AnimatedImage.TileVertically)
213
214             compare(tileModes3.width, 300)
215             compare(tileModes3.height, 150)
216             compare(tileModes3.fillMode, AnimatedImage.TileHorizontally)
217         }
218
219     }
220 }