From 74acbcc9f31aa2c64d076e1157979537d90d3f6d Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Fri, 28 Aug 2020 16:55:25 +0200 Subject: 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 --- tests/auto/styleimports/CMakeLists.txt | 11 +++- tests/auto/styleimports/ResourceStyle/qmldir | 2 - .../data/applicationWindowWithButton.qml | 64 ++++++++++++++++++++++ tests/auto/styleimports/data/platformSelectors.qml | 64 ---------------------- .../resources/ResourceStyle/Button.qml | 9 +++ .../styleimports/resources/ResourceStyle/qmldir | 2 + tests/auto/styleimports/styleimports.pro | 7 ++- tests/auto/styleimports/tst_styleimports.cpp | 47 +++++++++++++++- 8 files changed, 134 insertions(+), 72 deletions(-) delete mode 100644 tests/auto/styleimports/ResourceStyle/qmldir create mode 100644 tests/auto/styleimports/data/applicationWindowWithButton.qml delete mode 100644 tests/auto/styleimports/data/platformSelectors.qml create mode 100644 tests/auto/styleimports/resources/ResourceStyle/Button.qml create mode 100644 tests/auto/styleimports/resources/ResourceStyle/qmldir (limited to 'tests/auto') 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/ResourceStyle/qmldir b/tests/auto/styleimports/ResourceStyle/qmldir deleted file mode 100644 index 4cb1a835..00000000 --- a/tests/auto/styleimports/ResourceStyle/qmldir +++ /dev/null @@ -1,2 +0,0 @@ -module ResourceStyle -Button 2.15 Button.qml diff --git a/tests/auto/styleimports/data/applicationWindowWithButton.qml b/tests/auto/styleimports/data/applicationWindowWithButton.qml new file mode 100644 index 00000000..ba6af22c --- /dev/null +++ b/tests/auto/styleimports/data/applicationWindowWithButton.qml @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2020 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:BSD$ +** 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. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + title: "Test Application Window" + width: 400 + height: 400 + + property alias button: button + + Button { + id: button + } +} diff --git a/tests/auto/styleimports/data/platformSelectors.qml b/tests/auto/styleimports/data/platformSelectors.qml deleted file mode 100644 index ba6af22c..00000000 --- a/tests/auto/styleimports/data/platformSelectors.qml +++ /dev/null @@ -1,64 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2020 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:BSD$ -** 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. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -import QtQuick -import QtQuick.Controls - -ApplicationWindow { - title: "Test Application Window" - width: 400 - height: 400 - - property alias button: button - - Button { - id: button - } -} 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/resources/ResourceStyle/qmldir b/tests/auto/styleimports/resources/ResourceStyle/qmldir new file mode 100644 index 00000000..4cb1a835 --- /dev/null +++ b/tests/auto/styleimports/resources/ResourceStyle/qmldir @@ -0,0 +1,2 @@ +module ResourceStyle +Button 2.15 Button.qml 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 #include #include +#include #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(engine.rootObjects().first()); QVERIFY(window); @@ -241,6 +246,46 @@ void tst_StyleImports::importStyleWithoutControls() QTRY_VERIFY(success); } +void tst_StyleImports::fallbackStyleShouldNotOverwriteTheme_data() +{ + QTest::addColumn("style"); + QTest::addColumn("fallbackStyle"); + QTest::addColumn("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(engine.rootObjects().first()); + QVERIFY(window); + + QObject *button = window->property("button").value(); + QVERIFY(button); + + QQuickIconLabel *contentItem = button->property("contentItem").value(); + 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" -- cgit v1.2.3