]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinviewtest_allviewmodes.cpp
Make dolphinviewtest_columns more stable.
[dolphin.git] / src / tests / dolphinviewtest_allviewmodes.cpp
1 /*****************************************************************************
2 * Copyright (C) 2010-2011 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 if (mode() == DolphinView::ColumnView) {
41 // In Columns View mode, we need to create a new DolphinView after each
42 // test to make the file-related tests after the first one pass.
43 // TODO: Try to find out if there is a hidden bug in the Columns View that causes this.
44 delete m_view;
45 m_view = new DolphinView(KUrl(m_path), 0);
46 }
47 m_view->setMode(mode());
48 QVERIFY(verifyCorrectViewMode());
49 m_view->resize(200, 300);
50 m_view->show();
51 QTest::qWaitForWindowShown(m_view);
52 }
53
54 void DolphinViewTest_AllViewModes::cleanup() {
55 m_view->hide();
56 cleanupTestDir();
57 }
58
59 /**
60 * testSelection() checks the basic selection funtionality of DolphinView, including:
61 *
62 * selectedItems()
63 * selectedItemsCount()
64 * selectAll()
65 * invertSelection()
66 * clearSelection()
67 * hasSelection()
68 *
69 * and the signal selectionChanged(const KFileItemList& selection)
70 */
71
72 Q_DECLARE_METATYPE(KFileItemList)
73
74 void DolphinViewTest_AllViewModes::testSelection() {
75 const int totalItems = 50;
76
77 for (int i = 0; i < totalItems; i++) {
78 createFile(QString("%1").arg(i));
79 }
80 reloadViewAndWait();
81
82 // Start with an empty selection
83 m_view->clearSelection();
84
85 QCOMPARE(m_view->selectedItems().count(), 0);
86 QCOMPARE(m_view->selectedItemsCount(), 0);
87 QVERIFY(!m_view->hasSelection());
88
89 // First some simple tests where either all or no items are selected
90 m_view->selectAll();
91 verifySelectedItemsCount(totalItems);
92
93 m_view->invertSelection();
94 verifySelectedItemsCount(0);
95
96 m_view->invertSelection();
97 verifySelectedItemsCount(totalItems);
98
99 m_view->clearSelection();
100 verifySelectedItemsCount(0);
101
102 // Now we select individual items using mouse clicks
103 QModelIndex index = itemView()->model()->index(2, 0);
104 itemView()->scrollTo(index);
105 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView()->visualRect(index).center());
106 verifySelectedItemsCount(1);
107
108 index = itemView()->model()->index(45, 0);
109 itemView()->scrollTo(index);
110 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView()->visualRect(index).center());
111 verifySelectedItemsCount(2);
112
113 index = itemView()->model()->index(48, 0);
114 itemView()->scrollTo(index);
115 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ShiftModifier, itemView()->visualRect(index).center());
116 verifySelectedItemsCount(5);
117 }
118
119 /**
120 * Check that setting the directory view properties works.
121 */
122
123 void DolphinViewTest_AllViewModes::testViewPropertySettings()
124 {
125 // Create some files with different sizes and modification times to check the different sorting options
126 QDateTime now = QDateTime::currentDateTime();
127
128 createFile("a", "A file", now.addDays(-3));
129 createFile("b", "A larger file", now.addDays(0));
130 createDir("c", now.addDays(-2));
131 createFile("d", "The largest file in this directory", now.addDays(-1));
132 createFile("e", "An even larger file", now.addDays(-4));
133 createFile(".f");
134
135 reloadViewAndWait();
136
137 // First set all settings to the default.
138 m_view->setSorting(DolphinView::SortByName);
139 QCOMPARE(m_view->sorting(), DolphinView::SortByName);
140
141 m_view->setSortOrder(Qt::AscendingOrder);
142 QCOMPARE(m_view->sortOrder(), Qt::AscendingOrder);
143
144 m_view->setSortFoldersFirst(true);
145 QVERIFY(m_view->sortFoldersFirst());
146
147 m_view->setShowPreview(false);
148 QVERIFY(!m_view->showPreview());
149
150 m_view->setShowHiddenFiles(false);
151 QVERIFY(!m_view->showHiddenFiles());
152
153 /** Check that the sort order is correct for different kinds of settings */
154
155 // Sort by Name, ascending
156 QCOMPARE(m_view->sorting(), DolphinView::SortByName);
157 QCOMPARE(m_view->sortOrder(), Qt::AscendingOrder);
158 QCOMPARE(viewItems(), QStringList() << "c" << "a" << "b" << "d" << "e");
159
160 // Sort by Name, descending
161 m_view->setSortOrder(Qt::DescendingOrder);
162 QCOMPARE(m_view->sorting(), DolphinView::SortByName);
163 QCOMPARE(m_view->sortOrder(), Qt::DescendingOrder);
164 QCOMPARE(viewItems(), QStringList() << "c" << "e" << "d" << "b" << "a");
165
166 // Sort by Size, descending
167 m_view->setSorting(DolphinView::SortBySize);
168 QCOMPARE(m_view->sorting(), DolphinView::SortBySize);
169 QCOMPARE(m_view->sortOrder(), Qt::DescendingOrder);
170 QCOMPARE(viewItems(), QStringList() << "c" << "d" << "e" << "b" << "a");
171
172 // Sort by Size, ascending
173 m_view->setSortOrder(Qt::AscendingOrder);
174 QCOMPARE(m_view->sorting(), DolphinView::SortBySize);
175 QCOMPARE(m_view->sortOrder(), Qt::AscendingOrder);
176 QCOMPARE(viewItems(), QStringList() << "c" << "a" << "b" << "e" << "d");
177
178 // Sort by Date, ascending
179 m_view->setSorting(DolphinView::SortByDate);
180 QCOMPARE(m_view->sorting(), DolphinView::SortByDate);
181 QCOMPARE(m_view->sortOrder(), Qt::AscendingOrder);
182 QCOMPARE(viewItems(), QStringList() << "c" << "e" << "a" << "d" << "b");
183
184 // Sort by Date, descending
185 m_view->setSortOrder(Qt::DescendingOrder);
186 QCOMPARE(m_view->sorting(), DolphinView::SortByDate);
187 QCOMPARE(m_view->sortOrder(), Qt::DescendingOrder);
188 QCOMPARE(viewItems(), QStringList() << "c" << "b" << "d" << "a" << "e");
189
190 // Disable "Sort Folders First"
191 m_view->setSortFoldersFirst(false);
192 QVERIFY(!m_view->sortFoldersFirst());
193 QCOMPARE(viewItems(), QStringList()<< "b" << "d" << "c" << "a" << "e");
194
195 // Try again with Sort by Name, ascending
196 m_view->setSorting(DolphinView::SortByName);
197 m_view->setSortOrder(Qt::AscendingOrder);
198 QCOMPARE(m_view->sorting(), DolphinView::SortByName);
199 QCOMPARE(m_view->sortOrder(), Qt::AscendingOrder);
200 QCOMPARE(viewItems(), QStringList() << "a" << "b" << "c" << "d" << "e");
201
202 // Show hidden files. This triggers the dir lister
203 // -> we have to wait until loading the hidden files is finished
204 m_view->setShowHiddenFiles(true);
205 QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
206 QVERIFY(m_view->showHiddenFiles());
207 QCOMPARE(viewItems(), QStringList() << ".f" << "a" << "b" << "c" << "d" << "e");
208
209 // Previews
210 m_view->setShowPreview(true);
211 QVERIFY(m_view->showPreview());
212
213 // TODO: Check that the view properties are restored correctly when changing the folder and then going back.
214 }
215
216 /**
217 * testKeyboardFocus() checks whether a view grabs the keyboard focus.
218 *
219 * A view may never grab the keyboard focus itself and must respect the focus-state
220 * when switching the view mode, see
221 *
222 * https://bugs.kde.org/show_bug.cgi?id=261147
223 */
224
225 void DolphinViewTest_AllViewModes::testKeyboardFocus()
226 {
227 const DolphinView::Mode mode = m_view->mode();
228
229 // Move keyboard focus to another widget. To see that this is needed, run only this test,
230 // i.e., pass 'testKeyboardFocus' as a parameter on the command line.
231 QWidget widget;
232 widget.show();
233 QTest::qWaitForWindowShown(&widget);
234 widget.setFocus();
235
236 QVERIFY(!m_view->hasFocus());
237
238 // Switch view modes and verify that the view does not get the focus back
239 for (int i = 0; i <= DolphinView::MaxModeEnum; ++i) {
240 m_view->setMode(static_cast<DolphinView::Mode>(i));
241 QVERIFY(!m_view->hasFocus());
242 }
243
244 m_view->setMode(mode);
245 }
246
247 /**
248 * verifySelectedItemsCount(int) waits until the DolphinView's selectionChanged(const KFileItemList&)
249 * signal is received and checks that the selection state of the view is as expected.
250 */
251
252 void DolphinViewTest_AllViewModes::verifySelectedItemsCount(int itemsCount) const
253 {
254 QSignalSpy spySelectionChanged(m_view, SIGNAL(selectionChanged(const KFileItemList&)));
255 QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(selectionChanged(const KFileItemList&)), 500));
256
257 QCOMPARE(m_view->selectedItems().count(), itemsCount);
258 QCOMPARE(m_view->selectedItemsCount(), itemsCount);
259 QCOMPARE(spySelectionChanged.count(), 1);
260 QCOMPARE(qvariant_cast<KFileItemList>(spySelectionChanged.at(0).at(0)).count(), itemsCount);
261 if (itemsCount != 0) {
262 QVERIFY(m_view->hasSelection());
263 }
264 else {
265 QVERIFY(!m_view->hasSelection());
266 }
267 }
268
269 #include "dolphinviewtest_allviewmodes.moc"