Commit 39d1d7af authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

bookmarksrunner: Add test for BookmarkMatch class

This tests the relevance and conversion logic for this class.
parent aa5d6299
......@@ -8,4 +8,8 @@ file(COPY chrome/chrome-config-home DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/chro
ecm_add_test(firefox/testfirefoxbookmarks.cpp TEST_NAME testFirefoxBookmarks
LINK_LIBRARIES Qt::Test krunner_bookmarks_common
)
\ No newline at end of file
)
ecm_add_test(bookmarksmatchtest.cpp TEST_NAME testBookmarksMatch
LINK_LIBRARIES Qt::Test krunner_bookmarks_common
)
#include <QObject>
#include <QStandardPaths>
#include <QTest>
#include "browsers/firefox.h"
using namespace Plasma;
class TestBookmarksMatch : public QObject
{
Q_OBJECT
public:
explicit TestBookmarksMatch(QObject *parent = nullptr)
: QObject(parent)
{
}
private:
Firefox *m_firefox;
private Q_SLOTS:
void testQueryMatchConversion();
void testQueryMatchConversion_data();
void testAddToList();
};
void TestBookmarksMatch::testQueryMatchConversion()
{
QFETCH(QString, searchTerm);
QFETCH(QString, bookmarkTitle);
QFETCH(QString, bookmarkUrl);
QFETCH(QString, bookmarkDescription);
QFETCH(int, expectedMatchType);
QFETCH(qreal, expectedRelevance);
BookmarkMatch bookmarkMatch(QIcon::fromTheme("unknown"), searchTerm, bookmarkTitle, bookmarkUrl, bookmarkDescription);
QueryMatch match = bookmarkMatch.asQueryMatch(nullptr);
QCOMPARE(match.text(), bookmarkTitle);
QCOMPARE(match.data().toString(), bookmarkUrl);
QCOMPARE(match.type(), expectedMatchType);
QCOMPARE(match.relevance(), expectedRelevance);
}
void TestBookmarksMatch::testQueryMatchConversion_data()
{
QTest::addColumn<QString>("searchTerm");
QTest::addColumn<QString>("bookmarkTitle");
QTest::addColumn<QString>("bookmarkUrl");
QTest::addColumn<QString>("bookmarkDescription");
QTest::addColumn<int>("expectedMatchType");
QTest::addColumn<qreal>("expectedRelevance");
// clang-format off
QTest::newRow("no text match") << "krunner" << "KDE Community" << "https://somehost.com/" << "" << (int)QueryMatch::PossibleMatch << 0.18;
QTest::newRow("title partly matches") << "kde" << "KDE Community" << "https://somehost.com/" << "" << (int)QueryMatch::PossibleMatch << 0.45;
QTest::newRow("title exactly matches") << "kde community" << "KDE Community" << "https://somehost.com/" << "" << (int)QueryMatch::ExactMatch << 1.0;
QTest::newRow("url partly matches") << "somehost" << "KDE Community" << "https://somehost.com/" << "" << (int)QueryMatch::PossibleMatch << 0.2;
QTest::newRow("url exactly matches") << "https://somehost.com/" << "KDE Community" << "https://somehost.com/" << "" << (int)QueryMatch::PossibleMatch << 0.2;
QTest::newRow("description exactly matches") << "test" << "KDE Community" << "https://somehost.com/" << "test" << (int)QueryMatch::ExactMatch << 1.0;
QTest::newRow("description partly matches") << "test" << "KDE Community" << "https://somehost.com/" << "testme" << (int)QueryMatch::PossibleMatch << 0.3;
// clang-format on
}
void TestBookmarksMatch::testAddToList()
{
BookmarkMatch noMatch(QIcon(), "krunner", "KDE Community", "https://somehost.com/");
BookmarkMatch match(QIcon(), "kde", "KDE Community", "https://somehost.com/");
QList<BookmarkMatch> onlyMatching;
noMatch.addTo(onlyMatching, false);
match.addTo(onlyMatching, false);
QCOMPARE(onlyMatching.count(), 1);
QList<BookmarkMatch> allMatches;
noMatch.addTo(allMatches, true);
match.addTo(allMatches, true);
QCOMPARE(allMatches.count(), 2);
}
QTEST_MAIN(TestBookmarksMatch)
#include "bookmarksmatchtest.moc"
\ No newline at end of file
......@@ -4,11 +4,11 @@
#include "browsers/firefox.h"
class TestFirefoxBookmarks : public QObject
class TestBookmarksMatch : public QObject
{
Q_OBJECT
public:
explicit TestFirefoxBookmarks(QObject *parent = nullptr)
explicit TestBookmarksMatch(QObject *parent = nullptr)
: QObject(parent)
{
}
......@@ -23,13 +23,13 @@ private Q_SLOTS:
void testBookmarksQuery_data();
};
void TestFirefoxBookmarks::initTestCase()
void TestBookmarksMatch::initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
m_firefox = new Firefox(this, QFINDTESTDATA("firefox-config-home"));
}
void TestFirefoxBookmarks::testAllBookmarks()
void TestBookmarksMatch::testAllBookmarks()
{
m_firefox->prepare();
const QList<BookmarkMatch> matches = m_firefox->match(QStringLiteral("kde"), true);
......@@ -37,7 +37,7 @@ void TestFirefoxBookmarks::testAllBookmarks()
m_firefox->teardown();
}
void TestFirefoxBookmarks::testBookmarksQuery_data()
void TestBookmarksMatch::testBookmarksQuery_data()
{
QTest::addColumn<QString>("query");
QTest::addColumn<QStringList>("expectedUrls");
......@@ -49,7 +49,7 @@ void TestFirefoxBookmarks::testBookmarksQuery_data()
QTest::newRow("query that matches multiple titles") << "ubuntu" << QStringList{"http://www.ubuntu.com/", "http://wiki.ubuntu.com/"};
}
void TestFirefoxBookmarks::testBookmarksQuery()
void TestBookmarksMatch::testBookmarksQuery()
{
QFETCH(QString, query);
QFETCH(QStringList, expectedUrls);
......@@ -63,6 +63,6 @@ void TestFirefoxBookmarks::testBookmarksQuery()
m_firefox->teardown();
}
QTEST_MAIN(TestFirefoxBookmarks)
QTEST_MAIN(TestBookmarksMatch)
#include "testfirefoxbookmarks.moc"
Supports Markdown
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