0623a1016d315c464a8ecf72b4bccc293ab842f4
[profile/ivi/qtdeclarative.git] / tests / auto / qmltest / selftests / tst_selftests.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 skip(msg, file, line) {
86             failmsg = "skip:" + msg
87         }
88     }
89
90     TestCase {
91         id: testCase
92         when: false
93         optional: true
94         qtest_results: functions
95     }
96
97     function init() {
98         compare(functions.failmsg, "cleaned") // Checks for previous cleanup()
99         functions.failmsg = "invalid"
100     }
101
102     function cleanup() {
103         functions.failmsg = "cleaned"
104     }
105
106     function test_fail() {
107         compare(functions.failmsg, "invalid") // Checks that init() was run
108
109         var caught = false
110         try {
111             testCase.fail("foo")
112         } catch (e) {
113             compare(e.message, "QtQuickTest::fail")
114             compare(functions.failmsg, "foo")
115             caught = true
116         }
117         verify(caught)
118
119         caught = false
120         try {
121             testCase.fail()
122         } catch (e) {
123             compare(e.message, "QtQuickTest::fail")
124             compare(functions.failmsg, "")
125             caught = true
126         }
127         verify(caught)
128
129         caught = false
130         try {
131             testCase.fail(false)
132         } catch (e) {
133             compare(e.message, "QtQuickTest::fail")
134             compare(functions.failmsg, "false")
135             caught = true
136         }
137         verify(caught)
138
139         caught = false
140         try {
141             testCase.fail(3)
142         } catch (e) {
143             compare(e.message, "QtQuickTest::fail")
144             compare(functions.failmsg, "3")
145             caught = true
146         }
147         verify(caught)
148     }
149
150     function test_verify() {
151         compare(functions.failmsg, "invalid") // Checks that init() was run
152
153         try {
154             testCase.verify(true)
155         } catch (e) {
156             fail("verify(true) did not succeed")
157         }
158         compare(functions.failmsg, "verify-ok")
159
160         var caught = false;
161         try {
162             testCase.verify(false, "foo")
163         } catch (e) {
164             compare(e.message, "QtQuickTest::fail")
165             compare(functions.failmsg, "foo")
166             caught = true
167         }
168         verify(caught)
169
170         caught = false;
171         try {
172             testCase.verify(false)
173         } catch (e) {
174             compare(e.message, "QtQuickTest::fail")
175             compare(functions.failmsg, "")
176             caught = true
177         }
178         verify(caught)
179     }
180
181     function test_compare() {
182         compare(functions.failmsg, "invalid") // Checks that init() was run
183
184         try {
185             testCase.compare(23, 23)
186         } catch (e) {
187             fail("compare(23, 23) did not succeed")
188         }
189         compare(functions.failmsg, "compare-ok")
190
191         var caught = false;
192         try {
193             testCase.compare(23, 42, "foo")
194         } catch (e) {
195             compare(e.message, "QtQuickTest::fail")
196             compare(functions.failmsg, "foo")
197             compare(functions.actual, "23")
198             compare(functions.expected, "42")
199             caught = true
200         }
201         verify(caught)
202
203         caught = false;
204         try {
205             testCase.compare("abcdef", 42)
206         } catch (e) {
207             compare(e.message, "QtQuickTest::fail")
208             compare(functions.failmsg, "Compared values are not the same")
209             compare(functions.actual, "abcdef")
210             compare(functions.expected, "42")
211             caught = true
212         }
213         verify(caught)
214
215 /*
216         caught = false;
217         try {
218             testCase.compare(Qt.vector3d(1, 2, 3), Qt.vector3d(-1, 2, 3), "x")
219         } catch (e) {
220             compare(e.message, "QtQuickTest::fail")
221             compare(functions.failmsg, "x")
222             compare(functions.actual, "Qt.vector3d(1, 2, 3)")
223             compare(functions.expected, "Qt.vector3d(-1, 2, 3)")
224             caught = true
225         }
226         verify(caught)
227
228         caught = false;
229         try {
230             testCase.compare(Qt.vector3d(1, 2, 3), Qt.vector3d(1, -2, 3), "y")
231         } catch (e) {
232             compare(e.message, "QtQuickTest::fail")
233             compare(functions.failmsg, "y")
234             compare(functions.actual, "Qt.vector3d(1, 2, 3)")
235             compare(functions.expected, "Qt.vector3d(1, -2, 3)")
236             caught = true
237         }
238         verify(caught)
239
240         caught = false;
241         try {
242             testCase.compare(Qt.vector3d(1, 2, 3), Qt.vector3d(1, 2, -3), "z")
243         } catch (e) {
244             compare(e.message, "QtQuickTest::fail")
245             compare(functions.failmsg, "z")
246             compare(functions.actual, "Qt.vector3d(1, 2, 3)")
247             compare(functions.expected, "Qt.vector3d(1, 2, -3)")
248             caught = true
249         }
250         verify(caught)
251
252         caught = false;
253         try {
254             testCase.compare(Qt.vector3d(1, 2, 3), Qt.vector3d(1, 2, 3))
255         } catch (e) {
256             fail("vector compare did not succeed")
257         }
258         compare(functions.failmsg, "compare-ok")
259 */
260     }
261
262     function test_skip() {
263         compare(functions.failmsg, "invalid") // Checks that init() was run
264
265         var caught = false
266         try {
267             testCase.skip("foo")
268         } catch (e) {
269             compare(e.message, "QtQuickTest::skip")
270             compare(functions.failmsg, "skip:foo")
271             caught = true
272         }
273         verify(caught)
274
275         caught = false
276         try {
277             testCase.skip()
278         } catch (e) {
279             compare(e.message, "QtQuickTest::skip")
280             compare(functions.failmsg, "skip:")
281             caught = true
282         }
283         verify(caught)
284     }
285 }