Commit 371ea9b5 authored by Jonah Brüchert's avatar Jonah Brüchert 🌳 Committed by Rinigus Saar
Browse files

Some final cosmetic changes + add dbmanager test

parent 889cbc90
......@@ -4,10 +4,10 @@ find_package(Qt5 ${REQUIRED_QT_VERSION} CONFIG REQUIRED Test Sql Gui Quick)
include_directories(../src)
#ecm_add_test(urlmodeltest.cpp ../src/urlmodel.cpp
# TEST_NAME urlmodeltest
# LINK_LIBRARIES Qt5::Test
#)
ecm_add_test(dbmanagertest.cpp ../src/dbmanager.cpp ../src/iconimageprovider.cpp ../src/sqlquerymodel.cpp
TEST_NAME dbmanagertest
LINK_LIBRARIES Qt5::Test Qt5::Sql Qt5::Quick
)
ecm_add_test(useragenttest.cpp ../src/useragent.cpp
TEST_NAME useragenttest
......
/*
* Copyright 2020 Jonah Brüchert <jbb@kaidan.im>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License version 2 as published by the Free Software Foundation;
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <QtTest/QTest>
#include <QSignalSpy>
#include <QCoreApplication>
#include <QStandardPaths>
#include "dbmanager.h"
#include "sqlquerymodel.h"
class TabsModelTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase()
{
QCoreApplication::setOrganizationName("autotests");
QCoreApplication::setApplicationName("angelfish_dbmanagertest");
QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation));
dir.mkpath(".");
m_dbmanager = new DBManager();
}
void testAddBookmark()
{
QSignalSpy spy(m_dbmanager, &DBManager::databaseTableChanged);
m_dbmanager->addBookmark({{"url", "https://kde.org"}, {"title", "KDE"}, {"icon", "TESTDATA"}});
QCOMPARE(spy.count(), 1);
}
void testAddToHistory()
{
QSignalSpy spy(m_dbmanager, &DBManager::databaseTableChanged);
m_dbmanager->addToHistory({{"url", "https://kde.org"}, {"title", "KDE"}, {"icon", "TESTDATA"}});
QCOMPARE(spy.count(), 1);
}
void testLastVisited()
{
QSignalSpy spy(m_dbmanager, &DBManager::databaseTableChanged);
m_dbmanager->updateLastVisited("https://kde.org");
// Will be updated in both tables
QCOMPARE(spy.count(), 2);
}
void testRemoveBookmark()
{
QSignalSpy spy(m_dbmanager, &DBManager::databaseTableChanged);
m_dbmanager->removeBookmark("https://kde.org");
QCOMPARE(spy.count(), 1);
}
void testRemoveFromHistory()
{
QSignalSpy spy(m_dbmanager, &DBManager::databaseTableChanged);
m_dbmanager->removeBookmark("https://kde.org");
QCOMPARE(spy.count(), 1);
}
void testSqlQueryModelRoleNames()
{
auto model = new SqlQueryModel();
model->setQuery(QSqlQuery("SELECT * FROM history"));
QHash<int, QByteArray> expectedRoleNames = {
{ Qt::UserRole + 1, "url"},
{ Qt::UserRole + 2, "title"},
{ Qt::UserRole + 3, "icon"},
{ Qt::UserRole + 4, "lastVisited"}
};
QCOMPARE(model->roleNames(), expectedRoleNames);
}
private:
DBManager *m_dbmanager;
};
QTEST_MAIN(TabsModelTest);
#include "dbmanagertest.moc"
......@@ -27,7 +27,7 @@
#include <QDebug>
#include <QSqlError>
constexpr int QUERY_LIMIT=1000;
constexpr int QUERY_LIMIT = 1000;
BookmarksHistoryModel::BookmarksHistoryModel()
{
......
......@@ -28,7 +28,10 @@
BrowserManager *BrowserManager::s_instance = nullptr;
BrowserManager::BrowserManager(QObject *parent) : QObject(parent), m_dbmanager(new DBManager), m_settings(new QSettings(this))
BrowserManager::BrowserManager(QObject *parent)
: QObject(parent)
, m_dbmanager(new DBManager)
, m_settings(new QSettings(this))
{
connect(m_dbmanager, &DBManager::databaseTableChanged, this, &BrowserManager::databaseTableChanged);
}
......@@ -61,9 +64,9 @@ void BrowserManager::removeFromHistory(const QString &url)
m_dbmanager->removeFromHistory(url);
}
void BrowserManager::lastVisited(const QString &url)
void BrowserManager::updateLastVisited(const QString &url)
{
m_dbmanager->lastVisited(url);
m_dbmanager->updateLastVisited(url);
}
void BrowserManager::updateIcon(const QString &url, const QString &iconSource)
......
/***************************************************************************
/***************************************************************************
* *
* Copyright 2014 Sebastian Kügler <sebas@kde.org> *
* *
......@@ -73,7 +73,7 @@ public slots:
void addToHistory(const QVariantMap &pagedata);
void removeFromHistory(const QString &url);
void lastVisited(const QString &url);
void updateLastVisited(const QString &url);
void updateIcon(const QString &url, const QString &iconSource);
void setHomepage(const QString &homepage);
......
......@@ -173,7 +173,7 @@ WebEngineView {
request.icon = currentWebView.icon;
request.lastVisited = new Date();
BrowserManager.addToHistory(request);
BrowserManager.lastVisited(currentWebView.url);
BrowserManager.updateLastVisited(currentWebView.url);
}
grabThumb();
}
......
......@@ -192,7 +192,7 @@ void DBManager::updateIconRecord(const QString &table, const QString &url, const
emit databaseTableChanged(table);
}
void DBManager::lastVisitedRecord(const QString &table, const QString &url)
void DBManager::setLastVisitedRecord(const QString &table, const QString &url)
{
if (url.isEmpty()) return;
......@@ -226,10 +226,10 @@ void DBManager::removeFromHistory(const QString &url)
removeRecord(QStringLiteral("history"), url);
}
void DBManager::lastVisited(const QString &url)
void DBManager::updateLastVisited(const QString &url)
{
lastVisitedRecord(QStringLiteral("bookmarks"), url);
lastVisitedRecord(QStringLiteral("history"), url);
setLastVisitedRecord(QStringLiteral("bookmarks"), url);
setLastVisitedRecord(QStringLiteral("history"), url);
}
void DBManager::updateIcon(const QString &url, const QString &iconSource)
......
......@@ -49,7 +49,7 @@ public:
void removeFromHistory(const QString &url);
void updateIcon(const QString &url, const QString &iconSource);
void lastVisited(const QString &url);
void updateLastVisited(const QString &url);
private:
// version of database schema
......@@ -73,7 +73,7 @@ private:
void addRecord(const QString &table, const QVariantMap &pagedata);
void removeRecord(const QString &table, const QString &url);
void updateIconRecord(const QString &table, const QString &url, const QString &iconSource);
void lastVisitedRecord(const QString &table, const QString &url);
void setLastVisitedRecord(const QString &table, const QString &url);
};
#endif // DBMANAGER_H
......@@ -137,7 +137,7 @@ QImage IconImageProvider::requestImage(const QString &id, QSize *size, const QSi
qWarning() << Q_FUNC_INFO << "Failed to execute SQL statement";
qWarning() << query.lastQuery();
qWarning() << query.lastError();
return QImage();
return {};
}
if (query.next()) {
......@@ -150,5 +150,5 @@ QImage IconImageProvider::requestImage(const QString &id, QSize *size, const QSi
}
qWarning() << "Failed to find icon for" << id;
return QImage();
return {};
}
......@@ -55,7 +55,7 @@ QVariant SqlQueryModel::data(const QModelIndex &index, int role) const
QModelIndex modelIndex = this->index(index.row(), columnIdx);
return QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}
return QVariant();
return {};
}
QHash<int, QByteArray> SqlQueryModel::roleNames() const
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment