File indexing completed on 2024-04-21 03:53:40

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2012 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include <kshareddatacache.h>
0009 
0010 #include <QStandardPaths>
0011 #include <QTest>
0012 
0013 #include <QObject>
0014 #include <QStandardPaths>
0015 #include <QString>
0016 #include <string.h> // strcpy
0017 
0018 class KSharedDataCacheTest : public QObject
0019 {
0020     Q_OBJECT
0021 
0022 private Q_SLOTS:
0023     void initTestCase();
0024     void simpleInsert();
0025 };
0026 
0027 void KSharedDataCacheTest::initTestCase()
0028 {
0029 }
0030 
0031 void KSharedDataCacheTest::simpleInsert()
0032 {
0033     const QLatin1String cacheName("myTestCache");
0034     const QLatin1String key("mypic");
0035     // clear the cache
0036     QString cacheFile = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1String("/") + cacheName + QLatin1String(".kcache");
0037     QFile file(cacheFile);
0038     if (file.exists()) {
0039         QVERIFY(file.remove());
0040     }
0041     // insert something into it
0042     KSharedDataCache cache(cacheName, 5 * 1024 * 1024);
0043 #ifndef Q_OS_WIN // the windows implementation is currently only memory based and not really shared
0044     QVERIFY(file.exists()); // make sure we got the cache filename right
0045 #endif
0046     QByteArray data;
0047     data.resize(9228);
0048     strcpy(data.data(), "Hello world");
0049     QVERIFY(cache.insert(key, data));
0050     // read it out again
0051     QByteArray result;
0052     QVERIFY(cache.find(key, &result));
0053     QCOMPARE(result, data);
0054     // another insert
0055     strcpy(data.data(), "Hello KDE");
0056     QVERIFY(cache.insert(key, data));
0057     // and another read
0058     QVERIFY(cache.find(key, &result));
0059     QCOMPARE(result, data);
0060 }
0061 
0062 QTEST_MAIN(KSharedDataCacheTest)
0063 
0064 #include "kshareddatacachetest.moc"