1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
/*
* SPDX-FileCopyrightText: 2023 Kai Uwe Broulik <kde@broulik.de>
*
* SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "kopenaction_p.h"
#include "kstandardaction.h"
#include <QAction>
#include <QStandardPaths>
#include <QTest>
#include <QToolBar>
// Fake KActionCollection, just needs the name.
class KActionCollection : public QObject
{
Q_OBJECT
public:
explicit KActionCollection(QObject *parent = nullptr)
: QObject(parent)
{
}
Q_INVOKABLE QAction *action(const QString &name) const
{
return m_actions.value(name);
}
void addAction(QAction *action)
{
m_actions.insert(action->objectName(), action);
}
private:
QMap<QString, QAction *> m_actions;
};
class KOpenActionTest : public QObject
{
Q_OBJECT
public:
explicit KOpenActionTest(QObject *parent = nullptr);
private Q_SLOTS:
void initTestCase();
void testDefaults();
void testActionCollectionNoRecents();
void testActionCollectionWithRecents();
};
KOpenActionTest::KOpenActionTest(QObject *parent)
: QObject(parent)
{
}
void KOpenActionTest::initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
}
void KOpenActionTest::testDefaults()
{
std::unique_ptr<QAction> action{KStandardAction::open(nullptr, nullptr, nullptr)};
auto *openAction = qobject_cast<KOpenAction *>(action.get());
QVERIFY(openAction);
QCOMPARE(openAction->popupMode(), KToolBarPopupAction::NoPopup);
}
void KOpenActionTest::testActionCollectionNoRecents()
{
KActionCollection collection;
QAction *openAction = KStandardAction::open(nullptr, nullptr, nullptr);
openAction->setParent(&collection);
collection.addAction(openAction);
QToolBar toolBar;
toolBar.addAction(openAction);
// No recent documents actions, no popup.
QCOMPARE(qobject_cast<KOpenAction *>(openAction)->popupMode(), KToolBarPopupAction::NoPopup);
}
void KOpenActionTest::testActionCollectionWithRecents()
{
KActionCollection collection;
QAction *openAction = KStandardAction::open(nullptr, nullptr, nullptr);
// Simulates KActionCollection which creates the actions without a parent
// and then sets it later.
openAction->setParent(&collection);
collection.addAction(openAction);
KRecentFilesAction *openRecentAction = KStandardAction::openRecent(nullptr, nullptr, nullptr);
openRecentAction->setParent(&collection);
collection.addAction(openRecentAction);
QToolBar toolBar;
toolBar.addAction(openAction);
// No recent documents, no popup.
QCOMPARE(qobject_cast<KOpenAction *>(openAction)->popupMode(), KToolBarPopupAction::NoPopup);
openRecentAction->addUrl(QUrl(QStringLiteral("http://www.kde.org")));
// Got some recent documents, popup should be there now.
QCOMPARE(qobject_cast<KOpenAction *>(openAction)->popupMode(), KToolBarPopupAction::MenuButtonPopup);
}
QTEST_MAIN(KOpenActionTest)
#include "kopenactiontest.moc"
|