~ubuntu-branches/ubuntu/trusty/kdebase/trusty

« back to all changes in this revision

Viewing changes to apps/dolphin/src/tests/dolphindetailsviewtest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-12-07 12:42:37 UTC
  • mfrom: (1.1.54 upstream)
  • Revision ID: james.westby@ubuntu.com-20101207124237-w47k8kdpkkfs6p5v
Tags: 4:4.5.85-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include "views/dolphindetailsview.h"
25
25
#include "views/dolphinview.h"
26
26
#include "views/dolphinmodel.h"
27
 
#include "views/dolphindirlister.h"
28
27
#include "views/dolphinsortfilterproxymodel.h"
29
28
#include "views/zoomlevelinfo.h"
30
29
 
31
 
#include <KTempDir>
32
 
 
33
 
#include <QtCore/QDir>
34
30
#include <qtestmouse.h>
35
31
#include <qtestkeyboard.h>
36
32
 
40
36
 
41
37
private slots:
42
38
 
 
39
    void init() {
 
40
        m_view->setMode(DolphinView::DetailsView);
 
41
        m_detailsView = qobject_cast<DolphinDetailsView*>(itemView());
 
42
        QVERIFY(m_detailsView);
 
43
        m_detailsView->setFoldersExpandable(true);
 
44
        m_view->resize(400, 400);
 
45
        m_view->show();
 
46
        QTest::qWaitForWindowShown(m_view);
 
47
    }
 
48
 
 
49
    void cleanup() {
 
50
        m_view->hide();
 
51
        cleanupTestDir();
 
52
    }
 
53
 
43
54
    void testExpandedUrls();
44
55
 
45
56
    void bug217447_shiftArrowSelection();
46
57
    void bug234600_overlappingIconsWhenZooming();
 
58
    void bug257401_longFilenamesKeyboardNavigation();
47
59
 
48
60
private:
49
61
 
51
63
        const QModelIndex index = m_dolphinModel->indexForUrl(url);
52
64
        return m_proxyModel->mapFromSource(index);
53
65
    }
 
66
 
 
67
    DolphinDetailsView* m_detailsView;
54
68
};
55
69
 
56
70
/**
57
71
 * This test verifies that DolphinDetailsView::expandedUrls() returns the right set of URLs.
58
 
 * The test creates a folder hierarchy: 3 folders (a, b, c) contain 3 subfolders (also names a, b, c) each.
 
72
 * The test creates a folder hierarchy: 3 folders (a, b, c) contain 3 subfolders (also named a, b, c) each.
59
73
 * Each of those contains 3 further subfolders of the same name.
60
74
 */
61
75
 
62
76
void DolphinDetailsViewTest::testExpandedUrls()
63
77
{
 
78
    m_detailsView->setFoldersExpandable(true);
 
79
 
64
80
    QStringList files;
65
81
    QStringList subFolderNames;
66
82
    subFolderNames << "a" << "b" << "c";
74
90
    }
75
91
 
76
92
    createFiles(files);
77
 
 
78
 
    m_view->setMode(DolphinView::DetailsView);
79
 
    DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView());
80
 
    QVERIFY(detailsView);
81
 
    detailsView->setFoldersExpandable(true);
82
 
    m_view->resize(400, 400);
83
 
    m_view->show();
84
 
    QTest::qWaitForWindowShown(m_view);
85
93
    reloadViewAndWait();
86
94
 
87
95
    // We start with an empty set of expanded URLs.
88
96
    QSet<KUrl> expectedExpandedUrls;
89
 
    QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
 
97
    QCOMPARE(m_detailsView->expandedUrls(), expectedExpandedUrls);
90
98
 
91
99
    // Every time we expand a folder, we have to wait until the view has finished loading
92
100
    // its contents before we can expand further subfolders. We keep track of the reloading
99
107
 
100
108
    foreach(const QString& item, itemsToExpand) {
101
109
        KUrl url(m_path + item);
102
 
        detailsView->expand(proxyModelIndexForUrl(url));
 
110
        m_detailsView->expand(proxyModelIndexForUrl(url));
103
111
        expectedExpandedUrls += url;
104
 
        QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
 
112
        QCOMPARE(m_detailsView->expandedUrls(), expectedExpandedUrls);
105
113
 
106
114
        // Before we proceed, we have to make sure that the view has finished
107
115
        // loading the contents of the expanded folder.
117
125
 
118
126
    foreach(const QString& item, itemsToCollapse) {
119
127
        KUrl url(m_path + item);
120
 
        detailsView->collapse(proxyModelIndexForUrl(url));
 
128
        m_detailsView->collapse(proxyModelIndexForUrl(url));
121
129
        expectedExpandedUrls -= url;
122
 
        QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
 
130
        QCOMPARE(m_detailsView->expandedUrls(), expectedExpandedUrls);
123
131
    }
124
 
 
125
 
    m_view->hide();
126
 
    cleanupTestDir();
127
132
}
128
133
 
