File indexing completed on 2024-04-28 04:55:54

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "qmlhistoryapitest.h"
0019 #include "autotests.h"
0020 #include "mainapplication.h"
0021 #include "history.h"
0022 #include "qml/api/history/qmlhistoryitem.h"
0023 #include "qml/api/history/qmlhistory.h"
0024 
0025 Q_DECLARE_METATYPE(HistoryEntry)
0026 
0027 void QmlHistoryApiTest::initTestCase()
0028 {
0029 }
0030 
0031 void QmlHistoryApiTest::cleanupTestCase()
0032 {
0033 }
0034 
0035 void QmlHistoryApiTest::testAddition()
0036 {
0037     qRegisterMetaType<HistoryEntry>();
0038     QSignalSpy historySpy(mApp->history(), &History::historyEntryAdded);
0039     m_testHelper.evaluate(QL1S("Falkon.History.addUrl({"
0040                      "    url: 'https://example.com',"
0041                      "    title: 'Example Domain'"
0042                      "})"));
0043     QTRY_COMPARE(historySpy.count(), 1);
0044     HistoryEntry entry = qvariant_cast<HistoryEntry>(historySpy.at(0).at(0));
0045     QCOMPARE(entry.title, QSL("Example Domain"));
0046 
0047     auto object = m_testHelper.evaluateQObject(QSL("Falkon.History"));
0048     QSignalSpy qmlHistorySpy(object, SIGNAL(visited(QmlHistoryItem*)));
0049     mApp->history()->addHistoryEntry(QUrl(QSL("https://sample.com")), QSL("Sample Domain"));
0050     QTRY_COMPARE(qmlHistorySpy.count(), 1);
0051     mApp->history()->clearHistory();
0052 }
0053 
0054 void QmlHistoryApiTest::testSearch()
0055 {
0056     QSignalSpy historySpy(mApp->history(), &History::historyEntryAdded);
0057     mApp->history()->addHistoryEntry(QUrl(QSL("https://example.com")), QSL("Example Domain"));
0058     mApp->history()->addHistoryEntry(QUrl(QSL("https://another-example.com")), QSL("Another Example Domain"));
0059     mApp->history()->addHistoryEntry(QUrl(QSL("https://sample.com")), QSL("Sample Domain"));
0060     QTRY_COMPARE(historySpy.count(), 3);
0061     auto list = m_testHelper.evaluate(QSL("Falkon.History.search('example')")).toVariant().toList();
0062     QCOMPARE(list.length(), 2);
0063 }
0064 
0065 void QmlHistoryApiTest::testVisits()
0066 {
0067     int visits = m_testHelper.evaluate(QSL("Falkon.History.getVisits('https://sample.com')")).toInt();
0068     QCOMPARE(visits, 1);
0069     QSignalSpy historySpy(mApp->history(), &History::historyEntryEdited);
0070     mApp->history()->addHistoryEntry(QUrl(QSL("https://sample.com")), QSL("Sample Domain"));
0071     QTRY_COMPARE(historySpy.count(), 1);
0072     visits = m_testHelper.evaluate(QSL("Falkon.History.getVisits('https://sample.com')")).toInt();
0073     QCOMPARE(visits, 2);
0074 }
0075 
0076 void QmlHistoryApiTest::testRemoval()
0077 {
0078     QSignalSpy historySpy(mApp->history(), &History::historyEntryDeleted);
0079     m_testHelper.evaluate(QSL("Falkon.History.deleteUrl('https://sample.com')"));
0080     QTRY_COMPARE(historySpy.count(), 1);
0081 
0082     auto object = m_testHelper.evaluateQObject(QSL("Falkon.History"));
0083     QSignalSpy qmlHistorySpy(object, SIGNAL(visitRemoved(QmlHistoryItem*)));
0084     mApp->history()->deleteHistoryEntry(QSL("https://example.com"), QSL("Example Domain"));
0085     QTRY_COMPARE(qmlHistorySpy.count(), 1);
0086 }
0087 
0088 FALKONTEST_MAIN(QmlHistoryApiTest)