summaryrefslogtreecommitdiffstats
path: root/tests/auto/qml3d/QtQuickTest/TestCase.qml
blob: 49cb73c45a0a0fe36797657b768c286deb4964cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtQuick3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

import Qt 4.7
import "testlogger.js" as TestLogger

Item {
    id: testCase
    visible: false

    // Name of the test case to prefix the function name in messages.
    property string name

    // Set to true to start the test running.
    property bool when: true

    // Set to true once the test has completed.
    property bool completed: false

    // Set to true when the test is running but not yet complete.
    property bool running: false

    // Set to true if the test doesn't have to run (because some
    // other test failed which this one depends on).
    property bool optional: false

    // Internal private state
    property string currentTestCase
    property bool expectingFail
    property string expectFailMsg
    property bool prevWhen: true
    property int testId: -1

    TestReport { id: reporter }

    function fail(msg) {
        if (!msg)
            msg = "";
        if (expectingFail) {
            TestLogger.log_expect_fail(currentTestCase, expectFailMsg, msg)
            throw new Error("QtTest::expect_fail")
        } else {
            TestLogger.log_fail(currentTestCase, msg)
            throw new Error("QtTest::fail")
        }
    }

    function fail2(msg, msg2) {
        if (msg)
            fail(msg + ": " + msg2)
        else
            fail(msg2)
    }

    function verify(cond, msg) {
        if (!cond)
            fail(msg)
    }

    function compare(actual, expected, msg) {
        if (typeof actual == "number" && typeof expected == "number") {
            // Use a fuzzy compare if the two values are floats
            if (Math.abs(actual - expected) <= 0.00001)
                return
        } else if (typeof actual == "object" && typeof expected == "object") {
            // Does the expected value look like a vector3d?
            if ("x" in expected && "y" in expected && "z" in expected) {
                if (Math.abs(actual.x - expected.x) <= 0.00001 &&
                        Math.abs(actual.y - expected.y) <= 0.00001 &&
                        Math.abs(actual.z - expected.z) <= 0.00001)
                    return
                fail2(msg, "actual: Qt.vector3d(" +
                           actual.x + ", " + actual.y + ", " + actual.z +
                           "), expected: Qt.vector3d(" +
                           expected.x + ", " + expected.y + ", " + expected.z +
                           ")")
                return
            }
            if (actual == expected)
                return
        } else if (actual == expected) {
            return
        }
        fail2(msg, "actual: " + actual + ", expected: " + expected)
    }

    function skip(msg) {
        TestLogger.log_skip(currentTestCase, msg)
        throw new Error("QtTest::skip")
    }

    function expectFail(msg) {
        expectingFail = true
        expectFailMsg = msg
    }

    property variant testCaseResult

    function runInternal(prop, dataDriven, arg, tag) {
        currentTestCase = TestLogger.log_prefixed_name(name, prop)
        if (dataDriven && tag)
            currentTestCase += " [" + tag + "]"
        expectingFail = false
        var success = true
        try {
            testCaseResult = testCase[prop](arg)
            if (expectingFail) {
                success = false
                TestLogger.log_expect_fail_pass(currentTestCase)
            } else if (!dataDriven) {
                TestLogger.log_pass(currentTestCase)
            }
        } catch (e) {
            testCaseResult = []
            if (e.message == "QtTest::fail") {
                success = false
            } else if (e.message.indexOf("QtTest::") != 0) {
                // Test threw an unrecognized exception - fail.
                TestLogger.log_fail(currentTestCase, e.message)
                success = false
            }
        }
        return success
    }

    function run() {
        TestLogger.log_start_test(reporter)
        var success = true
        running = true
        var testList = []
        for (var prop in testCase) {
            if (prop.indexOf("test_") != 0)
                continue
            if (filterTestCases.length > 0) {
                // if there is a list, only run the tests in the list
                var excludeTest = true
                for (var i = 0; i < filterTestCases.length && excludeTest; i++)
                {
                    if (filterTestCases[i] == prop)
                        excludeTest = false
                }
                if (excludeTest)
                    continue
            }
            var tail = prop.lastIndexOf("_data");
            if (tail != -1 && tail == (prop.length - 5))
                continue
            testList.push(prop)
        }
        testList.sort()
        for (var index in testList) {
            var prop = testList[index]
            var datafunc = prop + "_data"
            if (datafunc in testCase) {
                if (runInternal(datafunc, true)) {
                    var table = testCaseResult
                    var successThis = true
                    var haveData = false
                    for (var index in table) {
                        haveData = true
                        var row = table[index]
                        if (!runInternal(prop, true, row, row.tag))
                            successThis = false
                    }
                    if (!haveData)
                        TestLogger.log_message("WARNING: no data supplied for " + prop + "() by " + datafunc + "()")
                    if (successThis) {
                        var prefix;
                        if (name)
                            prefix = name + "::"
                        currentTestCase = prefix + prop + "()"
                        TestLogger.log_pass(currentTestCase)
                    } else {
                        success = false
                    }
                } else {
                    success = false
                }
            } else {
                if (!runInternal(prop, false))
                    success = false
            }
        }
        currentTestCase = ""
        running = false
        completed = true
        TestLogger.log_complete_test(testId, reporter)
        return success
    }

    onWhenChanged: {
        if (when != prevWhen) {
            prevWhen = when
            if (when && !completed && !running)
                run()
        }
    }

    onOptionalChanged: {
        if (!completed) {
            if (optional)
                TestLogger.log_optional_test(testId)
            else
                TestLogger.log_mandatory_test(testId)
        }
    }

    Component.onCompleted: {
        testId = TestLogger.log_register_test(name)
        if (optional)
            TestLogger.log_optional_test(testId)
        prevWhen = when
        if (when && !completed && !running)
            run()
    }
}