diff options
| author | Vitaly Fanaskov <vitaly.fanaskov@qt.io> | 2019-08-06 15:47:50 +0200 |
|---|---|---|
| committer | Vitaly Fanaskov <vitaly.fanaskov@qt.io> | 2020-03-16 14:33:24 +0100 |
| commit | 1875ad7f92cad270cc5857d71096a4b46c27c562 (patch) | |
| tree | f8bfa68c889ea602e14d017e26030401ae7c1dc9 /tests | |
| parent | ea592334fdf12ce6625106c3f06bb57333690942 (diff) | |
Introduce new mechanism to manage palette functionality in QML
Main goals of these changes:
1) Add an ability to work with disabled and inactive palettes from QML
2) Eliminate massive code duplication in qtquickcontrols2 module
3) Provide easily extensible architecture for this piece of
functionality
Architectural part.
Palette
It was decided to not change existing QPalette, but add thin wrappers
around it to provide all required functionality. These wrappers are
highly coupled with QPalette class because of using some enum values
from it.
There are two new classes QQuickPalette and QQuickColorGroup.
QQuickPalette class inherits QQuickColorGroup class and represents
Active/All color group. QQuickPalette also provides an access to three
color groups: Active, Inactive, and Disabled.
In order to access colors the special class QQuickPaletteColorProvider
is used. This is a wrapper around QPalette that provides some
convenience functions.
Interface
The private property "palette" should be exposed.
Implementation
All private parts of classes that implement
QQuickAbstractPaletteProvider have to inherit
QQuickPaletteProviderPrivateBase class. This template class implement
all functionality: create palette, resolve dependencies, connect objects
etc. This is important to mention that related data is lazily
allocatable on demand only. Hence, there is no memory overhead for
regular items.
Change-Id: I911424b730451b1ad47f68fd8007953b66eddb28
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/auto/quick/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | tests/auto/quick/qquickcolorgroup/CMakeLists.txt | 8 | ||||
| -rw-r--r-- | tests/auto/quick/qquickcolorgroup/qquickcolorgroup.pro | 7 | ||||
| -rw-r--r-- | tests/auto/quick/qquickcolorgroup/tst_qquickcolorgroup.cpp | 117 | ||||
| -rw-r--r-- | tests/auto/quick/qquickdesignersupport/tst_qquickdesignersupport.cpp | 17 | ||||
| -rw-r--r-- | tests/auto/quick/qquickpalette/CMakeLists.txt | 8 | ||||
| -rw-r--r-- | tests/auto/quick/qquickpalette/qquickpalette.pro | 7 | ||||
| -rw-r--r-- | tests/auto/quick/qquickpalette/tst_qquickpalette.cpp | 304 | ||||
| -rw-r--r-- | tests/auto/quick/quick.pro | 2 | ||||
| -rw-r--r-- | tests/benchmarks/quick/colorresolving/colorresolving.pro | 14 | ||||
| -rw-r--r-- | tests/benchmarks/quick/colorresolving/data/tst_colorresolving.qml | 149 | ||||
| -rw-r--r-- | tests/benchmarks/quick/colorresolving/tst_colorresolving.cpp | 38 | ||||
| -rw-r--r-- | tests/benchmarks/quick/quick.pro | 3 |
13 files changed, 671 insertions, 5 deletions
diff --git a/tests/auto/quick/CMakeLists.txt b/tests/auto/quick/CMakeLists.txt index 187600dbf9..6cb3c23334 100644 --- a/tests/auto/quick/CMakeLists.txt +++ b/tests/auto/quick/CMakeLists.txt @@ -65,4 +65,6 @@ if(QT_FEATURE_private_tests) # add_subdirectory(touchmouse) # skip building until fixed in dev # special case add_subdirectory(scenegraph) add_subdirectory(sharedimage) + add_subdirectory(qquickcolorgroup) + add_subdirectory(qquickpalette) endif() diff --git a/tests/auto/quick/qquickcolorgroup/CMakeLists.txt b/tests/auto/quick/qquickcolorgroup/CMakeLists.txt new file mode 100644 index 0000000000..99506fc24d --- /dev/null +++ b/tests/auto/quick/qquickcolorgroup/CMakeLists.txt @@ -0,0 +1,8 @@ +qt_add_test(tst_qquickcolorgroup + SOURCES + tst_qquickcolorgroup.cpp + PUBLIC_LIBRARIES + Qt::CorePrivate + Qt::QmlPrivate + Qt::QuickPrivate +) diff --git a/tests/auto/quick/qquickcolorgroup/qquickcolorgroup.pro b/tests/auto/quick/qquickcolorgroup/qquickcolorgroup.pro new file mode 100644 index 0000000000..99c330199c --- /dev/null +++ b/tests/auto/quick/qquickcolorgroup/qquickcolorgroup.pro @@ -0,0 +1,7 @@ +CONFIG += testcase +TARGET = tst_qquickcolorgroup +SOURCES += tst_qquickcolorgroup.cpp + +macos:CONFIG -= app_bundle + +QT += core-private qml-private quick-private testlib diff --git a/tests/auto/quick/qquickcolorgroup/tst_qquickcolorgroup.cpp b/tests/auto/quick/qquickcolorgroup/tst_qquickcolorgroup.cpp new file mode 100644 index 0000000000..04cc81683c --- /dev/null +++ b/tests/auto/quick/qquickcolorgroup/tst_qquickcolorgroup.cpp @@ -0,0 +1,117 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** 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-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/qtest.h> +#include <QtTest/QSignalSpy> + +#include <QtGui/QPalette> + +#include <QtQuick/private/qquickcolorgroup_p.h> + +class tst_QQuickColorGroup : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void checkColorProperty(); + void checkColorProperty_data(); + + void colorGroupChangedWhenColorChanged(); +}; + +void tst_QQuickColorGroup::checkColorProperty() +{ + QFETCH(int, propertyIndex); + + auto property = QQuickColorGroup::staticMetaObject.property(propertyIndex); + + QVERIFY(property.isReadable()); + QVERIFY(property.isWritable()); + QVERIFY(property.isResettable()); + QVERIFY(property.hasNotifySignal()); + + const QQuickColorGroup defaultGroup; + QQuickColorGroup group; + + auto notifierSignature = QString::number(QSIGNAL_CODE) + property.notifySignal().methodSignature(); + QSignalSpy sp(&group, notifierSignature.toUtf8()); + + QVERIFY(property.write(&group, QColor(Qt::red))); + + QCOMPARE(qvariant_cast<QColor>(property.read(&group)), QColor(Qt::red)); + + QVERIFY(property.reset(&group)); + + QCOMPARE(qvariant_cast<QColor>(property.read(&group)), + qvariant_cast<QColor>(property.read(&defaultGroup))); + + constexpr int expectedNotificationsCount = 2; // One from write + one from reset + QCOMPARE(sp.count(), expectedNotificationsCount); +} + +void tst_QQuickColorGroup::checkColorProperty_data() +{ + QTest::addColumn<int>("propertyIndex"); + + auto mo = QQuickColorGroup::staticMetaObject; + for (int i = mo.propertyOffset(); i < mo.propertyCount(); ++i) { + auto property = mo.property(i); + if (property.type() == QVariant::Color) { + QTest::addRow("%s", property.name()) << i; + } + } +} + +void tst_QQuickColorGroup::colorGroupChangedWhenColorChanged() +{ + QQuickColorGroup group; + group.setGroupTag(QPalette::Active); + + QSignalSpy sp(&group, &QQuickColorGroup::changed); + + QVERIFY(group.mid() != Qt::blue); + + group.setMid(Qt::blue); + + QCOMPARE(sp.count(), 1); +} + +QTEST_MAIN(tst_QQuickColorGroup) + +#include "tst_qquickcolorgroup.moc" diff --git a/tests/auto/quick/qquickdesignersupport/tst_qquickdesignersupport.cpp b/tests/auto/quick/qquickdesignersupport/tst_qquickdesignersupport.cpp index 062a8b5c9a..a4ef25be92 100644 --- a/tests/auto/quick/qquickdesignersupport/tst_qquickdesignersupport.cpp +++ b/tests/auto/quick/qquickdesignersupport/tst_qquickdesignersupport.cpp @@ -461,9 +461,14 @@ void tst_qquickdesignersupport::testNotifyPropertyChangeCallBack() QCOMPARE(s_propertyName, QQuickDesignerSupport::PropertyName("gradient")); } +// We have to use this ugly approach, because the signature of +// registerFixResourcePathsForObjectCallBack doesn't accept +// a proper lambda with a capture list +static QVector<QObject*> s_allSubObjects; + static void fixResourcePathsForObjectCallBackFunction(QObject *object) { - s_object = object; + s_allSubObjects << object; } static void (*fixResourcePathsForObjectCallBackPointer)(QObject *) = &fixResourcePathsForObjectCallBackFunction; @@ -480,7 +485,7 @@ void tst_qquickdesignersupport::testFixResourcePathsForObjectCallBack() QVERIFY(rootItem); - s_object = nullptr; + s_allSubObjects.clear(); QQuickDesignerSupportItems::registerFixResourcePathsForObjectCallBack(fixResourcePathsForObjectCallBackPointer); @@ -490,8 +495,12 @@ void tst_qquickdesignersupport::testFixResourcePathsForObjectCallBack() QQuickDesignerSupportItems::tweakObjects(simpleItem); - //Check that the fixResourcePathsForObjectCallBack was called on simpleItem - QCOMPARE(simpleItem , s_object); + // Check that the fixResourcePathsForObjectCallBack was called on simpleItem + // NOTE: more objects are collected now. There is also at least a palette + // that created on demand. + QVERIFY(s_allSubObjects.contains(simpleItem)); + + s_allSubObjects.clear(); } void doComponentCompleteRecursive(QObject *object) diff --git a/tests/auto/quick/qquickpalette/CMakeLists.txt b/tests/auto/quick/qquickpalette/CMakeLists.txt new file mode 100644 index 0000000000..20a8081c48 --- /dev/null +++ b/tests/auto/quick/qquickpalette/CMakeLists.txt @@ -0,0 +1,8 @@ +qt_add_test(tst_qquickpalette + SOURCES + tst_qquickpalette.cpp + PUBLIC_LIBRARIES + Qt::CorePrivate + Qt::QmlPrivate + Qt::QuickPrivate +) diff --git a/tests/auto/quick/qquickpalette/qquickpalette.pro b/tests/auto/quick/qquickpalette/qquickpalette.pro new file mode 100644 index 0000000000..96c508d82a --- /dev/null +++ b/tests/auto/quick/qquickpalette/qquickpalette.pro @@ -0,0 +1,7 @@ +CONFIG += testcase +TARGET = tst_qquickpalette +SOURCES += tst_qquickpalette.cpp + +macos:CONFIG -= app_bundle + +QT += core-private qml-private quick-private testlib diff --git a/tests/auto/quick/qquickpalette/tst_qquickpalette.cpp b/tests/auto/quick/qquickpalette/tst_qquickpalette.cpp new file mode 100644 index 0000000000..e211a034ee --- /dev/null +++ b/tests/auto/quick/qquickpalette/tst_qquickpalette.cpp @@ -0,0 +1,304 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** 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-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/qtest.h> +#include <QtTest/QSignalSpy> + +#include <QtQuick/private/qquickpalette_p.h> +#include <QtQuick/private/qquickabstractpaletteprovider_p.h> +#include <QtQuick/private/qquickpalettecolorprovider_p.h> + +class tst_QQuickPalette : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void resolvingColor(); + void resolvingColor_data(); + + void newColorSubgroup(); + void newColorSubgroup_data(); + + void onlyRespectiveColorSubgroupChangedAfterAssigment(); + + void paletteChangedWhenColorGroupChanged(); + + void createDefault(); + + void changeCurrentColorGroup(); + + void inheritColor(); + + void inheritCurrentColor(); + + void overrideColor(); + + void resolveColor(); + + void createFromQtPalette(); + void convertToQtPalette(); +}; + +using GroupGetter = QQuickColorGroup* (QQuickPalette::* )() const; +Q_DECLARE_METATYPE(GroupGetter); + +void tst_QQuickPalette::resolvingColor() +{ + QFETCH(QPalette::ColorGroup, colorGroup); + QFETCH(GroupGetter, getter); + + QQuickPalette p; + p.setWindowText(Qt::red); + + auto g = (p.*getter)(); + + QVERIFY(g); + + g->setWindowText(Qt::green); + p.setCurrentGroup(colorGroup); + + QCOMPARE(p.windowText(), Qt::green); +} + +void tst_QQuickPalette::resolvingColor_data() +{ + QTest::addColumn<QPalette::ColorGroup>("colorGroup"); + QTest::addColumn<GroupGetter>("getter"); + + QTest::addRow("Inactive") << QPalette::Inactive << &QQuickPalette::inactive; + QTest::addRow("Disabled") << QPalette::Disabled << &QQuickPalette::disabled; +} + +using GroupSetter = void (QQuickPalette::* )(QQuickColorGroup *); +Q_DECLARE_METATYPE(GroupSetter); + +using GroupNotifier = void (QQuickPalette::* )(); +Q_DECLARE_METATYPE(GroupNotifier); + +void tst_QQuickPalette::newColorSubgroup() +{ + QFETCH(GroupGetter, getter); + QFETCH(GroupSetter, setter); + QFETCH(GroupNotifier, notifier); + + { + QQuickPalette p; + p.fromQPalette(Qt::blue); + + auto defaultGroup = (p.*getter)(); + QVERIFY(defaultGroup); + + QSignalSpy subgroupChanged(&p, notifier); + QSignalSpy paletteChanged(&p, &QQuickPalette::changed); + + QQuickPalette anotherPalette; + anotherPalette.fromQPalette(Qt::red); + (p.*setter)((anotherPalette.*getter)()); + + QCOMPARE(subgroupChanged.count(), 1); + QCOMPARE(paletteChanged.count(), 1); + } +} + +void tst_QQuickPalette::newColorSubgroup_data() +{ + QTest::addColumn<GroupGetter>("getter"); + QTest::addColumn<GroupSetter>("setter"); + QTest::addColumn<GroupNotifier>("notifier"); + + QTest::addRow("Active") << &QQuickPalette::active << &QQuickPalette::setActive + << &QQuickPalette::activeChanged; + QTest::addRow("Inactive") << &QQuickPalette::inactive << &QQuickPalette::setInactive + << &QQuickPalette::inactiveChanged; + QTest::addRow("Disabled") << &QQuickPalette::disabled << &QQuickPalette::setDisabled + << &QQuickPalette::disabledChanged; +} + +void tst_QQuickPalette::onlyRespectiveColorSubgroupChangedAfterAssigment() +{ + QQuickPalette palette; + palette.setWindow(Qt::red); + + QQuickPalette anotherPalette; + anotherPalette.active()->setWindow(Qt::green); + + // Only active subgroup should be copied + palette.setActive(anotherPalette.active()); + + QCOMPARE(palette.active()->window(), Qt::green); + QCOMPARE(palette.disabled()->window(), Qt::red); + QCOMPARE(palette.inactive()->window(), Qt::red); +} + +void tst_QQuickPalette::paletteChangedWhenColorGroupChanged() +{ + QQuickPalette p; + QSignalSpy sp(&p, &QQuickPalette::changed); + + p.active()->setMid(Qt::red); + p.inactive()->setMid(Qt::green); + p.disabled()->setMid(Qt::blue); + + QCOMPARE(sp.count(), 3); +} + +void tst_QQuickPalette::createDefault() +{ + QQuickPalette palette; + + QCOMPARE(palette.currentColorGroup(), QPalette::Active); + QCOMPARE(palette.active()->groupTag(), QPalette::Active); + QCOMPARE(palette.inactive()->groupTag(), QPalette::Inactive); + QCOMPARE(palette.disabled()->groupTag(), QPalette::Disabled); +} + +void tst_QQuickPalette::changeCurrentColorGroup() +{ + QQuickPalette palette; + + QSignalSpy ss(&palette, &QQuickPalette::changed); + palette.setCurrentGroup(QPalette::Disabled); + + QCOMPARE(palette.currentColorGroup(), QPalette::Disabled); + QCOMPARE(ss.count(), 1); +} + +void tst_QQuickPalette::inheritColor() +{ + QQuickPalette parentPalette; + parentPalette.setWindowText(Qt::red); + + QQuickPalette quickPalette; + quickPalette.inheritPalette(parentPalette.toQPalette()); + + QCOMPARE(quickPalette.windowText(), Qt::red); + + QQuickPalette childQuickPalette; + childQuickPalette.inheritPalette(quickPalette.toQPalette()); + + QCOMPARE(childQuickPalette.windowText(), Qt::red); +} + +void tst_QQuickPalette::inheritCurrentColor() +{ + QQuickPalette parentPalette; + parentPalette.setWindowText(Qt::green); + parentPalette.disabled()->setWindowText(Qt::red); + + + QQuickPalette quickPalette; + quickPalette.inheritPalette(parentPalette.toQPalette()); + quickPalette.setCurrentGroup(QPalette::Disabled); + + QCOMPARE(quickPalette.windowText(), Qt::red); +} + +void tst_QQuickPalette::overrideColor() +{ + QQuickPalette rootPalette; + rootPalette.setWindowText(Qt::red); + + QQuickPalette palette; + palette.inheritPalette(rootPalette.toQPalette()); + palette.setWindowText(Qt::yellow); + + QCOMPARE(palette.windowText(), Qt::yellow); + + QQuickPalette childPalette; + childPalette.inheritPalette(palette.toQPalette()); + childPalette.disabled()->setWindowText(Qt::green); + + // Color is not set for the current group. Use parent color + QCOMPARE(childPalette.windowText(), Qt::yellow); + + // Change current group to use color, specified for this particular group + childPalette.setCurrentGroup(QPalette::Disabled); + + QCOMPARE(childPalette.windowText(), Qt::green); +} + +void tst_QQuickPalette::resolveColor() +{ + QQuickPalette palette; + palette.setWindowText(Qt::red); + + // Disabled color should be red, because disabled palette is not specified + palette.setCurrentGroup(QPalette::Disabled); + QCOMPARE(palette.windowText(), Qt::red); + + // Color is changed for disabled palette, because current color group is QPalette::Disabled + palette.disabled()->setWindowText(Qt::yellow); + QCOMPARE(palette.windowText(), Qt::yellow); + QCOMPARE(palette.disabled()->windowText(), Qt::yellow); + + // Change color group back to active + palette.setCurrentGroup(QPalette::Active); + QCOMPARE(palette.windowText(), Qt::red); +} + +void tst_QQuickPalette::createFromQtPalette() +{ + QQuickPalette palette; + QPalette somePalette(Qt::red); + + QSignalSpy sp(&palette, &QQuickColorGroup::changed); + + palette.fromQPalette(QPalette()); + QCOMPARE(sp.count(), 0); + + palette.fromQPalette(somePalette); + QCOMPARE(sp.count(), 1); +} + +void tst_QQuickPalette::convertToQtPalette() +{ + QQuickPalette palette; + + QPalette somePalette(Qt::red); + palette.fromQPalette(somePalette); + + auto pp = palette.paletteProvider(); + QVERIFY(pp); + + QCOMPARE(palette.toQPalette(), somePalette.resolve(pp->defaultPalette())); +} + +QTEST_MAIN(tst_QQuickPalette) + +#include "tst_qquickpalette.moc" diff --git a/tests/auto/quick/quick.pro b/tests/auto/quick/quick.pro index 9ab7119903..71b5794e81 100644 --- a/tests/auto/quick/quick.pro +++ b/tests/auto/quick/quick.pro @@ -42,6 +42,8 @@ PRIVATETESTS += \ qquickstyledtext \ qquickstates \ qquicksystempalette \ + qquickcolorgroup \ + qquickpalette \ qquicktimeline QUICKTESTS += \ diff --git a/tests/benchmarks/quick/colorresolving/colorresolving.pro b/tests/benchmarks/quick/colorresolving/colorresolving.pro new file mode 100644 index 0000000000..dc238caf44 --- /dev/null +++ b/tests/benchmarks/quick/colorresolving/colorresolving.pro @@ -0,0 +1,14 @@ +TEMPLATE = app +TARGET = tst_colorresolving +CONFIG += qmltestcase + +macos:CONFIG -= app_bundle + +SOURCES += \ + $$PWD/tst_colorresolving.cpp + +OTHER_FILES += \ + $$PWD/data/*.qml + +TESTDATA += \ + $$PWD/data/tst_* diff --git a/tests/benchmarks/quick/colorresolving/data/tst_colorresolving.qml b/tests/benchmarks/quick/colorresolving/data/tst_colorresolving.qml new file mode 100644 index 0000000000..37aecaa3a4 --- /dev/null +++ b/tests/benchmarks/quick/colorresolving/data/tst_colorresolving.qml @@ -0,0 +1,149 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +import QtTest 1.2 +import QtQuick 6.0 + +TestCase { + id: testCase + width: 800 + height: 600 + visible: true + when: windowShown + + property var items: [] + property int items_count: 1000 + property int itemWidth : 50 + property int itemHeight: 50 + + // A reference point (added upon request) + function benchmark_do_nothing_with_palettes() {} + + // This test passes through all items in tree to resolve a color. No extra palettes created. + // Should be blazingly fast (approx. 0.01 msecs for 999 items). + function benchmark_color_lookup() { + // Make last item "visible" and create its palette + items[items_count - 1].color = items[items_count - 1].palette.button + + compare(items[0].palette.button, items[items_count - 1].palette.button, + "Color is not propagated to the last element.") + } + + // This test creates palettes for all elements in the tree. + function benchmark_create_all_palettes(data) { + populate_palettes() + check_palettes() + } + + // This test creates and resolves palettes for all elements in the tree. + function benchmark_create_and_resolve_all_palettes() { + populate_palettes() + resolve_palettes() + check_palettes() + } + + function init() { + // Re-create all items on each iteration of the benchmark. + + var componentStr = "import QtQuick 6.0; + + Rectangle { + x: mapFromItem(testCase, testCase.randomX(), testCase.randomY()).x + y: mapFromItem(testCase, testCase.randomX(), testCase.randomY()).y + + color: \"#7F696969\" + + width: testCase.itemWidth + height: testCase.itemHeight + }"; + items.push(createTemporaryQmlObject(componentStr, testCase)) + for (var i = 1; i < items_count; ++i) { + items.push(createTemporaryQmlObject(componentStr, items[i - 1])) + } + + // Create a pallet for item 0 + items[0].palette.button = randomRgba() + + // Make item "visible" (can be overlapped by children) + items[0].color = items[0].palette.button + } + + function cleanup() { + // Explicitly remove all "temporary" items to make sure that a memory + // will be released after each iteration of the benchmark. + for (var i = 0; i < items_count; ++i) { + items[i].destroy(); + } + + items = []; + } + + function randomColorComponent() { + return Math.floor(Math.random() * 256) / 255.0; + } + + function randomRgba() { + return Qt.rgba(randomColorComponent(), + randomColorComponent(), + randomColorComponent(), + randomColorComponent()); + } + + function randomCoordinate(len, itemLen) { return Math.floor(Math.random() * (len - itemLen + 1)); } + function randomX() { return randomCoordinate(width, itemWidth); } + function randomY() { return randomCoordinate(height, itemHeight); } + + function populate_palettes() { + for (var i = 1; i < items_count; ++i) { + items[i].color = items[i].palette.button + } + } + + function check_palettes() { + for (var j = 1; j < items_count; ++j) { + compare(items[j - 1].palette.button, items[j].palette.button, + "Color is not propagated to the next child element.") + } + } + + function resolve_palettes() { + // The loop is just in case. Doesn't affect the benchmark + do { + var randomColor = randomRgba() + } while (items[0].palette.button === randomColor) + + items[0].palette.button = randomColor + } +} diff --git a/tests/benchmarks/quick/colorresolving/tst_colorresolving.cpp b/tests/benchmarks/quick/colorresolving/tst_colorresolving.cpp new file mode 100644 index 0000000000..1ec4781d6c --- /dev/null +++ b/tests/benchmarks/quick/colorresolving/tst_colorresolving.cpp @@ -0,0 +1,38 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtQuickTest/quicktest.h> +QUICK_TEST_MAIN(tst_colorresolving) diff --git a/tests/benchmarks/quick/quick.pro b/tests/benchmarks/quick/quick.pro index 87df78bd2f..f6de5ec287 100644 --- a/tests/benchmarks/quick/quick.pro +++ b/tests/benchmarks/quick/quick.pro @@ -1,4 +1,5 @@ TEMPLATE = subdirs SUBDIRS += \ - events + events \ + colorresolving |
