File: kmoretoolstest_interactive.cpp

package info (click to toggle)
knewstuff 5.116.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 29,148 kB
  • sloc: cpp: 15,991; xml: 132; sh: 65; makefile: 11
file content (249 lines) | stat: -rw-r--r-- 9,440 bytes parent folder | download | duplicates (2)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
    SPDX-FileCopyrightText: 2014-2015 Gregor Mi <codestruct@posteo.org>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include <../src/kmoretools/kmoretools.h>
#include <../src/kmoretools/kmoretools_p.h>
#include <../src/kmoretools/kmoretoolsmenufactory.h>
#include <../src/kmoretools/kmoretoolspresets.h>

#include <QDialog>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QTest>

/**
 * Each test case starts a test GUI.
 * Run kmoretoolstest_interactive with the test case name as first parameter
 * (e.g. testDialogForGroupingNames) to run only this test GUI.
 */
class KMoreToolsTestInteractive : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void init();
    void cleanup();

    void testConfigDialogAllInstalled();
    void testConfigDialogSomeNotInstalled();
    void testConfigDialogNotInstalled1Service2Items();

    void test_buildMenu_WithQActions_interative1();

    void testDialogForGroupingNames();

    void testLazyMenu();

private:
    void testConfigDialogImpl(bool withNotInstalled, bool withMultipleItemsPerNotInstalledService, const QString &description);
};

void KMoreToolsTestInteractive::init()
{
}

void KMoreToolsTestInteractive::cleanup()
{
}

bool menuAtLeastOneActionWithText(const QMenu *menu, const QString &text)
{
    const auto lstActions = menu->actions();
    for (auto a : lstActions) {
        if (a->text() == text) {
            return true;
        }
    }

    return false;
}

void KMoreToolsTestInteractive::test_buildMenu_WithQActions_interative1()
{
    KMoreTools kmt(_("unittest-kmoretools/qactions")); // todo: disable copy-ctor!?

    auto f = [&kmt](QString title) { // NOTE: capture by reference! see https://en.wikipedia.org/wiki/Anonymous_function
        const auto menuBuilder = kmt.menuBuilder();
        menuBuilder->clear();
        QMenu menu;
        menuBuilder->addMenuItem(new QAction(_("Hallo 1"), &menu), _("id1"));
        menuBuilder->addMenuItem(new QAction(_("Hallo 2"), &menu), _("id2"));
        menuBuilder->addMenuItem(new QAction(_("Hallo 3"), &menu), _("id3"));

        menuBuilder->buildByAppendingToMenu(&menu);
        menuBuilder->showConfigDialog(title);
    };

    f(_("test_buildMenu_WithQActions 1"));
}

void KMoreToolsTestInteractive::testConfigDialogImpl(bool withNotInstalled, bool withMultipleItemsPerNotInstalledService, const QString &description)
{
    KMoreTools kmt(_("unittest-kmoretools/2"));
    const auto kateApp = kmt.registerServiceByDesktopEntryName(_("org.kde.kate"));
    const auto gitgApp = kmt.registerServiceByDesktopEntryName(_("gitg"));
    const auto notinstApp = kmt.registerServiceByDesktopEntryName(_("mynotinstalledapp"));
    const auto notinstApp2 = kmt.registerServiceByDesktopEntryName(_("mynotinstapp2"));
    notinstApp2->setHomepageUrl(QUrl(_("https://www.kde.org")));
    const auto menuBuilder = kmt.menuBuilder();
    menuBuilder->addMenuItem(kateApp);
    menuBuilder->addMenuItem(gitgApp);
    if (withNotInstalled) {
        auto item1 = menuBuilder->addMenuItem(notinstApp);
        item1->setInitialItemText(notinstApp->formatString(_("$Name - item 1")));

        menuBuilder->addMenuItem(notinstApp2);

        if (withMultipleItemsPerNotInstalledService) {
            auto item3 = menuBuilder->addMenuItem(notinstApp);
            item3->setInitialItemText(notinstApp->formatString(_("$Name - second item")));
        }
    }
    auto i1 = menuBuilder->addMenuItem(kateApp, KMoreTools::MenuSection_More);
    i1->setId(_("kate1"));
    i1->setInitialItemText(_("Kate more"));
    auto i2 = menuBuilder->addMenuItem(gitgApp, KMoreTools::MenuSection_More);
    i2->setId(_("gitg1"));
    i2->setInitialItemText(_("gitg more"));
    menuBuilder->showConfigDialog(description);

    // show resulting menu
    auto dlg = new QDialog();
    auto button = new QPushButton(_("Test the menu"), dlg);
    auto menu = new QMenu(dlg);
    menuBuilder->buildByAppendingToMenu(menu);
    button->setMenu(menu); // TODO: connect to the button click signal to always rebuild the menu
    auto label =
        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);
    label->setWordWrap(true);
    auto layout = new QHBoxLayout();
    layout->addWidget(button);
    layout->addWidget(label);
    dlg->setLayout(layout);
    QObject::connect(dlg, &QDialog::finished, dlg, [dlg]() {
        qDebug() << "delete dlg;";
        delete dlg;
    });
    dlg->exec();
}

