1
/* ============================================================
3
* This file is a part of digiKam project
4
* http://www.digikam.org
7
* Description : a test for the searchtextbar
9
* Copyright (C) 2010 by Johannes Wienke <languitar at semipol dot de>
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)
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.
22
* ============================================================ */
24
#include "searchtextbartest.moc"
28
#include <qstandarditemmodel.h>
30
#include <qtestkeyboard.h>
34
#include <qtest_kde.h>
39
#include "searchtextbar.h"
41
using namespace Digikam;
42
using namespace QTest;
44
QTEST_KDEMAIN(SearchTextBarTest, GUI)
46
void SearchTextBarTest::testHighlighting()
49
SearchTextBar textBar(0, "test");
50
QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::NEUTRAL);
52
// highlighting shall only occur if text is entered
53
textBar.slotSearchResult(true);
54
QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::NEUTRAL);
56
textBar.setText("test");
58
textBar.slotSearchResult(true);
59
QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::HAS_RESULT);
60
textBar.slotSearchResult(false);
61
QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::NO_RESULT);
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);
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);
77
// re-eetting highlighting to true must not change highlight state
78
textBar.setHighlightOnResult(true);
79
QCOMPARE(textBar.getCurrentHighlightState(), SearchTextBar::NO_RESULT);
83
void SearchTextBarTest::testSearchTextSettings()
86
SearchTextBar textBar(0, "test");
88
SearchTextSettings defaultSettings;
89
QCOMPARE(textBar.searchTextSettings(), defaultSettings);
92
connect(&textBar, SIGNAL(signalSearchTextSettings(const SearchTextSettings&)),
93
this, SLOT(newSearchTextSettings(const SearchTextSettings&)));
95
const QString textEntered = "hello world";
96
keyClicks(&textBar, textEntered);
98
QCOMPARE(textBar.searchTextSettings().caseSensitive, defaultSettings.caseSensitive);
99
QCOMPARE(textBar.searchTextSettings().text, textEntered);
101
QCOMPARE(callCount, textEntered.size());
102
QCOMPARE(lastSearchTextSettings.caseSensitive, defaultSettings.caseSensitive);
103
QCOMPARE(lastSearchTextSettings.text, textEntered);
105
/** @todo test case modifications, but how to click context menu? */
109
void SearchTextBarTest::newSearchTextSettings(const SearchTextSettings& settings)
111
lastSearchTextSettings = settings;
115
void SearchTextBarTest::testModelParsing()
118
SearchTextBar textBar(0, "test");
120
// create a simple test model
121
QStandardItemModel model(4, 1);
123
const int idRole = 120;
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");
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);
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);
151
textBar.setModel(&model, idRole, Qt::DisplayRole);
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));
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...
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));
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);
183
textBar.setModel(&newModel, idRole, Qt::DisplayRole);
185
QCOMPARE(textBar.completionObject()->items().size(), 2);
186
QVERIFY(textBar.completionObject()->items().contains(parent0 + "x"));
187
QVERIFY(textBar.completionObject()->items().contains(parent1 + "x"));