File indexing completed on 2024-10-13 09:36:45

0001 /*
0002     SPDX-FileCopyrightText: 2014-2015 Gregor Mi <codestruct@posteo.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include <../src/kmoretools/kmoretools.h>
0008 #include <../src/kmoretools/kmoretools_p.h>
0009 #include <../src/kmoretools/kmoretoolsmenufactory.h>
0010 #include <../src/kmoretools/kmoretoolspresets.h>
0011 
0012 #include <QDialog>
0013 #include <QHBoxLayout>
0014 #include <QLabel>
0015 #include <QLineEdit>
0016 #include <QPushButton>
0017 #include <QTest>
0018 
0019 /**
0020  * Each test case starts a test GUI.
0021  * Run kmoretoolstest_interactive with the test case name as first parameter
0022  * (e.g. testDialogForGroupingNames) to run only this test GUI.
0023  */
0024 class KMoreToolsTestInteractive : public QObject
0025 {
0026     Q_OBJECT
0027 
0028 private Q_SLOTS:
0029     void init();
0030     void cleanup();
0031 
0032     void testConfigDialogAllInstalled();
0033     void testConfigDialogSomeNotInstalled();
0034     void testConfigDialogNotInstalled1Service2Items();
0035 
0036     void test_buildMenu_WithQActions_interative1();
0037 
0038     void testDialogForGroupingNames();
0039 
0040     void testLazyMenu();
0041 
0042 private:
0043     void testConfigDialogImpl(bool withNotInstalled, bool withMultipleItemsPerNotInstalledService, const QString &description);
0044 };
0045 
0046 void KMoreToolsTestInteractive::init()
0047 {
0048 }
0049 
0050 void KMoreToolsTestInteractive::cleanup()
0051 {
0052 }
0053 
0054 bool menuAtLeastOneActionWithText(const QMenu *menu, const QString &text)
0055 {
0056     const auto lstActions = menu->actions();
0057     for (auto a : lstActions) {
0058         if (a->text() == text) {
0059             return true;
0060         }
0061     }
0062 
0063     return false;
0064 }
0065 
0066 void KMoreToolsTestInteractive::test_buildMenu_WithQActions_interative1()
0067 {
0068     KMoreTools kmt(_("unittest-kmoretools/qactions")); // todo: disable copy-ctor!?
0069 
0070     auto f = [&kmt](QString title) { // NOTE: capture by reference! see https://en.wikipedia.org/wiki/Anonymous_function
0071         const auto menuBuilder = kmt.menuBuilder();
0072         menuBuilder->clear();
0073         QMenu menu;
0074         menuBuilder->addMenuItem(new QAction(_("Hallo 1"), &menu), _("id1"));
0075         menuBuilder->addMenuItem(new QAction(_("Hallo 2"), &menu), _("id2"));
0076         menuBuilder->addMenuItem(new QAction(_("Hallo 3"), &menu), _("id3"));
0077 
0078         menuBuilder->buildByAppendingToMenu(&menu);
0079         menuBuilder->showConfigDialog(title);
0080     };
0081 
0082     f(_("test_buildMenu_WithQActions 1"));
0083 }
0084 
0085 void KMoreToolsTestInteractive::testConfigDialogImpl(bool withNotInstalled, bool withMultipleItemsPerNotInstalledService, const QString &description)
0086 {
0087     KMoreTools kmt(_("unittest-kmoretools/2"));
0088     const auto kateApp = kmt.registerServiceByDesktopEntryName(_("org.kde.kate"));
0089     const auto gitgApp = kmt.registerServiceByDesktopEntryName(_("gitg"));
0090     const auto notinstApp = kmt.registerServiceByDesktopEntryName(_("mynotinstalledapp"));
0091     const auto notinstApp2 = kmt.registerServiceByDesktopEntryName(_("mynotinstapp2"));
0092     notinstApp2->setHomepageUrl(QUrl(_("https://www.kde.org")));
0093     const auto menuBuilder = kmt.menuBuilder();
0094     menuBuilder->addMenuItem(kateApp);
0095     menuBuilder->addMenuItem(gitgApp);
0096     if (withNotInstalled) {
0097         auto item1 = menuBuilder->addMenuItem(notinstApp);
0098         item1->setInitialItemText(notinstApp->formatString(_("$Name - item 1")));
0099 
0100         menuBuilder->addMenuItem(notinstApp2);
0101 
0102         if (withMultipleItemsPerNotInstalledService) {
0103             auto item3 = menuBuilder->addMenuItem(notinstApp);
0104             item3->setInitialItemText(notinstApp->formatString(_("$Name - second item")));
0105         }
0106     }
0107     auto i1 = menuBuilder->addMenuItem(kateApp, KMoreTools::MenuSection_More);
0108     i1->setId(_("kate1"));
0109     i1->setInitialItemText(_("Kate more"));
0110     auto i2 = menuBuilder->addMenuItem(gitgApp, KMoreTools::MenuSection_More);
0111     i2->setId(_("gitg1"));
0112     i2->setInitialItemText(_("gitg more"));
0113     menuBuilder->showConfigDialog(description);
0114 
0115     // show resulting menu
0116     auto dlg = new QDialog();
0117     auto button = new QPushButton(_("Test the menu"), dlg);
0118     auto menu = new QMenu(dlg);
0119     menuBuilder->buildByAppendingToMenu(menu);
0120     button->setMenu(menu); // TODO: connect to the button click signal to always rebuild the menu
0121     auto label =
0122         new QLabel(_("Test the menu and hit Esc to exit if you are done. Note that changes made via the Configure dialog will have no immediate effect."), dlg);
0123     label->setWordWrap(true);
0124     auto layout = new QHBoxLayout();
0125     layout->addWidget(button);
0126     layout->addWidget(label);
0127     dlg->setLayout(layout);
0128     QObject::connect(dlg, &QDialog::finished, dlg, [dlg]() {
0129         qDebug() << "delete dlg;";
0130         delete dlg;
0131     });
0132     dlg->exec();
0133 }
0134 
0135 void KMoreToolsTestInteractive::testConfigDialogAllInstalled()
0136 {
0137     testConfigDialogImpl(false, false, _("TEST all installed"));
0138 }
0139 
0140 void KMoreToolsTestInteractive::testConfigDialogSomeNotInstalled()
0141 {
0142     testConfigDialogImpl(true, false, _("TEST some not installed"));
0143 }
0144 
0145 void KMoreToolsTestInteractive::testConfigDialogNotInstalled1Service2Items()
0146 {
0147     testConfigDialogImpl(true, true, _("TEST more than one item for one not installed service"));
0148 }
0149 
0150 void KMoreToolsTestInteractive::testDialogForGroupingNames()
0151 {
0152     // show resulting menu
0153     auto dlg = new QDialog();
0154     auto labelInfo = new QLabel(
0155         _("First, select a URL (leave the URL box empty to give no URL; don't forget to add file:// or https://). Then, select a grouping name. => A menu will "
0156           "be created that you can try out. KDE4/KF5: If an application does not start even there is the launch indicator, try: $ eval `dbus-launch`"),
0157         dlg);
0158     labelInfo->setWordWrap(true);
0159     auto selectButton = new QPushButton(_("Select grouping name..."), dlg);
0160     auto labelLineEdit = new QLabel(_("URL 1 (file://..., https://...)"), dlg);
0161     auto urlLineEdit = new QLineEdit(dlg);
0162     urlLineEdit->setText(_("file:///etc/bash.bashrc"));
0163     auto menuButton = new QPushButton(_("<wait for selection>"), dlg);
0164 
0165     const auto groupingNamesList = {_("disk-usage"),
0166                                     _("disk-partitions"),
0167                                     _("files-find"),
0168                                     _("font-tools"),
0169                                     _("git-clients-for-folder"),
0170                                     _("git-clients-and-actions"),
0171                                     _("icon-browser"),
0172                                     _("language-dictionary"),
0173                                     _("mouse-tools"),
0174                                     _("screenrecorder"),
0175                                     _("screenshot-take"),
0176                                     _("system-monitor-processes"),
0177                                     _("system-monitor-logs"),
0178                                     _("time-countdown")};
0179 
0180     KMoreToolsMenuFactory menuFactory(_("unittest-kmoretools/3"));
0181 
0182     auto groupingNamesMenu = new QMenu(dlg);
0183     QMenu *moreToolsMenu = nullptr;
0184     for (auto groupingName : groupingNamesList) {
0185         auto action = new QAction(groupingName, groupingNamesMenu);
0186         action->setData(groupingName);
0187         groupingNamesMenu->addAction(action);
0188 
0189         QObject::connect(action,
0190                          &QAction::triggered,
0191                          action,
0192                          [action, &menuFactory, &moreToolsMenu, urlLineEdit, menuButton]() { // clazy:exclude=lambda-in-connect
0193                              auto groupingName = action->data().toString();
0194                              QUrl url;
0195                              if (!urlLineEdit->text().isEmpty()) {
0196                                  url.setUrl(urlLineEdit->text());
0197                              }
0198                              moreToolsMenu = menuFactory.createMenuFromGroupingNames({groupingName}, url);
0199                              menuButton->setText(
0200                                  QString(_("menu for: '%1' (URL arg: %2)...")).arg(groupingName, url.isEmpty() ? _("<empty>") : _("<see URL 1>")));
0201                              menuButton->setMenu(moreToolsMenu);
0202                          });
0203     }
0204 
0205     selectButton->setMenu(groupingNamesMenu);
0206 
0207     auto hLayout = new QHBoxLayout();
0208     auto vLayout = new QVBoxLayout();
0209     hLayout->addWidget(labelInfo);
0210     hLayout->addLayout(vLayout);
0211     vLayout->addWidget(labelLineEdit);
0212     vLayout->addWidget(urlLineEdit);
0213     vLayout->addWidget(selectButton);
0214     vLayout->addWidget(menuButton);
0215     dlg->setLayout(hLayout);
0216     dlg->setBaseSize(300, 150);
0217     QObject::connect(dlg, &QDialog::finished, dlg, [dlg]() {
0218         qDebug() << "delete dlg;";
0219         delete dlg;
0220     });
0221     dlg->exec();
0222 }
0223 
0224 void KMoreToolsTestInteractive::testLazyMenu()
0225 {
0226     KMoreToolsMenuFactory menuFactory(_("unittest-kmoretools/4"));
0227 
0228     auto moreToolsMenu = menuFactory.createMenuFromGroupingNames({_("git-clients-for-folder")});
0229 
0230     auto dlg = new QDialog();
0231     auto button = new QPushButton(_("Test the lazy menu"), dlg);
0232     button->setMenu(moreToolsMenu);
0233     auto label =
0234         new QLabel(_("Test the menu and hit Esc to exit if you are done. Note that changes made via the Configure dialog will have no immediate effect."), dlg);
0235     label->setWordWrap(true);
0236     auto layout = new QHBoxLayout();
0237     layout->addWidget(button);
0238     layout->addWidget(label);
0239     dlg->setLayout(layout);
0240     QObject::connect(dlg, &QDialog::finished, dlg, [dlg]() {
0241         qDebug() << "delete dlg;";
0242         delete dlg;
0243     });
0244     dlg->exec();
0245 }
0246 
0247 QTEST_MAIN(KMoreToolsTestInteractive)
0248 
0249 #include "kmoretoolstest_interactive.moc"