~ubuntu-branches/ubuntu/saucy/digikam/saucy

« back to all changes in this revision

Viewing changes to tests/searchtextbartest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-12-21 23:19:11 UTC
  • mfrom: (1.2.33 upstream) (3.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20101221231911-z9jip7s5aht1jqn9
Tags: 2:1.7.0-1ubuntu1
* Merge from Debian Experimental. Remaining Ubuntu changes:
  - Export .pot name and copy to plugins in debian/rules
  - Version build-depends on kipi-plugins-dev to ensure build is against the
    same version on all archs
* Drop debian/patches/kubuntu_01_linker.diff, incoporated upstream
* Remove patches directory and unused patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ============================================================
 
2
 *
 
3
 * This file is a part of digiKam project
 
4
 * http://www.digikam.org
 
5
 *
 
6
 * Date        : 2010-01-12
 
7
 * Description : a test for the searchtextbar
 
8
 *
 
9
 * Copyright (C) 2010 by Johannes Wienke <languitar at semipol dot de>
 
10
 *
 
11
 * This program is free software; you can redistribute it
 
12
 * and/or modify it under the terms of the GNU General
 
13
 * Public License as published by the Free Software Foundation;
 
14
 * either version 2, or (at your option)
 
15
 * any later version.
 
16
 *
 
17
 * This program is distributed in the hope that it will be useful,
 
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
 * GNU General Public License for more details.
 
21
 *
 
22
 * ============================================================ */
 
23
 
 
24
#include "searchtextbartest.moc"
 
25
 
 
26
// Qt includes
 
27
 
 
28
#include <qstandarditemmodel.h>
 
29
#include <qtest.h>
 
30
#include <qtestkeyboard.h>
 
31
 
 
32
// KDE includes
 
33
 
 
34
#include <qtest_kde.h>
 
35
#include <kdebug.h>
 
36
 
 
37
// Local includes
 
38
 
 
39
#include "searchtextbar.h"
 
40
 
 
41
using namespace Digikam;
 
42
using namespace QTest;
 
43
 
 
44
QTEST_KDEMAIN(SearchTextBarTest, GUI)
 
45
 
 
46
void SearchTextBarTest::testHighlighting()
 
47
{
 
48
 
 
49
    SearchTextBar textBar(0, "test");
 
50
    QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::NEUTRAL);
 
51
 
 
52
    // highlighting shall only occur if text is entered
 
53
    textBar.slotSearchResult(true);
 
54
    QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::NEUTRAL);
 
55
 
 
56
    textBar.setText("test");
 
57
 
 
58
    textBar.slotSearchResult(true);
 
59
    QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::HAS_RESULT);
 
60
    textBar.slotSearchResult(false);
 
61
    QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::NO_RESULT);
 
62
 
 
63
    textBar.setHighlightOnResult(false);
 
64
    QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::NEUTRAL);
 
65
    textBar.slotSearchResult(true);
 
66
    QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::NEUTRAL);
 
67
    textBar.slotSearchResult(false);
 
68
    QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::NEUTRAL);
 
69
 
 
70
    textBar.setHighlightOnResult(true);
 
71
    QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::NEUTRAL);
 
72
    textBar.slotSearchResult(true);
 
73
    QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::HAS_RESULT);
 
74
    textBar.slotSearchResult(false);
 
75
    QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::NO_RESULT);
 
76
 
 
77
    // re-eetting highlighting to true must not change highlight state
 
78
    textBar.setHighlightOnResult(true);
 
79
    QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::NO_RESULT);
 
80
 
 
81
}
 
82
 
 
83
void SearchTextBarTest::testSearchTextSettings()
 
84
{
 
85
 
 
86
    SearchTextBar textBar(0, "test");
 
87
 
 
88
    SearchTextSettings defaultSettings;
 
89
    QCOMPARE(textBar.searchTextSettings(), defaultSettings);
 
90
 
 
91
    callCount = 0;
 
92
    connect(&textBar, SIGNAL(signalSearchTextSettings(const SearchTextSettings&)),
 
93
            this, SLOT(newSearchTextSettings(const SearchTextSettings&)));
 
94
 
 
95
    const QString textEntered = "hello world";
 
96
    keyClicks(&textBar, textEntered);
 
97
 
 
98
    QCOMPARE(textBar.searchTextSettings().caseSensitive, defaultSettings.caseSensitive);
 
99
    QCOMPARE(textBar.searchTextSettings().text, textEntered);
 
100
 
 
101
    QCOMPARE(callCount, textEntered.size());
 
102
    QCOMPARE(lastSearchTextSettings.caseSensitive, defaultSettings.caseSensitive);
 
103
    QCOMPARE(lastSearchTextSettings.text, textEntered);
 
104
 
 
105
    /** @todo test case modifications, but how to click context menu? */
 
106
 
 
107
}
 