void KMoreToolsTestInteractive::testConfigDialogAllInstalled()
{
    testConfigDialogImpl(false, false, _("TEST all installed"));
}

void KMoreToolsTestInteractive::testConfigDialogSomeNotInstalled()
{
    testConfigDialogImpl(true, false, _("TEST some not installed"));
}

void KMoreToolsTestInteractive::testConfigDialogNotInstalled1Service2Items()
{
    testConfigDialogImpl(true, true, _("TEST more than one item for one not installed service"));
}

void KMoreToolsTestInteractive::testDialogForGroupingNames()
{
    // show resulting menu
    auto dlg = new QDialog();
    auto labelInfo = new QLabel(
        _("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 "
          "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`"),
        dlg);
    labelInfo->setWordWrap(true);
    auto selectButton = new QPushButton(_("Select grouping name..."), dlg);
    auto labelLineEdit = new QLabel(_("URL 1 (file://..., https://...)"), dlg);
    auto urlLineEdit = new QLineEdit(dlg);
    urlLineEdit->setText(_("file:///etc/bash.bashrc"));
    auto menuButton = new QPushButton(_("<wait for selection>"), dlg);

    const auto groupingNamesList = {_("disk-usage"),
                                    _("disk-partitions"),
                                    _("files-find"),
                                    _("font-tools"),
                                    _("git-clients-for-folder"),
                                    _("git-clients-and-actions"),
                                    _("icon-browser"),
                                    _("language-dictionary"),
                                    _("mouse-tools"),
                                    _("screenrecorder"),
                                    _("screenshot-take"),
                                    _("system-monitor-processes"),
                                    _("system-monitor-logs"),
                                    _("time-countdown")};

    KMoreToolsMenuFactory menuFactory(_("unittest-kmoretools/3"));

    auto groupingNamesMenu = new QMenu(dlg);
    QMenu *moreToolsMenu = nullptr;
    for (auto groupingName : groupingNamesList) {
        auto action = new QAction(groupingName, groupingNamesMenu);
        action->setData(groupingName);
        groupingNamesMenu->addAction(action);

        QObject::connect(action,
                         &QAction::triggered,
                         action,
                         [action, &menuFactory, &moreToolsMenu, urlLineEdit, menuButton]() { // clazy:exclude=lambda-in-connect
                             auto groupingName = action->data().toString();
                             QUrl url;
                             if (!urlLineEdit->text().isEmpty()) {
                                 url.setUrl(urlLineEdit->text());
                             }
                             moreToolsMenu = menuFactory.createMenuFromGroupingNames({groupingName}, url);
                             menuButton->setText(
                                 QString(_("menu for: '%1' (URL arg: %2)...")).arg(groupingName, url.isEmpty() ? _("<empty>") : _("<see URL 1>")));
                             menuButton->setMenu(moreToolsMenu);
                         });
    }

    selectButton->setMenu(groupingNamesMenu);

    auto hLayout = new QHBoxLayout();
    auto vLayout = new QVBoxLayout();
    hLayout->addWidget(labelInfo);
    hLayout->addLayout(vLayout);
    vLayout->addWidget(labelLineEdit);
    vLayout->addWidget(urlLineEdit);
    vLayout->addWidget(selectButton);
    vLayout->addWidget(menuButton);
    dlg->setLayout(hLayout);
    dlg->setBaseSize(300, 150);
    QObject::connect(dlg, &QDialog::finished, dlg, [dlg]() {
        qDebug() << "delete dlg;";
        delete dlg;
    });
    dlg->exec();
}

void KMoreToolsTestInteractive::testLazyMenu()
{
    KMoreToolsMenuFactory menuFactory(_("unittest-kmoretools/4"));

    auto moreToolsMenu = menuFactory.createMenuFromGroupingNames({_("git-clients-for-folder")});

    auto dlg = new QDialog();
    auto button = new QPushButton(_("Test the lazy menu"), dlg);
    button->setMenu(moreToolsMenu);
    auto label =
        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);
    label->setWordWrap(true);
    auto layout = new QHBoxLayout();
    layout->addWidget(button);
    layout->addWidget(label);
    dlg->setLayout(layout);
    QObject::connect(dlg, &QDialog::finished, dlg, [dlg]() {
        qDebug() << "delete dlg;";
        delete dlg;
    });
    dlg->exec();
}

QTEST_MAIN(KMoreToolsTestInteractive)

#include "kmoretoolstest_interactive.moc"