diff options
Diffstat (limited to 'tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp')
-rw-r--r-- | tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp | 220 |
1 files changed, 156 insertions, 64 deletions
diff --git a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp index 492b206019..a9a1817b8a 100644 --- a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp +++ b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2019 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2019 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QtWidgets/QApplication> #include <QtWidgets/QCheckBox> @@ -63,6 +38,8 @@ #include <private/qstylesheetstyle_p.h> #include <private/qhighdpiscaling_p.h> #include <QtTest/private/qtesthelpers_p.h> +#include <qpa/qplatformtheme.h> +#include <QtWidgets/private/qapplication_p.h> using namespace QTestPrivate; @@ -117,6 +94,7 @@ private slots: void proxyStyle(); void dialogButtonBox(); void emptyStyleSheet(); + void toolTip_data(); void toolTip(); void embeddedFonts(); void opaquePaintEvent_data(); @@ -129,6 +107,7 @@ private slots: void QTBUG36933_brokenPseudoClassLookup(); void styleSheetChangeBeforePolish(); void placeholderColor(); + void accent(); void enumPropertySelector_data(); void enumPropertySelector(); //at the end because it mess with the style. @@ -143,6 +122,8 @@ private slots: void iconSizes_data(); void iconSizes(); + void inheritWidgetPalette_data(); + void inheritWidgetPalette(); private: static QColor COLOR(const QWidget &w) @@ -984,7 +965,7 @@ void tst_QStyleSheetStyle::focusColors() // ten pixels of the right color requires quite a many characters, as the // majority of the pixels will have slightly different colors due to the // anti-aliasing effect. -#if !defined(Q_OS_WIN32) && !(defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(Q_CC_INTEL)) +#if !defined(Q_OS_WIN32) && !(defined(Q_OS_LINUX) && defined(Q_CC_GNU)) QSKIP("This is a fragile test which fails on many esoteric platforms because of focus problems" " (for example, QTBUG-33959)." "That doesn't mean that the feature doesn't work in practice."); @@ -999,7 +980,6 @@ void tst_QStyleSheetStyle::focusColors() centerOnScreen(&frame); frame.show(); - QApplication::setActiveWindow(&frame); QVERIFY(QTest::qWaitForWindowActive(&frame)); for (QWidget *widget : frame.widgets()) { @@ -1045,7 +1025,6 @@ void tst_QStyleSheetStyle::hoverColors() QCursor::setPos(frame.geometry().topLeft() - QPoint(100, 0)); frame.show(); - QApplication::setActiveWindow(&frame); QVERIFY(QTest::qWaitForWindowActive(&frame)); QWindow *frameWindow = frame.windowHandle(); @@ -1220,20 +1199,31 @@ void tst_QStyleSheetStyle::tabAlignment() void tst_QStyleSheetStyle::tabFont_data() { - QTest::addColumn<int>("pixelSize"); + QTest::addColumn<QFont>("tabFont"); QTest::addColumn<QTabWidget::TabPosition>("tabPosition"); + QTest::addColumn<bool>("closable"); - QTest::newRow("medium, horizontal") << 24 << QTabWidget::North; - QTest::newRow("large, vertical") << 36 << QTabWidget::West; -} + QFont medium; + medium.setPixelSize(24); + QFont large; + large.setPixelSize(36); + QFont bold; + bold.setBold(true); -#include <QApplication> + QTest::newRow("medium, horizontal") << medium << QTabWidget::North << false; + QTest::newRow("large, vertical") << large << QTabWidget::West << false; + QTest::newRow("bold, horizontal, closable") << bold << QTabWidget::North << true; + QTest::newRow("bold, vertical, closable") << bold << QTabWidget::West << true; +} void tst_QStyleSheetStyle::tabFont() { - QFETCH(int, pixelSize); + QFETCH(QFont, tabFont); QFETCH(QTabWidget::TabPosition, tabPosition); + QFETCH(bool, closable); const bool vertical = tabPosition == QTabWidget::West || tabPosition == QTabWidget::East; + const QString tab0Text("Tab title"); + const QString tab1Text("Very Long Tab title"); // macOS style centers tabs and messes up the test QWindowsStyle windowsStyle; @@ -1242,18 +1232,22 @@ void tst_QStyleSheetStyle::tabFont() topLevel.setWindowTitle(QTest::currentTestFunction()); QTabWidget tabWidget; tabWidget.setStyle(&windowsStyle); - tabWidget.addTab(new QWidget,"Tab title"); tabWidget.setTabPosition(tabPosition); + tabWidget.addTab(new QWidget, tab0Text); + tabWidget.addTab(new QWidget, tab1Text); QTabWidget styledWidget; styledWidget.setStyle(&windowsStyle); styledWidget.setTabPosition(tabPosition); - styledWidget.addTab(new QWidget,"Tab title"); + styledWidget.addTab(new QWidget, tab0Text); + styledWidget.addTab(new QWidget, tab1Text); QTabBar *bar = tabWidget.tabBar(); QTabBar *styledBar = styledWidget.tabBar(); QVERIFY(bar && styledBar); bar->setStyle(&windowsStyle); + bar->setTabsClosable(closable); styledBar->setStyle(&windowsStyle); + styledBar->setTabsClosable(closable); QBoxLayout box(vertical ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom); box.addWidget(&tabWidget); @@ -1265,22 +1259,36 @@ void tst_QStyleSheetStyle::tabFont() topLevel.show(); QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); + const QFont defaultFont = tabWidget.font(); + if (QFontMetrics(defaultFont).size(Qt::TextShowMnemonic, tab0Text).width() >= + QFontMetrics(tabFont).size(Qt::TextShowMnemonic, tab0Text).width()) { + QSKIP("The used font is not larger when bold"); + } + const QRect defaultRect = bar->tabRect(0); QCOMPARE(styledBar->tabRect(0), defaultRect); - QFont font; - font.setPointSize(pixelSize); - tabWidget.setFont(font); - + tabWidget.setFont(tabFont); const QRect rectWithFont = bar->tabRect(0); if (vertical) QVERIFY(rectWithFont.height() > defaultRect.height()); else QVERIFY(rectWithFont.width() > defaultRect.width()); - styledWidget.setStyleSheet(QString("QTabBar { font-size: %1pt; }").arg(pixelSize)); + QString styleSheet = "QTabBar::tab:first {"; + if (tabFont.pixelSize() != -1) + styleSheet += QString(" font-size: %1px;").arg(tabFont.pixelSize()); + if (tabFont.bold()) + styleSheet += " font-weight: bold;"; + styleSheet += "}"; + + styledWidget.setStyleSheet(styleSheet); const QRect rectWithStyle = styledBar->tabRect(0); - QCOMPARE(rectWithStyle.size(), rectWithFont.size()); + + if (vertical) + QCOMPARE(rectWithStyle.height(), rectWithFont.height()); + else + QCOMPARE(rectWithStyle.width(), rectWithFont.width()); } void tst_QStyleSheetStyle::attributesList() @@ -1649,19 +1657,32 @@ private: const QString m_oldStyleName; }; +void tst_QStyleSheetStyle::toolTip_data() +{ + QTest::addColumn<QString>("style"); + + QTest::newRow("fusion") << QString("Fusion"); +#ifdef Q_OS_WINDOWS + QTest::newRow("windowsvista") << QString("WindowsVista"); +#endif +} + void tst_QStyleSheetStyle::toolTip() { + QFETCH(QString, style); + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) QSKIP("Wayland: This fails. Figure out why."); - qApp->setStyleSheet(QString()); QWidget w; w.resize(m_testSize); w.setWindowTitle(QTest::currentTestFunction()); + // Use "Fusion" to prevent the Vista style from clobbering the tooltip palette in polish(). - QStyle *fusionStyle = QStyleFactory::create(QLatin1String("Fusion")); - QVERIFY(fusionStyle); - ApplicationStyleSetter as(fusionStyle); + QStyle *appStyle = QStyleFactory::create(style); + QVERIFY(appStyle); + ApplicationStyleSetter as(appStyle); + QHBoxLayout layout(&w); w.setLayout(&layout); @@ -1689,37 +1710,65 @@ void tst_QStyleSheetStyle::toolTip() wid4->setToolTip("this is wid4"); wid4->setObjectName("wid4"); + QWidget *wid5 = new QPushButton("wid5", &w); + layout.addWidget(wid5); + wid5->setStyleSheet("QToolTip { background: #ff0; color: #f00 }"); + wid5->setToolTip("this is wid5"); + wid5->setObjectName("wid5"); + centerOnScreen(&w); w.show(); - QApplication::setActiveWindow(&w); QVERIFY(QTest::qWaitForWindowActive(&w)); - const QColor normalToolTip = QToolTip::palette().color(QPalette::Inactive, QPalette::ToolTipBase); + QColor normalToolTipBgColor = QToolTip::palette().color(QPalette::Inactive, QPalette::ToolTipBase); + +#ifdef Q_OS_MACOS + // macOS uses tool tip text color set in label palette + const QPalette *labelPalette = QGuiApplicationPrivate::platformTheme()->palette(QPlatformTheme::LabelPalette); + QColor normalToolTipFgColor = labelPalette->color(QPalette::Inactive, QPalette::ToolTipText); +#else + QColor normalToolTipFgColor = QToolTip::palette().color(QPalette::Inactive, QPalette::ToolTipText); +#endif + // Tooltip on the widget without stylesheet, then to other widget, // including one without stylesheet (the tooltip will be reused, // but its color must change) - const QWidgetList widgets{wid4, wid1, wid2, wid3, wid4}; - const QList<QColor> colors { normalToolTip, QColor("#ae2"), QColor("#f81"), QColor("#0b8"), - normalToolTip }; + const QWidgetList widgets{wid4, wid1, wid2, wid3, wid4, wid5}; + const QList<QColor> bgcolors { normalToolTipBgColor, QColor("#ae2"), QColor("#f81"), + QColor("#0b8"), normalToolTipBgColor, QColor("#ff0")}; + const QList<QColor> fgcolors { normalToolTipFgColor, normalToolTipFgColor, normalToolTipFgColor, + normalToolTipFgColor, normalToolTipFgColor, QColor("#f00")}; QWidgetList topLevels; - for (int i = 0; i < widgets.count() ; ++i) { + for (int i = 0; i < widgets.size() ; ++i) { QWidget *wid = widgets.at(i); - QColor col = colors.at(i); + QColor bgColor = bgcolors.at(i); + QColor fgColor = fgcolors.at(i); QToolTip::showText( QPoint(0,0) , "This is " + wid->objectName(), wid); topLevels = QApplication::topLevelWidgets(); QWidget *tooltip = nullptr; - for (QWidget *widget : qAsConst(topLevels)) { + for (QWidget *widget : std::as_const(topLevels)) { if (widget->inherits("QTipLabel")) { tooltip = widget; break; } } + QVERIFY(tooltip); QTRY_VERIFY(tooltip->isVisible()); // Wait until Roll-Effect is finished (Windows Vista) - QCOMPARE(tooltip->palette().color(tooltip->backgroundRole()), col); + +#ifdef Q_OS_WINDOWS + // If tooltip palette contains empty resolve mask, validate with inherited palette + if (!tooltip->palette().resolveMask()) { + bgColor = w.palette().color(tooltip->backgroundRole()); + fgColor = w.palette().color(tooltip->foregroundRole()); + } +#endif + + QCOMPARE(tooltip->palette().color(tooltip->backgroundRole()), bgColor); + QCOMPARE(tooltip->palette().color(tooltip->foregroundRole()), fgColor); } QToolTip::showText( QPoint(0,0) , "This is " + wid3->objectName(), wid3); @@ -1727,7 +1776,7 @@ void tst_QStyleSheetStyle::toolTip() delete wid3; //should not crash; QTest::qWait(10); topLevels = QApplication::topLevelWidgets(); - for (QWidget *widget : qAsConst(topLevels)) + for (QWidget *widget : std::as_const(topLevels)) widget->update(); //should not crash either } @@ -1837,7 +1886,6 @@ void tst_QStyleSheetStyle::complexWidgetFocus() centerOnScreen(&frame); frame.show(); - QApplication::setActiveWindow(&frame); QVERIFY(QTest::qWaitForWindowActive(&frame)); for (QWidget *widget : widgets) { widget->setFocus(); @@ -1926,7 +1974,6 @@ void tst_QStyleSheetStyle::task232085_spinBoxLineEditBg() centerOnScreen(&frame); frame.show(); - QApplication::setActiveWindow(&frame); spinbox->setFocus(); QVERIFY(QTest::qWaitForWindowActive(&frame)); @@ -2062,7 +2109,6 @@ void tst_QStyleSheetStyle::QTBUG36933_brokenPseudoClassLookup() QVERIFY(QTest::qWaitForWindowExposed(&widget)); widget.activateWindow(); - QApplication::setActiveWindow(&widget); QVERIFY(QTest::qWaitForWindowActive(&widget)); QHeaderView *verticalHeader = widget.verticalHeader(); @@ -2335,11 +2381,32 @@ void tst_QStyleSheetStyle::placeholderColor() QLineEdit le2; le2.setEnabled(false); le1.ensurePolished(); - QCOMPARE(le1.palette().placeholderText(), red); + QColor phColor = le1.palette().placeholderText().color(); + QCOMPARE(phColor.rgb(), red.rgb()); + QVERIFY(phColor.alpha() < red.alpha()); + le2.ensurePolished(); - QCOMPARE(le2.palette().placeholderText(), red); + phColor = le2.palette().placeholderText().color(); + QCOMPARE(phColor.rgb(), red.rgb()); + QVERIFY(phColor.alpha() < red.alpha()); + le2.setEnabled(true); - QCOMPARE(le2.palette().placeholderText(), red); + phColor = le2.palette().placeholderText().color(); + QCOMPARE(phColor.rgb(), red.rgb()); + QVERIFY(phColor.alpha() < red.alpha()); + + const char *phSpec = "#aabbccdd"; + le1.setStyleSheet(QString("QLineEdit { placeholder-text-color: %1; }").arg(phSpec)); + QCOMPARE(le1.palette().placeholderText().color(), QColor(phSpec)); +} + +void tst_QStyleSheetStyle::accent() +{ + QLineEdit lineEdit; + const QColor universe(42, 42, 42); + lineEdit.setStyleSheet(QString("QLineEdit { accent-color: %1; }").arg(universe.name())); + lineEdit.ensurePolished(); + QCOMPARE(lineEdit.palette().accent().color(), universe); } void tst_QStyleSheetStyle::enumPropertySelector_data() @@ -2436,6 +2503,31 @@ void tst_QStyleSheetStyle::iconSizes() QCOMPARE(button.iconSize(), iconSize); } +void tst_QStyleSheetStyle::inheritWidgetPalette_data() +{ + QTest::addColumn<const QString>("styleSheet"); + QTest::addColumn<const QColor>("phColorPalette"); + + QTest::addRow("blueAndGreen") << "QLineEdit {color: rgb(0,0,255);}" << QColor(Qt::green); + QTest::addRow("emptyStyleSheet") << QString() << QColor(Qt::green); + +} + +void tst_QStyleSheetStyle::inheritWidgetPalette() +{ + QFETCH(const QString, styleSheet); + QFETCH(const QColor, phColorPalette); + + QLineEdit edit; + QPalette palette = edit.palette(); + palette.setBrush(QPalette::PlaceholderText, phColorPalette); + edit.setPalette(palette); + edit.setStyleSheet(styleSheet); + const QColor phColor = edit.palette().placeholderText().color(); + + QCOMPARE(phColor, phColorPalette); +} + QTEST_MAIN(tst_QStyleSheetStyle) #include "tst_qstylesheetstyle.moc" |