File indexing completed on 2024-04-14 03:51:12
0001 /* 0002 SPDX-FileCopyrightText: 2007 Simon Hausmann <hausmann@kde.org> 0003 SPDX-FileCopyrightText: 2022 David Redondo <kde@david-redondo.de> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "kstandardactiontest.h" 0009 0010 #include <QAction> 0011 #include <QStandardPaths> 0012 #include <QTest> 0013 0014 #include "kstandardaction.h" 0015 0016 void tst_KStandardAction::initTestCase() 0017 { 0018 QStandardPaths::setTestModeEnabled(true); 0019 } 0020 0021 void tst_KStandardAction::shortcutForActionId() 0022 { 0023 QList<QKeySequence> stdShortcut = KStandardShortcut::shortcut(KStandardShortcut::Cut); 0024 0025 QAction *cut = KStandardAction::cut(nullptr); 0026 QList<QKeySequence> actShortcut = cut->shortcuts(); 0027 QCOMPARE(cut->property("defaultShortcuts").value<QList<QKeySequence>>(), actShortcut); 0028 QVERIFY(stdShortcut == actShortcut); 0029 delete cut; 0030 0031 cut = KStandardAction::create(KStandardAction::Cut, nullptr, nullptr, nullptr); 0032 actShortcut = cut->shortcuts(); 0033 QVERIFY(stdShortcut == actShortcut); 0034 delete cut; 0035 } 0036 0037 void tst_KStandardAction::changingShortcut() 0038 { 0039 #ifdef Q_OS_WINDOWS 0040 QSKIP("DBus notifications are disabled on Windows"); 0041 #endif 0042 0043 // GIVEN 0044 KStandardShortcut::saveShortcut(KStandardShortcut::Cut, KStandardShortcut::hardcodedDefaultShortcut(KStandardShortcut::Cut)); 0045 const QList<QKeySequence> newShortcut{Qt::CTRL | Qt::Key_Adiaeresis}; 0046 QVERIFY(newShortcut != KStandardShortcut::cut()); 0047 0048 std::unique_ptr<QAction> action(KStandardAction::cut(nullptr)); 0049 std::unique_ptr<QAction> action2(KStandardAction::create(KStandardAction::Cut, nullptr, nullptr, nullptr)); 0050 QCOMPARE(action->shortcuts(), KStandardShortcut::cut()); 0051 QCOMPARE(action2->shortcuts(), KStandardShortcut::cut()); 0052 0053 // WHEN 0054 KStandardShortcut::saveShortcut(KStandardShortcut::Cut, newShortcut); 0055 0056 // THEN 0057 // (wait for KStandardShortcut::shortcutWatcher to notify about the config file change, and for KStandardAction to update the actions...) 0058 QTRY_COMPARE(action->shortcuts(), newShortcut); 0059 QTRY_COMPARE(action2->shortcuts(), newShortcut); 0060 } 0061 0062 class Receiver : public QObject 0063 { 0064 Q_OBJECT 0065 public: 0066 Receiver() 0067 : triggered(false) 0068 { 0069 } 0070 0071 bool triggered; 0072 QUrl lastUrl; 0073 0074 public Q_SLOTS: 0075 void onTriggered() 0076 { 0077 triggered = true; 0078 } 0079 0080 void onUrlSelected(const QUrl &url) 0081 { 0082 lastUrl = url; 0083 } 0084 }; 0085 0086 void tst_KStandardAction::testCreateNewStyle() 0087 { 0088 Receiver receiver; 0089 QAction *action1 = KStandardAction::create(KStandardAction::Next, &receiver, &Receiver::onTriggered, &receiver); 0090 QVERIFY(!receiver.triggered); 0091 action1->trigger(); 0092 QVERIFY(receiver.triggered); 0093 0094 // check that it works with lambdas as well 0095 bool triggered = false; 0096 auto onTriggered = [&] { 0097 triggered = true; 0098 }; 0099 QAction *action2 = KStandardAction::create(KStandardAction::Copy, &receiver, onTriggered, &receiver); 0100 QVERIFY(!triggered); 0101 action2->trigger(); 0102 QVERIFY(triggered); 0103 0104 // check ConfigureToolbars 0105 triggered = false; 0106 QAction *action3 = KStandardAction::create(KStandardAction::ConfigureToolbars, &receiver, onTriggered, &receiver); 0107 QVERIFY(!triggered); 0108 action3->trigger(); // a queued connection should be used here 0109 QVERIFY(!triggered); 0110 QCoreApplication::processEvents(); 0111 QVERIFY(triggered); 0112 0113 QUrl expectedUrl = QUrl(QStringLiteral("file:///foo/bar")); 0114 KRecentFilesAction *recent = KStandardAction::openRecent(&receiver, &Receiver::onUrlSelected, &receiver); 0115 QCOMPARE(receiver.lastUrl, QUrl()); 0116 recent->urlSelected(expectedUrl); 0117 QCOMPARE(receiver.lastUrl, expectedUrl); 0118 0119 // same again with lambda 0120 QUrl url; 0121 KRecentFilesAction *recent2 = KStandardAction::openRecent( 0122 &receiver, 0123 [&](const QUrl &u) { 0124 url = u; 0125 }, 0126 &receiver); 0127 QCOMPARE(url, QUrl()); 0128 recent2->urlSelected(expectedUrl); 0129 QCOMPARE(url, expectedUrl); 0130 0131 // make sure the asserts don't trigger (action has the correct type) 0132 KToggleAction *toggle1 = KStandardAction::showMenubar(&receiver, &Receiver::onTriggered, &receiver); 0133 QVERIFY(toggle1); 0134 KToggleAction *toggle2 = KStandardAction::showStatusbar(&receiver, &Receiver::onTriggered, &receiver); 0135 QVERIFY(toggle2); 0136 KToggleFullScreenAction *toggle3 = KStandardAction::fullScreen(&receiver, &Receiver::onTriggered, new QWidget, &receiver); 0137 QVERIFY(toggle3); 0138 } 0139 0140 void tst_KStandardAction::testCreateOldStyle() 0141 { 0142 Receiver receiver; 0143 QAction *action1 = KStandardAction::create(KStandardAction::Next, &receiver, SLOT(onTriggered()), &receiver); 0144 QVERIFY(!receiver.triggered); 0145 action1->trigger(); 0146 QVERIFY(receiver.triggered); 0147 0148 // check ConfigureToolbars 0149 receiver.triggered = false; 0150 QAction *action3 = KStandardAction::create(KStandardAction::ConfigureToolbars, &receiver, SLOT(onTriggered()), &receiver); 0151 QVERIFY(!receiver.triggered); 0152 action3->trigger(); // a queued connection should be used here 0153 QVERIFY(!receiver.triggered); 0154 QCoreApplication::processEvents(); 0155 QVERIFY(receiver.triggered); 0156 0157 QUrl expectedUrl = QUrl(QStringLiteral("file:///foo/bar")); 0158 KRecentFilesAction *recent = KStandardAction::openRecent(&receiver, SLOT(onUrlSelected(QUrl)), &receiver); 0159 QCOMPARE(receiver.lastUrl, QUrl()); 0160 recent->urlSelected(expectedUrl); 0161 QCOMPARE(receiver.lastUrl, expectedUrl); 0162 0163 // make sure the asserts don't trigger (action has the correct type) 0164 KToggleAction *toggle1 = KStandardAction::showMenubar(&receiver, SLOT(onTriggered()), &receiver); 0165 QVERIFY(toggle1); 0166 KToggleAction *toggle2 = KStandardAction::showStatusbar(&receiver, SLOT(onTriggered()), &receiver); 0167 QVERIFY(toggle2); 0168 auto w = new QWidget; 0169 KToggleFullScreenAction *toggle3 = KStandardAction::fullScreen(&receiver, SLOT(onTriggered()), w, &receiver); 0170 QVERIFY(toggle3); 0171 delete w; 0172 } 0173 0174 QTEST_MAIN(tst_KStandardAction) 0175 0176 #include "kstandardactiontest.moc" 0177 #include "moc_kstandardactiontest.cpp"