File indexing completed on 2025-01-05 03:58:08
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2009-12-23 0007 * Description : a test for the StateSavingObject class 0008 * 0009 * SPDX-FileCopyrightText: 2009 by Johannes Wienke <languitar at semipol dot de> 0010 * 0011 * SPDX-License-Identifier: GPL-2.0-or-later 0012 * 0013 * ============================================================ */ 0014 0015 // Disable static analysis with clang as it report false positives. 0016 #ifndef __clang_analyzer__ 0017 0018 #include "statesavingobject_utest.h" 0019 0020 // Qt includes 0021 0022 #include <QBuffer> 0023 #include <QPointer> 0024 0025 // KDE includes 0026 0027 #include <kconfiggroup.h> 0028 #include <ksharedconfig.h> 0029 0030 // Local includes 0031 0032 0033 using namespace Digikam; 0034 0035 QTEST_GUILESS_MAIN(StateSavingObjectTest) 0036 0037 class Q_DECL_HIDDEN StubStateSaver::Private 0038 { 0039 public: 0040 0041 Private() 0042 : loadCalls(0), 0043 saveCalls(0) 0044 { 0045 } 0046 0047 unsigned int loadCalls; 0048 unsigned int saveCalls; 0049 }; 0050 0051 StubStateSaver::StubStateSaver(QObject* const parent) 0052 : QObject(parent), 0053 StateSavingObject(this), 0054 d(new Private) 0055 { 0056 } 0057 0058 StubStateSaver::~StubStateSaver() 0059 { 0060 delete d; 0061 } 0062 0063 KConfigGroup StubStateSaver::getGroup() 0064 { 0065 return getConfigGroup(); 0066 } 0067 0068 QString StubStateSaver::getEntryKey(const QString& base) 0069 { 0070 return entryName(base); 0071 } 0072 0073 void StubStateSaver::doLoadState() 0074 { 0075 d->loadCalls++; 0076 } 0077 0078 void StubStateSaver::doSaveState() 0079 { 0080 d->saveCalls++; 0081 } 0082 0083 bool StubStateSaver::loadCalled() 0084 { 0085 return d->loadCalls > 0; 0086 } 0087 0088 bool StubStateSaver::saveCalled() 0089 { 0090 return d->saveCalls > 0; 0091 } 0092 0093 unsigned int StubStateSaver::numLoadCalls() 0094 { 0095 return d->loadCalls; 0096 } 0097 0098 unsigned int StubStateSaver::numSaveCalls() 0099 { 0100 return d->saveCalls; 0101 } 0102 0103 // ----------------------------------------------------------------------------- 0104 0105 StateSavingObjectTest::StateSavingObjectTest(QObject* const parent) 0106 : QObject(parent) 0107 { 0108 } 0109 0110 void StateSavingObjectTest::testGroupName() 0111 { 0112 StubStateSaver saver; 0113 QCOMPARE(saver.getGroup().name(), QLatin1String("<default>")); 0114 0115 const QString name = QLatin1String("testName 2"); 0116 saver.setObjectName(name); 0117 QCOMPARE(saver.getGroup().name(), name); 0118 0119 KConfigGroup group = KSharedConfig::openConfig()->group(QLatin1String("SimpleTest Group")); 0120 saver.setConfigGroup(group); 0121 QCOMPARE(saver.getGroup().name(), group.name()); 0122 0123 // setting object name must not change returned group 0124 saver.setObjectName(QLatin1String("new Name")); 0125 QCOMPARE(saver.getGroup().name(), group.name()); 0126 0127 // resetting group must work 0128 KConfigGroup group2 = KSharedConfig::openConfig()->group(QLatin1String("Another SimpleTest Group")); 0129 saver.setConfigGroup(group2); 0130 QCOMPARE(saver.getGroup().name(), group2.name()); 0131 } 0132 0133 void StateSavingObjectTest::testPrefixUsage() 0134 { 0135 // default, empty prefix 0136 StubStateSaver saver; 0137 QCOMPARE(saver.getEntryKey(QLatin1String("")), QLatin1String("")); 0138 QCOMPARE(saver.getEntryKey(QLatin1String("test")), QLatin1String("test")); 0139 0140 const QString prefix = QLatin1String(" _Pr efix_ "); 0141 saver.setEntryPrefix(prefix); 0142 QCOMPARE(saver.getEntryKey(QLatin1String("")), prefix); 0143 QString tmp = prefix + QLatin1String("test"); 0144 QCOMPARE(saver.getEntryKey(QLatin1String("test")), tmp); 0145 } 0146 0147 void StateSavingObjectTest::testDirectCalling() 0148 { 0149 StubStateSaver loader; 0150 QVERIFY(!loader.loadCalled()); 0151 loader.loadState(); 0152 QVERIFY(loader.loadCalled()); 0153 QVERIFY(!loader.saveCalled()); 0154 0155 StubStateSaver saver; 0156 QVERIFY(!saver.saveCalled()); 0157 saver.saveState(); 0158 QVERIFY(saver.saveCalled()); 0159 QVERIFY(!saver.loadCalled()); 0160 } 0161 0162 void StateSavingObjectTest::testDirectChildrenLoading() 0163 { 0164 StubStateSaver* const parentSaver = new StubStateSaver(nullptr); 0165 QPointer<StubStateSaver> directChild1 = new StubStateSaver(parentSaver); 0166 QPointer<StubStateSaver> directChild2 = new StubStateSaver(parentSaver); 0167 QPointer<StubStateSaver> indirectChild = new StubStateSaver(directChild1); 0168 0169 parentSaver->setStateSavingDepth(StateSavingObject::DIRECT_CHILDREN); 0170 parentSaver->loadState(); 0171 0172 QVERIFY(parentSaver->loadCalled()); 0173 QVERIFY(directChild1->loadCalled()); 0174 QVERIFY(directChild2->loadCalled()); 0175 QVERIFY(!indirectChild->loadCalled()); 0176 0177 QVERIFY(!parentSaver->saveCalled()); 0178 QVERIFY(!directChild1->saveCalled()); 0179 QVERIFY(!directChild2->saveCalled()); 0180 QVERIFY(!indirectChild->saveCalled()); 0181 0182 delete parentSaver; 0183 } 0184 0185 void StateSavingObjectTest::testDirectChildrenSaving() 0186 { 0187 StubStateSaver* const parentSaver = new StubStateSaver(nullptr); 0188 QPointer<StubStateSaver> directChild1 = new StubStateSaver(parentSaver); 0189 QPointer<StubStateSaver> directChild2 = new StubStateSaver(parentSaver); 0190 QPointer<StubStateSaver> indirectChild = new StubStateSaver(directChild1); 0191 0192 parentSaver->setStateSavingDepth(StateSavingObject::DIRECT_CHILDREN); 0193 parentSaver->saveState(); 0194 0195 QVERIFY(parentSaver->saveCalled()); 0196 QVERIFY(directChild1->saveCalled()); 0197 QVERIFY(directChild2->saveCalled()); 0198 QVERIFY(!indirectChild->saveCalled()); 0199 0200 QVERIFY(!parentSaver->loadCalled()); 0201 QVERIFY(!directChild1->loadCalled()); 0202 QVERIFY(!directChild2->loadCalled()); 0203 QVERIFY(!indirectChild->loadCalled()); 0204 0205 delete parentSaver; 0206 } 0207 0208 void StateSavingObjectTest::testRecursiveChildrenLoading() 0209 { 0210 StubStateSaver* const parentSaver = new StubStateSaver(nullptr); 0211 QPointer<StubStateSaver> directChild1 = new StubStateSaver(parentSaver); 0212 QPointer<StubStateSaver> directChild2 = new StubStateSaver(parentSaver); 0213 QPointer<StubStateSaver> indirectChild1 = new StubStateSaver(directChild1); 0214 QPointer<StubStateSaver> indirectChild2 = new StubStateSaver(directChild2); 0215 QPointer<StubStateSaver> indirectChild3 = new StubStateSaver(directChild2); 0216 QBuffer* const directChildStateless = new QBuffer(parentSaver); 0217 QPointer<StubStateSaver> indirectStatelessChild = new StubStateSaver(directChildStateless); 0218 0219 parentSaver->setStateSavingDepth(StateSavingObject::RECURSIVE); 0220 directChild1->setStateSavingDepth(StateSavingObject::RECURSIVE); 0221 0222 parentSaver->loadState(); 0223 0224 QVERIFY(parentSaver->loadCalled()); 0225 QVERIFY(directChild1->loadCalled()); 0226 QVERIFY(directChild2->loadCalled()); 0227 QVERIFY(indirectChild1->loadCalled()); 0228 QVERIFY(indirectChild2->loadCalled()); 0229 QVERIFY(indirectChild3->loadCalled()); 0230 QVERIFY(indirectStatelessChild->loadCalled()); 0231 0232 const unsigned int desiredCalls = 1; 0233 QCOMPARE(parentSaver->numLoadCalls(), desiredCalls); 0234 QCOMPARE(directChild1->numLoadCalls(), desiredCalls); 0235 QCOMPARE(directChild2->numLoadCalls(), desiredCalls); 0236 QCOMPARE(indirectChild1->numLoadCalls(), desiredCalls); 0237 QCOMPARE(indirectChild2->numLoadCalls(), desiredCalls); 0238 QCOMPARE(indirectChild3->numLoadCalls(), desiredCalls); 0239 QCOMPARE(indirectStatelessChild->numLoadCalls(), desiredCalls); 0240 0241 QCOMPARE(directChild1->getStateSavingDepth(), StateSavingObject::RECURSIVE); 0242 0243 delete parentSaver; 0244 } 0245 0246 void StateSavingObjectTest::testRecursiveChildrenSaving() 0247 { 0248 StubStateSaver* const parentSaver = new StubStateSaver(nullptr); 0249 QPointer<StubStateSaver> directChild1 = new StubStateSaver(parentSaver); 0250 QPointer<StubStateSaver> directChild2 = new StubStateSaver(parentSaver); 0251 QPointer<StubStateSaver> indirectChild1 = new StubStateSaver(directChild1); 0252 QPointer<StubStateSaver> indirectChild2 = new StubStateSaver(directChild2); 0253 QPointer<StubStateSaver> indirectChild3 = new StubStateSaver(directChild2); 0254 QBuffer* const directChildStateless = new QBuffer(parentSaver); 0255 QPointer<StubStateSaver> indirectStatelessChild = new StubStateSaver(directChildStateless); 0256 0257 parentSaver->setStateSavingDepth(StateSavingObject::RECURSIVE); 0258 directChild1->setStateSavingDepth(StateSavingObject::RECURSIVE); 0259 0260 parentSaver->saveState(); 0261 0262 QVERIFY(parentSaver->saveCalled()); 0263 QVERIFY(directChild1->saveCalled()); 0264 QVERIFY(directChild2->saveCalled()); 0265 QVERIFY(indirectChild1->saveCalled()); 0266 QVERIFY(indirectChild2->saveCalled()); 0267 QVERIFY(indirectChild3->saveCalled()); 0268 QVERIFY(indirectStatelessChild->saveCalled()); 0269 0270 const unsigned int desiredCalls = 1; 0271 QCOMPARE(parentSaver->numSaveCalls(), desiredCalls); 0272 QCOMPARE(directChild1->numSaveCalls(), desiredCalls); 0273 QCOMPARE(directChild2->numSaveCalls(), desiredCalls); 0274 QCOMPARE(indirectChild1->numSaveCalls(), desiredCalls); 0275 QCOMPARE(indirectChild2->numSaveCalls(), desiredCalls); 0276 QCOMPARE(indirectChild3->numSaveCalls(), desiredCalls); 0277 QCOMPARE(indirectStatelessChild->numSaveCalls(), desiredCalls); 0278 0279 QCOMPARE(directChild1->getStateSavingDepth(), StateSavingObject::RECURSIVE); 0280 0281 delete parentSaver; 0282 } 0283 0284 #endif // __clang_analyzer__ 0285 0286 #include "moc_statesavingobject_utest.cpp"