]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinviewtest_allviewmodes.cpp
Encapsulate the creation and handling of the directory lister, the model and proxy...
[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 // TODO: DolphinView::invertSelection() does not work in combination with DolphinView::hasSelection(). Might
87 // be a Qt-issue - further investigations are needed.
88 //m_view->invertSelection();
89 //verifySelectedItemsCount(0);
90 //
91 //m_view->invertSelection();
92 //verifySelectedItemsCount(totalItems);
93
94 m_view->clearSelection();
95 verifySelectedItemsCount(0);
96
97 // Now we select individual items using mouse clicks
98 QModelIndex index = itemView()->model()->index(2, 0);
99 itemView()->scrollTo(index);
100 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView()->visualRect(index).center());
101 verifySelectedItemsCount(1);
102
103 index = itemView()->model()->index(45, 0);
104 itemView()->scrollTo(index);
105 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView()->visualRect(index).center());
106 verifySelectedItemsCount(2);
107
108 index = itemView()->model()->index(48, 0);
109 itemView()->scrollTo(index);
110 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ShiftModifier, itemView()->visualRect(index).center());
111 verifySelectedItemsCount(5);
112 }
113
114 /**
115 * testKeyboardFocus() checks whether a view grabs the keyboard focus.
116 *
117 * A view may never grab the keyboard focus itself and must respect the focus-state
118 * when switching the view mode.
119 */
120
121 void DolphinViewTest_AllViewModes::testKeyboardFocus()
122 {
123 const DolphinView::Mode mode = m_view->mode();
124
125 QVERIFY(!m_view->hasFocus());
126 for (int i = 0; i <= DolphinView::MaxModeEnum; ++i) {
127 m_view->setMode(static_cast<DolphinView::Mode>(i));
128 QVERIFY(!m_view->hasFocus());
129 }
130
131 m_view->setMode(mode);
132 }
133
134 /**
135 * verifySelectedItemsCount(int) waits until the DolphinView's selectionChanged(const KFileItemList&)
136 * signal is received and checks that the selection state of the view is as expected.
137 */
138
139 void DolphinViewTest_AllViewModes::verifySelectedItemsCount(int itemsCount) const
140 {
141 QSignalSpy spySelectionChanged(m_view, SIGNAL(selectionChanged(const KFileItemList&)));
142 QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(selectionChanged(const KFileItemList&)), 500));
143
144 QCOMPARE(m_view->selectedItems().count(), itemsCount);
145 QCOMPARE(m_view->selectedItemsCount(), itemsCount);
146 QCOMPARE(spySelectionChanged.count(), 1);
147 QCOMPARE(qvariant_cast<KFileItemList>(spySelectionChanged.at(0).at(0)).count(), itemsCount);
148 if (itemsCount != 0) {
149 QVERIFY(m_view->hasSelection());
150 }
151 else {
152 if (mode() == DolphinView::ColumnView &&
153 itemView()->selectionModel()->selectedIndexes().count() == 0 &&
154 itemView()->selectionModel()->hasSelection()) {
155 QEXPECT_FAIL("",
156 "The selection model's hasSelection() method returns true, but there are no selected indexes. Needs to be investigated.",
157 Continue);
158 }
159
160 QVERIFY(!m_view->hasSelection());
161 }
162 }
163
164 #include "dolphinviewtest_allviewmodes.moc"