diff --git a/runners/bookmarks/CMakeLists.txt b/runners/bookmarks/CMakeLists.txt --- a/runners/bookmarks/CMakeLists.txt +++ b/runners/bookmarks/CMakeLists.txt @@ -32,8 +32,8 @@ ${krunner_bookmarks_common_SRCS} bookmarksrunner.cpp browserfactory.cpp - browsers/chromefindprofile.cpp browsers/chrome.cpp + browsers/chromeprofile.cpp browsers/firefox.cpp browsers/kdebrowser.cpp browsers/opera.cpp @@ -45,7 +45,7 @@ set(krunner_bookmarks_test_SRCS ${krunner_bookmarks_common_SRCS} - browsers/chromefindprofile.cpp + browsers/chromeprofile.cpp browsers/chrome.cpp ) diff --git a/runners/bookmarks/browserfactory.cpp b/runners/bookmarks/browserfactory.cpp --- a/runners/bookmarks/browserfactory.cpp +++ b/runners/bookmarks/browserfactory.cpp @@ -23,7 +23,6 @@ #include "browsers/kdebrowser.h" #include "browsers/firefox.h" #include "browsers/opera.h" -#include "browsers/chromefindprofile.h" #include "browsers/chrome.h" Browser *BrowserFactory::find(const QString& browserName, QObject* parent) @@ -38,9 +37,9 @@ } else if (browserName.contains(QLatin1String("opera"), Qt::CaseInsensitive)) { m_previousBrowser = new Opera(parent); } else if (browserName.contains(QLatin1String("chrome"), Qt::CaseInsensitive)) { - m_previousBrowser = new Chrome(new FindChromeProfile(QStringLiteral("google-chrome"), QDir::homePath(), parent), parent); + m_previousBrowser = new Chrome(QStringLiteral("google-chrome"), parent); } else if (browserName.contains(QLatin1String("chromium"), Qt::CaseInsensitive)) { - m_previousBrowser = new Chrome(new FindChromeProfile(QStringLiteral("chromium"), QDir::homePath(), parent), parent); + m_previousBrowser = new Chrome(QStringLiteral("chromium"), parent); } else { m_previousBrowser = new KDEBrowser(parent); } diff --git a/runners/bookmarks/browsers/chrome.h b/runners/bookmarks/browsers/chrome.h --- a/runners/bookmarks/browsers/chrome.h +++ b/runners/bookmarks/browsers/chrome.h @@ -23,20 +23,16 @@ #define CHROME_H #include "browser.h" -#include "findprofile.h" #include - #include -class QJsonObject; - class ProfileBookmarks; class Chrome : public QObject, public Browser { Q_OBJECT public: - explicit Chrome(FindProfile *findProfile, QObject* parent = nullptr); + explicit Chrome(const QString &appName, QObject *parent = nullptr); ~Chrome() override; QList match(const QString &term, bool addEveryThing) override; public Q_SLOTS: diff --git a/runners/bookmarks/browsers/chrome.cpp b/runners/bookmarks/browsers/chrome.cpp --- a/runners/bookmarks/browsers/chrome.cpp +++ b/runners/bookmarks/browsers/chrome.cpp @@ -21,49 +21,37 @@ #include "chrome.h" #include "faviconfromblob.h" -#include "browsers/findprofile.h" #include #include #include #include #include #include -#include "bookmarksrunner_defs.h" -#include +#include "chromeprofile.h" -class ProfileBookmarks { -public: - ProfileBookmarks(const Profile &profile) : m_profile(profile) {} - inline QJsonArray bookmarks() { return m_bookmarks; } - inline Profile profile() { return m_profile; } - void tearDown() { m_profile.favicon()->teardown(); clear(); } - void add(const QJsonObject &bookmarkEntry) { m_bookmarks << bookmarkEntry; } - void clear() { m_bookmarks = QJsonArray(); } -private: - Profile m_profile; - QJsonArray m_bookmarks; -}; -Chrome::Chrome( FindProfile* findProfile, QObject* parent ) +Chrome::Chrome(const QString &appName, QObject *parent) : QObject(parent), m_watcher(new KDirWatch(this)), m_dirty(false) { - const auto profiles = findProfile->find(); - for(const Profile &profile : profiles) { - updateCacheFile(profile.faviconSource(), profile.faviconCache()); - m_profileBookmarks << new ProfileBookmarks(profile); - m_watcher->addFile(profile.path()); + m_profileBookmarks = ProfileBookmarks::findAllProfiles(appName); + for (ProfileBookmarks *profile : qAsConst(m_profileBookmarks)) { + // Update cache file, then create favicon + if (updateCacheFile(profile->faviconSource(), profile->faviconCache()) != Error) { + profile->setFavicon(FaviconFromBlob::chrome(profile->name(), profile->faviconCache(), parent)); + } else { + profile->setFavicon(new FallbackFavicon(parent)); + } + m_watcher->addFile(profile->path()); } connect(m_watcher, &KDirWatch::created, [=] { m_dirty = true; }); } Chrome::~Chrome() { - for(ProfileBookmarks *profileBookmark : qAsConst(m_profileBookmarks)) { - delete profileBookmark; - } + qDeleteAll(m_profileBookmarks); } QList Chrome::match(const QString &term, bool addEveryThing) @@ -83,11 +71,10 @@ QList results; const auto bookmarks = profileBookmarks->bookmarks(); - Favicon *favicon = profileBookmarks->profile().favicon(); for (const QJsonValue &bookmarkValue : bookmarks) { const QJsonObject bookmark = bookmarkValue.toObject(); const QString url = bookmark.value(QStringLiteral("url")).toString(); - BookmarkMatch bookmarkMatch(favicon->iconFor(url), term, bookmark.value(QStringLiteral("name")).toString(), url); + BookmarkMatch bookmarkMatch(profileBookmarks->iconFor(url), term, bookmark.value(QStringLiteral("name")).toString(), url); bookmarkMatch.addTo(results, addEveryThing); } return results; @@ -97,9 +84,8 @@ { m_dirty = false; for(ProfileBookmarks *profileBookmarks : qAsConst(m_profileBookmarks)) { - Profile profile = profileBookmarks->profile(); profileBookmarks->clear(); - QFile bookmarksFile(profile.path()); + QFile bookmarksFile(profileBookmarks->path()); if (!bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) { continue; } @@ -115,8 +101,8 @@ for (const QJsonValue &folder : entries) { parseFolder(folder.toObject(), profileBookmarks); } - updateCacheFile(profile.faviconSource(), profile.faviconCache()); - profile.favicon()->prepare(); + updateCacheFile(profileBookmarks->faviconSource(), profileBookmarks->faviconCache()); + profileBookmarks->favicon()->prepare(); } } diff --git a/runners/bookmarks/browsers/chromefindprofile.h b/runners/bookmarks/browsers/chromefindprofile.h deleted file mode 100644 --- a/runners/bookmarks/browsers/chromefindprofile.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2007 Glenn Ergeerts - * Copyright 2012 Glenn Ergeerts - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Library General Public License as - * published by the Free Software Foundation; either version 2, or - * (at your option) any later version. - * - * This program 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 General Public License for more details - * - * You should have received a copy of the GNU Library General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - - -#ifndef CHROMEFINDPROFILE_H -#define CHROMEFINDPROFILE_H - -#include -#include -#include "browsers/findprofile.h" - -class FindChromeProfile : public QObject, public FindProfile -{ - -public: - explicit FindChromeProfile (const QString& applicationName, const QString &homeDirectory = QDir::homePath(), QObject* parent = nullptr ); - QList find() override; -private: - QString const m_applicationName; - QString const m_homeDirectory; -}; - -#endif // CHROMEFINDPROFILE_H diff --git a/runners/bookmarks/browsers/chromefindprofile.cpp b/runners/bookmarks/browsers/chromefindprofile.cpp deleted file mode 100644 --- a/runners/bookmarks/browsers/chromefindprofile.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2007 Glenn Ergeerts - * Copyright 2012 Glenn Ergeerts - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Library General Public License as - * published by the Free Software Foundation; either version 2, or - * (at your option) any later version. - * - * This program 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 General Public License for more details - * - * You should have received a copy of the GNU Library General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - - -#include "chromefindprofile.h" -#include -#include -#include -#include -#include -#include "bookmarksrunner_defs.h" -#include "faviconfromblob.h" -#include - -FindChromeProfile::FindChromeProfile (const QString &applicationName, const QString &homeDirectory, QObject* parent ) - : QObject(parent), m_applicationName(applicationName), m_homeDirectory(homeDirectory) -{ -} - -QList FindChromeProfile::find() -{ - QString configDirectory = QStringLiteral("%1/.config/%2") - .arg(m_homeDirectory, m_applicationName); - QString localStateFileName = QStringLiteral("%1/Local State") - .arg(configDirectory); - - QList profiles; - - QFile localStateFile(localStateFileName); - - if (!localStateFile.open(QIODevice::ReadOnly | QIODevice::Text)) { - return profiles; - } - QJsonDocument jdoc = QJsonDocument::fromJson(localStateFile.readAll()); - - if(jdoc.isNull()) { - qDebug() << "error opening " << QFileInfo(localStateFile).absoluteFilePath(); - return profiles; - } - - QVariantMap localState = jdoc.object().toVariantMap(); - QVariantMap profilesConfig = localState.value(QStringLiteral("profile")).toMap().value(QStringLiteral("info_cache")).toMap(); - - for(const QString &profile : profilesConfig.keys()) { - const QString profilePath = QStringLiteral("%1/%2").arg(configDirectory, profile); - const QString profileBookmarksPath = QStringLiteral("%1/%2").arg(profilePath, QStringLiteral("Bookmarks")); - profiles << Profile(profileBookmarksPath, profile, FaviconFromBlob::chrome(profilePath, this)); - } - - return profiles; -} diff --git a/runners/bookmarks/browsers/chromeprofile.h b/runners/bookmarks/browsers/chromeprofile.h new file mode 100644 --- /dev/null +++ b/runners/bookmarks/browsers/chromeprofile.h @@ -0,0 +1,34 @@ +#ifndef CHROMEPROFILE_H +#define CHROMEPROFILE_H + +#include +#include +#include +#include "favicon.h" + +class ProfileBookmarks { +private: + ProfileBookmarks(const QString &path, const QString &name); +public: + static QList findAllProfiles(const QString &applicationName); + inline QJsonArray bookmarks() { return m_bookmarks; } + inline void tearDown() { m_favicon->teardown(); clear(); } + inline void add(const QJsonObject &bookmarkEntry) { m_bookmarks << bookmarkEntry; } + inline void clear() { m_bookmarks = QJsonArray(); } + inline QString path() const { return m_path; } + inline QString name() const { return m_name; } + inline Favicon *favicon() const { return m_favicon; } + inline QIcon iconFor(const QString &url) { return m_favicon->iconFor(url); }; + inline void setFavicon(Favicon *favicon) { m_favicon = favicon; } + inline QString faviconSource() const { return m_faviconSource; } + inline QString faviconCache() const { return m_faviconCache; } +private: + QJsonArray m_bookmarks; + QString m_path; + QString m_name; + Favicon *m_favicon; + QString m_faviconSource; + QString m_faviconCache; +}; + +#endif //CHROMEPROFILE_H diff --git a/runners/bookmarks/browsers/chromeprofile.cpp b/runners/bookmarks/browsers/chromeprofile.cpp new file mode 100644 --- /dev/null +++ b/runners/bookmarks/browsers/chromeprofile.cpp @@ -0,0 +1,43 @@ +#include "chromeprofile.h" + +#include +#include +#include + +ProfileBookmarks::ProfileBookmarks(const QString &path, const QString &name) + : m_path(path), m_name(name), m_favicon(nullptr) +{ + // Remove "Bookmarks" from end of path + m_faviconSource = path.chopped(9) + QStringLiteral("Favicons"); + m_faviconCache = QStringLiteral("%1/KRunner-Chrome-Favicons-%2.sqlite") + .arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), name); +} + +QList ProfileBookmarks::findAllProfiles(const QString &applicationName) +{ + const QString configDirectory = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + '/' + applicationName; + const QString localStateFileName = QStringLiteral("%1/Local State").arg(configDirectory); + QList profiles; + QFile localStateFile(localStateFileName); + + if (!localStateFile.open(QIODevice::ReadOnly | QIODevice::Text)) { + return profiles; + } + const QJsonDocument jdoc = QJsonDocument::fromJson(localStateFile.readAll()); + if (jdoc.isNull()) { + return profiles; + } + + const QVariantMap localState = jdoc.object().toVariantMap(); + const auto profilesConfigMap = localState.value(QStringLiteral("profile")).toMap() + .value(QStringLiteral("info_cache")).toMap(); + QMap::const_iterator i = profilesConfigMap.constBegin(); + while (i != profilesConfigMap.constEnd()) { + const QString profile = i.key(); + const QString profilePath = configDirectory + '/' + profile; + const QString profileBookmarksPath = profilePath + '/' + QStringLiteral("Bookmarks"); + profiles << new ProfileBookmarks(profileBookmarksPath, profile); + ++i; + } + return profiles; +} diff --git a/runners/bookmarks/browsers/findprofile.h b/runners/bookmarks/browsers/findprofile.h deleted file mode 100644 --- a/runners/bookmarks/browsers/findprofile.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2007 Glenn Ergeerts - * Copyright 2012 Glenn Ergeerts - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Library General Public License as - * published by the Free Software Foundation; either version 2, or - * (at your option) any later version. - * - * This program 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 General Public License for more details - * - * You should have received a copy of the GNU Library General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -#ifndef FIND_PROFILE_H -#define FIND_PROFILE_H -#include -#include -#include - -class Favicon; -class Profile { -public: - Profile(const QString &path, const QString &name, Favicon *favicon) : m_path(path), m_name(name), m_favicon(favicon){ - // Remove "Bookmarks" from end of path - m_faviconSource = path.chopped(9) + QStringLiteral("Favicons"); - m_faviconCache = QStringLiteral("%1/KRunner-Chrome-Favicons-%2.sqlite") - .arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), name); - } - inline QString path() const { return m_path; } - inline QString name() const { return m_name; } - inline Favicon *favicon() const { return m_favicon; } - inline QString faviconSource() const { return m_faviconSource; } - inline QString faviconCache() const { return m_faviconCache; } -private: - QString m_path; - QString m_name; - Favicon *m_favicon; - QString m_faviconSource; - QString m_faviconCache; -}; - -class FindProfile { -public: - virtual QList find() = 0; - virtual ~FindProfile() {} -}; - -#endif diff --git a/runners/bookmarks/faviconfromblob.h b/runners/bookmarks/faviconfromblob.h --- a/runners/bookmarks/faviconfromblob.h +++ b/runners/bookmarks/faviconfromblob.h @@ -29,7 +29,7 @@ { Q_OBJECT public: - static FaviconFromBlob *chrome(const QString &profileDirectory, QObject *parent = nullptr); + static FaviconFromBlob *chrome(const QString &profileName, const QString &faviconCache, QObject *parent = nullptr); static FaviconFromBlob *firefox(FetchSqlite *fetchSqlite, QObject *parent = nullptr); ~FaviconFromBlob() override; QIcon iconFor(const QString &url) override; diff --git a/runners/bookmarks/faviconfromblob.cpp b/runners/bookmarks/faviconfromblob.cpp --- a/runners/bookmarks/faviconfromblob.cpp +++ b/runners/bookmarks/faviconfromblob.cpp @@ -35,11 +35,8 @@ #include #include -FaviconFromBlob *FaviconFromBlob::chrome(const QString &profileDirectory, QObject *parent) +FaviconFromBlob *FaviconFromBlob::chrome(const QString &profileName, const QString &faviconCache, QObject *parent) { - QString profileName = QFileInfo(profileDirectory).fileName(); - QString faviconCache = QStringLiteral("%1/KRunner-Chrome-Favicons-%2.sqlite") - .arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), profileName); FetchSqlite *fetchSqlite = new FetchSqlite(faviconCache, parent); QString faviconQuery; @@ -71,9 +68,9 @@ FaviconFromBlob::FaviconFromBlob(const QString &profileName, const QString &query, const QString &blobColumn, FetchSqlite *fetchSqlite, QObject *parent) : Favicon(parent), m_query(query), m_blobcolumn(blobColumn), m_fetchsqlite(fetchSqlite) { + // Directory where the icons from the DB blob are cached m_profileCacheDirectory = QStringLiteral("%1/KRunner-Favicons-%2") .arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), profileName); - //qDebug() << "got cache directory: " << m_profileCacheDirectory; cleanCacheDirectory(); QDir().mkpath(m_profileCacheDirectory); } @@ -109,12 +106,14 @@ QMap bindVariables; bindVariables.insert(QStringLiteral(":url"), url); QList faviconFound = m_fetchsqlite->query(m_query, bindVariables); - if(faviconFound.isEmpty()) return defaultIcon(); + if(faviconFound.isEmpty()) { + return defaultIcon(); + } - QByteArray iconData = faviconFound.first().value(m_blobcolumn).toByteArray(); - //qDebug() << "Favicon found: " << iconData.size() << " bytes"; - if(iconData.size() <=0) + const QByteArray iconData = faviconFound.first().value(m_blobcolumn).toByteArray(); + if(iconData.isEmpty()) { return defaultIcon(); + } iconFile.open(QFile::WriteOnly); iconFile.write(iconData); diff --git a/runners/bookmarks/fetchsqlite.h b/runners/bookmarks/fetchsqlite.h --- a/runners/bookmarks/fetchsqlite.h +++ b/runners/bookmarks/fetchsqlite.h @@ -39,7 +39,7 @@ ~FetchSqlite() override; void prepare(); void teardown(); - QList query(const QString &sql, QMap bindObjects); + QList query(const QString &sql, const QMap &bindObjects); QList query(const QString &sql); QStringList tables(QSql::TableType type = QSql::Tables); diff --git a/runners/bookmarks/fetchsqlite.cpp b/runners/bookmarks/fetchsqlite.cpp --- a/runners/bookmarks/fetchsqlite.cpp +++ b/runners/bookmarks/fetchsqlite.cpp @@ -80,27 +80,26 @@ return db; } -QList FetchSqlite::query(const QString &sql, QMap bindObjects) +QList FetchSqlite::query(const QString &sql, const QMap &bindObjects) { QMutexLocker lock(&m_mutex); auto db = openDbConnection(m_databaseFile); if (!db.isValid()) { + qCWarning(RUNNER_BOOKMARKS()) << "Invalid Database"; return QList(); } - //qDebug() << "query: " << sql; QSqlQuery query(db); query.prepare(sql); for (auto entry = bindObjects.constKeyValueBegin(); entry != bindObjects.constKeyValueEnd(); ++entry) { query.bindValue((*entry).first, (*entry).second); - //qDebug() << "* Bound " << variableName << " to " << query.boundValue(variableName); } if(!query.exec()) { - QSqlError error = db.lastError(); - //qDebug() << "query failed: " << error.text() << " (" << error.type() << ", " << error.number() << ")"; - //qDebug() << query.lastQuery(); + qCWarning(RUNNER_BOOKMARKS()) << "Error when executing statement" << query.lastQuery() + << bindObjects << query.lastError(); + return QList(); } QList result; diff --git a/runners/bookmarks/tests/CMakeLists.txt b/runners/bookmarks/tests/CMakeLists.txt --- a/runners/bookmarks/tests/CMakeLists.txt +++ b/runners/bookmarks/tests/CMakeLists.txt @@ -4,5 +4,4 @@ ecm_add_test(testchromebookmarks.cpp TEST_NAME testChromeBookmarks LINK_LIBRARIES Qt5::Test krunner_bookmarks_test ) - -file(COPY chrome-config-home DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) +add_custom_target(copy-test-files ALL COMMAND mkdir -p ~/.qttest/config/ && cp -r ${CMAKE_CURRENT_SOURCE_DIR}/chromium ~/.qttest/config/) diff --git a/runners/bookmarks/tests/chrome-config-home/Chrome-Bookmarks-Sample.json b/runners/bookmarks/tests/chromium/Default/Bookmarks rename from runners/bookmarks/tests/chrome-config-home/Chrome-Bookmarks-Sample.json rename to runners/bookmarks/tests/chromium/Default/Bookmarks diff --git a/runners/bookmarks/tests/chrome-config-home/.config/chromium/Local State b/runners/bookmarks/tests/chromium/Local State rename from runners/bookmarks/tests/chrome-config-home/.config/chromium/Local State rename to runners/bookmarks/tests/chromium/Local State diff --git a/runners/bookmarks/tests/chrome-config-home/Chrome-Bookmarks-SecondProfile.json b/runners/bookmarks/tests/chromium/Profile 1/Bookmarks rename from runners/bookmarks/tests/chrome-config-home/Chrome-Bookmarks-SecondProfile.json rename to runners/bookmarks/tests/chromium/Profile 1/Bookmarks diff --git a/runners/bookmarks/tests/testchromebookmarks.h b/runners/bookmarks/tests/testchromebookmarks.h --- a/runners/bookmarks/tests/testchromebookmarks.h +++ b/runners/bookmarks/tests/testchromebookmarks.h @@ -23,15 +23,6 @@ #define TESTCHROMEBOOKMARKS_H #include -#include "browsers/findprofile.h" - -class FakeFindProfile : public FindProfile { -public: - FakeFindProfile(const QList &profiles) : m_profiles(profiles) {} - QList find() override { return m_profiles; } -private: - QList m_profiles; -}; class TestChromeBookmarks : public QObject { @@ -41,16 +32,11 @@ private Q_SLOTS: void initTestCase(); void bookmarkFinderShouldFindEachProfileDirectory(); - void bookmarkFinderShouldReportNoProfilesOnErrors(); void itShouldFindNothingWhenPrepareIsNotCalled(); - void itShouldGracefullyExitWhenFileIsNotFound(); void itShouldFindAllBookmarks(); void itShouldFindOnlyMatches(); void itShouldClearResultAfterCallingTeardown(); - void itShouldFindBookmarksFromAllProfiles(); - -private: - QScopedPointer m_findBookmarksInCurrentDirectory; + void itShouldDisplayFallbackIcon(); }; diff --git a/runners/bookmarks/tests/testchromebookmarks.cpp b/runners/bookmarks/tests/testchromebookmarks.cpp --- a/runners/bookmarks/tests/testchromebookmarks.cpp +++ b/runners/bookmarks/tests/testchromebookmarks.cpp @@ -22,51 +22,31 @@ #include #include #include "browsers/chrome.h" -#include "browsers/chromefindprofile.h" +#include "browsers/chromeprofile.h" #include "favicon.h" using namespace Plasma; void TestChromeBookmarks::initTestCase() { - m_findBookmarksInCurrentDirectory.reset(new FakeFindProfile(QList({Profile("chrome-config-home/Chrome-Bookmarks-Sample.json", "Sample", new FallbackFavicon())}))); + QStandardPaths::setTestModeEnabled(true); } void TestChromeBookmarks::bookmarkFinderShouldFindEachProfileDirectory() { - FindChromeProfile findChrome("chromium", "./chrome-config-home"); - QString profileTemplate = QString("./chrome-config-home/.config/%1/%2/Bookmarks"); - - QList profiles = findChrome.find(); + QString profileTemplate = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QString("/%2/%3/Bookmarks"); + QList profiles = ProfileBookmarks::findAllProfiles("chromium"); QCOMPARE(profiles.size(), 2); - QCOMPARE(profiles[0].path(), profileTemplate.arg("chromium").arg("Default")); - QCOMPARE(profiles[1].path(), profileTemplate.arg("chromium").arg("Profile 1")); -} - -void TestChromeBookmarks::bookmarkFinderShouldReportNoProfilesOnErrors() -{ - FindChromeProfile findChrome("chromium", "./no-config-directory"); - - QList profiles = findChrome.find(); - QCOMPARE(profiles.size(), 0); + QCOMPARE(profiles[0]->path(), profileTemplate.arg("chromium", "Default")); + QCOMPARE(profiles[1]->path(), profileTemplate.arg("chromium", "Profile 1")); } - void TestChromeBookmarks::itShouldFindNothingWhenPrepareIsNotCalled() { - Chrome *chrome = new Chrome(m_findBookmarksInCurrentDirectory.data(), this); - QCOMPARE(chrome->match("any", true).size(), 0); -} - -void TestChromeBookmarks::itShouldGracefullyExitWhenFileIsNotFound() -{ - FakeFindProfile finder(QList() << Profile("FileNotExisting.json", QString(), nullptr)); - Chrome *chrome = new Chrome(&finder, this); - chrome->prepare(); - QCOMPARE(chrome->match("any", true).size(), 0); + Chrome chrome("chromium"); + QCOMPARE(chrome.match("any", true).size(), 0); } - void verifyMatch(BookmarkMatch &match, const QString &title, const QString &url, qreal relevance, QueryMatch::Type type) { QueryMatch queryMatch = match.asQueryMatch(nullptr); QCOMPARE(queryMatch.text(), title); @@ -78,48 +58,44 @@ void TestChromeBookmarks::itShouldFindAllBookmarks() { - Chrome *chrome = new Chrome(m_findBookmarksInCurrentDirectory.data(), this); - chrome->prepare(); - QList matches = chrome->match("any", true); - QCOMPARE(matches.size(), 3); + Chrome chrome("chromium"); + chrome.prepare(); + QList matches = chrome.match("any", true); + // The results from both profiles are displayed + QCOMPARE(matches.size(), 4); verifyMatch(matches[0], "some bookmark in bookmark bar", "https://somehost.com/", 0.18, QueryMatch::PossibleMatch); verifyMatch(matches[1], "bookmark in other bookmarks", "https://otherbookmarks.com/", 0.18, QueryMatch::PossibleMatch); verifyMatch(matches[2], "bookmark in somefolder", "https://somefolder.com/", 0.18, QueryMatch::PossibleMatch); + verifyMatch(matches[3], "bookmark in secondProfile", "https://secondprofile.com/", 0.18, QueryMatch::PossibleMatch); } void TestChromeBookmarks::itShouldFindOnlyMatches() { - Chrome *chrome = new Chrome(m_findBookmarksInCurrentDirectory.data(), this); - chrome->prepare(); - QList matches = chrome->match("other", false); + Chrome chrome("chromium"); + chrome.prepare(); + QList matches = chrome.match("other", false); QCOMPARE(matches.size(), 1); verifyMatch(matches[0], "bookmark in other bookmarks", "https://otherbookmarks.com/", 0.45, QueryMatch::PossibleMatch); } void TestChromeBookmarks::itShouldClearResultAfterCallingTeardown() { - Chrome *chrome = new Chrome(m_findBookmarksInCurrentDirectory.data(), this); - chrome->prepare(); - QCOMPARE(chrome->match("any", true).size(), 3); - chrome->teardown(); - QCOMPARE(chrome->match("any", true).size(), 0); + Chrome chrome("chromium"); + chrome.prepare(); + QCOMPARE(chrome.match("any", true).size(), 4); + chrome.teardown(); + QCOMPARE(chrome.match("any", true).size(), 0); } -void TestChromeBookmarks::itShouldFindBookmarksFromAllProfiles() +void TestChromeBookmarks::itShouldDisplayFallbackIcon() { - FakeFindProfile findBookmarksFromAllProfiles(QList() - << Profile("chrome-config-home/Chrome-Bookmarks-Sample.json", "Sample", new FallbackFavicon(this)) - << Profile("chrome-config-home/Chrome-Bookmarks-SecondProfile.json", "SecondProfile", new FallbackFavicon(this)) ); - Chrome *chrome = new Chrome(&findBookmarksFromAllProfiles, this); - chrome->prepare(); - QList matches = chrome->match("any", true); - QCOMPARE(matches.size(), 4); - verifyMatch(matches[0], "some bookmark in bookmark bar", "https://somehost.com/", 0.18, QueryMatch::PossibleMatch); - verifyMatch(matches[1], "bookmark in other bookmarks", "https://otherbookmarks.com/", 0.18, QueryMatch::PossibleMatch); - verifyMatch(matches[2], "bookmark in somefolder", "https://somefolder.com/", 0.18, QueryMatch::PossibleMatch); - verifyMatch(matches[3], "bookmark in secondProfile", "https://secondprofile.com/", 0.18, QueryMatch::PossibleMatch); + Chrome chrome("chromium"); + chrome.prepare(); + QList matches = chrome.match("any", true); + for (auto &match : matches) { + QCOMPARE(match.asQueryMatch(nullptr).icon().name(), "bookmarks"); + } } - QTEST_MAIN(TestChromeBookmarks);