aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2016-06-28 10:13:17 +0200
committerMitch Curtis <mitch.curtis@qt.io>2016-07-20 07:55:27 +0000
commitaa5f09a0d22e9fa6e125afc798f6b3289088b791 (patch)
tree3d9c8844e0b22f7fac7b157fcee6f3a540414681 /tests
parent877ce4d446b34375096f5e7fae30101405d7ea95 (diff)
Add tryVerify()
This is useful for conditions which can't be tested using tryCompare. One such situation is that of the currentItem property of ListView. In Qt Quick Controls 2, the currentItem property of the ListView that is internally a child of Tumbler can be null for a certain period of time, so using tryCompare() would result in errors due to trying to access a property of a null object: tryCompare(tumblerView.currentItem, "text", "2") The current workaround is to use wait(50) in a for loop, which is ugly and could lead to flaky tests: for (var delay = 1000; delay >= 0; delay -= 50) { if (tumblerView.currentItem) break; wait(50); } verify(tumblerView.currentItem); compare(tumblerView.currentItem.text, data.currentIndex.toString()); Using tryVerify(), we can first ensure that currentItem isn't null, and then use a regular synchronous compare afterwards: tryVerify(function(){ return tumblerView.currentItem; }); compare(tumbler.currentItem.text, data.currentIndex.toString()); [ChangeLog][QtTest][TestCase] Added tryVerify() function to allow verification of asynchronous conditions that can't be tested using tryCompare(). Change-Id: Ie93052b650f7fe0bf26853054a8f0f35a483e387 Task-number: QTBUG-19708 Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qmltest/selftests/tst_tryVerify.qml72
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/auto/qmltest/selftests/tst_tryVerify.qml b/tests/auto/qmltest/selftests/tst_tryVerify.qml
new file mode 100644
index 0000000000..6f29d8643d
--- /dev/null
+++ b/tests/auto/qmltest/selftests/tst_tryVerify.qml
@@ -0,0 +1,72 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.8
+import QtTest 1.1
+
+TestCase {
+ name: "tst_tryVerify"
+
+ Item {
+ id: item
+ }
+
+ QtObject {
+ id: itemContainer
+ property Item i
+ }
+
+ Timer {
+ id: timer
+ interval: 100
+ onTriggered: itemContainer.i = item
+ }
+
+ function resetTimer() {
+ itemContainer.i = null;
+ timer.restart();
+ }
+
+ function test_tryVerify() {
+ timer.start();
+ tryVerify(function(){ return itemContainer.i; }, 200, "string");
+ compare(itemContainer.i, item);
+
+ resetTimer();
+ tryVerify(function(){ return itemContainer.i; }, 200);
+ compare(itemContainer.i, item);
+
+ resetTimer();
+ tryVerify(function(){ return itemContainer.i; });
+ compare(itemContainer.i, item);
+
+ resetTimer();
+ tryVerify(function(){ return !itemContainer.i; }, 0, "string");
+ verify(!itemContainer.i);
+ }
+}