Merge branch 'qtquick2'
[profile/ivi/qtdeclarative.git] / tests / auto / qmltest / selftests / tst_selftests.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
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 TestCase {
46     name: "SelfTests"
47
48     // Replace the TestResult functions in "testCase" with hooks
49     // that record the events but don't send them to QTestLib.
50     QtObject {
51         id: functions
52         property string failmsg: "cleaned"
53         property string actual: ""
54         property string expected: ""
55         property variant functionsToRun: []
56
57         function fail(msg, file, line) {
58             failmsg = msg
59         }
60
61         function verify(cond, msg, file, line) {
62             if (cond) {
63                 failmsg = "verify-ok"
64                 return true
65             } else {
66                 failmsg = msg
67                 return false
68             }
69         }
70
71         function compare(success, msg, act, exp, file, line) {
72             if (success) {
73                 failmsg = "compare-ok"
74                 actual = ""
75                 expected = ""
76                 return true
77             } else {
78                 failmsg = msg
79                 actual = act
80                 expected = exp
81                 return false
82             }
83         }
84
85         function skipSingle(msg, file, line) {
86             failmsg = "skipSingle:" + msg
87         }
88
89         function skipAll(msg, file, line) {
90             failmsg = "skipAll:" + msg
91         }
92     }
93
94     TestCase {
95         id: testCase
96         when: false
97         optional: true
98         qtest_results: functions
99     }
100
101     function init() {
102         compare(functions.failmsg, "cleaned") // Checks for previous cleanup()
103         functions.failmsg = "invalid"
104     }
105
106     function cleanup() {
107         functions.failmsg = "cleaned"
108     }
109
110     function test_fail() {
111         compare(functions.failmsg, "invalid") // Checks that init() was run
112
113         var caught = false
114         try {
115             testCase.fail("foo")
116         } catch (e) {
117             compare(e.message, "QtQuickTest::fail")
118             compare(functions.failmsg, "foo")
119             caught = true
120         }
121         verify(caught)
122
123         caught = false
124         try {
125             testCase.fail()
126         } catch (e) {
127             compare(e.message, "QtQuickTest::fail")
128             compare(functions.failmsg, "")
129             caught = true
130         }
131         verify(caught)
132
133         caught = false
134         try {
135             testCase.fail(false)
136         } catch (e) {
137             compare(e.message, "QtQuickTest::fail")
138             compare(functions.failmsg, "false")
139             caught = true
140         }
141         verify(caught)
142
143         caught = false
144         try {
145             testCase.fail(3)
146         } catch (e) {
147             compare(e.message, "QtQuickTest::fail")
148             compare(functions.failmsg, "3")
149             caught = true
150         }
151         verify(caught)
152     }
153
154     function test_verify() {
155         compare(functions.failmsg, "invalid") // Checks that init() was run
156
157         try {
158             testCase.verify(true)
159         } catch (e) {
160             fail("verify(true) did not succeed")
161         }
162         compare(functions.failmsg, "verify-ok")
163
164         var caught = false;
165         try {
166             testCase.verify(false, "foo")
167         } catch (e) {
168             compare(e.message, "QtQuickTest::fail")
169             compare(functions.failmsg, "foo")
170             caught = true
171         }
172         verify(caught)
173
174         caught = false;
175         try {
176             testCase.verify(false)
177         } catch (e) {
178             compare(e.message, "QtQuickTest::fail")
179             compare(functions.failmsg, "")
180             caught = true
181         }
182         verify(caught)
183     }
184
185     function test_compare() {
186         compare(functions.failmsg, "invalid") // Checks that init() was run
187
188         try {
189             testCase.compare(23, 23)
190         } catch (e) {
191             fail("compare(23, 23) did not succeed")
192         }
193         compare(functions.failmsg, "compare-ok")
194
195         var caught = false;
196         try {
197             testCase.compare(23, 42, "foo")
198         } catch (e) {
199             compare(e.message, "QtQuickTest::fail")
200             compare(functions.failmsg, "foo")
201             compare(functions.actual, "23")
202             compare(functions.expected, "42")
203             caught = true
204         }
205         verify(caught)
206
207         caught = false;
208         try {
209             testCase.compare("abcdef", 42)
210         } catch (e) {
211             compare(e.message, "QtQuickTest::fail")
212             compare(functions.failmsg, "Compared values are not the same")
213             compare(functions.actual, "abcdef")
214             compare(functions.expected, "42")
215             caught = true
216         }
217         verify(caught)
218
219         caught = false;
220         try {
221             testCase.compare(Qt.vector3d(1, 2, 3), Qt.vector3d(-1, 2, 3), "x")
222         } catch (e) {
223             compare(e.message, "QtQuickTest::fail")
224             compare(functions.failmsg, "x")
225             compare(functions.actual, "Qt.vector3d(1, 2, 3)")
226             compare(functions.expected, "Qt.vector3d(-1, 2, 3)")
227             caught = true
228         }
229         verify(caught)
230
231         caught = false;
232         try {
233             testCase.compare(Qt.vector3d(1, 2, 3), Qt.vector3d(1, -2, 3), "y")
234         } catch (e) {
235             compare(e.message, "QtQuickTest::fail")
236             compare(functions.failmsg, "y")
237             compare(functions.actual, "Qt.vector3d(1, 2, 3)")
238             compare(functions.expected, "Qt.vector3d(1, -2, 3)")
239             caught = true
240         }
241         verify(caught)
242
243         caught = false;
244         try {
245             testCase.compare(Qt.vector3d(1, 2, 3), Qt.vector3d(1, 2, -3), "z")
246         } catch (e) {
247             compare(e.message, "QtQuickTest::fail")
248             compare(functions.failmsg, "z")
249             compare(functions.actual, "Qt.vector3d(1, 2, 3)")
250             compare(functions.expected, "Qt.vector3d(1, 2, -3)")
251             caught = true
252         }
253         verify(caught)
254
255         caught = false;
256         try {
257             testCase.compare(Qt.vector3d(1, 2, 3), Qt.vector3d(1, 2, 3))
258         } catch (e) {
259             fail("vector compare did not succeed")
260         }
261         compare(functions.failmsg, "compare-ok")
262     }
263
264     function test_skip() {
265         compare(functions.failmsg, "invalid") // Checks that init() was run
266
267         var caught = false
268         try {
269             testCase.skip("foo")
270         } catch (e) {
271             compare(e.message, "QtQuickTest::skip")
272             compare(functions.failmsg, "skipSingle:foo")
273             caught = true
274         }
275         verify(caught)
276
277         caught = false
278         try {
279             testCase.skip()
280         } catch (e) {
281             compare(e.message, "QtQuickTest::skip")
282             compare(functions.failmsg, "skipSingle:")
283             caught = true
284         }
285         verify(caught)
286
287         caught = false
288         try {
289             testCase.skipAll("foo")
290         } catch (e) {
291             compare(e.message, "QtQuickTest::skip")
292             compare(functions.failmsg, "skipAll:foo")
293             caught = true
294         }
295         verify(caught)
296
297         caught = false
298         try {
299             testCase.skipAll()
300         } catch (e) {
301             compare(e.message, "QtQuickTest::skip")
302             compare(functions.failmsg, "skipAll:")
303             caught = true
304         }
305         verify(caught)
306     }
307 }