108
 
 
109
void SearchTextBarTest::newSearchTextSettings(const SearchTextSettings& settings)
 
110
{
 
111
    lastSearchTextSettings = settings;
 
112
    callCount++;
 
113
}
 
114
 
 
115
void SearchTextBarTest::testModelParsing()
 
116
{
 
117
 
 
118
    SearchTextBar textBar(0, "test");
 
119
 
 
120
    // create a simple test model
 
121
    QStandardItemModel model(4, 1);
 
122
 
 
123
    const int idRole = 120;
 
124
 
 
125
    const QString parent0("test row 0");
 
126
    const QString parent1("test row 1");
 
127
    const QString parent2("test row 2");
 
128
    const QString parent3("test row 3");
 
129
    const QString firstChild("first child");
 
130
 
 
131
    QModelIndex index = model.index(0, 0, QModelIndex());
 
132
    model.setData(index, QVariant(parent0), Qt::DisplayRole);
 
133
    model.setData(index, QVariant(0), idRole);
 
134
    index = model.index(1, 0, QModelIndex());
 
135
    model.setData(index, QVariant(parent1), Qt::DisplayRole);
 
136
    model.setData(index, QVariant(1), idRole);
 
137
    index = model.index(2, 0, QModelIndex());
 
138
    model.setData(index, QVariant(parent2), Qt::DisplayRole);
 
139
    model.setData(index, QVariant(2), idRole);
 
140
    index = model.index(3, 0, QModelIndex());
 
141
    model.setData(index, QVariant(parent3), Qt::DisplayRole);
 
142
    model.setData(index, QVariant(3), idRole);
 
143
 
 
144
    // add a child to the last index to ensure tree parsing
 
145
    model.insertRow(0, index);
 
146
    model.insertColumn(0, index);
 
147
    QModelIndex childIndex = model.index(0, 0, index);
 
148
    model.setData(childIndex, QVariant(firstChild), Qt::DisplayRole);
 
149
    model.setData(childIndex, QVariant(10), idRole);
 
150
 
 
151
    textBar.setModel(&model, idRole, Qt::DisplayRole);
 
152
 
 
153
    // check that all entries are in the completion object now
 
154
    QCOMPARE(textBar.completionObject()->items().size(), 5);
 
155
    QVERIFY(textBar.completionObject()->items().contains(parent0));
 
156
    QVERIFY(textBar.completionObject()->items().contains(parent1));
 
157
    QVERIFY(textBar.completionObject()->items().contains(parent2));
 
158
    QVERIFY(textBar.completionObject()->items().contains(parent3));
 
159
    QVERIFY(textBar.completionObject()->items().contains(firstChild));
 
160
 
 
161
    /**
 
162
     * @todo I can't test inserting new items, because this stub model only
 
163
     * supports adding columns after having added a row with 0 columns. The
 
164
     * searchtextbar is only registered at rowInserted, so that there won't be
 
165
     * a valid index in this row yet...
 
166
     */
 
167
 
 
168
    // check that deleting an item is mirrored
 
169
    model.removeRow(1, QModelIndex());
 
170
    QCOMPARE(textBar.completionObject()->items().size(), 4);
 
171
    QVERIFY(!textBar.completionObject()->items().contains(parent1));
 
172
 
 
173
    // ensure that resetting a completely new model works
 
174
    QStandardItemModel newModel(2, 1);
 
175
    // use the same ids by purpose
 
176
    index = newModel.index(0, 0, QModelIndex());
 
177
    newModel.setData(index, QVariant(parent0 + "x"), Qt::DisplayRole);
 
178
    newModel.setData(index, QVariant(0), idRole);
 
179
    index = newModel.index(1, 0, QModelIndex());
 
180
    newModel.setData(index, QVariant(parent1 + "x"), Qt::DisplayRole);
 
181
    newModel.setData(index, QVariant(1), idRole);
 
182
 
 
183
    textBar.setModel(&newModel, idRole, Qt::DisplayRole);
 
184
 
 
185
    QCOMPARE(textBar.completionObject()->items().size(), 2);
 
186
    QVERIFY(textBar.completionObject()->items().contains(parent0 + "x"));
 
187
    QVERIFY(textBar.completionObject()->items().contains(parent1 + "x"));
 
188
 
 
189
}