]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinviewtest_allviewmodes.cpp
Add a unit test for the basic selection-related functionality of
[dolphin.git] / src / tests / dolphinviewtest_allviewmodes.cpp
1 /***************************************************************************
2 * Copyright (C) 2010 by Frank Reininghaus (frank78ac@googlemail.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 "dolphinviewtest_allviewmodes.h"
21
22 #include <qtest_kde.h>
23
24 #include "testbase.h"
25
26 #include "views/dolphinview.h"
27 #include "views/dolphinmodel.h"
28 #include "views/dolphindirlister.h"
29 #include "views/dolphinsortfilterproxymodel.h"
30
31 #include <qtestmouse.h>
32 #include <qtestkeyboard.h>
33
34 DolphinViewTest_AllViewModes::DolphinViewTest_AllViewModes() {
35 // Need to register KFileItemList for use with QSignalSpy
36 qRegisterMetaType<KFileItemList>("KFileItemList");
37 }
38
39 void DolphinViewTest_AllViewModes::init() {
40 m_view->setMode(mode());
41 QVERIFY(verifyCorrectViewMode());
42 m_view->resize(200, 300);
43 m_view->show();
44 QTest::qWaitForWindowShown(m_view);
45 }
46
47 void DolphinViewTest_AllViewModes::cleanup() {
48 m_view->hide();
49 cleanupTestDir();
50 }
51
52 /**
53 * testSelection() checks the basic selection funtionality of DolphinView, including:
54 *
55 * selectedItems()
56 * selectedItemsCount()
57 * selectAll()
58 * invertSelection()
59 * clearSelection()
60 * hasSelection()
61 *
62 * and the signal selectionChanged(const KFileItemList& selection)
63 */
64
65 Q_DECLARE_METATYPE(KFileItemList)
66
67 void DolphinViewTest_AllViewModes::testSelection() {
68 const int totalItems = 50;
69
70 for (int i = 0; i < totalItems; i++) {
71 createFile(QString("%1").arg(i));
72 }
73 reloadViewAndWait();
74
75 // Start with an empty selection
76 m_view->clearSelection();
77
78 QCOMPARE(m_view->selectedItems().count(), 0);
79 QCOMPARE(m_view->selectedItemsCount(), 0);
80 QVERIFY(!m_view->hasSelection());
81
82 // First some simple tests where either all or no items are selected
83 m_view->selectAll();
84 verifySelectedItemsCount(totalItems);
85
86 m_view->invertSelection();
87 verifySelectedItemsCount(0);
88
89 m_view->invertSelection();
90 verifySelectedItemsCount(totalItems);
91
92 m_view->clearSelection();
93 verifySelectedItemsCount(0);
94
95 // Now we select individual items using mouse clicks
96 QModelIndex index = itemView()->model()->index(2, 0);
97 itemView()->scrollTo(index);
98 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView()->visualRect(index).center());
99 verifySelectedItemsCount(1);
100
101 index = itemView()->model()->index(45, 0);
102 itemView()->scrollTo(index);
103 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView()->visualRect(index).center());
104 verifySelectedItemsCount(2);
105
106 index = itemView()->model()->index(48, 0);
107 itemView()->scrollTo(index);
108 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ShiftModifier, itemView()->visualRect(index).center());
109 verifySelectedItemsCount(5);
110 }
111
112 /**
113 * verifySelectedItemsCount(int) waits until the DolphinView's selectionChanged(const KFileItemList&)
114 * signal is received and checks that the selection state of the view is as expected.
115 */
116
117 void DolphinViewTest_AllViewModes::verifySelectedItemsCount(int itemsCount) const
118 {
119 QSignalSpy spySelectionChanged(m_view, SIGNAL(selectionChanged(const KFileItemList&)));
120 QTest::kWaitForSignal(m_view, SIGNAL(selectionChanged(const KFileItemList&)));
121
122 QCOMPARE(m_view->selectedItems().count(), itemsCount);
123 QCOMPARE(m_view->selectedItemsCount(), itemsCount);
124 QCOMPARE(spySelectionChanged.count(), 1);
125 QCOMPARE(qvariant_cast<KFileItemList>(spySelectionChanged.at(0).at(0)).count(), itemsCount);
126 if (itemsCount != 0) {
127 QVERIFY(m_view->hasSelection());
128 }
129 else {
130 if (mode() == DolphinView::ColumnView &&
131 itemView()->selectionModel()->selectedIndexes().count() == 0 &&
132 itemView()->selectionModel()->hasSelection()) {
133 QEXPECT_FAIL("",
134 "The selection model's hasSelection() method returns true, but there are no selected indexes. Needs to be investigated.",
135 Continue);
136 }
137
138 QVERIFY(!m_view->hasSelection());
139 }
140 }
141
142 #include "dolphinviewtest_allviewmodes.moc"