File indexing completed on 2024-06-16 05:10:59

0001 /*
0002     SPDX-FileCopyrightText: 2007 Glenn Ergeerts <glenn.ergeerts@telenet.be>
0003     SPDX-FileCopyrightText: 2012 Marco Gulino <marco.gulino@xpeppers.com>
0004     SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "testchromebookmarks.h"
0010 #include "browsers/chrome.h"
0011 #include "browsers/chromefindprofile.h"
0012 #include "favicon.h"
0013 #include <QTest>
0014 
0015 using namespace KRunner;
0016 
0017 void TestChromeBookmarks::initTestCase()
0018 {
0019     m_configHome = QFINDTESTDATA("chrome-config-home");
0020     m_findBookmarksInCurrentDirectory.reset(
0021         new FakeFindProfile(QList<Profile>({Profile(m_configHome + "/Chrome-Bookmarks-Sample.json", "Sample", new FallbackFavicon())})));
0022 }
0023 
0024 void TestChromeBookmarks::bookmarkFinderShouldFindEachProfileDirectory()
0025 {
0026     FindChromeProfile findChrome("chromium", m_configHome);
0027     QString profileTemplate = m_configHome + "/.config/%1/%2/Bookmarks";
0028 
0029     QList<Profile> profiles = findChrome.find();
0030     QCOMPARE(profiles.size(), 2);
0031     QCOMPARE(profiles[0].path(), profileTemplate.arg("chromium", "Default"));
0032     QCOMPARE(profiles[1].path(), profileTemplate.arg("chromium", "Profile 1"));
0033 }
0034 
0035 void TestChromeBookmarks::bookmarkFinderShouldReportNoProfilesOnErrors()
0036 {
0037     FindChromeProfile findChrome("chromium", "./no-config-directory");
0038     QList<Profile> profiles = findChrome.find();
0039     QCOMPARE(profiles.size(), 0);
0040 }
0041 
0042 void TestChromeBookmarks::itShouldFindNothingWhenPrepareIsNotCalled()
0043 {
0044     Chrome *chrome = new Chrome(m_findBookmarksInCurrentDirectory.get(), this);
0045     QCOMPARE(chrome->match("any", true).size(), 0);
0046 }
0047 
0048 void TestChromeBookmarks::itShouldGracefullyExitWhenFileIsNotFound()
0049 {
0050     FakeFindProfile finder(QList<Profile>() << Profile("FileNotExisting.json", QString(), nullptr));
0051     Chrome *chrome = new Chrome(&finder, this);
0052     chrome->prepare();
0053     QCOMPARE(chrome->match("any", true).size(), 0);
0054 }
0055 
0056 void verifyMatch(BookmarkMatch &match, const QString &title, const QString &url)
0057 {
0058     QCOMPARE(match.bookmarkTitle(), title);
0059     QCOMPARE(match.bookmarkUrl(), url);
0060 }
0061 
0062 void TestChromeBookmarks::itShouldFindAllBookmarks()
0063 {
0064     Chrome *chrome = new Chrome(m_findBookmarksInCurrentDirectory.get(), this);
0065     chrome->prepare();
0066     QList<BookmarkMatch> matches = chrome->match("any", true);
0067     QCOMPARE(matches.size(), 3);
0068     verifyMatch(matches[0], "some bookmark in bookmark bar", "https://somehost.com/");
0069     verifyMatch(matches[1], "bookmark in other bookmarks", "https://otherbookmarks.com/");
0070     verifyMatch(matches[2], "bookmark in somefolder", "https://somefolder.com/");
0071 }
0072 
0073 void TestChromeBookmarks::itShouldFindOnlyMatches()
0074 {
0075     Chrome *chrome = new Chrome(m_findBookmarksInCurrentDirectory.get(), this);
0076     chrome->prepare();
0077     QList<BookmarkMatch> matches = chrome->match("other", false);
0078     QCOMPARE(matches.size(), 1);
0079     verifyMatch(matches[0], "bookmark in other bookmarks", "https://otherbookmarks.com/");
0080 }
0081 
0082 void TestChromeBookmarks::itShouldClearResultAfterCallingTeardown()
0083 {
0084     Chrome *chrome = new Chrome(m_findBookmarksInCurrentDirectory.get(), this);
0085     chrome->prepare();
0086     QCOMPARE(chrome->match("any", true).size(), 3);
0087     chrome->teardown();
0088     QCOMPARE(chrome->match("any", true).size(), 0);
0089 }
0090 
0091 void TestChromeBookmarks::itShouldFindBookmarksFromAllProfiles()
0092 {
0093     FakeFindProfile findBookmarksFromAllProfiles(
0094         QList<Profile>{Profile(m_configHome + "/Chrome-Bookmarks-Sample.json", "Sample", new FallbackFavicon(this)),
0095                        Profile(m_configHome + "/Chrome-Bookmarks-SecondProfile.json", "SecondProfile", new FallbackFavicon(this))});
0096     Chrome *chrome = new Chrome(&findBookmarksFromAllProfiles, this);
0097     chrome->prepare();
0098     QList<BookmarkMatch> matches = chrome->match("any", true);
0099     QCOMPARE(matches.size(), 4);
0100     verifyMatch(matches[0], "some bookmark in bookmark bar", "https://somehost.com/");
0101     verifyMatch(matches[1], "bookmark in other bookmarks", "https://otherbookmarks.com/");
0102     verifyMatch(matches[2], "bookmark in somefolder", "https://somefolder.com/");
0103     verifyMatch(matches[3], "bookmark in secondProfile", "https://secondprofile.com/");
0104 }
0105 
0106 QTEST_MAIN(TestChromeBookmarks);