diff options
Diffstat (limited to 'tests/auto/quickcontrols/palette/tst_palette.cpp')
-rw-r--r-- | tests/auto/quickcontrols/palette/tst_palette.cpp | 203 |
1 files changed, 194 insertions, 9 deletions
diff --git a/tests/auto/quickcontrols/palette/tst_palette.cpp b/tests/auto/quickcontrols/palette/tst_palette.cpp index e3a53f0c21..75ee023bb7 100644 --- a/tests/auto/quickcontrols/palette/tst_palette.cpp +++ b/tests/auto/quickcontrols/palette/tst_palette.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QtTest/qtest.h> #include <QtGui/qpalette.h> @@ -7,8 +7,11 @@ #include <QtQml/qqmlengine.h> #include <QtQml/qqmlcomponent.h> #include <QtQuickTestUtils/private/qmlutils_p.h> +#include <QtQuickTestUtils/private/visualtestutils_p.h> +#include <QtQuickControlsTestUtils/private/controlstestutils_p.h> #include <QtQuick/private/qquickitem_p.h> #include <QtQuickTemplates2/private/qquickapplicationwindow_p.h> +#include <QtQuickTemplates2/private/qquickcombobox_p.h> #include <QtQuickTemplates2/private/qquickcontrol_p.h> #include <QtQuickTemplates2/private/qquickcontrol_p_p.h> #include <QtQuickTemplates2/private/qquickpopup_p.h> @@ -16,14 +19,10 @@ #include <QtQuickTemplates2/private/qquicktheme_p_p.h> #include <QtQuickTemplates2/private/qquickbutton_p.h> #include <QtQuickControls2/qquickstyle.h> +#include <QSignalSpy> -//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)))); +using namespace QQuickVisualTestUtils; +using namespace QQuickControlsTestUtils; class tst_palette : public QQmlDataTest { @@ -40,6 +39,7 @@ private slots: void inheritance_data(); void inheritance(); + void childPopupInheritance(); void defaultPalette_data(); void defaultPalette(); @@ -52,6 +52,12 @@ private slots: void updateBindings(); void resolve(); + + void resetColor(); + void updateBindingPalette(); + + void comboBoxPopup_data(); + void comboBoxPopup(); }; tst_palette::tst_palette() @@ -106,6 +112,7 @@ void tst_palette::palette_data() customPalette.setColor(QPalette::Window, QColor("plum")); customPalette.setColor(QPalette::WindowText, QColor("salmon")); customPalette.setColor(QPalette::PlaceholderText, QColor("magenta")); + customPalette.setColor(QPalette::Accent, QColor("darkkhaki")); QTest::newRow("Control:custom") << "palette-control-custom.qml" << customPalette; QTest::newRow("AppWindow:custom") << "palette-appwindow-custom.qml" << customPalette; @@ -127,7 +134,7 @@ void tst_palette::palette() QVariant var = object->property("palette"); QVERIFY(var.isValid()); - COMPARE_PALETTES(var.value<QQuickPalette*>()->toQPalette(), expectedPalette); + QCOMPARE(var.value<QQuickPalette*>()->toQPalette(), expectedPalette); } void tst_palette::inheritance_data() @@ -203,6 +210,40 @@ void tst_palette::inheritance() QCOMPARE(grandChildPalette->window(), windowPalette->window()); } +// The child popups in inheritance() don't test actual nested child popups, +// only popups that are children of items and the items within those popups. +// We need to specifically test this to prevent QTBUG-115707 from happening again. +void tst_palette::childPopupInheritance() +{ + QQuickControlsApplicationHelper helper(this, QLatin1String("childPopupInheritance.qml")); + QVERIFY2(helper.ready, helper.failureMessage()); + + const auto *windowPrivate = QQuickWindowPrivate::get(helper.window); + const auto windowsWindowTextColor = windowPrivate->palette()->toQPalette().color(QPalette::WindowText); + + // parentPopup sets windowText explicitly, so its label should use that color. + auto *parentPopup = helper.appWindow->findChild<QQuickPopup *>("parentPopup"); + QVERIFY(parentPopup); + parentPopup->open(); + QTRY_VERIFY(parentPopup->isOpened()); + auto *parentPopupLabel = helper.appWindow->findChild<QObject *>("parentPopupLabel"); + QVERIFY(parentPopupLabel); + QCOMPARE(parentPopupLabel->property("color").value<QColor>(), "#ffdead"); + + // All other child popups don't set anything explicitly, and should inherit from their window. + auto *childPopup = helper.appWindow->findChild<QQuickPopup *>("childPopup"); + QVERIFY(childPopup); + auto *childPopupLabel = helper.appWindow->findChild<QObject *>("childPopupLabel"); + QVERIFY(childPopupLabel); + QCOMPARE(childPopupLabel->property("color").value<QColor>(), windowsWindowTextColor); + + auto *grandchildPopup = helper.appWindow->findChild<QQuickPopup *>("grandchildPopup"); + QVERIFY(grandchildPopup); + auto *grandchildPopupLabel = helper.appWindow->findChild<QObject *>("grandchildPopupLabel"); + QVERIFY(grandchildPopupLabel); + QCOMPARE(grandchildPopupLabel->property("color").value<QColor>(), windowsWindowTextColor); +} + class TestTheme : public QQuickTheme { public: @@ -427,6 +468,150 @@ void tst_palette::resolve() control->property("palette").value<QQuickPalette*>()->windowText()); } +void tst_palette::resetColor() +{ + QQmlEngine engine; + QQmlComponent component(&engine); + component.loadUrl(testFileUrl("reset-color.qml")); + + QScopedPointer<QQuickApplicationWindow> window(qobject_cast<QQuickApplicationWindow*>(component.create())); + QVERIFY2(!window.isNull(), qPrintable(component.errorString())); + auto windowPalette = window->property("palette").value<QQuickPalette*>(); + QVERIFY(windowPalette); + + auto control = window->property("control").value<QQuickControl*>(); + QVERIFY(control); + auto controlPalette = control->property("palette").value<QQuickPalette*>(); + QVERIFY(controlPalette); + auto item1Palette = window->property("item1Palette").value<QQuickPalette*>(); + QVERIFY(item1Palette); + auto item2Palette = window->property("item2Palette").value<QQuickPalette*>(); + QVERIFY(item2Palette); + + QCOMPARE(controlPalette->disabled()->window(), item2Palette->disabled()->window()); + QCOMPARE(controlPalette->disabled()->text(), item1Palette->disabled()->text()); + QCOMPARE(controlPalette->disabled()->windowText(), windowPalette->disabled()->windowText()); + + { + QSignalSpy spy(controlPalette, &QQuickPalette::changed); + item1Palette->disabled()->setText(Qt::red); + QVERIFY(spy.count() == 1 || spy.wait()); + QCOMPARE(controlPalette->disabled()->text(), QColor(Qt::red)); + } + + { + QSignalSpy spy(controlPalette, &QQuickPalette::changed); + item1Palette->disabled()->setWindowText(Qt::red); + QVERIFY(spy.count() == 1 || spy.wait()); + QCOMPARE(controlPalette->disabled()->windowText(), QColor(Qt::red)); + } + + { + QSignalSpy spy(controlPalette, &QQuickPalette::changed); + item2Palette->disabled()->setWindowText(Qt::blue); + QVERIFY(spy.count() == 1 || spy.wait()); + QCOMPARE(controlPalette->disabled()->windowText(), QColor(Qt::blue)); + } + + { + QSignalSpy spy(controlPalette, &QQuickPalette::changed); + QMetaObject::invokeMethod(window.get(), "resetColor", Qt::DirectConnection); + QCOMPARE(controlPalette->window(), windowPalette->window()); + windowPalette->setWindow(Qt::green); + QCOMPARE(controlPalette->window(), QColor(Qt::green)); + QVERIFY(spy.count() >= 2); + } + + { + QSignalSpy spy(controlPalette, &QQuickPalette::changed); + QMetaObject::invokeMethod(window.get(), "resetGroup", Qt::DirectConnection); + QCOMPARE(controlPalette->disabled()->windowText(), windowPalette->disabled()->windowText()); + windowPalette->disabled()->setWindow(Qt::blue); + QCOMPARE(controlPalette->disabled()->window(), QColor(Qt::blue)); + item2Palette->disabled()->setWindow(Qt::red); + QCOMPARE(controlPalette->disabled()->window(), QColor(Qt::blue)); + if (spy.count() == 0) + spy.wait(); + QCOMPARE(spy.count(), 2); + } +} + +void tst_palette::updateBindingPalette() +{ + QQmlEngine engine; + QQmlComponent component(&engine); + component.loadUrl(testFileUrl("palette-appwindow-bindingpalette.qml")); + + QScopedPointer<QQuickApplicationWindow> window(qobject_cast<QQuickApplicationWindow*>(component.create())); + QVERIFY2(!window.isNull(), qPrintable(component.errorString())); + auto *windowPalette = window->property("palette").value<QQuickPalette *>(); + QVERIFY(windowPalette); + auto *customPalette = window->property("cstmPalette").value<QQuickPalette *>(); + QVERIFY(customPalette); + + QCOMPARE(windowPalette->buttonText(), QColor("white")); + + QColor buttonTextColor("red"); + customPalette->setButtonText(buttonTextColor); + QCOMPARE(customPalette->buttonText(), buttonTextColor); + QCOMPARE(windowPalette->buttonText(), customPalette->buttonText()); +} + +void tst_palette::comboBoxPopup_data() +{ + QTest::addColumn<QString>("style"); + QTest::addColumn<QString>("qmlFilePath"); + + QTest::newRow("Window, Basic") << "Basic" << "comboBoxPopupWithWindow.qml"; + QTest::newRow("ApplicationWindow, Basic") << "Basic" << "comboBoxPopupWithApplicationWindow.qml"; + QTest::newRow("Window, Fusion") << "Fusion" << "comboBoxPopupWithWindow.qml"; + QTest::newRow("ApplicationWindow, Fusion") << "Fusion" << "comboBoxPopupWithApplicationWindow.qml"; +} + +// Unlike regular popups, which should inherit their palette from the window and not the parent popup, +// combo box popups should inherit their palette from the combo box itself. +void tst_palette::comboBoxPopup() +{ + QFETCH(QString, style); + QFETCH(QString, qmlFilePath); + + qmlClearTypeRegistrations(); + QQuickStyle::setStyle(style); + + QQuickApplicationHelper helper(this, qmlFilePath); + QVERIFY2(helper.ready, helper.failureMessage()); + QQuickWindow *window = helper.window; + window->show(); + QVERIFY(QTest::qWaitForWindowExposed(window)); + + const auto *windowPalette = window->property("palette").value<QQuickPalette *>(); + QVERIFY(windowPalette); + + const auto *popup = window->property("popup").value<QQuickPopup *>(); + QVERIFY(popup); + const auto *popupBackground = popup->background(); + QCOMPARE(popupBackground->property("color"), QColorConstants::Red); + QCOMPARE(popupBackground->property("palette").value<QQuickPalette*>()->toQPalette().window().color(), + QColorConstants::Red); + + // This has the default palette. + const auto *topLevelComboBox = window->property("topLevelComboBox").value<QQuickComboBox *>(); + QVERIFY(topLevelComboBox); + const auto *topLevelComboBoxBackground = topLevelComboBox->popup()->background(); + QCOMPARE_NE(topLevelComboBoxBackground->property("color"), QColorConstants::Red); + QCOMPARE_NE(topLevelComboBoxBackground->property("palette").value<QQuickPalette*>()->toQPalette().window().color(), + QColorConstants::Red); + + // The popup that this combo box is in has its window role set to red, + // so the combo box's popup background should be red too. + const auto *comboBoxInPopup = window->property("comboBoxInPopup").value<QQuickComboBox *>(); + QVERIFY(comboBoxInPopup); + const auto *comboBoxInPopupBackground = comboBoxInPopup->popup()->background(); + QCOMPARE(comboBoxInPopupBackground->property("color"), QColorConstants::Red); + QCOMPARE(comboBoxInPopupBackground->property("palette").value<QQuickPalette*>()->toQPalette().window().color(), + QColorConstants::Red); +} + QTEST_MAIN(tst_palette) #include "tst_palette.moc" |