File indexing completed on 2024-05-12 04:42:03

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 "kmoretools.h"
0008 #include "kmoretools_p.h"
0009 #include "kmoretoolsmenufactory.h"
0010 #include "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");
0069 
0070     const auto menuBuilder = kmt.menuBuilder();
0071     menuBuilder->clear();
0072     QMenu menu;
0073     menuBuilder->addMenuItem(new QAction("Hallo 1", &menu), "id1");
0074     menuBuilder->addMenuItem(new QAction("Hallo 2", &menu), "id2");
0075     menuBuilder->addMenuItem(new QAction("Hallo 3", &menu), "id3");
0076 
0077     menuBuilder->buildByAppendingToMenu(&menu);
0078     menuBuilder->showConfigDialog("test_buildMenu_WithQActions 1");
0079 }
0080 
0081 void KMoreToolsTestInteractive::testConfigDialogImpl(bool withNotInstalled, bool withMultipleItemsPerNotInstalledService, const QString &description)
0082 {
0083     KMoreTools kmt("unittest-kmoretools/2");
0084     const auto kateApp = kmt.registerServiceByDesktopEntryName("org.kde.kate");
0085     const auto gitgApp = kmt.registerServiceByDesktopEntryName("gitg");
0086     const auto notinstApp = kmt.registerServiceByDesktopEntryName("mynotinstalledapp");
0087     const auto notinstApp2 = kmt.registerServiceByDesktopEntryName("mynotinstapp2");
0088     notinstApp2->setHomepageUrl(QUrl("https://www.kde.org"));
0089     const auto menuBuilder = kmt.menuBuilder();
0090     menuBuilder->addMenuItem(kateApp);
0091     menuBuilder->addMenuItem(gitgApp);
0092     if (withNotInstalled) {
0093         auto item1 = menuBuilder->addMenuItem(notinstApp);
0094         item1->setInitialItemText(notinstApp->formatString("$Name - item 1"));
0095 
0096         menuBuilder->addMenuItem(notinstApp2);
0097 
0098         if (withMultipleItemsPerNotInstalledService) {
0099             auto item3 = menuBuilder->addMenuItem(notinstApp);
0100             item3->setInitialItemText(notinstApp->formatString("$Name - second item"));
0101         }
0102     }
0103     auto i1 = menuBuilder->addMenuItem(kateApp, KMoreTools::MenuSection_More);
0104     i1->setId("kate1");
0105     i1->setInitialItemText("Kate more");
0106     auto i2 = menuBuilder->addMenuItem(gitgApp, KMoreTools::MenuSection_More);
0107     i2->setId("gitg1");
0108     i2->setInitialItemText("gitg more");
0109     menuBuilder->showConfigDialog(description);
0110 
0111     // show resulting menu
0112     auto dlg = new QDialog();
0113     auto button = new QPushButton("Test the menu", dlg);
0114     auto menu = new QMenu(dlg);
0115     menuBuilder->buildByAppendingToMenu(menu);
0116     button->setMenu(menu); // TODO: connect to the button click signal to always rebuild the menu
0117     auto label =
0118         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);
0119     label->setWordWrap(true);
0120     auto layout = new QHBoxLayout();
0121     layout->addWidget(button);
0122     layout->addWidget(label);
0123     dlg->setLayout(layout);
0124     QObject::connect(dlg, &QDialog::finished, dlg, [dlg]() {
0125         qDebug() << "delete dlg;";
0126         delete dlg;
0127     });
0128     dlg->exec();
0129 }
0130 
0131 void KMoreToolsTestInteractive::testConfigDialogAllInstalled()
0132 {
0133     testConfigDialogImpl(false, false, "TEST all installed");
0134 }
0135 
0136 void KMoreToolsTestInteractive::testConfigDialogSomeNotInstalled()
0137 {
0138     testConfigDialogImpl(true, false, "TEST some not installed");
0139 }
0140 
0141 void KMoreToolsTestInteractive::testConfigDialogNotInstalled1Service2Items()
0142 {
0143     testConfigDialogImpl(true, true, "TEST more than one item for one not installed service");
0144 }
0145 
0146 void KMoreToolsTestInteractive::testDialogForGroupingNames()
0147 {
0148     // show resulting menu
0149     auto dlg = new QDialog();
0150     auto labelInfo = new QLabel(
0151         "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 "
0152         "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`",
0153         dlg);
0154     labelInfo->setWordWrap(true);
0155     auto selectButton = new QPushButton("Select grouping name...", dlg);
0156     auto labelLineEdit = new QLabel("URL 1 (file://..., https://...", dlg);
0157     auto urlLineEdit = new QLineEdit(dlg);
0158     urlLineEdit->setText("file:///etc/bash.bashrc");
0159     auto menuButton = new QPushButton("<wait for selection>", dlg);
0160 
0161     const auto groupingNamesList = {
0162         "disk-usage",
0163         "disk-partitions",
0164         "files-find",
0165         "font-tools",
0166         "git-clients-for-folder",
0167         "git-clients-and-actions",
0168         "icon-browser",
0169         "language-dictionary",
0170         "mouse-tools",
0171         "screenrecorder",
0172         "screenshot-take",
0173         "system-monitor-processes",
0174         "system-monitor-logs",
0175         "time-countdown",
0176     };
0177 
0178     KMoreToolsMenuFactory menuFactory("unittest-kmoretools/3");
0179 
0180     auto groupingNamesMenu = new QMenu(dlg);
0181     QMenu *moreToolsMenu = nullptr;
0182     for (auto groupingName : groupingNamesList) {
0183         auto action = new QAction(groupingName, groupingNamesMenu);
0184         action->setData(groupingName);
0185         groupingNamesMenu->addAction(action);
0186 
0187         QObject::connect(action,
0188                          &QAction::triggered,
0189                          action,
0190                          [action, &menuFactory, &moreToolsMenu, urlLineEdit, menuButton]() { // clazy:exclude=lambda-in-connect
0191                              auto groupingName = action->data().toString();
0192                              QUrl url;
0193                              if (!urlLineEdit->text().isEmpty()) {
0194                                  url.setUrl(urlLineEdit->text());
0195                              }
0196                              moreToolsMenu = menuFactory.createMenuFromGroupingNames({groupingName}, url);
0197                              menuButton->setText(QString("menu for: '%1' (URL arg: %2...").arg(groupingName, url.isEmpty() ? "<empty>" : "<see URL 1>"));
0198                              menuButton->setMenu(moreToolsMenu);
0199                          });
0200     }
0201 
0202     selectButton->setMenu(groupingNamesMenu);
0203 
0204     auto hLayout = new QHBoxLayout();
0205     auto vLayout = new QVBoxLayout();
0206     hLayout->addWidget(labelInfo);
0207     hLayout->addLayout(vLayout);
0208     vLayout->addWidget(labelLineEdit);
0209     vLayout->addWidget(urlLineEdit);
0210     vLayout->addWidget(selectButton);
0211     vLayout->addWidget(menuButton);
0212     dlg->setLayout(hLayout);
0213     dlg->setBaseSize(300, 150);
0214     QObject::connect(dlg, &QDialog::finished, dlg, [dlg]() {
0215         qDebug() << "delete dlg;";
0216         delete dlg;
0217     });
0218     dlg->exec();
0219 }
0220 
0221 void KMoreToolsTestInteractive::testLazyMenu()
0222 {
0223     KMoreToolsMenuFactory menuFactory("unittest-kmoretools/4");
0224 
0225     auto moreToolsMenu = menuFactory.createMenuFromGroupingNames({"git-clients-for-folder"});
0226 
0227     auto dlg = new QDialog();
0228     auto button = new QPushButton("Test the lazy menu", dlg);
0229     button->setMenu(moreToolsMenu);
0230     auto label =
0231         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);
0232     label->setWordWrap(true);
0233     auto layout = new QHBoxLayout();
0234     layout->addWidget(button);
0235     layout->addWidget(label);
0236     dlg->setLayout(layout);
0237     QObject::connect(dlg, &QDialog::finished, dlg, [dlg]() {
0238         qDebug() << "delete dlg;";
0239         delete dlg;
0240     });
0241     dlg->exec();
0242 }
0243 
0244 QTEST_MAIN(KMoreToolsTestInteractive)
0245 
0246 #include "kmoretoolstest_interactive.moc"