129
134
/**
151
156
    for (int i = 0; i < 100; i++) {
152
157
        createFile(QString("%1").arg(i));
153
158
    }
154
 
 
155
 
    m_view->setMode(DolphinView::DetailsView);
156
 
    DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView());
157
 
    QVERIFY(detailsView);
158
 
    m_view->resize(1000, 400);
159
 
    m_view->show();
160
 
    QTest::qWaitForWindowShown(m_view);
161
159
    reloadViewAndWait();
162
160
 
163
161
    // Select the first item
164
 
    QModelIndex index0 = detailsView->model()->index(0, 0);
165
 
    detailsView->setCurrentIndex(index0);
166
 
    QCOMPARE(detailsView->currentIndex(), index0);
 
162
    QModelIndex index0 = m_detailsView->model()->index(0, 0);
 
163
    m_detailsView->setCurrentIndex(index0);
 
164
    QCOMPARE(m_detailsView->currentIndex(), index0);
167
165
 
168
166
    // Before we test Shift-selection, we verify that the root cause is fixed a bit more
169
167
    // directly: we check that passing the corners or the center of an item's visualRect
170
168
    // to itemAt() returns the item (and not an invalid model index).
171
 
    QRect rect = detailsView->visualRect(index0);
172
 
    QCOMPARE(detailsView->indexAt(rect.center()), index0);
173
 
    QCOMPARE(detailsView->indexAt(rect.topLeft()), index0);
174
 
    QCOMPARE(detailsView->indexAt(rect.topRight()), index0);
175
 
    QCOMPARE(detailsView->indexAt(rect.bottomLeft()), index0);
176
 
    QCOMPARE(detailsView->indexAt(rect.bottomRight()), index0);
 
169
    QRect rect = m_detailsView->visualRect(index0);
 
170
    QCOMPARE(m_detailsView->indexAt(rect.center()), index0);
 
171
    QCOMPARE(m_detailsView->indexAt(rect.topLeft()), index0);
 
172
    QCOMPARE(m_detailsView->indexAt(rect.topRight()), index0);
 
173
    QCOMPARE(m_detailsView->indexAt(rect.bottomLeft()), index0);
 
174
    QCOMPARE(m_detailsView->indexAt(rect.bottomRight()), index0);
177
175
 
178
176
    // Another way to test this is to Ctrl-click the center of the visualRect.
179
177
    // The selection state of the item should be toggled.
180
 
    detailsView->clearSelection();
181
 
    QItemSelectionModel* selectionModel = detailsView->selectionModel();
 
178
    m_detailsView->clearSelection();
 
179
    QItemSelectionModel* selectionModel = m_detailsView->selectionModel();
182
180
    QCOMPARE(selectionModel->selectedIndexes().count(), 0);
183
181
 
184
 
    QTest::mouseClick(detailsView->viewport(), Qt::LeftButton, Qt::ControlModifier, rect.center());
 
182
    QTest::mouseClick(m_detailsView->viewport(), Qt::LeftButton, Qt::ControlModifier, rect.center());
185
183
    QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
186
184
    QCOMPARE(selectedIndexes.count(), 1);
187
185
    QVERIFY(selectedIndexes.contains(index0));
192
190
    int current = 1;
193
191
 
194
192
    while (current < 100) {
195
 
        QTest::keyClick(detailsView->viewport(), Qt::Key_Down, Qt::ShiftModifier);
196
 
        QModelIndex currentIndex = detailsView->model()->index(current, 0);
197
 
        QCOMPARE(detailsView->currentIndex(), currentIndex);
 
193
        QTest::keyClick(m_detailsView->viewport(), Qt::Key_Down, Qt::ShiftModifier);
 
194
        QModelIndex currentIndex = m_detailsView->model()->index(current, 0);
 
195
        QCOMPARE(m_detailsView->currentIndex(), currentIndex);
198
196
 
199
197
        selectedIndexes = selectionModel->selectedIndexes();
200
198
        QCOMPARE(selectedIndexes.count(), current + 1);
202
200
 
203
201
        current++;
204
202
    }
205
 
 
206
 
    m_view->hide();
207
 
    cleanupTestDir();
208
203
}
209
204
 
210
205
/**
220
215
{
221
216
    QStringList files;
222
217
    files << "a" << "b" << "c" << "d";
 
218
 
223
219
    createFiles(files);
224
 
 
225
 
    m_view->setMode(DolphinView::DetailsView);
226
 
    DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView());
227
 
    QVERIFY(detailsView);
228
 
    m_view->resize(400, 400);
229
 
    m_view->show();
230
 
    QTest::qWaitForWindowShown(m_view);
231
220
    reloadViewAndWait();
232
221
 
233
 
    QModelIndex index0 = detailsView->model()->index(0, 0);
234
 
    detailsView->setCurrentIndex(index0);
235
 
    QCOMPARE(detailsView->currentIndex(), index0);
 
222
    QModelIndex index0 = m_detailsView->model()->index(0, 0);
 
223
    m_detailsView->setCurrentIndex(index0);
 
224
    QCOMPARE(m_detailsView->currentIndex(), index0);
236
225
 
237
226
    // Setting the zoom level to the minimum value and triggering DolphinDetailsView::currentChanged(...)
238
227
    // should make sure that the bug is triggered.
 
228
    int zoomLevelBackup = m_view->zoomLevel();
239
229
    int zoomLevel = ZoomLevelInfo::minimumLevel();
240
230
    m_view->setZoomLevel(zoomLevel);
241
231
 
242
 
    QModelIndex index1 = detailsView->model()->index(1, 0);
243
 
    detailsView->setCurrentIndex(index1);
244
 
    QCOMPARE(detailsView->currentIndex(), index1);
 
232
    QModelIndex index1 = m_detailsView->model()->index(1, 0);
 
233
    m_detailsView->setCurrentIndex(index1);
 
234
    QCOMPARE(m_detailsView->currentIndex(), index1);
245
235
 
246
236
    // Increase the zoom level successively to the maximum.
247
237
    while(zoomLevel < ZoomLevelInfo::maximumLevel()) {
248
238
        zoomLevel++;
249
239
        m_view->setZoomLevel(zoomLevel);
 
240
        QCOMPARE(m_view->zoomLevel(), zoomLevel);
250
241
 
251
242
        //Check for each zoom level that the height of each item is at least the icon size.
252
 
        QVERIFY(detailsView->visualRect(index1).height() >= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel));
253
 
    }
254
 
 
255
 
    m_view->hide();
256
 
    cleanupTestDir();
 
243
        QVERIFY(m_detailsView->visualRect(index1).height() >= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel));
 
244
    }
 
245
 
 
246
    m_view->setZoomLevel(zoomLevelBackup);
 
247
}
 
248
 
 
249
/**
 
250
 * The width of the visualRect of an item is usually replaced by the width of the file name.
 
251
 * However, if the file name is wider then the view's name column, this leads to problems with
 
252
 * keyboard navigation if files with very long names are present in the current folder, see
 
253
 * 
 
254
 * https://bugs.kde.org/show_bug.cgi?id=257401
 
255
 *
 
256
 * This test checks that the visualRect of an item is never wider than the "Name" column.
 
257
 */
 
