fix qmltest bugs
[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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
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 /*
220         caught = false;
221         try {
222             testCase.compare(Qt.vector3d(1, 2, 3), Qt.vector3d(-1, 2, 3), "x")
223         } catch (e) {
224             compare(e.message, "QtQuickTest::fail")
225             compare(functions.failmsg, "x")
226             compare(functions.actual, "Qt.vector3d(1, 2, 3)")
227             compare(functions.expected, "Qt.vector3d(-1, 2, 3)")
228             caught = true
229         }
230         verify(caught)
231
232         caught = false;
233         try {
234             testCase.compare(Qt.vector3d(1, 2, 3), Qt.vector3d(1, -2, 3), "y")
235         } catch (e) {
236             compare(e.message, "QtQuickTest::fail")
237             compare(functions.failmsg, "y")
238             compare(functions.actual, "Qt.vector3d(1, 2, 3)")
239             compare(functions.expected, "Qt.vector3d(1, -2, 3)")
240             caught = true
241         }
242         verify(caught)
243
244         caught = false;
245         try {
246             testCase.compare(Qt.vector3d(1, 2, 3), Qt.vector3d(1, 2, -3), "z")
247         } catch (e) {
248             compare(e.message, "QtQuickTest::fail")
249             compare(functions.failmsg, "z")
250             compare(functions.actual, "Qt.vector3d(1, 2, 3)")
251             compare(functions.expected, "Qt.vector3d(1, 2, -3)")
252             caught = true
253         }
254         verify(caught)
255
256         caught = false;
257         try {
258             testCase.compare(Qt.vector3d(1, 2, 3), Qt.vector3d(1, 2, 3))
259         } catch (e) {
260             fail("vector compare did not succeed")
261         }
262         compare(functions.failmsg, "compare-ok")
263 */
264     }
265
266     function test_skip() {
267         compare(functions.failmsg, "invalid") // Checks that init() was run
268
269         var caught = false
270         try {
271             testCase.skip("foo")
272         } catch (e) {
273             compare(e.message, "QtQuickTest::skip")
274             compare(functions.failmsg, "skipSingle:foo")
275             caught = true
276         }
277         verify(caught)
278
279         caught = false
280         try {
281             testCase.skip()
282         } catch (e) {
283             compare(e.message, "QtQuickTest::skip")
284             compare(functions.failmsg, "skipSingle:")
285             caught = true
286         }
287         verify(caught)
288
289         caught = false
290         try {
291             testCase.skipAll("foo")
292         } catch (e) {
293             compare(e.message, "QtQuickTest::skip")
294             compare(functions.failmsg, "skipAll:foo")
295             caught = true
296         }
297         verify(caught)
298
299         caught = false
300         try {
301             testCase.skipAll()
302         } catch (e) {
303             compare(e.message, "QtQuickTest::skip")
304             compare(functions.failmsg, "skipAll:")
305             caught = true
306         }
307         verify(caught)
308     }
309 }