26 #include <config-kleopatra.h>
30 Q_DECLARE_METATYPE(QModelIndex)
39 connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,
int,
int)),
40 this, SLOT(runAllTests()));
41 connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,
int,
int)),
42 this, SLOT(runAllTests()));
43 connect(model, SIGNAL(columnsInserted(QModelIndex,
int,
int)),
44 this, SLOT(runAllTests()));
45 connect(model, SIGNAL(columnsRemoved(QModelIndex,
int,
int)),
46 this, SLOT(runAllTests()));
47 connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
48 this, SLOT(runAllTests()));
49 connect(model, SIGNAL(headerDataChanged(Qt::Orientation,
int,
int)),
50 this, SLOT(runAllTests()));
51 connect(model, SIGNAL(layoutAboutToBeChanged()),
this, SLOT(runAllTests()));
52 connect(model, SIGNAL(layoutChanged()),
this, SLOT(runAllTests()));
53 connect(model, SIGNAL(modelReset()),
this, SLOT(runAllTests()));
54 connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,
int,
int)),
55 this, SLOT(runAllTests()));
56 connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,
int,
int)),
57 this, SLOT(runAllTests()));
58 connect(model, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
59 this, SLOT(runAllTests()));
60 connect(model, SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
61 this, SLOT(runAllTests()));
64 connect(model, SIGNAL(layoutAboutToBeChanged()),
65 this, SLOT(layoutAboutToBeChanged()));
66 connect(model, SIGNAL(layoutChanged()),
67 this, SLOT(layoutChanged()));
69 connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,
int,
int)),
70 this, SLOT(rowsAboutToBeInserted(QModelIndex,
int,
int)));
71 connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,
int,
int)),
72 this, SLOT(rowsAboutToBeRemoved(QModelIndex,
int,
int)));
73 connect(model, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
74 this, SLOT(rowsInserted(QModelIndex,
int,
int)));
75 connect(model, SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
76 this, SLOT(rowsRemoved(QModelIndex,
int,
int)));
85 nonDestructiveBasicTest();
98 void ModelTest::nonDestructiveBasicTest()
100 Q_ASSERT(model->buddy(QModelIndex()) == QModelIndex());
101 model->canFetchMore(QModelIndex());
102 Q_ASSERT(model->columnCount(QModelIndex()) >= 0);
103 Q_ASSERT(model->data(QModelIndex()) == QVariant());
105 model->fetchMore(QModelIndex());
106 fetchingMore =
false;
107 Qt::ItemFlags
flags = model->flags(QModelIndex());
108 Q_ASSERT(flags == Qt::ItemIsDropEnabled || flags == 0);
109 model->hasChildren(QModelIndex());
110 model->hasIndex(0, 0);
111 model->headerData(0, Qt::Horizontal);
113 model->itemData(QModelIndex());
115 model->match(QModelIndex(), -1, cache);
117 Q_ASSERT(model->parent(QModelIndex()) == QModelIndex());
118 Q_ASSERT(model->rowCount() >= 0);
120 model->setData(QModelIndex(), variant, -1);
121 model->setHeaderData(-1, Qt::Horizontal, QVariant());
122 model->setHeaderData(0, Qt::Horizontal, QVariant());
123 model->setHeaderData(999999, Qt::Horizontal, QVariant());
125 model->sibling(0, 0, QModelIndex());
126 model->span(QModelIndex());
127 model->supportedDropActions();
135 void ModelTest::rowCount()
138 QModelIndex topIndex = model->index(0, 0, QModelIndex());
139 int rows = model->rowCount(topIndex);
142 Q_ASSERT(model->hasChildren(topIndex) ==
true);
144 QModelIndex secondLevelIndex = model->index(0, 0, topIndex);
145 if (secondLevelIndex.isValid()) {
147 rows = model->rowCount(secondLevelIndex);
150 Q_ASSERT(model->hasChildren(secondLevelIndex) ==
true);
160 void ModelTest::columnCount()
163 QModelIndex topIndex = model->index(0, 0, QModelIndex());
164 Q_ASSERT(model->columnCount(topIndex) >= 0);
167 QModelIndex childIndex = model->index(0, 0, topIndex);
168 if (childIndex.isValid())
169 Q_ASSERT(model->columnCount(childIndex) >= 0);
178 void ModelTest::hasIndex()
181 Q_ASSERT(model->hasIndex(-2, -2) ==
false);
182 Q_ASSERT(model->hasIndex(-2, 0) ==
false);
183 Q_ASSERT(model->hasIndex(0, -2) ==
false);
185 int rows = model->rowCount();
186 int columns = model->columnCount();
189 Q_ASSERT(model->hasIndex(rows, columns) ==
false);
190 Q_ASSERT(model->hasIndex(rows + 1, columns + 1) ==
false);
193 Q_ASSERT(model->hasIndex(0, 0) ==
true);
202 void ModelTest::index()
205 Q_ASSERT(model->index(-2, -2) == QModelIndex());
206 Q_ASSERT(model->index(-2, 0) == QModelIndex());
207 Q_ASSERT(model->index(0, -2) == QModelIndex());
209 int rows = model->rowCount();
210 int columns = model->columnCount();
216 Q_ASSERT(model->index(rows, columns) == QModelIndex());
217 Q_ASSERT(model->index(0, 0).isValid() ==
true);
220 QModelIndex a = model->index(0, 0);
221 QModelIndex b = model->index(0, 0);
231 void ModelTest::parent()
235 Q_ASSERT(model->parent(QModelIndex()) == QModelIndex());
237 if (model->rowCount() == 0)
247 QModelIndex topIndex = model->index(0, 0, QModelIndex());
248 Q_ASSERT(model->parent(topIndex) == QModelIndex());
252 if (model->rowCount(topIndex) > 0) {
253 QModelIndex childIndex = model->index(0, 0, topIndex);
254 Q_ASSERT(model->parent(childIndex) == topIndex);
260 QModelIndex topIndex1 = model->index(0, 1, QModelIndex());
261 if (model->rowCount(topIndex1) > 0) {
262 QModelIndex childIndex = model->index(0, 0, topIndex);
263 QModelIndex childIndex1 = model->index(0, 0, topIndex1);
264 Q_ASSERT(childIndex != childIndex1);
269 checkChildren(QModelIndex());
286 void ModelTest::checkChildren(
const QModelIndex &parent,
int currentDepth)
289 QModelIndex p = parent;
294 if (model->canFetchMore(parent)) {
296 model->fetchMore(parent);
297 fetchingMore =
false;
300 int rows = model->rowCount(parent);
301 int columns = model->columnCount(parent);
304 Q_ASSERT(model->hasChildren(parent));
308 Q_ASSERT(columns >= 0);
310 Q_ASSERT(model->hasChildren(parent) ==
true);
315 Q_ASSERT(model->hasIndex(rows + 1, 0, parent) ==
false);
316 for (
int r = 0; r < rows; ++r) {
317 if (model->canFetchMore(parent)) {
319 model->fetchMore(parent);
320 fetchingMore =
false;
322 Q_ASSERT(model->hasIndex(r, columns + 1, parent) ==
false);
323 for (
int c = 0; c < columns; ++c) {
324 Q_ASSERT(model->hasIndex(r, c, parent) ==
true);
325 QModelIndex index = model->index(r, c, parent);
327 Q_ASSERT(index.isValid() ==
true);
330 QModelIndex modifiedIndex = model->index(r, c, parent);
331 Q_ASSERT(index == modifiedIndex);
334 QModelIndex a = model->index(r, c, parent);
335 QModelIndex b = model->index(r, c, parent);
339 Q_ASSERT(index.model() == model);
340 Q_ASSERT(index.row() == r);
341 Q_ASSERT(index.column() == c);
344 Q_ASSERT(model->data(index, Qt::DisplayRole).isValid() ==
true);
359 Q_ASSERT(model->parent(index) == parent);
362 if (model->hasChildren(index) && currentDepth < 10 ) {
364 checkChildren(index, ++currentDepth);
368 QModelIndex newerIndex = model->index(r, c, parent);
369 Q_ASSERT(index == newerIndex);
377 void ModelTest::data()
380 Q_ASSERT(!model->data(QModelIndex()).isValid());
382 if (model->rowCount() == 0)
386 Q_ASSERT(model->index(0, 0).isValid());
389 Q_ASSERT(model->setData(QModelIndex(), QLatin1String(
"foo"), Qt::DisplayRole) ==
false);
392 QVariant variant = model->data(model->index(0, 0), Qt::ToolTipRole);
393 if (variant.isValid()) {
394 Q_ASSERT(qVariantCanConvert<QString>(variant));
396 variant = model->data(model->index(0, 0), Qt::StatusTipRole);
397 if (variant.isValid()) {
398 Q_ASSERT(qVariantCanConvert<QString>(variant));
400 variant = model->data(model->index(0, 0), Qt::WhatsThisRole);
401 if (variant.isValid()) {
402 Q_ASSERT(qVariantCanConvert<QString>(variant));
406 variant = model->data(model->index(0, 0), Qt::SizeHintRole);
407 if (variant.isValid()) {
408 Q_ASSERT(qVariantCanConvert<QSize>(variant));
412 QVariant fontVariant = model->data(model->index(0, 0), Qt::FontRole);
413 if (fontVariant.isValid()) {
414 Q_ASSERT(qVariantCanConvert<QFont>(fontVariant));
418 QVariant textAlignmentVariant = model->data(model->index(0, 0), Qt::TextAlignmentRole);
419 if (textAlignmentVariant.isValid()) {
420 int alignment = textAlignmentVariant.toInt();
421 Q_ASSERT(alignment == Qt::AlignLeft ||
422 alignment == Qt::AlignRight ||
423 alignment == Qt::AlignHCenter ||
424 alignment == Qt::AlignJustify ||
425 alignment == Qt::AlignTop ||
426 alignment == Qt::AlignBottom ||
427 alignment == Qt::AlignVCenter ||
428 alignment == Qt::AlignCenter ||
429 alignment == Qt::AlignAbsolute ||
430 alignment == Qt::AlignLeading ||
431 alignment == Qt::AlignTrailing);
435 QVariant colorVariant = model->data(model->index(0, 0), Qt::BackgroundColorRole);
436 if (colorVariant.isValid()) {
437 Q_ASSERT(qVariantCanConvert<QColor>(colorVariant));
440 colorVariant = model->data(model->index(0, 0), Qt::TextColorRole);
441 if (colorVariant.isValid()) {
442 Q_ASSERT(qVariantCanConvert<QColor>(colorVariant));
446 QVariant checkStateVariant = model->data(model->index(0, 0), Qt::CheckStateRole);
447 if (checkStateVariant.isValid()) {
448 int state = checkStateVariant.toInt();
449 Q_ASSERT(state == Qt::Unchecked ||
450 state == Qt::PartiallyChecked ||
451 state == Qt::Checked);
465 c.oldSize = model->rowCount(parent);
466 c.last = model->data(model->index(start - 1, 0, parent));
467 c.next = model->data(model->index(start, 0, parent));
478 Changing c = insert.pop();
479 Q_ASSERT(c.parent == parent);
480 Q_ASSERT(c.oldSize + (end - start + 1) == model->rowCount(parent));
481 Q_ASSERT(c.last == model->data(model->index(start - 1, 0, c.parent)));
490 Q_ASSERT(c.next == model->data(model->index(end + 1, 0, c.parent)));
495 for (
int i = 0; i < qBound(0, model->rowCount(), 100); ++i)
496 changing.append(QPersistentModelIndex(model->index(i, 0)));
501 for (
int i = 0; i < changing.count(); ++i) {
502 QPersistentModelIndex p = changing[i];
503 Q_ASSERT(p == model->index(p.row(), p.column(), p.parent()));
517 c.oldSize = model->rowCount(parent);
518 c.last = model->data(model->index(start - 1, 0, parent));
519 c.next = model->data(model->index(end + 1, 0, parent));
530 Changing c =
remove.pop();
531 Q_ASSERT(c.parent == parent);
532 Q_ASSERT(c.oldSize - (end - start + 1) == model->rowCount(parent));
533 Q_ASSERT(c.last == model->data(model->index(start - 1, 0, c.parent)));
534 Q_ASSERT(c.next == model->data(model->index(start, 0, c.parent)));
static const char * flags[]
void rowsInserted(const QModelIndex &parent, int start, int end)
void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
void rowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
void rowsRemoved(const QModelIndex &parent, int start, int end)
void layoutAboutToBeChanged()