File indexing completed on 2024-09-01 10:18:47
0001 /* 0002 SPDX-FileCopyrightText: 2019 David Hallas <david@davidhallas.dk> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include <kbookmarkmanager.h> 0008 #include <kbookmarkmenu.h> 0009 #include <kbookmarkowner.h> 0010 #if KBOOKMARKS_BUILD_DEPRECATED_SINCE(5, 69) 0011 #include <KActionCollection> 0012 #endif 0013 0014 #include <QMenu> 0015 #include <QStandardPaths> 0016 #include <QStringLiteral> 0017 #include <QTest> 0018 0019 class TestKBookmarkOwner; 0020 0021 class KBookmarkMenuTest : public QObject 0022 { 0023 Q_OBJECT 0024 private Q_SLOTS: 0025 void tabsOpen_data(); 0026 void tabsOpen(); 0027 void tabsOpenChanges(); 0028 #if KBOOKMARKS_BUILD_DEPRECATED_SINCE(5, 69) 0029 void tabsOpenActionCollection_data(); 0030 void tabsOpenActionCollection(); 0031 void tabsOpenChangesActionCollection(); 0032 #endif 0033 }; 0034 0035 static bool hasBookmarkEditorInstalled() 0036 { 0037 static bool isInstalled = !QStandardPaths::findExecutable(QStringLiteral("keditbookmarks")).isEmpty(); 0038 return isInstalled; 0039 } 0040 0041 static int actionCountWithoutBookmarkTabsAsFolder() 0042 { 0043 return hasBookmarkEditorInstalled() ? 3 : 2; 0044 } 0045 0046 static int actionCountWithBookmarkTabsAsFolder() 0047 { 0048 return hasBookmarkEditorInstalled() ? 4 : 3; 0049 } 0050 0051 class TestKBookmarkOwner final : public KBookmarkOwner 0052 { 0053 public: 0054 explicit TestKBookmarkOwner(bool supportsTabs_); 0055 void openBookmark(const KBookmark &bm, Qt::MouseButtons mb, Qt::KeyboardModifiers km) override; 0056 bool supportsTabs() const override; 0057 0058 private: 0059 const bool m_supportsTabs; 0060 }; 0061 0062 TestKBookmarkOwner::TestKBookmarkOwner(bool supportsTabs_) 0063 : m_supportsTabs(supportsTabs_) 0064 { 0065 } 0066 0067 void TestKBookmarkOwner::openBookmark(const KBookmark &, Qt::MouseButtons, Qt::KeyboardModifiers) 0068 { 0069 } 0070 0071 bool TestKBookmarkOwner::supportsTabs() const 0072 { 0073 return m_supportsTabs; 0074 } 0075 0076 #define VERIFY_MENU_WITHOUT_BOOKMARK_TABS_AS_FOLDER() \ 0077 do { \ 0078 QCOMPARE(menu->actions().count(), actionCountWithoutBookmarkTabsAsFolder()); \ 0079 QCOMPARE(menu->actions().at(0)->text(), QStringLiteral("&Add Bookmark")); \ 0080 QCOMPARE(menu->actions().at(1)->text(), QStringLiteral("New Bookmark Folder...")); \ 0081 if (hasBookmarkEditorInstalled()) { \ 0082 QCOMPARE(menu->actions().at(2)->text(), QStringLiteral("&Edit Bookmarks...")); \ 0083 } \ 0084 } while (false) 0085 0086 #define VERIFY_MENU_WITH_BOOKMARK_TABS_AS_FOLDER() \ 0087 do { \ 0088 QCOMPARE(menu->actions().count(), actionCountWithBookmarkTabsAsFolder()); \ 0089 QCOMPARE(menu->actions().at(0)->text(), QStringLiteral("&Add Bookmark")); \ 0090 QCOMPARE(menu->actions().at(1)->text(), QStringLiteral("Bookmark Tabs as Folder...")); \ 0091 QCOMPARE(menu->actions().at(2)->text(), QStringLiteral("New Bookmark Folder...")); \ 0092 if (hasBookmarkEditorInstalled()) { \ 0093 QCOMPARE(menu->actions().at(3)->text(), QStringLiteral("&Edit Bookmarks...")); \ 0094 } \ 0095 } while (false) 0096 0097 void KBookmarkMenuTest::tabsOpen_data() 0098 { 0099 QTest::addColumn<bool>("supportsTabs"); 0100 QTest::addColumn<int>("numberOfOpenTabs"); 0101 0102 const bool TabsNotSupported(false); 0103 QTest::newRow("Tabs not supported - 0 tabs open") << TabsNotSupported << 0; 0104 QTest::newRow("Tabs not supported - 1 tabs open") << TabsNotSupported << 1; 0105 QTest::newRow("Tabs not supported - 2 tabs open") << TabsNotSupported << 2; 0106 const bool TabsSupported(true); 0107 QTest::newRow("Tabs supported - 0 tabs open") << TabsSupported << 0; 0108 QTest::newRow("Tabs supported - 1 tabs open") << TabsSupported << 1; 0109 QTest::newRow("Tabs supported - 2 tabs open") << TabsSupported << 2; 0110 } 0111 0112 void KBookmarkMenuTest::tabsOpen() 0113 { 0114 QFETCH(bool, supportsTabs); 0115 QFETCH(int, numberOfOpenTabs); 0116 auto manager = KBookmarkManager::createTempManager(); 0117 std::unique_ptr<TestKBookmarkOwner> bookmarkOwner(new TestKBookmarkOwner(supportsTabs)); 0118 QMenu *menu = new QMenu; 0119 std::unique_ptr<KBookmarkMenu> testObject(new KBookmarkMenu(manager, bookmarkOwner.get(), menu)); 0120 testObject->setNumberOfOpenTabs(numberOfOpenTabs); 0121 testObject->ensureUpToDate(); 0122 if (supportsTabs && numberOfOpenTabs > 1) { 0123 VERIFY_MENU_WITH_BOOKMARK_TABS_AS_FOLDER(); 0124 } else { 0125 VERIFY_MENU_WITHOUT_BOOKMARK_TABS_AS_FOLDER(); 0126 } 0127 } 0128 0129 void KBookmarkMenuTest::tabsOpenChanges() 0130 { 0131 auto manager = KBookmarkManager::createTempManager(); 0132 std::unique_ptr<TestKBookmarkOwner> bookmarkOwner(new TestKBookmarkOwner(true)); 0133 QMenu *menu = new QMenu; 0134 std::unique_ptr<KBookmarkMenu> testObject(new KBookmarkMenu(manager, bookmarkOwner.get(), menu)); 0135 testObject->ensureUpToDate(); 0136 // If the number of open tabs has not been set it will default to 2 0137 const int DefaultNumberOfOpenTabs = 2; 0138 QCOMPARE(testObject->numberOfOpenTabs(), DefaultNumberOfOpenTabs); 0139 testObject->ensureUpToDate(); 0140 VERIFY_MENU_WITH_BOOKMARK_TABS_AS_FOLDER(); 0141 testObject->setNumberOfOpenTabs(1); 0142 testObject->ensureUpToDate(); 0143 VERIFY_MENU_WITHOUT_BOOKMARK_TABS_AS_FOLDER(); 0144 testObject->setNumberOfOpenTabs(2); 0145 testObject->ensureUpToDate(); 0146 VERIFY_MENU_WITH_BOOKMARK_TABS_AS_FOLDER(); 0147 } 0148 0149 #if KBOOKMARKS_BUILD_DEPRECATED_SINCE(5, 69) 0150 void KBookmarkMenuTest::tabsOpenActionCollection_data() 0151 { 0152 tabsOpen_data(); 0153 } 0154 0155 void KBookmarkMenuTest::tabsOpenActionCollection() 0156 { 0157 QFETCH(bool, supportsTabs); 0158 QFETCH(int, numberOfOpenTabs); 0159 auto manager = KBookmarkManager::createTempManager(); 0160 std::unique_ptr<TestKBookmarkOwner> bookmarkOwner(new TestKBookmarkOwner(supportsTabs)); 0161 QMenu *menu = new QMenu; 0162 std::unique_ptr<KActionCollection> actionCollection(new KActionCollection(nullptr, QString())); 0163 std::unique_ptr<KBookmarkMenu> testObject(new KBookmarkMenu(manager, bookmarkOwner.get(), menu, actionCollection.get())); 0164 testObject->setNumberOfOpenTabs(numberOfOpenTabs); 0165 testObject->ensureUpToDate(); 0166 if (supportsTabs && numberOfOpenTabs > 1) { 0167 VERIFY_MENU_WITH_BOOKMARK_TABS_AS_FOLDER(); 0168 } else { 0169 VERIFY_MENU_WITHOUT_BOOKMARK_TABS_AS_FOLDER(); 0170 } 0171 } 0172 0173 void KBookmarkMenuTest::tabsOpenChangesActionCollection() 0174 { 0175 auto manager = KBookmarkManager::createTempManager(); 0176 std::unique_ptr<TestKBookmarkOwner> bookmarkOwner(new TestKBookmarkOwner(true)); 0177 QMenu *menu = new QMenu; 0178 std::unique_ptr<KActionCollection> actionCollection(new KActionCollection(nullptr, QString())); 0179 std::unique_ptr<KBookmarkMenu> testObject(new KBookmarkMenu(manager, bookmarkOwner.get(), menu, actionCollection.get())); 0180 testObject->ensureUpToDate(); 0181 // If the number of open tabs has not been set it will default to 2 0182 const int DefaultNumberOfOpenTabs = 2; 0183 QCOMPARE(testObject->numberOfOpenTabs(), DefaultNumberOfOpenTabs); 0184 testObject->ensureUpToDate(); 0185 VERIFY_MENU_WITH_BOOKMARK_TABS_AS_FOLDER(); 0186 testObject->setNumberOfOpenTabs(1); 0187 testObject->ensureUpToDate(); 0188 VERIFY_MENU_WITHOUT_BOOKMARK_TABS_AS_FOLDER(); 0189 testObject->setNumberOfOpenTabs(2); 0190 testObject->ensureUpToDate(); 0191 VERIFY_MENU_WITH_BOOKMARK_TABS_AS_FOLDER(); 0192 } 0193 #endif 0194 0195 QTEST_MAIN(KBookmarkMenuTest) 0196 0197 #include "kbookmarkmenutest.moc"