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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/*
SPDX-FileCopyrightText: 2007 Simon Hausmann <hausmann@kde.org>
SPDX-FileCopyrightText: 2022 David Redondo <kde@david-redondo.de>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "kstandardactiontest.h"
#include <QAction>
#include <QStandardPaths>
#include <QTest>
#include "kstandardaction.h"
void tst_KStandardAction::initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
}
void tst_KStandardAction::shortcutForActionId()
{
QList<QKeySequence> stdShortcut = KStandardShortcut::shortcut(KStandardShortcut::Cut);
QAction *cut = KStandardAction::cut(nullptr);
QList<QKeySequence> actShortcut = cut->shortcuts();
QCOMPARE(cut->property("defaultShortcuts").value<QList<QKeySequence>>(), actShortcut);
QVERIFY(stdShortcut == actShortcut);
delete cut;
cut = KStandardAction::create(KStandardAction::Cut, nullptr, nullptr, nullptr);
actShortcut = cut->shortcuts();
QVERIFY(stdShortcut == actShortcut);
delete cut;
}
void tst_KStandardAction::changingShortcut()
{
#ifdef Q_OS_WINDOWS
QSKIP("DBus notifications are disabled on Windows");
#endif
// GIVEN
KStandardShortcut::saveShortcut(KStandardShortcut::Cut, KStandardShortcut::hardcodedDefaultShortcut(KStandardShortcut::Cut));
const QList<QKeySequence> newShortcut{Qt::CTRL + Qt::Key_Adiaeresis};
QVERIFY(newShortcut != KStandardShortcut::cut());
std::unique_ptr<QAction> action(KStandardAction::cut(nullptr));
std::unique_ptr<QAction> action2(KStandardAction::create(KStandardAction::Cut, nullptr, nullptr, nullptr));
QCOMPARE(action->shortcuts(), KStandardShortcut::cut());
QCOMPARE(action2->shortcuts(), KStandardShortcut::cut());
// WHEN
KStandardShortcut::saveShortcut(KStandardShortcut::Cut, newShortcut);
// THEN
// (wait for KStandardShortcut::shortcutWatcher to notify about the config file change, and for KStandardAction to update the actions...)
QTRY_COMPARE(action->shortcuts(), newShortcut);
QTRY_COMPARE(action2->shortcuts(), newShortcut);
}
class Receiver : public QObject
{
Q_OBJECT
public:
Receiver()
: triggered(false)
{
}
bool triggered;
QUrl lastUrl;
public Q_SLOTS:
void onTriggered()
{
triggered = true;
}
void onUrlSelected(const QUrl &url)
{
lastUrl = url;
}
};
void tst_KStandardAction::testCreateNewStyle()
{
Receiver receiver;
QAction *action1 = KStandardAction::create(KStandardAction::Next, &receiver, &Receiver::onTriggered, &receiver);
QVERIFY(!receiver.triggered);
action1->trigger();
QVERIFY(receiver.triggered);
// check that it works with lambdas as well
bool triggered = false;
auto onTriggered = [&] {
triggered = true;
};
QAction *action2 = KStandardAction::create(KStandardAction::Copy, &receiver, onTriggered, &receiver);
QVERIFY(!triggered);
action2->trigger();
QVERIFY(triggered);
// check ConfigureToolbars
triggered = false;
QAction *action3 = KStandardAction::create(KStandardAction::ConfigureToolbars, &receiver, onTriggered, &receiver);
QVERIFY(!triggered);
action3->trigger(); // a queued connection should be used here
QVERIFY(!triggered);
QCoreApplication::processEvents();
QVERIFY(triggered);
QUrl expectedUrl = QUrl(QStringLiteral("file:///foo/bar"));
KRecentFilesAction *recent = KStandardAction::openRecent(&receiver, &Receiver::onUrlSelected, &receiver);
QCOMPARE(receiver.lastUrl, QUrl());
recent->urlSelected(expectedUrl);
QCOMPARE(receiver.lastUrl, expectedUrl);
// same again with lambda
QUrl url;
KRecentFilesAction *recent2 = KStandardAction::openRecent(
&receiver,
[&](const QUrl &u) {
url = u;
},
&receiver);
QCOMPARE(url, QUrl());
recent2->urlSelected(expectedUrl);
QCOMPARE(url, expectedUrl);
// make sure the asserts don't trigger (action has the correct type)
KToggleAction *toggle1 = KStandardAction::showMenubar(&receiver, &Receiver::onTriggered, &receiver);
QVERIFY(toggle1);
KToggleAction *toggle2 = KStandardAction::showStatusbar(&receiver, &Receiver::onTriggered, &receiver);
QVERIFY(toggle2);
KToggleFullScreenAction *toggle3 = KStandardAction::fullScreen(&receiver, &Receiver::onTriggered, new QWidget, &receiver);
QVERIFY(toggle3);
}
void tst_KStandardAction::testCreateOldStyle()
{
Receiver receiver;
QAction *action1 = KStandardAction::create(KStandardAction::Next, &receiver, SLOT(onTriggered()), &receiver);
QVERIFY(!receiver.triggered);
action1->trigger();
QVERIFY(receiver.triggered);
// check ConfigureToolbars
receiver.triggered = false;
QAction *action3 = KStandardAction::create(KStandardAction::ConfigureToolbars, &receiver, SLOT(onTriggered()), &receiver);
QVERIFY(!receiver.triggered);
action3->trigger(); // a queued connection should be used here
QVERIFY(!receiver.triggered);
QCoreApplication::processEvents();
QVERIFY(receiver.triggered);
QUrl expectedUrl = QUrl(QStringLiteral("file:///foo/bar"));
KRecentFilesAction *recent = KStandardAction::openRecent(&receiver, SLOT(onUrlSelected(QUrl)), &receiver);
QCOMPARE(receiver.lastUrl, QUrl());
recent->urlSelected(expectedUrl);
QCOMPARE(receiver.lastUrl, expectedUrl);
// make sure the asserts don't trigger (action has the correct type)
KToggleAction *toggle1 = KStandardAction::showMenubar(&receiver, SLOT(onTriggered()), &receiver);
QVERIFY(toggle1);
KToggleAction *toggle2 = KStandardAction::showStatusbar(&receiver, SLOT(onTriggered()), &receiver);
QVERIFY(toggle2);
auto w = new QWidget;
KToggleFullScreenAction *toggle3 = KStandardAction::fullScreen(&receiver, SLOT(onTriggered()), w, &receiver);
QVERIFY(toggle3);
delete w;
}
QTEST_MAIN(tst_KStandardAction)
#include "kstandardactiontest.moc"
#include "moc_kstandardactiontest.cpp"
|