File: kcmdbartest.cpp

package info (click to toggle)
kf6-kconfigwidgets 6.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 24,020 kB
  • sloc: cpp: 6,309; sh: 18; makefile: 7
file content (107 lines) | stat: -rw-r--r-- 3,231 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
/*
    SPDX-FileCopyrightText: 2023 Waqar Ahmed <waqar.17a@gmail.com>

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

#include <QAbstractItemModel>
#include <QAbstractItemView>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QListView>
#include <QMainWindow>
#include <QObject>
#include <QStandardPaths>
#include <QTest>

#include "kcommandbar.h"

static void setEnvironment()
{
    QStandardPaths::setTestModeEnabled(true);
}

class KCommandBarTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void testNoCentralWidget()
    {
        QMainWindow w;
        w.setCentralWidget(nullptr);
        w.show();

        KCommandBar b(&w);
        b.show();
    }

    void testNoMainWindowParent()
    {
        QMainWindow w;
        auto central = new QWidget(&w);
        w.setCentralWidget(central);
        auto l = new QHBoxLayout(central);
        auto lv = new QListView();
        l->addWidget(lv);
        l->addWidget(new QWidget(), 2);
        w.showMaximized();
        // QTest::qWaitForWindowExposed(&w);

        KCommandBar b(lv);
        b.show();
        // QTest::qWait(100000);
    }

    void testLastUsedActionRestored()
    {
        QMainWindow w;
        auto central = new QWidget(&w);
        w.setCentralWidget(central);

        KCommandBar::ActionGroup ag;
        ag.name = QStringLiteral("Group1");
        QAction *a = new QAction(QStringLiteral("Act1"), &w);
        a->setObjectName("act1");
        ag.actions << a;
        a = new QAction(QStringLiteral("Act2"), &w);
        a->setObjectName("act2");
        ag.actions << a;
        {
            KCommandBar b(&w);
            b.setActions({ag});

            auto treeView = b.findChild<QAbstractItemView *>();
            auto lineEdit = b.findChild<QLineEdit *>();
            QVERIFY(treeView);
            QVERIFY(lineEdit);
            QVERIFY(treeView->model());
            QCOMPARE(treeView->model()->rowCount(), 3);
            QCOMPARE(treeView->model()->index(1, 0).data().toString(), QStringLiteral("Group1: %1").arg(ag.actions[0]->text()));
            QCOMPARE(treeView->model()->index(2, 0).data().toString(), QStringLiteral("Group1: %1").arg(ag.actions[1]->text()));

            QTest::sendKeyEvent(QTest::KeyAction::Press, treeView, Qt::Key_Down, {}, Qt::NoModifier);
            QTest::sendKeyEvent(QTest::KeyAction::Press, treeView, Qt::Key_Down, {}, Qt::NoModifier);
            QCOMPARE(treeView->currentIndex().data().toString(), QStringLiteral("Group1: Act2"));

            lineEdit->returnPressed();
        }

        {
            KCommandBar b(&w);
            b.setActions({ag});

            auto treeView = b.findChild<QAbstractItemView *>();
            QCOMPARE(treeView->model()->rowCount(), 3);
            // Act2 is at the top now
            QCOMPARE(treeView->model()->index(0, 0).data().toString(), QStringLiteral("Group1: %1").arg(ag.actions[1]->text()));
            // Act1 is at the end
            QCOMPARE(treeView->model()->index(2, 0).data().toString(), QStringLiteral("Group1: %1").arg(ag.actions[0]->text()));
        }
    }
};

Q_COREAPP_STARTUP_FUNCTION(setEnvironment)

QTEST_MAIN(KCommandBarTest)

#include "kcmdbartest.moc"