File indexing completed on 2024-04-28 05:45:21

0001 /*
0002  * SPDX-FileCopyrightText: 2023 Méven Car <meven@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kitemviews/kfileitemlistview.h"
0008 #include "kitemviews/kfileitemmodel.h"
0009 #include "kitemviews/kitemlistcontainer.h"
0010 #include "kitemviews/kitemlistcontroller.h"
0011 #include "kitemviews/kitemlistselectionmanager.h"
0012 #include "testdir.h"
0013 
0014 #include <QSignalSpy>
0015 #include <QStandardPaths>
0016 #include <QTest>
0017 
0018 class KItemListControllerExpandTest : public QObject
0019 {
0020     Q_OBJECT
0021 
0022 private Q_SLOTS:
0023     void initTestCase();
0024     void cleanupTestCase();
0025 
0026     void init();
0027     void cleanup();
0028 
0029     void testDirExpand();
0030 
0031 private:
0032     KFileItemListView *m_view;
0033     KItemListController *m_controller;
0034     KItemListSelectionManager *m_selectionManager;
0035     KFileItemModel *m_model;
0036     TestDir *m_testDir;
0037     KItemListContainer *m_container;
0038     QSignalSpy *m_spyDirectoryLoadingCompleted;
0039 };
0040 
0041 void KItemListControllerExpandTest::initTestCase()
0042 {
0043     QStandardPaths::setTestModeEnabled(true);
0044     qRegisterMetaType<KItemSet>("KItemSet");
0045 
0046     m_testDir = new TestDir();
0047     m_model = new KFileItemModel();
0048     m_view = new KFileItemListView();
0049     m_controller = new KItemListController(m_model, m_view, this);
0050     m_container = new KItemListContainer(m_controller);
0051 #ifndef QT_NO_ACCESSIBILITY
0052     m_view->setAccessibleParentsObject(m_container);
0053 #endif
0054     m_controller = m_container->controller();
0055     m_controller->setSelectionBehavior(KItemListController::MultiSelection);
0056     m_selectionManager = m_controller->selectionManager();
0057 
0058     QStringList files;
0059     files << "dir1/file1";
0060     files << "dir1/file2";
0061     files << "dir1/file3";
0062     files << "dir1/file4";
0063     files << "dir1/file5";
0064 
0065     files << "dir2/file1";
0066     files << "dir2/file2";
0067     files << "dir2/file3";
0068     files << "dir2/file4";
0069     files << "dir2/file5";
0070 
0071     files << "dir3/file1";
0072     files << "dir3/file2";
0073     files << "dir3/file3";
0074     files << "dir3/file4";
0075     files << "dir3/file5";
0076 
0077     m_testDir->createFiles(files);
0078     m_model->loadDirectory(m_testDir->url());
0079     m_spyDirectoryLoadingCompleted = new QSignalSpy(m_model, &KFileItemModel::directoryLoadingCompleted);
0080     QVERIFY(m_spyDirectoryLoadingCompleted->wait());
0081 
0082     m_container->show();
0083     QVERIFY(QTest::qWaitForWindowExposed(m_container));
0084 }
0085 void KItemListControllerExpandTest::cleanupTestCase()
0086 {
0087     delete m_container;
0088     m_container = nullptr;
0089 
0090     delete m_testDir;
0091     m_testDir = nullptr;
0092 }
0093 
0094 void KItemListControllerExpandTest::init()
0095 {
0096     m_selectionManager->setCurrentItem(0);
0097     QCOMPARE(m_selectionManager->currentItem(), 0);
0098 
0099     m_selectionManager->clearSelection();
0100     QVERIFY(!m_selectionManager->hasSelection());
0101 }
0102 
0103 void KItemListControllerExpandTest::cleanup()
0104 {
0105 }
0106 
0107 void KItemListControllerExpandTest::testDirExpand()
0108 {
0109     m_view->setItemLayout(KFileItemListView::DetailsLayout);
0110     QCOMPARE(m_view->itemLayout(), KFileItemListView::DetailsLayout);
0111     m_view->setSupportsItemExpanding(true);
0112 
0113     // intial state
0114     QCOMPARE(m_spyDirectoryLoadingCompleted->count(), 1);
0115     QCOMPARE(m_model->count(), 3);
0116     QCOMPARE(m_selectionManager->currentItem(), 0);
0117     QCOMPARE(m_selectionManager->selectedItems().count(), 0);
0118 
0119     // extend first folder
0120     QTest::keyClick(m_container, Qt::Key_Right);
0121     QVERIFY(m_spyDirectoryLoadingCompleted->wait());
0122     QCOMPARE(m_model->count(), 8);
0123     QCOMPARE(m_selectionManager->currentItem(), 0);
0124     QCOMPARE(m_selectionManager->selectedItems().count(), 0);
0125 
0126     // collapse folder
0127     QTest::keyClick(m_container, Qt::Key_Left);
0128 
0129     QCOMPARE(m_model->count(), 3);
0130     QCOMPARE(m_selectionManager->currentItem(), 0);
0131     QCOMPARE(m_selectionManager->selectedItems().count(), 0);
0132 
0133     // make the first folder selected
0134     QTest::keyClick(m_container, Qt::Key_Down);
0135     QCOMPARE(m_model->count(), 3);
0136     QCOMPARE(m_selectionManager->currentItem(), 1);
0137     QCOMPARE(m_selectionManager->selectedItems().count(), 1);
0138 
0139     QTest::keyClick(m_container, Qt::Key_Up);
0140     QCOMPARE(m_model->count(), 3);
0141     QCOMPARE(m_selectionManager->currentItem(), 0);
0142     QCOMPARE(m_selectionManager->selectedItems().count(), 1);
0143 
0144     // expand the two first folders
0145     QTest::keyClick(m_container, Qt::Key_Down, Qt::ShiftModifier);
0146     QCOMPARE(m_model->count(), 3);
0147     QCOMPARE(m_selectionManager->currentItem(), 1);
0148     QCOMPARE(m_selectionManager->selectedItems().count(), 2);
0149 
0150     // precondition
0151     QCOMPARE(m_spyDirectoryLoadingCompleted->count(), 2);
0152 
0153     // expand selected folders
0154     QTest::keyClick(m_container, Qt::Key_Right);
0155     QVERIFY(QTest::qWaitFor(
0156         [this]() {
0157             return m_spyDirectoryLoadingCompleted->count() == 3;
0158         },
0159         100));
0160     QCOMPARE(m_model->count(), 8);
0161     QCOMPARE(m_selectionManager->currentItem(), 6);
0162     QCOMPARE(m_selectionManager->selectedItems().count(), 2);
0163 
0164     // collapse the folders
0165     QTest::keyClick(m_container, Qt::Key_Left);
0166     QCOMPARE(m_model->count(), 3);
0167     QCOMPARE(m_selectionManager->currentItem(), 1);
0168     QCOMPARE(m_selectionManager->selectedItems().count(), 2);
0169 
0170     // select third folder
0171     QTest::keyClick(m_container, Qt::Key_Down, Qt::ShiftModifier);
0172     QCOMPARE(m_model->count(), 3);
0173     QCOMPARE(m_selectionManager->currentItem(), 2);
0174     QCOMPARE(m_selectionManager->selectedItems().count(), 3);
0175 
0176     // precondition
0177     QCOMPARE(m_spyDirectoryLoadingCompleted->count(), 3);
0178 
0179     // expand the three folders
0180     QTest::keyClick(m_container, Qt::Key_Right);
0181 
0182     QVERIFY(QTest::qWaitFor(
0183         [this]() {
0184             return m_spyDirectoryLoadingCompleted->count() == 6;
0185         },
0186         300));
0187 
0188     QCOMPARE(m_model->count(), 18);
0189     QCOMPARE(m_selectionManager->currentItem(), 12);
0190     QCOMPARE(m_selectionManager->selectedItems().count(), 3);
0191 
0192     // collapse the folders
0193     QTest::keyClick(m_container, Qt::Key_Left);
0194     QCOMPARE(m_model->count(), 3);
0195     QCOMPARE(m_selectionManager->currentItem(), 2);
0196     QCOMPARE(m_selectionManager->selectedItems().count(), 3);
0197 
0198     // shift select the directories
0199     QTest::keyClick(m_container, Qt::Key_Up);
0200     QCOMPARE(m_model->count(), 3);
0201     QCOMPARE(m_selectionManager->currentItem(), 1);
0202     QCOMPARE(m_selectionManager->selectedItems().count(), 1);
0203 
0204     QTest::keyClick(m_container, Qt::Key_Up);
0205     QCOMPARE(m_model->count(), 3);
0206     QCOMPARE(m_selectionManager->currentItem(), 0);
0207     QCOMPARE(m_selectionManager->selectedItems().count(), 1);
0208 
0209     QTest::keyClick(m_container, Qt::Key_Down, Qt::ShiftModifier);
0210     QTest::keyClick(m_container, Qt::Key_Down, Qt::ShiftModifier);
0211 
0212     QCOMPARE(m_model->count(), 3);
0213     QCOMPARE(m_selectionManager->currentItem(), 2);
0214     QCOMPARE(m_selectionManager->selectedItems().count(), 3);
0215 
0216     // expand the three folders with shift modifier
0217     QTest::keyClick(m_container, Qt::Key_Right, Qt::ShiftModifier);
0218 
0219     QVERIFY(QTest::qWaitFor(
0220         [this]() {
0221             return m_spyDirectoryLoadingCompleted->count() == 9;
0222         },
0223         100));
0224 
0225     QCOMPARE(m_model->count(), 18);
0226     QCOMPARE(m_selectionManager->currentItem(), 12);
0227     QCOMPARE(m_selectionManager->selectedItems().count(), 13);
0228 
0229     // collapse the folders
0230     QTest::keyClick(m_container, Qt::Key_Left);
0231     QCOMPARE(m_model->count(), 3);
0232     QCOMPARE(m_selectionManager->currentItem(), 2);
0233     QCOMPARE(m_selectionManager->selectedItems().count(), 3);
0234 }
0235 
0236 QTEST_MAIN(KItemListControllerExpandTest)
0237 
0238 #include "kitemlistcontrollerexpandtest.moc"