File indexing completed on 2024-04-14 03:51:11

0001 /*
0002     SPDX-FileCopyrightText: 2023 Waqar Ahmed <waqar.17a@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include <QAbstractItemModel>
0008 #include <QAbstractItemView>
0009 #include <QHBoxLayout>
0010 #include <QLineEdit>
0011 #include <QListView>
0012 #include <QMainWindow>
0013 #include <QObject>
0014 #include <QStandardPaths>
0015 #include <QTest>
0016 
0017 #include "kcommandbar.h"
0018 
0019 static void setEnvironment()
0020 {
0021     QStandardPaths::setTestModeEnabled(true);
0022 }
0023 
0024 class KCommandBarTest : public QObject
0025 {
0026     Q_OBJECT
0027 private Q_SLOTS:
0028     void testNoCentralWidget()
0029     {
0030         QMainWindow w;
0031         w.setCentralWidget(nullptr);
0032         w.show();
0033 
0034         KCommandBar b(&w);
0035         b.show();
0036     }
0037 
0038     void testNoMainWindowParent()
0039     {
0040         QMainWindow w;
0041         auto central = new QWidget(&w);
0042         w.setCentralWidget(central);
0043         auto l = new QHBoxLayout(central);
0044         auto lv = new QListView();
0045         l->addWidget(lv);
0046         l->addWidget(new QWidget(), 2);
0047         w.showMaximized();
0048         // QTest::qWaitForWindowExposed(&w);
0049 
0050         KCommandBar b(lv);
0051         b.show();
0052         // QTest::qWait(100000);
0053     }
0054 
0055     void testLastUsedActionRestored()
0056     {
0057         QMainWindow w;
0058         auto central = new QWidget(&w);
0059         w.setCentralWidget(central);
0060 
0061         KCommandBar::ActionGroup ag;
0062         ag.name = QStringLiteral("Group1");
0063         QAction *a = new QAction(QStringLiteral("Act1"), &w);
0064         a->setObjectName("act1");
0065         ag.actions << a;
0066         a = new QAction(QStringLiteral("Act2"), &w);
0067         a->setObjectName("act2");
0068         ag.actions << a;
0069         {
0070             KCommandBar b(&w);
0071             b.setActions({ag});
0072 
0073             auto treeView = b.findChild<QAbstractItemView *>();
0074             auto lineEdit = b.findChild<QLineEdit *>();
0075             QVERIFY(treeView);
0076             QVERIFY(lineEdit);
0077             QVERIFY(treeView->model());
0078             QCOMPARE(treeView->model()->rowCount(), 3);
0079             QCOMPARE(treeView->model()->index(1, 0).data().toString(), QStringLiteral("Group1: %1").arg(ag.actions[0]->text()));
0080             QCOMPARE(treeView->model()->index(2, 0).data().toString(), QStringLiteral("Group1: %1").arg(ag.actions[1]->text()));
0081 
0082             QTest::sendKeyEvent(QTest::KeyAction::Press, treeView, Qt::Key_Down, {}, Qt::NoModifier);
0083             QTest::sendKeyEvent(QTest::KeyAction::Press, treeView, Qt::Key_Down, {}, Qt::NoModifier);
0084             QCOMPARE(treeView->currentIndex().data().toString(), QStringLiteral("Group1: Act2"));
0085 
0086             lineEdit->returnPressed();
0087         }
0088 
0089         {
0090             KCommandBar b(&w);
0091             b.setActions({ag});
0092 
0093             auto treeView = b.findChild<QAbstractItemView *>();
0094             QCOMPARE(treeView->model()->rowCount(), 3);
0095             // Act2 is at the top now
0096             QCOMPARE(treeView->model()->index(0, 0).data().toString(), QStringLiteral("Group1: %1").arg(ag.actions[1]->text()));
0097             // Act1 is at the end
0098             QCOMPARE(treeView->model()->index(2, 0).data().toString(), QStringLiteral("Group1: %1").arg(ag.actions[0]->text()));
0099         }
0100     }
0101 };
0102 
0103 Q_COREAPP_STARTUP_FUNCTION(setEnvironment)
0104 
0105 QTEST_MAIN(KCommandBarTest)
0106 
0107 #include "kcmdbartest.moc"