diff options
Diffstat (limited to 'tests/auto/quickcontrols/palette')
21 files changed, 966 insertions, 0 deletions
diff --git a/tests/auto/quickcontrols/palette/CMakeLists.txt b/tests/auto/quickcontrols/palette/CMakeLists.txt new file mode 100644 index 0000000000..d0dad952a6 --- /dev/null +++ b/tests/auto/quickcontrols/palette/CMakeLists.txt @@ -0,0 +1,70 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +# Generated from palette.pro. + +if (NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_palette LANGUAGES C CXX ASM) + find_package(Qt6BuildInternals COMPONENTS STANDALONE_TEST) +endif() + +##################################################################### +## tst_palette Test: +##################################################################### + +# Collect test data +file(GLOB_RECURSE test_data_glob + RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + data/*) +list(APPEND test_data ${test_data_glob}) + +qt_internal_add_test(tst_palette + SOURCES + tst_palette.cpp + DEFINES + QQC2_IMPORT_PATH=\\\"${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/quickcontrols\\\" + LIBRARIES + Qt::CorePrivate + Qt::Gui + Qt::GuiPrivate + Qt::QmlPrivate + Qt::QuickControls2 + Qt::QuickControls2Private + Qt::QuickControlsTestUtilsPrivate + Qt::QuickPrivate + Qt::QuickTemplates2Private + Qt::QuickTest + Qt::QuickTestUtilsPrivate + Qt::TestPrivate + TESTDATA ${test_data} +) + +# Resources: +set(qmake_immediate_resource_files + "qtquickcontrols2.conf" +) + +qt_internal_add_resource(tst_palette "qmake_immediate" + PREFIX + "/" + FILES + ${qmake_immediate_resource_files} +) + + +#### Keys ignored in scope 1:.:.:palette.pro:<TRUE>: +# OTHER_FILES = "data/*.qml" + +## Scopes: +##################################################################### + +qt_internal_extend_target(tst_palette CONDITION ANDROID OR IOS + DEFINES + QT_QMLTEST_DATADIR=\\\":/data\\\" +) + +qt_internal_extend_target(tst_palette CONDITION NOT ANDROID AND NOT IOS + DEFINES + QT_QMLTEST_DATADIR=\\\"${CMAKE_CURRENT_SOURCE_DIR}/data\\\" +) diff --git a/tests/auto/quickcontrols/palette/data/bindings.qml b/tests/auto/quickcontrols/palette/data/bindings.qml new file mode 100644 index 0000000000..a584007e54 --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/bindings.qml @@ -0,0 +1,53 @@ +// Copyright (C) 2020 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Window +import QtQuick.Controls +import QtQuick.Layouts + +ApplicationWindow { + id: window + objectName: "window" + width: 600 + height: 800 + visible: true + + property alias disabledButton: disabledButton + property alias enabledButton: enabledButton + + palette { + active { + button: "khaki" + buttonText: "bisque" + } + + inactive { + button: "khaki" + buttonText: "bisque" + } + + disabled { + buttonText: "lavender" + button: "coral" + } + } + + ColumnLayout { + Button { + id: disabledButton + text: "Disabled" + enabled: false + + palette.disabled.button: "aqua" + palette.disabled.buttonText: "azure" + } + + Button { + id: enabledButton + text: "Enabled" + + palette: disabledButton.palette + } + } +} diff --git a/tests/auto/quickcontrols/palette/data/inheritance-childcontrol.qml b/tests/auto/quickcontrols/palette/data/inheritance-childcontrol.qml new file mode 100644 index 0000000000..f476020517 --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/inheritance-childcontrol.qml @@ -0,0 +1,29 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + id: window + + property alias control: control + property alias child: child + property alias grandChild: grandChild + + Item { + Control { + id: control + + Control { + id: child + + Item { + Control { + id: grandChild + } + } + } + } + } +} diff --git a/tests/auto/quickcontrols/palette/data/inheritance-childpopup.qml b/tests/auto/quickcontrols/palette/data/inheritance-childpopup.qml new file mode 100644 index 0000000000..8b7a033bd7 --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/inheritance-childpopup.qml @@ -0,0 +1,29 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + id: window + + property alias control: control + property alias child: child + property alias grandChild: grandChild + + Item { + Popup { + id: control + + Control { + id: child + + Item { + Control { + id: grandChild + } + } + } + } + } +} diff --git a/tests/auto/quickcontrols/palette/data/inheritance-control.qml b/tests/auto/quickcontrols/palette/data/inheritance-control.qml new file mode 100644 index 0000000000..2cd5c125f4 --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/inheritance-control.qml @@ -0,0 +1,27 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + id: window + + property alias control: control + property alias child: child + property alias grandChild: grandChild + + Control { + id: control + + Control { + id: child + + Item { + Control { + id: grandChild + } + } + } + } +} diff --git a/tests/auto/quickcontrols/palette/data/inheritance-dynamicchildcontrol.qml b/tests/auto/quickcontrols/palette/data/inheritance-dynamicchildcontrol.qml new file mode 100644 index 0000000000..18305058bc --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/inheritance-dynamicchildcontrol.qml @@ -0,0 +1,28 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + id: window + + property Control control + property Control child + property Control grandChild + + Item { + id: childItem + } + + Component { + id: component + Control { } + } + + Component.onCompleted: { + control = component.createObject(childItem) + child = component.createObject(control) + grandChild = component.createObject(child) + } +} diff --git a/tests/auto/quickcontrols/palette/data/inheritance-dynamicchildpopup.qml b/tests/auto/quickcontrols/palette/data/inheritance-dynamicchildpopup.qml new file mode 100644 index 0000000000..6b16f1a46b --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/inheritance-dynamicchildpopup.qml @@ -0,0 +1,33 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + id: window + + property Popup control + property Control child + property Control grandChild + + Item { + id: childItem + } + + Component { + id: popupComponent + Popup { } + } + + Component { + id: controlComponent + Control { } + } + + Component.onCompleted: { + control = popupComponent.createObject(childItem) + child = controlComponent.createObject(control.contentItem) + grandChild = controlComponent.createObject(child) + } +} diff --git a/tests/auto/quickcontrols/palette/data/inheritance-dynamiccontrol.qml b/tests/auto/quickcontrols/palette/data/inheritance-dynamiccontrol.qml new file mode 100644 index 0000000000..df0e13bf2c --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/inheritance-dynamiccontrol.qml @@ -0,0 +1,24 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + id: window + + property Control control + property Control child + property Control grandChild + + Component { + id: component + Control { } + } + + Component.onCompleted: { + control = component.createObject(contentItem) + child = component.createObject(control) + grandChild = component.createObject(child) + } +} diff --git a/tests/auto/quickcontrols/palette/data/inheritance-dynamicpopup.qml b/tests/auto/quickcontrols/palette/data/inheritance-dynamicpopup.qml new file mode 100644 index 0000000000..76b7d7cbda --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/inheritance-dynamicpopup.qml @@ -0,0 +1,29 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + id: window + + property Popup control + property Control child + property Control grandChild + + Component { + id: popupComponent + Popup { } + } + + Component { + id: controlComponent + Control { } + } + + Component.onCompleted: { + control = popupComponent.createObject(window) + child = controlComponent.createObject(control.contentItem) + grandChild = controlComponent.createObject(child) + } +} diff --git a/tests/auto/quickcontrols/palette/data/inheritance-popup.qml b/tests/auto/quickcontrols/palette/data/inheritance-popup.qml new file mode 100644 index 0000000000..a5010f0f11 --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/inheritance-popup.qml @@ -0,0 +1,27 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + id: window + + property alias control: control + property alias child: child + property alias grandChild: grandChild + + Popup { + id: control + + Control { + id: child + + Item { + Control { + id: grandChild + } + } + } + } +} diff --git a/tests/auto/quickcontrols/palette/data/listview.qml b/tests/auto/quickcontrols/palette/data/listview.qml new file mode 100644 index 0000000000..92816fde19 --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/listview.qml @@ -0,0 +1,32 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + id: window + width: 200 + height: 200 + + property alias listView: listView + + palette.highlight: "red" + + ListView { + id: listView + anchors.fill: parent + model: 1 + delegate: Column { + property alias control: control + property alias label: label + property alias textarea: textarea + property alias textfield: textfield + + Control { id: control } + Label { id: label } + TextArea { id: textarea } + TextField { id: textfield } + } + } +} diff --git a/tests/auto/quickcontrols/palette/data/palette-appwindow-custom.qml b/tests/auto/quickcontrols/palette/data/palette-appwindow-custom.qml new file mode 100644 index 0000000000..22876b90fa --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/palette-appwindow-custom.qml @@ -0,0 +1,28 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + palette.alternateBase: "aqua" + palette.base: "azure" + palette.brightText: "beige" + palette.button: "bisque" + palette.buttonText: "chocolate" + palette.dark: "coral" + palette.highlight: "crimson" + palette.highlightedText: "fuchsia" + palette.light: "gold" + palette.link: "indigo" + palette.linkVisited: "ivory" + palette.mid: "khaki" + palette.midlight: "lavender" + palette.shadow: "linen" + palette.text: "moccasin" + palette.toolTipBase: "navy" + palette.toolTipText: "orchid" + palette.window: "plum" + palette.windowText: "salmon" + palette.placeholderText: "magenta" +} diff --git a/tests/auto/quickcontrols/palette/data/palette-appwindow-default.qml b/tests/auto/quickcontrols/palette/data/palette-appwindow-default.qml new file mode 100644 index 0000000000..f9c767b876 --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/palette-appwindow-default.qml @@ -0,0 +1,8 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +ApplicationWindow { +} diff --git a/tests/auto/quickcontrols/palette/data/palette-control-custom.qml b/tests/auto/quickcontrols/palette/data/palette-control-custom.qml new file mode 100644 index 0000000000..4c4206cdfd --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/palette-control-custom.qml @@ -0,0 +1,28 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +Control { + palette.alternateBase: "aqua" + palette.base: "azure" + palette.brightText: "beige" + palette.button: "bisque" + palette.buttonText: "chocolate" + palette.dark: "coral" + palette.highlight: "crimson" + palette.highlightedText: "fuchsia" + palette.light: "gold" + palette.link: "indigo" + palette.linkVisited: "ivory" + palette.mid: "khaki" + palette.midlight: "lavender" + palette.shadow: "linen" + palette.text: "moccasin" + palette.toolTipBase: "navy" + palette.toolTipText: "orchid" + palette.window: "plum" + palette.windowText: "salmon" + palette.placeholderText: "magenta" +} diff --git a/tests/auto/quickcontrols/palette/data/palette-control-default.qml b/tests/auto/quickcontrols/palette/data/palette-control-default.qml new file mode 100644 index 0000000000..7cd58e12b3 --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/palette-control-default.qml @@ -0,0 +1,8 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +Control { +} diff --git a/tests/auto/quickcontrols/palette/data/palette-popup-custom.qml b/tests/auto/quickcontrols/palette/data/palette-popup-custom.qml new file mode 100644 index 0000000000..c534d30156 --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/palette-popup-custom.qml @@ -0,0 +1,28 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +Popup { + palette.alternateBase: "aqua" + palette.base: "azure" + palette.brightText: "beige" + palette.button: "bisque" + palette.buttonText: "chocolate" + palette.dark: "coral" + palette.highlight: "crimson" + palette.highlightedText: "fuchsia" + palette.light: "gold" + palette.link: "indigo" + palette.linkVisited: "ivory" + palette.mid: "khaki" + palette.midlight: "lavender" + palette.shadow: "linen" + palette.text: "moccasin" + palette.toolTipBase: "navy" + palette.toolTipText: "orchid" + palette.window: "plum" + palette.windowText: "salmon" + palette.placeholderText: "magenta" +} diff --git a/tests/auto/quickcontrols/palette/data/palette-popup-default.qml b/tests/auto/quickcontrols/palette/data/palette-popup-default.qml new file mode 100644 index 0000000000..b9db73c9fb --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/palette-popup-default.qml @@ -0,0 +1,8 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +Popup { +} diff --git a/tests/auto/quickcontrols/palette/data/resolve.qml b/tests/auto/quickcontrols/palette/data/resolve.qml new file mode 100644 index 0000000000..78cf091848 --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/resolve.qml @@ -0,0 +1,21 @@ +// Copyright (C) 2022 zccrs <zccrs@live.com>, JiDe Zhang <zhangjide@uniontech.com>. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + id: window + property alias control: control + + palette {window: "blue"; windowText: "white"} + + Control { + id: control + } + + function changeColors(color) { + control.palette.window = color + window.palette.windowText = color + } +} diff --git a/tests/auto/quickcontrols/palette/data/set-palette.qml b/tests/auto/quickcontrols/palette/data/set-palette.qml new file mode 100644 index 0000000000..b987a7d6b1 --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/set-palette.qml @@ -0,0 +1,19 @@ +// Copyright (C) 2020 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls + +Control { + palette { + active { + buttonText: "azure" + button: "khaki" + } + + disabled { + buttonText: "lavender" + button: "coral" + } + } +} diff --git a/tests/auto/quickcontrols/palette/qtquickcontrols2.conf b/tests/auto/quickcontrols/palette/qtquickcontrols2.conf new file mode 100644 index 0000000000..c5de6b36dd --- /dev/null +++ b/tests/auto/quickcontrols/palette/qtquickcontrols2.conf @@ -0,0 +1,5 @@ +[Basic] +Palette\Base=#efefef + +[Basic\Palette] +Text=#101010 diff --git a/tests/auto/quickcontrols/palette/tst_palette.cpp b/tests/auto/quickcontrols/palette/tst_palette.cpp new file mode 100644 index 0000000000..e3a53f0c21 --- /dev/null +++ b/tests/auto/quickcontrols/palette/tst_palette.cpp @@ -0,0 +1,432 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#include <QtTest/qtest.h> +#include <QtGui/qpalette.h> +#include <QtGui/private/qguiapplication_p.h> +#include <QtQml/qqmlengine.h> +#include <QtQml/qqmlcomponent.h> +#include <QtQuickTestUtils/private/qmlutils_p.h> +#include <QtQuick/private/qquickitem_p.h> +#include <QtQuickTemplates2/private/qquickapplicationwindow_p.h> +#include <QtQuickTemplates2/private/qquickcontrol_p.h> +#include <QtQuickTemplates2/private/qquickcontrol_p_p.h> +#include <QtQuickTemplates2/private/qquickpopup_p.h> +#include <QtQuickTemplates2/private/qquickpopup_p_p.h> +#include <QtQuickTemplates2/private/qquicktheme_p_p.h> +#include <QtQuickTemplates2/private/qquickbutton_p.h> +#include <QtQuickControls2/qquickstyle.h> + +//using namespace QQuickVisualTestUtils; + +// Need a more descriptive failure message: QTBUG-87039 +#define COMPARE_PALETTES(actualPalette, expectedPalette) \ + QVERIFY2(actualPalette == expectedPalette, \ + qPrintable(QString::fromLatin1("\n Actual: %1\n Expected: %2") \ + .arg(QDebug::toString(actualPalette)).arg(QDebug::toString(expectedPalette)))); + +class tst_palette : public QQmlDataTest +{ + Q_OBJECT + +public: + tst_palette(); + +private slots: + void initTestCase() override; + + void palette_data(); + void palette(); + + void inheritance_data(); + void inheritance(); + + void defaultPalette_data(); + void defaultPalette(); + + void listView_data(); + void listView(); + + void setDynamicallyCreatedPalette(); + void createBindings(); + void updateBindings(); + + void resolve(); +}; + +tst_palette::tst_palette() + : QQmlDataTest(QT_QMLTEST_DATADIR) +{ +} + +void tst_palette::initTestCase() +{ + QQuickStyle::setStyle("Basic"); + + QQmlDataTest::initTestCase(); + + // Import QtQuick.Controls to initialize styles and themes so that + // QQuickControlPrivate::themePalette() returns a palette from the + // style's theme instead of the platform's theme. + QQmlEngine engine; + QQmlComponent component(&engine); + component.setData("import QtQuick.Controls; Control { }", QUrl()); + delete component.create(); +} + +void tst_palette::palette_data() +{ + QTest::addColumn<QString>("testFile"); + QTest::addColumn<QPalette>("expectedPalette"); + + const QPalette defaultPalette = QQuickTheme::palette(QQuickTheme::System); + + QTest::newRow("Control") << "palette-control-default.qml" << defaultPalette; + QTest::newRow("AppWindow") << "palette-appwindow-default.qml" << defaultPalette; + QTest::newRow("Popup") << "palette-popup-default.qml" << defaultPalette; + + QPalette customPalette; + customPalette.setColor(QPalette::AlternateBase, QColor("aqua")); + customPalette.setColor(QPalette::Base, QColor("azure")); + customPalette.setColor(QPalette::BrightText, QColor("beige")); + customPalette.setColor(QPalette::Button, QColor("bisque")); + customPalette.setColor(QPalette::ButtonText, QColor("chocolate")); + customPalette.setColor(QPalette::Dark, QColor("coral")); + customPalette.setColor(QPalette::Highlight, QColor("crimson")); + customPalette.setColor(QPalette::HighlightedText, QColor("fuchsia")); + customPalette.setColor(QPalette::Light, QColor("gold")); + customPalette.setColor(QPalette::Link, QColor("indigo")); + customPalette.setColor(QPalette::LinkVisited, QColor("ivory")); + customPalette.setColor(QPalette::Mid, QColor("khaki")); + customPalette.setColor(QPalette::Midlight, QColor("lavender")); + customPalette.setColor(QPalette::Shadow, QColor("linen")); + customPalette.setColor(QPalette::Text, QColor("moccasin")); + customPalette.setColor(QPalette::ToolTipBase, QColor("navy")); + customPalette.setColor(QPalette::ToolTipText, QColor("orchid")); + customPalette.setColor(QPalette::Window, QColor("plum")); + customPalette.setColor(QPalette::WindowText, QColor("salmon")); + customPalette.setColor(QPalette::PlaceholderText, QColor("magenta")); + + QTest::newRow("Control:custom") << "palette-control-custom.qml" << customPalette; + QTest::newRow("AppWindow:custom") << "palette-appwindow-custom.qml" << customPalette; + QTest::newRow("Popup:custom") << "palette-popup-custom.qml" << customPalette; +} + +void tst_palette::palette() +{ + QFETCH(QString, testFile); + QFETCH(QPalette, expectedPalette); + + QQmlEngine engine; + QQmlComponent component(&engine); + component.loadUrl(testFileUrl(testFile)); + + QScopedPointer<QObject> object(component.create()); + QVERIFY2(!object.isNull(), qPrintable(component.errorString())); + + QVariant var = object->property("palette"); + QVERIFY(var.isValid()); + + COMPARE_PALETTES(var.value<QQuickPalette*>()->toQPalette(), expectedPalette); +} + +void tst_palette::inheritance_data() +{ + QTest::addColumn<QString>("testFile"); + + QTest::newRow("Control") << "inheritance-control.qml"; + QTest::newRow("Child Control") << "inheritance-childcontrol.qml"; + QTest::newRow("Dynamic Control") << "inheritance-dynamiccontrol.qml"; + QTest::newRow("Dynamic Child Control") << "inheritance-dynamicchildcontrol.qml"; + + QTest::newRow("Popup") << "inheritance-popup.qml"; + QTest::newRow("Child Popup") << "inheritance-childpopup.qml"; + QTest::newRow("Dynamic Popup") << "inheritance-dynamicpopup.qml"; + QTest::newRow("Dynamic Child Popup") << "inheritance-dynamicchildpopup.qml"; +} + +void tst_palette::inheritance() +{ + QFETCH(QString, testFile); + + QQmlEngine engine; + QQmlComponent component(&engine); + component.loadUrl(testFileUrl(testFile)); + + QScopedPointer<QQuickApplicationWindow> window(qobject_cast<QQuickApplicationWindow *>(component.create())); + QVERIFY2(!window.isNull(), qPrintable(component.errorString())); + + QObject *control = window->property("control").value<QObject *>(); + QObject *child = window->property("child").value<QObject *>(); + QObject *grandChild = window->property("grandChild").value<QObject *>(); + QVERIFY(control && child && grandChild); + + const QPalette defaultPalette = QQuickTheme::palette(QQuickTheme::System); + + auto windowPalette = QQuickWindowPrivate::get(window.get())->palette(); + + QCOMPARE(windowPalette->toQPalette(), defaultPalette); + + auto controlPalette = control->property("palette").value<QQuickPalette*>(); + auto childPalette = child->property("palette").value<QQuickPalette*>(); + auto grandChildPalette = grandChild->property("palette").value<QQuickPalette*>(); + QVERIFY(controlPalette && childPalette && grandChildPalette); + + QCOMPARE(controlPalette->toQPalette(), defaultPalette); + QCOMPARE(childPalette->toQPalette(), defaultPalette); + QCOMPARE(grandChildPalette->toQPalette(), defaultPalette); + + childPalette->setBase(Qt::red); + childPalette->setText(Qt::green); + childPalette->setButton(Qt::blue); + + QCOMPARE(childPalette->base(), grandChildPalette->base()); + QCOMPARE(childPalette->text(), grandChildPalette->text()); + QCOMPARE(childPalette->button(), grandChildPalette->button()); + + windowPalette->setWindow(Qt::gray); + QCOMPARE(controlPalette->window(), windowPalette->window()); + + childPalette->setWindow(Qt::red); + QCOMPARE(childPalette->window(), Qt::red); + + grandChildPalette->setWindow(Qt::blue); + QCOMPARE(grandChildPalette->window(), Qt::blue); + + auto childMo = child->metaObject(); + childMo->property(childMo->indexOfProperty("palette")).reset(child); + QCOMPARE(childPalette->window(), windowPalette->window()); + QCOMPARE(grandChildPalette->window(), Qt::blue); + + auto grandChildMo = grandChild->metaObject(); + grandChildMo->property(grandChildMo->indexOfProperty("palette")).reset(grandChild); + QCOMPARE(grandChildPalette->window(), windowPalette->window()); +} + +class TestTheme : public QQuickTheme +{ +public: + static const uint NPalettes = QQuickTheme::Tumbler + 1; + + TestTheme() + { + for (uint i = 0; i < NPalettes; ++i) + setPalette(static_cast<Scope>(i), QPalette(QColor::fromRgb(i))); + } +}; + +Q_DECLARE_METATYPE(QQuickTheme::Scope) + +void tst_palette::defaultPalette_data() +{ + QTest::addColumn<QString>("control"); + QTest::addColumn<QQuickTheme::Scope>("scope"); + + QTest::newRow("AbstractButton") << "AbstractButton" << QQuickTheme::System; + QTest::newRow("ApplicationWindow") << "ApplicationWindow" << QQuickTheme::System; + QTest::newRow("Button") << "Button" << QQuickTheme::Button; + QTest::newRow("CheckBox") << "CheckBox" << QQuickTheme::CheckBox; + QTest::newRow("CheckDelegate") << "CheckDelegate" << QQuickTheme::ListView; + QTest::newRow("ComboBox") << "ComboBox" << QQuickTheme::ComboBox; + QTest::newRow("Container") << "Container" << QQuickTheme::System; + QTest::newRow("Control") << "Control" << QQuickTheme::System; + QTest::newRow("Dial") << "Dial" << QQuickTheme::System; + QTest::newRow("Dialog") << "Dialog" << QQuickTheme::System; + QTest::newRow("DialogButtonBox") << "DialogButtonBox" << QQuickTheme::System; + QTest::newRow("Drawer") << "Drawer" << QQuickTheme::System; + QTest::newRow("Frame") << "Frame" << QQuickTheme::System; + QTest::newRow("GroupBox") << "GroupBox" << QQuickTheme::GroupBox; + QTest::newRow("ItemDelegate") << "ItemDelegate" << QQuickTheme::ItemView; + QTest::newRow("Label") << "Label" << QQuickTheme::Label; + QTest::newRow("Menu") << "Menu" << QQuickTheme::Menu; + QTest::newRow("MenuItem") << "MenuItem" << QQuickTheme::Menu; + QTest::newRow("MenuSeparator") << "MenuSeparator" << QQuickTheme::Menu; + QTest::newRow("Page") << "Page" << QQuickTheme::System; + QTest::newRow("Pane") << "Pane" << QQuickTheme::System; + QTest::newRow("Popup") << "Popup" << QQuickTheme::System; + QTest::newRow("ProgressBar") << "ProgressBar" << QQuickTheme::System; + QTest::newRow("RadioButton") << "RadioButton" << QQuickTheme::RadioButton; + QTest::newRow("RadioDelegate") << "RadioDelegate" << QQuickTheme::ListView; + QTest::newRow("RangeSlider") << "RangeSlider" << QQuickTheme::System; + QTest::newRow("RoundButton") << "RoundButton" << QQuickTheme::Button; + QTest::newRow("ScrollBar") << "ScrollBar" << QQuickTheme::System; + QTest::newRow("ScrollIndicator") << "ScrollIndicator" << QQuickTheme::System; + QTest::newRow("Slider") << "Slider" << QQuickTheme::System; + QTest::newRow("SpinBox") << "SpinBox" << QQuickTheme::SpinBox; + QTest::newRow("SwipeDelegate") << "SwipeDelegate" << QQuickTheme::ListView; + QTest::newRow("Switch") << "Switch" << QQuickTheme::Switch; + QTest::newRow("SwitchDelegate") << "SwitchDelegate" << QQuickTheme::ListView; + QTest::newRow("TabBar") << "TabBar" << QQuickTheme::TabBar; + QTest::newRow("TabButton") << "TabButton" << QQuickTheme::TabBar; + QTest::newRow("TextArea") << "TextArea" << QQuickTheme::TextArea; + QTest::newRow("TextField") << "TextField" << QQuickTheme::TextField; + QTest::newRow("ToolBar") << "ToolBar" << QQuickTheme::ToolBar; + QTest::newRow("ToolButton") << "ToolButton" << QQuickTheme::ToolBar; + QTest::newRow("ToolSeparator") << "ToolSeparator" << QQuickTheme::ToolBar; + QTest::newRow("ToolTip") << "ToolTip" << QQuickTheme::ToolTip; + QTest::newRow("Tumbler") << "Tumbler" << QQuickTheme::Tumbler; +} + +void tst_palette::defaultPalette() +{ + QFETCH(QString, control); + QFETCH(QQuickTheme::Scope, scope); + + QQmlEngine engine; + QQmlComponent component(&engine); + component.setData(QString("import QtQuick.Controls; %1 { }").arg(control).toUtf8(), QUrl()); + + // The call to setData() above causes QQuickBasicTheme to be set as the current theme, + // so we must make sure we only set our theme afterwards. + std::unique_ptr<QQuickTheme> oldTheme(QQuickThemePrivate::instance.release()); + QQuickThemePrivate::instance.reset(new TestTheme); + + QScopedPointer<QObject> object(component.create()); + QVERIFY2(!object.isNull(), qPrintable(component.errorString())); + + QVariant var = object->property("palette"); + QVERIFY(var.isValid()); + + QPalette expectedPalette = QQuickTheme::palette(scope); + auto actualPalette = var.value<QQuickPalette*>(); + QVERIFY(actualPalette); + QCOMPARE(actualPalette->toQPalette(), expectedPalette); + + QQuickThemePrivate::instance.reset(oldTheme.release()); +} + +void tst_palette::listView_data() +{ + QTest::addColumn<QString>("objectName"); + + QTest::newRow("Control") << "control"; + QTest::newRow("Label") << "label"; + QTest::newRow("TextArea") << "textarea"; + QTest::newRow("TextField") << "textfield"; +} + +void tst_palette::listView() +{ + QFETCH(QString, objectName); + + QQmlEngine engine; + QQmlComponent component(&engine); + component.loadUrl(testFileUrl("listview.qml")); + + QScopedPointer<QQuickApplicationWindow> window(qobject_cast<QQuickApplicationWindow *>(component.create())); + QVERIFY2(!window.isNull(), qPrintable(component.errorString())); + + window->show(); + QVERIFY(QTest::qWaitForWindowActive(window.data())); + + QQuickItem *listView = window->property("listView").value<QQuickItem *>(); + QVERIFY(listView); + + QQuickItem *contentItem = listView->property("contentItem").value<QQuickItem *>(); + QVERIFY(contentItem); + + QVERIFY(QMetaObject::invokeMethod(listView, "forceLayout")); + + QQuickItem *column = contentItem->childItems().value(0); + QVERIFY(column); + + QQuickItem *control = column->property(objectName.toUtf8()).value<QQuickItem *>(); + QVERIFY(control); + + QCOMPARE(QQuickItemPrivate::get(control)->palette()->highlight(), Qt::red); +} + +void tst_palette::setDynamicallyCreatedPalette() +{ + QQmlEngine engine; + QQmlComponent component(&engine); + component.loadUrl(testFileUrl("set-palette.qml")); + + QScopedPointer<QObject> object(component.create()); + QVERIFY2(!object.isNull(), qPrintable(component.errorString())); + + QVariant var = object->property("palette"); + QVERIFY(var.isValid()); + + auto palette = var.value<QQuickPalette*>(); + QVERIFY(palette); + + QCOMPARE(palette->buttonText(), QColor("azure")); + QCOMPARE(palette->button(), QColor("khaki")); + + QCOMPARE(palette->disabled()->buttonText(), QColor("lavender")); + QCOMPARE(palette->disabled()->button(), QColor("coral")); +} + +void tst_palette::createBindings() +{ + QQmlEngine engine; + QQmlComponent component(&engine); + component.loadUrl(testFileUrl("bindings.qml")); + + QScopedPointer<QObject> window(component.create()); + QVERIFY2(!window.isNull(), qPrintable(component.errorString())); + + auto disabledButton = window->property("disabledButton").value<QQuickButton*>(); + QVERIFY(disabledButton); + + auto enabledButton = window->property("enabledButton").value<QQuickButton*>(); + QVERIFY(enabledButton); + + QCOMPARE(QQuickItemPrivate::get(disabledButton)->palette()->button(), QColor("aqua")); + QCOMPARE(QQuickItemPrivate::get(disabledButton)->palette()->buttonText(), QColor("azure")); + + QCOMPARE(QQuickItemPrivate::get(enabledButton)->palette()->button(), QColor("khaki")); + QCOMPARE(QQuickItemPrivate::get(enabledButton)->palette()->buttonText(), QColor("bisque")); + + QCOMPARE(QQuickItemPrivate::get(enabledButton)->palette()->disabled()->button(), QColor("aqua")); + QCOMPARE(QQuickItemPrivate::get(enabledButton)->palette()->disabled()->buttonText(), QColor("azure")); +} + +void tst_palette::updateBindings() +{ + QQmlEngine engine; + QQmlComponent component(&engine); + component.loadUrl(testFileUrl("bindings.qml")); + + QScopedPointer<QObject> window(component.create()); + QVERIFY2(!window.isNull(), qPrintable(component.errorString())); + + auto disabledButton = window->property("disabledButton").value<QQuickButton*>(); + QVERIFY(disabledButton); + + auto enabledButton = window->property("enabledButton").value<QQuickButton*>(); + QVERIFY(enabledButton); + + QQuickItemPrivate::get(disabledButton)->palette()->disabled()->setButton(QColor("navy")); + enabledButton->setEnabled(false); + + QCOMPARE(QQuickItemPrivate::get(enabledButton)->palette()->button(), QColor("navy")); +} + +void tst_palette::resolve() +{ + QQmlEngine engine; + QQmlComponent component(&engine); + component.loadUrl(testFileUrl("resolve.qml")); + + QScopedPointer<QObject> window(component.create()); + QVERIFY2(!window.isNull(), qPrintable(component.errorString())); + + auto control = window->property("control").value<QQuickControl*>(); + QVERIFY(control); + + QCOMPARE(window->property("palette").value<QQuickPalette*>()->window(), + control->property("palette").value<QQuickPalette*>()->window()); + QCOMPARE(window->property("palette").value<QQuickPalette*>()->windowText(), + control->property("palette").value<QQuickPalette*>()->windowText()); + QMetaObject::invokeMethod(window.get(), "changeColors", Q_ARG(QVariant, QColor(Qt::red))); + QVERIFY(window->property("palette").value<QQuickPalette*>()->window() + != control->property("palette").value<QQuickPalette*>()->window()); + QCOMPARE(window->property("palette").value<QQuickPalette*>()->windowText(), + control->property("palette").value<QQuickPalette*>()->windowText()); +} + +QTEST_MAIN(tst_palette) + +#include "tst_palette.moc" |