258
 
 
259
void DolphinDetailsViewTest::bug257401_longFilenamesKeyboardNavigation() {
 
260
    QString name;
 
261
    for (int i = 0; i < 20; i++) {
 
262
        name += "mmmmmmmmmm";
 
263
        createFile(name);
 
264
    }
 
265
    reloadViewAndWait();
 
266
 
 
267
    // Select the first item
 
268
    QModelIndex index0 = m_detailsView->model()->index(0, 0);
 
269
    m_detailsView->setCurrentIndex(index0);
 
270
    QCOMPARE(m_detailsView->currentIndex(), index0);
 
271
    QVERIFY(m_detailsView->visualRect(index0).width() < m_detailsView->columnWidth(DolphinModel::Name));
 
272
 
 
273
    QItemSelectionModel* selectionModel = m_detailsView->selectionModel();
 
274
    QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
 
275
    QCOMPARE(selectedIndexes.count(), 1);
 
276
    QVERIFY(selectedIndexes.contains(index0));
 
277
 
 
278
    // Move down successively using the "Down" key and check that current item
 
279
    // and selection are as expected.
 
280
    for (int i = 0; i < 19; i++) {
 
281
        QTest::keyClick(m_detailsView->viewport(), Qt::Key_Down, Qt::NoModifier);
 
282
        QModelIndex currentIndex = m_detailsView->model()->index(i + 1, 0);
 
283
        QCOMPARE(m_detailsView->currentIndex(), currentIndex);
 
284
        QVERIFY(m_detailsView->visualRect(currentIndex).width() <= m_detailsView->columnWidth(DolphinModel::Name));
 
285
        selectedIndexes = selectionModel->selectedIndexes();
 
286
        QCOMPARE(selectedIndexes.count(), 1);
 
287
        QVERIFY(selectedIndexes.contains(currentIndex));
 
288
    }
257
289
}
258
290
 
259
291
QTEST_KDEMAIN(DolphinDetailsViewTest, GUI)
260
292
 
261
 
#include "dolphindetailsviewtest.moc"
 
 
b'\\ No newline at end of file'
 
293
#include "dolphindetailsviewtest.moc"