┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/tests/dolphinviewtest_allviewmodes.cpp
blob: 710ed9d6598af204e1470a5fd145dc40e71e5c70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/***************************************************************************
 *   Copyright (C) 2010 by Frank Reininghaus ([email protected])    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

#include "dolphinviewtest_allviewmodes.h"

#include <qtest_kde.h>

#include "testbase.h"

#include "views/dolphinview.h"
#include "views/dolphinmodel.h"
#include "views/dolphindirlister.h"
#include "views/dolphinsortfilterproxymodel.h"

#include <qtestmouse.h>
#include <qtestkeyboard.h>

DolphinViewTest_AllViewModes::DolphinViewTest_AllViewModes() {
    // Need to register KFileItemList for use with QSignalSpy
    qRegisterMetaType<KFileItemList>("KFileItemList");
}

void DolphinViewTest_AllViewModes::init() {
    m_view->setMode(mode());
    QVERIFY(verifyCorrectViewMode());
    m_view->resize(200, 300);
    m_view->show();
    QTest::qWaitForWindowShown(m_view);
}

void DolphinViewTest_AllViewModes::cleanup() {
    m_view->hide();
    cleanupTestDir();
}

/**
 * testSelection() checks the basic selection funtionality of DolphinView, including:
 *
 * selectedItems()
 * selectedItemsCount()
 * selectAll()
 * invertSelection()
 * clearSelection()
 * hasSelection()
 *
 * and the signal selectionChanged(const KFileItemList& selection)
 */

Q_DECLARE_METATYPE(KFileItemList)

void DolphinViewTest_AllViewModes::testSelection() {
    const int totalItems = 50;

    for (int i = 0; i < totalItems; i++) {
        createFile(QString("%1").arg(i));
    }
    reloadViewAndWait();

    // Start with an empty selection
    m_view->clearSelection();

    QCOMPARE(m_view->selectedItems().count(), 0);
    QCOMPARE(m_view->selectedItemsCount(), 0);
    QVERIFY(!m_view->hasSelection());

    // First some simple tests where either all or no items are selected
    m_view->selectAll();
    verifySelectedItemsCount(totalItems);

    m_view->invertSelection();
    verifySelectedItemsCount(0);

    m_view->invertSelection();
    verifySelectedItemsCount(totalItems);

    m_view->clearSelection();
    verifySelectedItemsCount(0);

    // Now we select individual items using mouse clicks
    QModelIndex index = itemView()->model()->index(2, 0);
    itemView()->scrollTo(index);
    QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView()->visualRect(index).center());
    verifySelectedItemsCount(1);

    index = itemView()->model()->index(45, 0);
    itemView()->scrollTo(index);
    QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView()->visualRect(index).center());
    verifySelectedItemsCount(2);

    index = itemView()->model()->index(48, 0);
    itemView()->scrollTo(index);
    QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ShiftModifier, itemView()->visualRect(index).center());
    verifySelectedItemsCount(5);
}

/**
 * verifySelectedItemsCount(int) waits until the DolphinView's selectionChanged(const KFileItemList&)
 * signal is received and checks that the selection state of the view is as expected.
 */

void DolphinViewTest_AllViewModes::verifySelectedItemsCount(int itemsCount) const
{
    QSignalSpy spySelectionChanged(m_view, SIGNAL(selectionChanged(const KFileItemList&)));
    QTest::kWaitForSignal(m_view, SIGNAL(selectionChanged(const KFileItemList&)));

    QCOMPARE(m_view->selectedItems().count(), itemsCount);
    QCOMPARE(m_view->selectedItemsCount(), itemsCount);
    QCOMPARE(spySelectionChanged.count(), 1);
    QCOMPARE(qvariant_cast<KFileItemList>(spySelectionChanged.at(0).at(0)).count(), itemsCount);
    if (itemsCount != 0) {
        QVERIFY(m_view->hasSelection());
    }
    else {
        if (mode() == DolphinView::ColumnView &&
            itemView()->selectionModel()->selectedIndexes().count() == 0 &&
            itemView()->selectionModel()->hasSelection()) {
            QEXPECT_FAIL("",
                         "The selection model's hasSelection() method returns true, but there are no selected indexes. Needs to be investigated.",
                         Continue);
        }

        QVERIFY(!m_view->hasSelection());
    }
}

#include "dolphinviewtest_allviewmodes.moc"