diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2020-08-28 16:55:25 +0200 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2020-09-24 13:48:39 +0200 |
commit | 74acbcc9f31aa2c64d076e1157979537d90d3f6d (patch) | |
tree | c896835ef345e8663aee2b6bf4b13e5779a6fd58 /tests/auto | |
parent | 6059a7765b123a6f3fb221db9674a995bae1881c (diff) |
Fix fallback styles overwriting themes
In Qt 5, QtQuickControls2Plugin::registerTypes() was responsible for
calling initializeTheme() on each style plugin. Now that we delegate
more work to the QML engine, each style plugin calls initializeTheme()
via registerTypes().
To avoid fallback styles overwriting font and palette data set by the
current style, we need to check if the theme has been intialized before
calling initializeTheme(). To do this, we add a static
"themeInitialized" bool that QQuickStylePlugin sets to true after
calling intializeTheme() for the first time. It checks this value and
avoids calling intializeTheme() if it's true.
We also need to make QQuickStylePlugin ensure that the theme it's
initializing belongs to the current style.
Fixes: QTBUG-86303
Change-Id: Ie65e646677c78622829f4949c41cb79204cf5786
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/styleimports/CMakeLists.txt | 11 | ||||
-rw-r--r-- | tests/auto/styleimports/data/applicationWindowWithButton.qml (renamed from tests/auto/styleimports/data/platformSelectors.qml) | 0 | ||||
-rw-r--r-- | tests/auto/styleimports/resources/ResourceStyle/Button.qml | 9 | ||||
-rw-r--r-- | tests/auto/styleimports/resources/ResourceStyle/qmldir (renamed from tests/auto/styleimports/ResourceStyle/qmldir) | 0 | ||||
-rw-r--r-- | tests/auto/styleimports/styleimports.pro | 7 | ||||
-rw-r--r-- | tests/auto/styleimports/tst_styleimports.cpp | 47 |
6 files changed, 68 insertions, 6 deletions
diff --git a/tests/auto/styleimports/CMakeLists.txt b/tests/auto/styleimports/CMakeLists.txt index b5910e92..86db761a 100644 --- a/tests/auto/styleimports/CMakeLists.txt +++ b/tests/auto/styleimports/CMakeLists.txt @@ -24,6 +24,7 @@ qt_add_test(tst_styleimports Qt::GuiPrivate Qt::QmlPrivate Qt::QuickControls2 + Qt::QuickControls2ImplPrivate Qt::QuickControls2Private Qt::QuickPrivate Qt::QuickTemplates2Private @@ -33,13 +34,19 @@ qt_add_test(tst_styleimports # Resources: set(qmake_resourcestyle_resource_files - "ResourceStyle/Button.qml" - "ResourceStyle/qmldir" + "resources/ResourceStyle/Button.qml" + "resources/ResourceStyle/qmldir" +) +set(qmake_resourcestyle_resource_files + "ResourceStyle/Button.qml" # special case: QTBUG-86726 + "ResourceStyle/qmldir" # special case ) qt_add_resource(tst_styleimports "qmake_resourcestyle" PREFIX "/" + BASE + "resources" FILES ${qmake_resourcestyle_resource_files} ) diff --git a/tests/auto/styleimports/data/platformSelectors.qml b/tests/auto/styleimports/data/applicationWindowWithButton.qml index ba6af22c..ba6af22c 100644 --- a/tests/auto/styleimports/data/platformSelectors.qml +++ b/tests/auto/styleimports/data/applicationWindowWithButton.qml diff --git a/tests/auto/styleimports/resources/ResourceStyle/Button.qml b/tests/auto/styleimports/resources/ResourceStyle/Button.qml new file mode 100644 index 00000000..3a97e9de --- /dev/null +++ b/tests/auto/styleimports/resources/ResourceStyle/Button.qml @@ -0,0 +1,9 @@ +import QtQuick.Controls.impl 2.15 +import QtQuick.Templates 2.15 as T +T.Button { + objectName: "ResourceStyle" + + contentItem: IconLabel { + color: "salmon" + } +} diff --git a/tests/auto/styleimports/ResourceStyle/qmldir b/tests/auto/styleimports/resources/ResourceStyle/qmldir index 4cb1a835..4cb1a835 100644 --- a/tests/auto/styleimports/ResourceStyle/qmldir +++ b/tests/auto/styleimports/resources/ResourceStyle/qmldir diff --git a/tests/auto/styleimports/styleimports.pro b/tests/auto/styleimports/styleimports.pro index 774d93e9..398ce7c2 100644 --- a/tests/auto/styleimports/styleimports.pro +++ b/tests/auto/styleimports/styleimports.pro @@ -4,14 +4,15 @@ SOURCES += tst_styleimports.cpp macos:CONFIG -= app_bundle -QT += core-private gui-private qml-private quick-private quickcontrols2-private testlib +QT += core-private gui-private qml-private quick-private quickcontrols2-private quickcontrols2impl-private testlib include (../shared/util.pri) resourcestyle.prefix = / +resourcestyle.base = resources resourcestyle.files += \ - $$PWD/ResourceStyle/Button.qml \ - $$PWD/ResourceStyle/qmldir + $$PWD/resources/ResourceStyle/Button.qml \ + $$PWD/resources/ResourceStyle/qmldir RESOURCES += resourcestyle TESTDATA = data/* diff --git a/tests/auto/styleimports/tst_styleimports.cpp b/tests/auto/styleimports/tst_styleimports.cpp index 8b47dee7..fa524098 100644 --- a/tests/auto/styleimports/tst_styleimports.cpp +++ b/tests/auto/styleimports/tst_styleimports.cpp @@ -43,6 +43,7 @@ #include <QtQuick/qquickwindow.h> #include <QtQuickControls2/qquickstyle.h> #include <QtQuickControls2/private/qquickstyle_p.h> +#include <QtQuickControls2Impl/private/qquickiconlabel_p.h> #include "../shared/util.h" @@ -62,6 +63,9 @@ private slots: void importStyleWithoutControls_data(); void importStyleWithoutControls(); + + void fallbackStyleShouldNotOverwriteTheme_data(); + void fallbackStyleShouldNotOverwriteTheme(); }; void tst_StyleImports::initTestCase() @@ -151,6 +155,7 @@ void tst_StyleImports::select() QQuickStyle::setFallbackStyle(fallback); QQmlEngine engine; + engine.addImportPath(QLatin1String(":/")); engine.addImportPath(directory()); engine.addImportPath(dataDirectory()); QQmlComponent component(&engine); @@ -185,7 +190,7 @@ void tst_StyleImports::platformSelectors() QQmlApplicationEngine engine; engine.addImportPath(dataDirectory()); - engine.load(testFileUrl("platformSelectors.qml")); + engine.load(testFileUrl("applicationWindowWithButton.qml")); QQuickWindow *window = qobject_cast<QQuickWindow*>(engine.rootObjects().first()); QVERIFY(window); @@ -241,6 +246,46 @@ void tst_StyleImports::importStyleWithoutControls() QTRY_VERIFY(success); } +void tst_StyleImports::fallbackStyleShouldNotOverwriteTheme_data() +{ + QTest::addColumn<QString>("style"); + QTest::addColumn<QString>("fallbackStyle"); + QTest::addColumn<QColor>("expectedContentItemColor"); + + QTest::addRow("style=Fusion,fallbackStyle=Material") + << QString::fromLatin1("Fusion") << QString::fromLatin1("Material") << QColor::fromRgb(0x252525); + QTest::addRow("style=ResourceStyle,fallbackStyle=Material") + << QString::fromLatin1("ResourceStyle") << QString::fromLatin1("Material") << QColor("salmon"); +} + +void tst_StyleImports::fallbackStyleShouldNotOverwriteTheme() +{ + QFETCH(QString, style); + QFETCH(QString, fallbackStyle); + QFETCH(QColor, expectedContentItemColor); + + QQuickStyle::setStyle(style); + QQuickStyle::setFallbackStyle(fallbackStyle); + + QQmlApplicationEngine engine; + engine.addImportPath(QLatin1String(":/")); + engine.addImportPath(dataDirectory()); + engine.load(testFileUrl("applicationWindowWithButton.qml")); + QVERIFY(!engine.rootObjects().isEmpty()); + QQuickWindow *window = qobject_cast<QQuickWindow*>(engine.rootObjects().first()); + QVERIFY(window); + + QObject *button = window->property("button").value<QObject*>(); + QVERIFY(button); + + QQuickIconLabel *contentItem = button->property("contentItem").value<QQuickIconLabel*>(); + QVERIFY(contentItem); + + // For example: the Fusion style provides Button.qml, so the Button's text color + // should be that of QPalette::ButtonText from QQuickFusionTheme. + QCOMPARE(contentItem->color(), expectedContentItemColor); +} + QTEST_MAIN(tst_StyleImports) #include "tst_styleimports.moc" |