diff options
Diffstat (limited to 'tests/auto/quickcontrols/styleimports')
8 files changed, 158 insertions, 6 deletions
diff --git a/tests/auto/quickcontrols/styleimports/CMakeLists.txt b/tests/auto/quickcontrols/styleimports/CMakeLists.txt index 4d19521f2e..d18a7effc1 100644 --- a/tests/auto/quickcontrols/styleimports/CMakeLists.txt +++ b/tests/auto/quickcontrols/styleimports/CMakeLists.txt @@ -6,7 +6,7 @@ if (NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) cmake_minimum_required(VERSION 3.16) project(tst_styleimports LANGUAGES C CXX ASM) - find_package(Qt6BuildInternals COMPONENTS STANDALONE_TEST) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) endif() ##################################################################### diff --git a/tests/auto/quickcontrols/styleimports/data/applicationWindowWithButton.qml b/tests/auto/quickcontrols/styleimports/data/applicationWindowWithButton.qml index 512bd0bb08..50f0e43419 100644 --- a/tests/auto/quickcontrols/styleimports/data/applicationWindowWithButton.qml +++ b/tests/auto/quickcontrols/styleimports/data/applicationWindowWithButton.qml @@ -1,7 +1,6 @@ -// Copyright (C) 2020 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only -import QtQuick import QtQuick.Controls ApplicationWindow { diff --git a/tests/auto/quickcontrols/styleimports/data/customStyleSelector.qml b/tests/auto/quickcontrols/styleimports/data/customStyleSelector.qml index a3def8b14b..020d091156 100644 --- a/tests/auto/quickcontrols/styleimports/data/customStyleSelector.qml +++ b/tests/auto/quickcontrols/styleimports/data/customStyleSelector.qml @@ -1,5 +1,5 @@ // Copyright (C) 2022 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only import QtQuick import QtQuick.Controls diff --git a/tests/auto/quickcontrols/styleimports/data/importStyleWithQmlDirFallback.qml b/tests/auto/quickcontrols/styleimports/data/importStyleWithQmlDirFallback.qml new file mode 100644 index 0000000000..3202fca1cd --- /dev/null +++ b/tests/auto/quickcontrols/styleimports/data/importStyleWithQmlDirFallback.qml @@ -0,0 +1,17 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +import StyleThatImportsMaterial + +ApplicationWindow { + title: "Test Application Window" + width: 400 + height: 400 + + property alias button: button + + Button { + id: button + text: "QmlOnly Button" + } +} diff --git a/tests/auto/quickcontrols/styleimports/data/styles/FileSystemStyle/Button.qml b/tests/auto/quickcontrols/styleimports/data/styles/FileSystemStyle/Button.qml index a5078b14c2..71c451ab0f 100644 --- a/tests/auto/quickcontrols/styleimports/data/styles/FileSystemStyle/Button.qml +++ b/tests/auto/quickcontrols/styleimports/data/styles/FileSystemStyle/Button.qml @@ -1,4 +1,9 @@ import QtQuick.Templates as T T.Button { + id: control objectName: "FileSystemStyle" + contentItem: T.Label { + text: control.text + color: "#0000ff" + } } diff --git a/tests/auto/quickcontrols/styleimports/data/styles/StyleThatImportsMaterial/Button.qml b/tests/auto/quickcontrols/styleimports/data/styles/StyleThatImportsMaterial/Button.qml new file mode 100644 index 0000000000..b7e77c2864 --- /dev/null +++ b/tests/auto/quickcontrols/styleimports/data/styles/StyleThatImportsMaterial/Button.qml @@ -0,0 +1,34 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +import QtQuick +import QtQuick.Templates as T + +T.Button { + id: control + + implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset, + implicitContentWidth + leftPadding + rightPadding) + implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset, + implicitContentHeight + topPadding + bottomPadding) + + padding: 6 + horizontalPadding: padding + 2 + spacing: 6 + + contentItem: T.Label { + text: control.text + font: control.font + color: "#ff0000" + verticalAlignment: T.Label.AlignVCenter + } + + background: Rectangle { + implicitWidth: 100 + implicitHeight: 40 + visible: !control.flat || control.down || control.checked || control.highlighted + color: "#ccc" + border.color: control.palette.highlight + border.width: control.visualFocus ? 2 : 0 + } +} diff --git a/tests/auto/quickcontrols/styleimports/data/styles/StyleThatImportsMaterial/qmldir b/tests/auto/quickcontrols/styleimports/data/styles/StyleThatImportsMaterial/qmldir new file mode 100644 index 0000000000..eeffe70d4e --- /dev/null +++ b/tests/auto/quickcontrols/styleimports/data/styles/StyleThatImportsMaterial/qmldir @@ -0,0 +1,3 @@ +module StyleThatImportsMaterial +Button 1.0 Button.qml +import QtQuick.Controls.Material diff --git a/tests/auto/quickcontrols/styleimports/tst_styleimports.cpp b/tests/auto/quickcontrols/styleimports/tst_styleimports.cpp index 175541296a..a88c154448 100644 --- a/tests/auto/quickcontrols/styleimports/tst_styleimports.cpp +++ b/tests/auto/quickcontrols/styleimports/tst_styleimports.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 <QtCore/qregularexpression.h> #include <QtGui/qpalette.h> @@ -15,7 +15,12 @@ #include <QtQuickControls2/qquickstyle.h> #include <QtQuickControls2/private/qquickstyle_p.h> #include <QtQuickControls2Impl/private/qquickiconlabel_p.h> +#include <QtQuickControlsTestUtils/private/controlstestutils_p.h> #include <QtQuickControlsTestUtils/private/qtest_quickcontrols_p.h> +#include <QtQuickTemplates2/private/qquickapplicationwindow_p.h> +#include <QtQuickTemplates2/private/qquickbutton_p.h> + +using namespace QQuickControlsTestUtils; class tst_StyleImports : public QQmlDataTest { @@ -38,6 +43,9 @@ private slots: void fallbackStyleShouldNotOverwriteTheme_data(); void fallbackStyleShouldNotOverwriteTheme(); + void fallbackStyleThemeRespected_data(); + void fallbackStyleThemeRespected(); + void attachedTypesAvailable_data(); void attachedTypesAvailable(); }; @@ -278,6 +286,92 @@ void tst_StyleImports::fallbackStyleShouldNotOverwriteTheme() QCOMPARE(contentItem->color(), expectedContentItemColor); } +enum FallbackMethod { + QmlDirImport, + EnvVar +}; + +void tst_StyleImports::fallbackStyleThemeRespected_data() +{ + QTest::addColumn<QString>("qmlFilePath"); + QTest::addColumn<QString>("runtimeStyle"); + QTest::addColumn<FallbackMethod>("fallbackMethod"); + QTest::addColumn<Qt::ColorScheme>("colorScheme"); + QTest::addColumn<QColor>("expectedButtonTextColor"); + QTest::addColumn<QColor>("expectedWindowColor"); + + // Taken from qquickmaterialstyle.cpp. + static const QRgb materialBackgroundColorLight = 0xFFFFFBFE; + static const QRgb materialBackgroundColorDark = 0xFF1C1B1F; + + // Notes: + // - FileSystemStyle has blue button text. + // - StyleThatImportsMaterial has red button text. + // - All rows result in Material being the fallback. + + QTest::newRow("import controls, env var fallback, light") << "applicationWindowWithButton.qml" + << "FileSystemStyle" << EnvVar << Qt::ColorScheme::Light + << QColor::fromRgb(0x0000ff) << QColor::fromRgba(materialBackgroundColorLight); + QTest::newRow("import controls, env var fallback, dark") << "applicationWindowWithButton.qml" + << "FileSystemStyle" << EnvVar << Qt::ColorScheme::Dark + << QColor::fromRgb(0x0000ff) << QColor::fromRgba(materialBackgroundColorDark); + + QTest::newRow("import style, qmldir fallback, light") << "importStyleWithQmlDirFallback.qml" + << "" << QmlDirImport << Qt::ColorScheme::Light + << QColor::fromRgb(0xff0000) << QColor::fromRgba(materialBackgroundColorLight); + QTest::newRow("import style, qmldir fallback, dark") << "importStyleWithQmlDirFallback.qml" + << "" << QmlDirImport << Qt::ColorScheme::Dark + << QColor::fromRgb(0xff0000) << QColor::fromRgba(materialBackgroundColorDark); +} + +// Tests that a fallback style's (the Material style, in this case) theme settings +// are respected for both run-time and compile-time style selection. +void tst_StyleImports::fallbackStyleThemeRespected() +{ + QFETCH(QString, qmlFilePath); + QFETCH(QString, runtimeStyle); + QFETCH(FallbackMethod, fallbackMethod); + QFETCH(Qt::ColorScheme, colorScheme); + QFETCH(QColor, expectedButtonTextColor); + QFETCH(QColor, expectedWindowColor); + + const char *materialThemeEnvVarName = "QT_QUICK_CONTROLS_MATERIAL_THEME"; + const QString originalMaterialTheme = qgetenv(materialThemeEnvVarName); + qputenv(materialThemeEnvVarName, colorScheme == Qt::ColorScheme::Light ? "Light" : "Dark"); + + // Only set this if it's not empty, because setting an empty style + // will still cause it be resolved and we end up using the platform default. + if (!runtimeStyle.isEmpty()) + QQuickStyle::setStyle(runtimeStyle); + + const char *fallbackStyleEnvVarName = "QT_QUICK_CONTROLS_FALLBACK_STYLE"; + const QString originalFallbackStyle = qgetenv(fallbackStyleEnvVarName); + if (fallbackMethod == EnvVar) + qputenv(fallbackStyleEnvVarName, "Material"); + + auto cleanup = qScopeGuard([&]() { + qputenv(materialThemeEnvVarName, qPrintable(originalMaterialTheme)); + qputenv(fallbackStyleEnvVarName, qPrintable(originalFallbackStyle)); + }); + + QQuickControlsApplicationHelper helper(this, qmlFilePath, {}, + QStringList() << dataDirectory() + QLatin1String("/styles")); + QVERIFY2(helper.ready, helper.failureMessage()); + + auto button = helper.window->property("button").value<QQuickButton*>(); + QVERIFY(button); + // contentItem should be a label with "salmon" text color. + QCOMPARE(button->contentItem()->property("color").value<QColor>(), expectedButtonTextColor); + QCOMPARE(helper.appWindow->color(), expectedWindowColor); + + // If using run-time style selection, check that QQuickStyle reports the correct values. + // QQuickStyle is not supported when using compile-time style selection. + if (!runtimeStyle.isEmpty()) { + QCOMPARE(QQuickStyle::name(), runtimeStyle); + QCOMPARE(QQuickStylePrivate::fallbackStyle(), "Material"); + } +} + void tst_StyleImports::attachedTypesAvailable_data() { QTest::addColumn<QString>("import"); |