File indexing completed on 2024-04-14 04:52:45

0001 /* This file is part of KDE
0002     SPDX-FileCopyrightText: 2006 David Faure <faure@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QTest>
0008 #include <QSignalSpy>
0009 #include <konqhistorymanager.h>
0010 
0011 #include <QObject>
0012 #include <QStandardPaths>
0013 
0014 class HistoryManagerTest : public QObject
0015 {
0016     Q_OBJECT
0017 
0018 private Q_SLOTS:
0019     void initTestCase();
0020     void testGetSetMaxCount();
0021     void testGetSetMaxAge();
0022     void testAddHistoryEntry();
0023 };
0024 
0025 QTEST_MAIN(HistoryManagerTest)
0026 
0027 void HistoryManagerTest::initTestCase()
0028 {
0029     QStandardPaths::setTestModeEnabled(true);
0030 }
0031 
0032 void HistoryManagerTest::testGetSetMaxCount()
0033 {
0034     KonqHistoryManager mgr(nullptr);
0035     const int oldMaxCount = mgr.maxCount();
0036     qDebug("oldMaxCount=%d", oldMaxCount);
0037     mgr.emitSetMaxCount(4242);
0038     QTest::qWait(100);   // ### fragile. We have no signal to wait for, so we must just wait a little bit...
0039     // Yes this is just a set+get test, but given that it goes via DBUS before changing the member variable
0040     // we do test quite a lot with it. We can't really instantiate two KonqHistoryManagers (same dbus path),
0041     // so we'd need two processes to test the dbus signal 'for real', if the setter changed the member var...
0042     QCOMPARE(mgr.maxCount(), 4242);
0043     mgr.emitSetMaxCount(oldMaxCount);
0044     QTest::qWait(100);   // ### fragile. Wait again otherwise the change will be lost
0045     QCOMPARE(mgr.maxCount(), oldMaxCount);
0046 }
0047 
0048 void HistoryManagerTest::testGetSetMaxAge()
0049 {
0050     KonqHistoryManager mgr(nullptr);
0051     const int oldMaxAge = mgr.maxAge();
0052     qDebug("oldMaxAge=%d", oldMaxAge);
0053     mgr.emitSetMaxAge(4242);
0054     QTest::qWait(100);   // ### fragile. We have no signal to wait for, so we must just wait a little bit...
0055     QCOMPARE(mgr.maxAge(), 4242);
0056     mgr.emitSetMaxAge(oldMaxAge);
0057     QTest::qWait(100);   // ### fragile. Wait again otherwise the change will be lost
0058     QCOMPARE(mgr.maxAge(), oldMaxAge);
0059 }
0060 
0061 static void waitForAddedSignal(KonqHistoryManager *mgr)
0062 {
0063     QEventLoop eventLoop;
0064     QObject::connect(mgr, &KonqHistoryManager::entryAdded, &eventLoop, &QEventLoop::quit);
0065     eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
0066 }
0067 
0068 static void waitForRemovedSignal(KonqHistoryManager *mgr)
0069 {
0070     QEventLoop eventLoop;
0071     QObject::connect(mgr, &KonqHistoryManager::entryRemoved, &eventLoop, &QEventLoop::quit);
0072     eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
0073 }
0074 
0075 void HistoryManagerTest::testAddHistoryEntry()
0076 {
0077     KonqHistoryManager mgr(nullptr);
0078     qRegisterMetaType<KonqHistoryEntry>("KonqHistoryEntry");
0079     QSignalSpy addedSpy(&mgr, &KonqHistoryManager::entryAdded);
0080     QSignalSpy removedSpy(&mgr,  &KonqHistoryManager::entryRemoved);
0081     const QUrl url(QStringLiteral("http://user@historymgrtest.org/"));
0082     const QString typedUrl = QStringLiteral("http://www.example.net");
0083     const QString title = QStringLiteral("The Title");
0084     mgr.addPending(url, typedUrl, title);
0085 
0086     waitForAddedSignal(&mgr);
0087 
0088     QCOMPARE(addedSpy.count(), 1);
0089     QCOMPARE(removedSpy.count(), 0);
0090     QList<QVariant> args = addedSpy[0];
0091     QCOMPARE(args.count(), 1);
0092     KonqHistoryEntry entry = qvariant_cast<KonqHistoryEntry>(args[0]);
0093     QCOMPARE(entry.url.url(), url.url());
0094     QCOMPARE(entry.typedUrl, typedUrl);
0095     QCOMPARE(entry.title, QString());   // not set yet, still pending
0096     QCOMPARE(int(entry.numberOfTimesVisited), 1);
0097 
0098     // Now confirm it
0099     mgr.confirmPending(url, typedUrl, title);
0100     // ## alternate code path: mgr.removePending()
0101 
0102     waitForAddedSignal(&mgr);
0103 
0104     QCOMPARE(addedSpy.count(), 2);
0105     QCOMPARE(removedSpy.count(), 0);
0106     args = addedSpy[1];
0107     QCOMPARE(args.count(), 1);
0108     entry = qvariant_cast<KonqHistoryEntry>(args[0]);
0109     QCOMPARE(entry.url.url(), url.url());
0110     QCOMPARE(entry.typedUrl, typedUrl);
0111     QCOMPARE(entry.title, title);   // now it's there
0112     QCOMPARE(int(entry.numberOfTimesVisited), 1);
0113 
0114     // Now clean it up
0115 
0116     mgr.emitRemoveFromHistory(url);
0117 
0118     waitForRemovedSignal(&mgr);
0119 
0120     QCOMPARE(removedSpy.count(), 1);
0121     QCOMPARE(addedSpy.count(), 2);   // unchanged
0122     args = removedSpy[0];
0123     QCOMPARE(args.count(), 1);
0124     entry = qvariant_cast<KonqHistoryEntry>(args[0]);
0125     QCOMPARE(entry.url.url(), url.url());
0126     QCOMPARE(entry.typedUrl, typedUrl);
0127     QCOMPARE(entry.title, title);
0128     QCOMPARE(int(entry.numberOfTimesVisited), 1);
0129 }
0130 
0131 #include "historymanagertest.moc"