summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2022-10-01 18:04:56 +0200
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2022-10-28 20:52:31 +0200
commitbf6cad8ba5efa9231970ff6db1e6ffcdbdeebd40 (patch)
tree1e7bf4e83affa755a3cc733032d7b8d9522a0ada /tests
parentf35ad8f32f17a3a9164e2bf8d5d80b3ff3981b69 (diff)
Make sure that palette cache keys are unique
After 1d961491d817490da156769ddce6fce48a0bce4a, palettes are different if they either have different brush data, or a different private. Two privates can share data, but still must generate different cache keys. The cacheKey has so far been composted of the serial number of the Data struct, and a detach number that is incremented when we detach the private. This failed for two reasons: - the implicit copy constructor of the Data class copied the serial number, when it should have incremented it. Fix that by member- initializing the serial number rather than doing it only in the default constructor. The member initialization is also executed for the copy constructor. - the detach_no logic as it was implemented does not guarantee that two copies of the same palette that share data, but have different resolve masks (and thus different privates) have different detach_no values. Use a static serial counter for that number as well. Amend the test case to verfiy that cache keys, and the elements of the cache keys, change when they are expected to. Fixes: QTBUG-106984 Change-Id: I84d7055ce8bfe0d42f1f8e9766f3f1ad610f4ec8 Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io> (cherry picked from commit 109e088c7c5d0c9325966e88d55fd9f7a58f67ea) Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/gui/kernel/qpalette/tst_qpalette.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp b/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp
index a81e6187e5..13365b01fe 100644
--- a/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp
+++ b/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp
@@ -47,6 +47,7 @@ private Q_SLOTS:
void noBrushesSetForDefaultPalette();
void cannotCheckIfInvalidBrushSet();
void checkIfBrushForCurrentGroupSet();
+ void cacheKey();
};
void tst_QPalette::roleValues_data()
@@ -290,5 +291,61 @@ 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 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;
+
+ QVERIFY(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;
+ QCOMPARE(differentMaskSerNo, defaultSerNo);
+ QVERIFY(differentMaskSerNo != defaultDetachNo);
+ QVERIFY(differentMaskKey != defaultCacheKey);
+ QVERIFY(differentMaskKey != differentDataKey);
+
+ // shallow detach, both privates reference the same data
+ copyDifferentMaskAndData.setResolveMask(0xeeeeeeeeeeeeeeee);
+ const auto modifiedCacheKey = copyDifferentMaskAndData.cacheKey();
+ QVERIFY(modifiedCacheKey != copyDifferentMask.cacheKey());
+ QVERIFY(modifiedCacheKey != defaultCacheKey);
+ QVERIFY(modifiedCacheKey != copyDifferentData.cacheKey());
+ QVERIFY(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;
+ QVERIFY(modifiedAllSerNo != defaultSerNo);
+ QVERIFY(modifiedAllDetachNo != defaultDetachNo);
+
+ QVERIFY(modifiedAllKey != copyDifferentMask.cacheKey());
+ QVERIFY(modifiedAllKey != defaultCacheKey);
+ QVERIFY(modifiedAllKey != differentDataKey);
+ QVERIFY(modifiedAllKey != modifiedCacheKey);
+}
+
QTEST_MAIN(tst_QPalette)
#include "tst_qpalette.moc"