summaryrefslogtreecommitdiffstats
path: root/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/gui/kernel/qpalette/tst_qpalette.cpp')
-rw-r--r--tests/auto/gui/kernel/qpalette/tst_qpalette.cpp177
1 files changed, 144 insertions, 33 deletions
diff --git a/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp b/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp
index 9ef313f36d..c21828bee2 100644
--- a/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp
+++ b/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp
@@ -1,30 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 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) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
@@ -47,6 +22,8 @@ private Q_SLOTS:
void noBrushesSetForDefaultPalette();
void cannotCheckIfInvalidBrushSet();
void checkIfBrushForCurrentGroupSet();
+ void cacheKey();
+ void dataStream();
};
void tst_QPalette::roleValues_data()
@@ -75,9 +52,10 @@ void tst_QPalette::roleValues_data()
QTest::newRow("QPalette::ToolTipBase") << int(QPalette::ToolTipBase) << 18;
QTest::newRow("QPalette::ToolTipText") << int(QPalette::ToolTipText) << 19;
QTest::newRow("QPalette::PlaceholderText") << int(QPalette::PlaceholderText) << 20;
+ QTest::newRow("QPalette::Accent") << int(QPalette::Accent) << 21;
// Change this value as you add more roles.
- QTest::newRow("QPalette::NColorRoles") << int(QPalette::NColorRoles) << 21;
+ QTest::newRow("QPalette::NColorRoles") << int(QPalette::NColorRoles) << 22;
}
void tst_QPalette::roleValues()
@@ -122,6 +100,21 @@ void tst_QPalette::resolve()
QVERIFY(p2ResolvedTo1 != p1);
QVERIFY(p2ResolvedTo1 != p2);
+
+ QPalette p3;
+ // ensure the resolve mask is full
+ for (int r = 0; r < QPalette::NColorRoles; ++r)
+ p3.setBrush(QPalette::All, QPalette::ColorRole(r), Qt::red);
+ const QPalette::ResolveMask fullMask = p3.resolveMask();
+
+ QPalette p3ResolvedToP1 = p3.resolve(p1);
+ QVERIFY(p3ResolvedToP1.isCopyOf(p3));
+
+ QPalette p4;
+ QCOMPARE(p4.resolveMask(), QPalette::ResolveMask{});
+ // resolve must detach even if p4 has no mask
+ p4 = p4.resolve(p3);
+ QCOMPARE(p3.resolveMask(), fullMask);
}
@@ -207,9 +200,6 @@ void tst_QPalette::setBrush()
const QPalette pp = p;
QVERIFY(pp.isCopyOf(p));
- // Setting the same brush won't detach
- p.setBrush(QPalette::Disabled, QPalette::Button, Qt::green);
- QVERIFY(pp.isCopyOf(p));
}
void tst_QPalette::isBrushSet()
@@ -234,7 +224,7 @@ void tst_QPalette::isBrushSet()
QVERIFY(!p2.isBrushSet(QPalette::Active, QPalette::Dark));
p2.setBrush(QPalette::Active, QPalette::Dark, p2.brush(QPalette::Active, QPalette::Dark));
QVERIFY(!p3.isBrushSet(QPalette::Active, QPalette::Dark));
- QVERIFY(!p2.isBrushSet(QPalette::Active, QPalette::Dark));
+ QVERIFY(p2.isBrushSet(QPalette::Active, QPalette::Dark));
}
void tst_QPalette::setAllPossibleBrushes()
@@ -248,8 +238,14 @@ void tst_QPalette::setAllPossibleBrushes()
}
for (int r = 0; r < QPalette::NColorRoles; ++r) {
+ const QPalette::ColorRole role = static_cast<QPalette::ColorRole>(r);
for (int g = 0; g < QPalette::NColorGroups; ++g) {
- QVERIFY(p.isBrushSet(QPalette::ColorGroup(g), QPalette::ColorRole(r)));
+ const QPalette::ColorGroup group = static_cast<QPalette::ColorGroup>(g);
+ // NoRole has no resolve bit => isBrushSet returns false
+ if (role == QPalette::NoRole)
+ QVERIFY(!p.isBrushSet(group, role));
+ else
+ QVERIFY(p.isBrushSet(group, role));
}
}
}
@@ -277,5 +273,120 @@ void tst_QPalette::checkIfBrushForCurrentGroupSet()
QVERIFY(p.isBrushSet(QPalette::Current, QPalette::Link));
}
+void tst_QPalette::cacheKey()
+{
+ const QPalette defaultPalette;
+ // precondition: all palettes are expected to have contrasting text on base
+ QVERIFY(defaultPalette.base() != defaultPalette.text());
+ const auto defaultCacheKey = defaultPalette.cacheKey();
+ const auto defaultSerNo = defaultCacheKey >> 32;
+ const auto defaultDetachNo = defaultCacheKey & 0xffffffff;
+
+ QPalette changeTwicePalette(defaultPalette);
+ changeTwicePalette.setBrush(QPalette::All, QPalette::ButtonText, Qt::red);
+ const auto firstChangeCacheKey = changeTwicePalette.cacheKey();
+ QCOMPARE_NE(firstChangeCacheKey, defaultCacheKey);
+ changeTwicePalette.setBrush(QPalette::All, QPalette::ButtonText, Qt::green);
+ const auto secondChangeCacheKey = changeTwicePalette.cacheKey();
+ QCOMPARE_NE(firstChangeCacheKey, secondChangeCacheKey);
+
+ QPalette copyDifferentData(defaultPalette);
+ QPalette copyDifferentMask(defaultPalette);
+ QPalette copyDifferentMaskAndData(defaultPalette);
+
+ QCOMPARE(defaultPalette.cacheKey(), copyDifferentData.cacheKey());
+
+ // deep detach of both private and data
+ copyDifferentData.setBrush(QPalette::Base, defaultPalette.text());
+ const auto differentDataKey = copyDifferentData.cacheKey();
+ const auto differentDataSerNo = differentDataKey >> 32;
+ const auto differentDataDetachNo = differentDataKey & 0xffffffff;
+ auto loggerDeepDetach = qScopeGuard([&](){
+ qDebug() << "Deep detach serial" << differentDataSerNo;
+ qDebug() << "Deep detach detach number" << differentDataDetachNo;
+ });
+
+ QCOMPARE_NE(copyDifferentData.cacheKey(), defaultCacheKey);
+ QCOMPARE(defaultPalette.cacheKey(), defaultCacheKey);
+
+ // shallow detach, both privates reference the same data
+ copyDifferentMask.setResolveMask(0xffffffffffffffff);
+ const auto differentMaskKey = copyDifferentMask.cacheKey();
+ const auto differentMaskSerNo = differentMaskKey >> 32;
+ const auto differentMaskDetachNo = differentMaskKey & 0xffffffff;
+ auto loggerShallowDetach = qScopeGuard([&](){
+ qDebug() << "Shallow detach serial" << differentMaskSerNo;
+ qDebug() << "Shallow detach detach number" << differentMaskDetachNo;
+ });
+
+ QCOMPARE(differentMaskSerNo, defaultSerNo);
+ QCOMPARE_NE(differentMaskSerNo, defaultDetachNo);
+ QCOMPARE_NE(differentMaskKey, defaultCacheKey);
+ QCOMPARE_NE(differentMaskKey, differentDataKey);
+
+ // shallow detach, both privates reference the same data
+ copyDifferentMaskAndData.setResolveMask(0xeeeeeeeeeeeeeeee);
+ const auto modifiedCacheKey = copyDifferentMaskAndData.cacheKey();
+ QCOMPARE_NE(modifiedCacheKey, copyDifferentMask.cacheKey());
+ QCOMPARE_NE(modifiedCacheKey, defaultCacheKey);
+ QCOMPARE_NE(modifiedCacheKey, copyDifferentData.cacheKey());
+ QCOMPARE_NE(copyDifferentMask.cacheKey(), defaultCacheKey);
+
+ // full detach - both key elements are different
+ copyDifferentMaskAndData.setBrush(QPalette::Base, defaultPalette.text());
+ const auto modifiedAllKey = copyDifferentMaskAndData.cacheKey();
+ const auto modifiedAllSerNo = modifiedAllKey >> 32;
+ const auto modifiedAllDetachNo = modifiedAllKey & 0xffffffff;
+ QCOMPARE_NE(modifiedAllSerNo, defaultSerNo);
+ QCOMPARE_NE(modifiedAllDetachNo, defaultDetachNo);
+
+ QCOMPARE_NE(modifiedAllKey, copyDifferentMask.cacheKey());
+ QCOMPARE_NE(modifiedAllKey, defaultCacheKey);
+ QCOMPARE_NE(modifiedAllKey, differentDataKey);
+ QCOMPARE_NE(modifiedAllKey, modifiedCacheKey);
+
+ loggerDeepDetach.dismiss();
+ loggerShallowDetach.dismiss();
+}
+
+void tst_QPalette::dataStream()
+{
+ const QColor highlight(42, 42, 42);
+ const QColor accent(13, 13, 13);
+ QPalette palette;
+ palette.setBrush(QPalette::Highlight, highlight);
+ palette.setBrush(QPalette::Accent, accent);
+
+ // When saved with Qt_6_5 or earlier, Accent defaults to Highlight
+ {
+ QByteArray b;
+ {
+ QDataStream stream(&b, QIODevice::WriteOnly);
+ stream.setVersion(QDataStream::Qt_6_5);
+ stream << palette;
+ }
+ QPalette test;
+ QDataStream stream (&b, QIODevice::ReadOnly);
+ stream.setVersion(QDataStream::Qt_6_5);
+ stream >> test;
+ QCOMPARE(test.accent().color(), highlight);
+ }
+
+ // When saved with Qt_6_6 or later, Accent is saved explicitly
+ {
+ QByteArray b;
+ {
+ QDataStream stream(&b, QIODevice::WriteOnly);
+ stream.setVersion(QDataStream::Qt_6_6);
+ stream << palette;
+ }
+ QPalette test;
+ QDataStream stream (&b, QIODevice::ReadOnly);
+ stream.setVersion(QDataStream::Qt_6_6);
+ stream >> test;
+ QCOMPARE(test.accent().color(), accent);
+ }
+}
+
QTEST_MAIN(tst_QPalette)
#include "tst_qpalette.moc"