File indexing completed on 2024-04-28 03:53:23
0001 /* 0002 * SPDX-FileCopyrightText: 2023 Kai Uwe Broulik <kde@broulik.de> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include "kopenaction_p.h" 0008 #include "kstandardaction.h" 0009 0010 #include <QAction> 0011 #include <QStandardPaths> 0012 #include <QTest> 0013 #include <QToolBar> 0014 0015 // Fake KActionCollection, just needs the name. 0016 class KActionCollection : public QObject 0017 { 0018 Q_OBJECT 0019 0020 public: 0021 explicit KActionCollection(QObject *parent = nullptr) 0022 : QObject(parent) 0023 { 0024 } 0025 0026 Q_INVOKABLE QAction *action(const QString &name) const 0027 { 0028 return m_actions.value(name); 0029 } 0030 0031 void addAction(QAction *action) 0032 { 0033 m_actions.insert(action->objectName(), action); 0034 } 0035 0036 private: 0037 QMap<QString, QAction *> m_actions; 0038 }; 0039 0040 class KOpenActionTest : public QObject 0041 { 0042 Q_OBJECT 0043 public: 0044 explicit KOpenActionTest(QObject *parent = nullptr); 0045 0046 private Q_SLOTS: 0047 void initTestCase(); 0048 0049 void testDefaults(); 0050 void testActionCollectionNoRecents(); 0051 void testActionCollectionWithRecents(); 0052 }; 0053 0054 KOpenActionTest::KOpenActionTest(QObject *parent) 0055 : QObject(parent) 0056 { 0057 } 0058 0059 void KOpenActionTest::initTestCase() 0060 { 0061 QStandardPaths::setTestModeEnabled(true); 0062 } 0063 0064 void KOpenActionTest::testDefaults() 0065 { 0066 std::unique_ptr<QAction> action{KStandardAction::open(nullptr, nullptr, nullptr)}; 0067 0068 auto *openAction = qobject_cast<KOpenAction *>(action.get()); 0069 QVERIFY(openAction); 0070 0071 QCOMPARE(openAction->popupMode(), KToolBarPopupAction::NoPopup); 0072 } 0073 0074 void KOpenActionTest::testActionCollectionNoRecents() 0075 { 0076 KActionCollection collection; 0077 0078 QAction *openAction = KStandardAction::open(nullptr, nullptr, nullptr); 0079 openAction->setParent(&collection); 0080 collection.addAction(openAction); 0081 0082 QToolBar toolBar; 0083 toolBar.addAction(openAction); 0084 0085 // No recent documents actions, no popup. 0086 QCOMPARE(qobject_cast<KOpenAction *>(openAction)->popupMode(), KToolBarPopupAction::NoPopup); 0087 } 0088 0089 void KOpenActionTest::testActionCollectionWithRecents() 0090 { 0091 KActionCollection collection; 0092 0093 QAction *openAction = KStandardAction::open(nullptr, nullptr, nullptr); 0094 // Simulates KActionCollection which creates the actions without a parent 0095 // and then sets it later. 0096 openAction->setParent(&collection); 0097 collection.addAction(openAction); 0098 0099 KRecentFilesAction *openRecentAction = KStandardAction::openRecent(nullptr, nullptr, nullptr); 0100 openRecentAction->setParent(&collection); 0101 collection.addAction(openRecentAction); 0102 0103 QToolBar toolBar; 0104 toolBar.addAction(openAction); 0105 0106 // No recent documents, no popup. 0107 QCOMPARE(qobject_cast<KOpenAction *>(openAction)->popupMode(), KToolBarPopupAction::NoPopup); 0108 0109 openRecentAction->addUrl(QUrl(QStringLiteral("http://www.kde.org"))); 0110 0111 // Got some recent documents, popup should be there now. 0112 QCOMPARE(qobject_cast<KOpenAction *>(openAction)->popupMode(), KToolBarPopupAction::MenuButtonPopup); 0113 } 0114 0115 QTEST_MAIN(KOpenActionTest) 0116 0117 #include "kopenactiontest.moc"