]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kfileitemlistviewtest.cpp
775602f9f899924e54091e797979fafdf75e9348
[dolphin.git] / src / tests / kfileitemlistviewtest.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "kitemviews/kfileitemlistview.h"
21 #include "kitemviews/kfileitemmodel.h"
22 #include "kitemviews/private/kfileitemmodeldirlister.h"
23 #include "testdir.h"
24
25 #include <QGraphicsView>
26 #include <QTest>
27 #include <QSignalSpy>
28
29 class KFileItemListViewTest : public QObject
30 {
31 Q_OBJECT
32
33 private slots:
34 void init();
35 void cleanup();
36 void testGroupedItemChanges();
37
38 private:
39 KFileItemListView* m_listView;
40 KFileItemModel* m_model;
41 TestDir* m_testDir;
42 QGraphicsView* m_graphicsView;
43 };
44
45 void KFileItemListViewTest::init()
46 {
47 qRegisterMetaType<KItemRangeList>("KItemRangeList");
48 qRegisterMetaType<KFileItemList>("KFileItemList");
49
50 m_testDir = new TestDir();
51 m_model = new KFileItemModel();
52 m_model->m_dirLister->setAutoUpdate(false);
53
54 m_listView = new KFileItemListView();
55 m_listView->onModelChanged(m_model, nullptr);
56
57 m_graphicsView = new QGraphicsView();
58 m_graphicsView->show();
59 QVERIFY(QTest::qWaitForWindowExposed(m_graphicsView));
60 }
61
62 void KFileItemListViewTest::cleanup()
63 {
64 delete m_graphicsView;
65 m_graphicsView = nullptr;
66
67 delete m_listView;
68 m_listView = nullptr;
69
70 delete m_model;
71 m_model = nullptr;
72
73 delete m_testDir;
74 m_testDir = nullptr;
75 }
76
77 /**
78 * If grouping is enabled, the group headers must be updated
79 * when items have been inserted or removed. This updating
80 * may only be done after all multiple ranges have been inserted
81 * or removed and not after each individual range (see description
82 * in #ifndef QT_NO_DEBUG-block of KItemListView::slotItemsInserted()
83 * and KItemListView::slotItemsRemoved()). This test inserts and
84 * removes multiple ranges and will trigger the Q_ASSERT in the
85 * ifndef QT_NO_DEBUG-block in case if a group-header will be updated
86 * too early.
87 */
88 void KFileItemListViewTest::testGroupedItemChanges()
89 {
90 QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
91 QVERIFY(itemsInsertedSpy.isValid());
92 QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved);
93 QVERIFY(itemsRemovedSpy.isValid());
94
95 m_model->setGroupedSorting(true);
96
97 m_testDir->createFiles({"1", "3", "5"});
98 m_model->loadDirectory(m_testDir->url());
99 QVERIFY(itemsInsertedSpy.wait());
100 QCOMPARE(m_model->count(), 3);
101
102 m_testDir->createFiles({"2", "4"});
103 m_model->m_dirLister->updateDirectory(m_testDir->url());
104 QVERIFY(itemsInsertedSpy.wait());
105 QCOMPARE(m_model->count(), 5);
106
107 m_testDir->removeFiles({"1", "3", "5"});
108 m_model->m_dirLister->updateDirectory(m_testDir->url());
109 QVERIFY(itemsRemovedSpy.wait());
110 QCOMPARE(m_model->count(), 2);
111 }
112
113 QTEST_MAIN(KFileItemListViewTest)
114
115 #include "kfileitemlistviewtest.moc"