summaryrefslogtreecommitdiffstats
path: root/src/imports/testlib/TestCase.qml
blob: 9e57b2ef80140ce34c61b397ee29a785c0754b6e (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, 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.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 1.0
import QtQuickTest 1.0
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

    // Property that is set to true when the main window is shown.
    // We need to set the property value in an odd way to handle
    // both qmlviewer and the QtQuickTest module test wrapper.
    property bool windowShown: Qt.qtest_wrapper ? qtest.windowShown : false

    // Internal private state
    property bool prevWhen: true
    property int testId: -1
    property variant testCaseResult

    TestResult { id: results }

    function fail(msg) {
        if (!msg)
            msg = "";
        results.fail(msg, Qt.qtest_caller_file(), Qt.qtest_caller_line())
        throw new Error("QtQuickTest::fail")
    }

    function verify(cond, msg) {
        if (!msg)
            msg = "";
        if (!results.verify(cond, msg, Qt.qtest_caller_file(), Qt.qtest_caller_line()))
            throw new Error("QtQuickTest::fail")
    }

    function compareInternal(actual, expected) {
        var success = false
        if (typeof actual == "number" && typeof expected == "number") {
            // Use a fuzzy compare if the two values are floats
            if (Math.abs(actual - expected) <= 0.00001)
                success = true
        } 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) {
                    success = true
                }
            } else if (actual == expected) {
                success = true
            }
        } else if (actual == expected) {
            success = true
        }
        return success
    }

    function formatValue(value) {
        if (typeof value == "object") {
            if ("x" in value && "y" in value && "z" in value) {
                return "Qt.vector3d(" + value.x + ", " +
                       value.y + ", " + value.z + ")"
            }
        }
        return value
    }

    function compare(actual, expected, msg) {
        var act = formatValue(actual)
        var exp = formatValue(expected)
        var success = compareInternal(actual, expected)
        if (!msg)
            msg = ""
        if (!results.compare(success, msg, act, exp, Qt.qtest_caller_file(), Qt.qtest_caller_line()))
            throw new Error("QtQuickTest::fail")
    }

    function tryCompare(obj, prop, value, timeout) {
        if (!timeout)
            timeout = 5000
        if (!compareInternal(obj[prop], value))
            wait(0)
        var i = 0
        while (i < timeout && !compareInternal(obj[prop], value)) {
            wait(50)
            i += 50
        }
        var actual = obj[prop]
        var act = formatValue(actual)
        var exp = formatValue(value)
        var success = compareInternal(actual, value)
        if (!results.compare(success, "property " + prop, act, exp, Qt.qtest_caller_file(), Qt.qtest_caller_line()))
            throw new Error("QtQuickTest::fail")
    }

    function skip(msg) {
        if (!msg)
            msg = ""
        results.skipSingle(msg, Qt.qtest_caller_file(), Qt.qtest_caller_line())
        throw new Error("QtQuickTest::skip")
    }

    function skipAll(msg) {
        if (!msg)
            msg = ""
        results.skipAll(msg, Qt.qtest_caller_file(), Qt.qtest_caller_line())
        throw new Error("QtQuickTest::skip")
    }

    function expectFail(tag, msg) {
        if (!tag)
            tag = ""
        if (!msg)
            msg = ""
        if (!results.expectFail(tag, msg, Qt.qtest_caller_file(), Qt.qtest_caller_line()))
            throw new Error("QtQuickTest::expectFail")
    }

    function expectFailContinue(tag, msg) {
        if (!tag)
            tag = ""
        if (!msg)
            msg = ""
        if (!results.expectFailContinue(tag, msg, Qt.qtest_caller_file(), Qt.qtest_caller_line()))
            throw new Error("QtQuickTest::expectFail")
    }

    function warn(msg) {
        if (!msg)
            msg = ""
        results.warn(msg);
    }

    function ignoreWarning(msg) {
        if (!msg)
            msg = ""
        results.ignoreWarning(msg)
    }

    function wait(ms) {
        results.wait(ms)
    }

    function sleep(ms) {
        results.sleep(ms)
    }

    // Functions that can be overridden in subclasses for init/cleanup duties.
    function initTestCase() {}
    function cleanupTestCase() {}
    function init() {}
    function cleanup() {}

    function runInternal(prop, arg) {
        try {
            testCaseResult = testCase[prop](arg)
        } catch (e) {
            testCaseResult = []
            if (e.message.indexOf("QtQuickTest::") != 0) {
                // Test threw an unrecognized exception - fail.
                fail(e.message)
            }
        }
        return !results.dataFailed
    }

    function runFunction(prop, arg) {
        results.functionType = TestResult.InitFunc
        runInternal("init")
        if (!results.skipped) {
            results.functionType = TestResult.Func
            runInternal(prop, arg)
            results.functionType = TestResult.CleanupFunc
            runInternal("cleanup")
        }
        results.functionType = TestResult.NoWhere
    }

    function runBenchmarkFunction(prop, arg) {
        results.startMeasurement()
        do {
            results.beginDataRun()
            do {
                // Run the initialization function.
                results.functionType = TestResult.InitFunc
                runInternal("init")
                if (results.skipped)
                    break

                // Execute the benchmark function.
                results.functionType = TestResult.Func
                if (prop.indexOf("benchmark_once_") != 0)
                    results.startBenchmark(TestResult.RepeatUntilValidMeasurement, results.dataTag)
                else
                    results.startBenchmark(TestResult.RunOnce, results.dataTag)
                while (!results.isBenchmarkDone()) {
                    if (!runInternal(prop, arg))
                        break
                    results.nextBenchmark()
                }
                results.stopBenchmark()

                // Run the cleanup function.
                results.functionType = TestResult.CleanupFunc
                runInternal("cleanup")
                results.functionType = TestResult.NoWhere
            } while (!results.measurementAccepted())
            results.endDataRun()
        } while (results.needsMoreMeasurements())
    }

    function run() {
        if (TestLogger.log_start_test()) {
            results.reset()
            results.testCaseName = name
            results.startLogging()
        } else {
            results.testCaseName = name
        }
        running = true

        // Run the initTestCase function.
        results.functionName = "initTestCase"
        results.functionType = TestResult.InitFunc
        var runTests = true
        if (!runInternal("initTestCase"))
            runTests = false
        results.finishTestFunction()

        // Run the test methods.
        var testList = []
        if (runTests) {
            for (var prop in testCase) {
                if (prop.indexOf("test_") != 0 && prop.indexOf("benchmark_") != 0)
                    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"
            var isBenchmark = (prop.indexOf("benchmark_") == 0)
            results.functionName = prop
            if (datafunc in testCase) {
                results.functionType = TestResult.DataFunc
                if (runInternal(datafunc)) {
                    var table = testCaseResult
                    var haveData = false
                    results.initTestTable()
                    for (var index in table) {
                        haveData = true
                        var row = table[index]
                        if (!row.tag)
                            row.tag = "row " + index    // Must have something
                        results.dataTag = row.tag
                        if (isBenchmark)
                            runBenchmarkFunction(prop, row)
                        else
                            runFunction(prop, row)
                        results.dataTag = ""
                    }
                    if (!haveData)
                        results.warn("no data supplied for " + prop + "() by " + datafunc + "()")
                    results.clearTestTable()
                }
            } else if (isBenchmark) {
                runBenchmarkFunction(prop, null, isBenchmark)
            } else {
                runFunction(prop, null, isBenchmark)
            }
            results.finishTestFunction()
            results.skipped = false
        }

        // Run the cleanupTestCase function.
        results.skipped = false
        results.functionName = "cleanupTestCase"
        results.functionType = TestResult.CleanupFunc
        runInternal("cleanupTestCase")

        // Clean up and exit.
        running = false
        completed = true
        results.finishTestFunction()
        results.functionName = ""

        // Stop if there are no more tests to be run.
        if (!TestLogger.log_complete_test(testId)) {
            results.stopLogging()
            Qt.quit()
        }
        results.testCaseName = ""
    }

    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)
        }
    }

    // The test framework will set qtest.windowShown when the
    // window is actually shown.  If we are running with qmlviewer,
    // then this won't happen.  So we use a timer instead.
    Timer {
        id: windowShowTimer
        interval: 100
        repeat: false
        onTriggered: { windowShown = true }
    }

    Component.onCompleted: {
        testId = TestLogger.log_register_test(name)
        if (optional)
            TestLogger.log_optional_test(testId)
        prevWhen = when
        var isQmlViewer = Qt.qtest_wrapper ? false : true
        if (isQmlViewer)
            windowShowTimer.running = true
        if (when && !completed && !running)
            run()
    }
}