26 #include <QtGui/QtGui>
30 Q_DECLARE_METATYPE(QModelIndex)
39 connect(model, SIGNAL(columnsAboutToBeInserted(
const QModelIndex &,
int,
int)),
40 this, SLOT(runAllTests()));
41 connect(model, SIGNAL(columnsAboutToBeRemoved(
const QModelIndex &,
int,
int)),
42 this, SLOT(runAllTests()));
43 connect(model, SIGNAL(columnsInserted(
const QModelIndex &,
int,
int)),
44 this, SLOT(runAllTests()));
45 connect(model, SIGNAL(columnsRemoved(
const QModelIndex &,
int,
int)),
46 this, SLOT(runAllTests()));
47 connect(model, SIGNAL(dataChanged(
const QModelIndex &,
const 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(
const QModelIndex &,
int,
int)),
55 this, SLOT(runAllTests()));
56 connect(model, SIGNAL(rowsAboutToBeRemoved(
const QModelIndex &,
int,
int)),
57 this, SLOT(runAllTests()));
58 connect(model, SIGNAL(rowsInserted(
const QModelIndex &,
int,
int)),
59 this, SLOT(runAllTests()));
60 connect(model, SIGNAL(rowsRemoved(
const QModelIndex &,
int,
int)),
61 this, SLOT(runAllTests()));
64 connect(model, SIGNAL(rowsAboutToBeInserted(
const QModelIndex &,
int,
int)),
65 this, SLOT(rowsAboutToBeInserted(
const QModelIndex &,
int,
int)));
66 connect(model, SIGNAL(rowsAboutToBeRemoved(
const QModelIndex &,
int,
int)),
67 this, SLOT(rowsAboutToBeRemoved(
const QModelIndex &,
int,
int)));
68 connect(model, SIGNAL(rowsInserted(
const QModelIndex &,
int,
int)),
69 this, SLOT(rowsInserted(
const QModelIndex &,
int,
int)));
70 connect(model, SIGNAL(rowsRemoved(
const QModelIndex &,
int,
int)),
71 this, SLOT(rowsRemoved(
const QModelIndex &,
int,
int)));
80 nonDestructiveBasicTest();
93 void ModelTest::nonDestructiveBasicTest()
95 Q_ASSERT(model->buddy(QModelIndex()) == QModelIndex());
96 model->canFetchMore(QModelIndex());
97 Q_ASSERT(model->columnCount(QModelIndex()) >= 0);
98 Q_ASSERT(model->data(QModelIndex()) == QVariant());
100 model->fetchMore(QModelIndex());
101 fetchingMore =
false;
102 Qt::ItemFlags flags = model->flags(QModelIndex());
103 Q_ASSERT(flags == Qt::ItemIsDropEnabled || flags == 0);
104 model->hasChildren(QModelIndex());
105 model->hasIndex(0, 0);
106 model->headerData(0, Qt::Horizontal);
108 model->itemData(QModelIndex());
110 model->match(QModelIndex(), -1, cache);
112 Q_ASSERT(model->parent(QModelIndex()) == QModelIndex());
113 Q_ASSERT(model->rowCount() >= 0);
115 model->setData(QModelIndex(), variant, -1);
116 model->setHeaderData(-1, Qt::Horizontal, QVariant());
117 model->setHeaderData(0, Qt::Horizontal, QVariant());
118 model->setHeaderData(999999, Qt::Horizontal, QVariant());
119 QMap<int, QVariant> roles;
120 model->sibling(0, 0, QModelIndex());
121 model->span(QModelIndex());
122 model->supportedDropActions();
130 void ModelTest::rowCount()
133 QModelIndex topIndex = model->index(0, 0, QModelIndex());
134 int rows = model->rowCount(topIndex);
137 Q_ASSERT(model->hasChildren(topIndex) ==
true);
139 QModelIndex secondLevelIndex = model->index(0, 0, topIndex);
140 if (secondLevelIndex.isValid()) {
142 rows = model->rowCount(secondLevelIndex);
145 Q_ASSERT(model->hasChildren(secondLevelIndex) ==
true);
155 void ModelTest::columnCount()
158 QModelIndex topIndex = model->index(0, 0, QModelIndex());
159 Q_ASSERT(model->columnCount(topIndex) >= 0);
162 QModelIndex childIndex = model->index(0, 0, topIndex);
163 if (childIndex.isValid())
164 Q_ASSERT(model->columnCount(childIndex) >= 0);
173 void ModelTest::hasIndex()
176 Q_ASSERT(model->hasIndex(-2, -2) ==
false);
177 Q_ASSERT(model->hasIndex(-2, 0) ==
false);
178 Q_ASSERT(model->hasIndex(0, -2) ==
false);
180 int rows = model->rowCount();
181 int columns = model->columnCount();
184 Q_ASSERT(model->hasIndex(rows, columns) ==
false);
185 Q_ASSERT(model->hasIndex(rows + 1, columns + 1) ==
false);
188 Q_ASSERT(model->hasIndex(0, 0) ==
true);
197 void ModelTest::index()
200 Q_ASSERT(model->index(-2, -2) == QModelIndex());
201 Q_ASSERT(model->index(-2, 0) == QModelIndex());
202 Q_ASSERT(model->index(0, -2) == QModelIndex());
204 int rows = model->rowCount();
205 int columns = model->columnCount();
211 Q_ASSERT(model->index(rows, columns) == QModelIndex());
212 Q_ASSERT(model->index(0, 0).isValid() ==
true);
215 QModelIndex a = model->index(0, 0);
216 QModelIndex b = model->index(0, 0);
226 void ModelTest::parent()
230 Q_ASSERT(model->parent(QModelIndex()) == QModelIndex());
232 if (model->rowCount() == 0)
242 QModelIndex topIndex = model->index(0, 0, QModelIndex());
243 Q_ASSERT(model->parent(topIndex) == QModelIndex());
247 if (model->rowCount(topIndex) > 0) {
248 QModelIndex childIndex = model->index(0, 0, topIndex);
249 Q_ASSERT(model->parent(childIndex) == topIndex);
255 QModelIndex topIndex1 = model->index(0, 1, QModelIndex());
256 if (model->rowCount(topIndex1) > 0) {
257 QModelIndex childIndex = model->index(0, 0, topIndex);
258 QModelIndex childIndex1 = model->index(0, 0, topIndex1);
259 Q_ASSERT(childIndex != childIndex1);
264 checkChildren(QModelIndex());
281 void ModelTest::checkChildren(
const QModelIndex &parent,
int currentDepth)
284 QModelIndex p = parent;
289 if (model->canFetchMore(parent)) {
291 model->fetchMore(parent);
292 fetchingMore =
false;
295 int rows = model->rowCount(parent);
296 int columns = model->columnCount(parent);
299 Q_ASSERT(model->hasChildren(parent));
303 Q_ASSERT(columns >= 0);
305 Q_ASSERT(model->hasChildren(parent) ==
true);
310 Q_ASSERT(model->hasIndex(rows + 1, 0, parent) ==
false);
311 for (
int r = 0; r < rows; ++r) {
312 if (model->canFetchMore(parent)) {
314 model->fetchMore(parent);
315 fetchingMore =
false;
317 Q_ASSERT(model->hasIndex(r, columns + 1, parent) ==
false);
318 for (
int c = 0; c < columns; ++c) {
319 Q_ASSERT(model->hasIndex(r, c, parent) ==
true);
320 QModelIndex index = model->index(r, c, parent);
322 Q_ASSERT(index.isValid() ==
true);
325 QModelIndex modifiedIndex = model->index(r, c, parent);
326 Q_ASSERT(index == modifiedIndex);
329 QModelIndex a = model->index(r, c, parent);
330 QModelIndex b = model->index(r, c, parent);
334 Q_ASSERT(index.model() == model);
335 Q_ASSERT(index.row() == r);
336 Q_ASSERT(index.column() == c);
356 Q_ASSERT(model->parent(index) == parent);
359 if (model->hasChildren(index) && currentDepth < 10 ) {
361 checkChildren(index, ++currentDepth);
365 QModelIndex newerIndex = model->index(r, c, parent);
366 Q_ASSERT(index == newerIndex);
374 void ModelTest::data()
377 Q_ASSERT(!model->data(QModelIndex()).isValid());
379 if (model->rowCount() == 0)
383 Q_ASSERT(model->index(0, 0).isValid());
386 Q_ASSERT(model->setData(QModelIndex(), QLatin1String(
"foo"), Qt::DisplayRole) ==
false);
389 QVariant variant = model->data(model->index(0, 0), Qt::ToolTipRole);
390 if (variant.isValid()) {
391 Q_ASSERT(qVariantCanConvert<QString>(variant));
393 variant = model->data(model->index(0, 0), Qt::StatusTipRole);
394 if (variant.isValid()) {
395 Q_ASSERT(qVariantCanConvert<QString>(variant));
397 variant = model->data(model->index(0, 0), Qt::WhatsThisRole);
398 if (variant.isValid()) {
399 Q_ASSERT(qVariantCanConvert<QString>(variant));
403 variant = model->data(model->index(0, 0), Qt::SizeHintRole);
404 if (variant.isValid()) {
405 Q_ASSERT(qVariantCanConvert<QSize>(variant));
409 QVariant fontVariant = model->data(model->index(0, 0), Qt::FontRole);
410 if (fontVariant.isValid()) {
411 Q_ASSERT(qVariantCanConvert<QFont>(fontVariant));
415 QVariant textAlignmentVariant = model->data(model->index(0, 0), Qt::TextAlignmentRole);
416 if (textAlignmentVariant.isValid()) {
417 int alignment = textAlignmentVariant.toInt();
418 Q_ASSERT(alignment == Qt::AlignLeft ||
419 alignment == Qt::AlignRight ||
420 alignment == Qt::AlignHCenter ||
421 alignment == Qt::AlignJustify ||
422 alignment == Qt::AlignTop ||
423 alignment == Qt::AlignBottom ||
424 alignment == Qt::AlignVCenter ||
425 alignment == Qt::AlignCenter ||
426 alignment == Qt::AlignAbsolute ||
427 alignment == Qt::AlignLeading ||
428 alignment == Qt::AlignTrailing);
432 QVariant colorVariant = model->data(model->index(0, 0), Qt::BackgroundColorRole);
433 if (colorVariant.isValid()) {
434 Q_ASSERT(qVariantCanConvert<QColor>(colorVariant));
437 colorVariant = model->data(model->index(0, 0), Qt::TextColorRole);
438 if (colorVariant.isValid()) {
439 Q_ASSERT(qVariantCanConvert<QColor>(colorVariant));
443 QVariant checkStateVariant = model->data(model->index(0, 0), Qt::CheckStateRole);
444 if (checkStateVariant.isValid()) {
445 int state = checkStateVariant.toInt();
446 Q_ASSERT(state == Qt::Unchecked ||
447 state == Qt::PartiallyChecked ||
448 state == Qt::Checked);
462 c.oldSize = model->rowCount(parent);
463 c.last = model->data(model->index(start - 1, 0, parent));
464 c.next = model->data(model->index(start, 0, parent));
475 Changing c = insert.pop();
476 Q_ASSERT(c.parent == parent);
477 Q_ASSERT(c.oldSize + (end - start + 1) == model->rowCount(parent));
478 Q_ASSERT(c.last == model->data(model->index(start - 1, 0, c.parent)));
487 Q_ASSERT(c.next == model->data(model->index(end + 1, 0, c.parent)));
499 c.oldSize = model->rowCount(parent);
500 c.last = model->data(model->index(start - 1, 0, parent));
501 c.next = model->data(model->index(end + 1, 0, parent));
512 Changing c =
remove.pop();
513 Q_ASSERT(c.parent == parent);
514 Q_ASSERT(c.oldSize - (end - start + 1) == model->rowCount(parent));
515 Q_ASSERT(c.last == model->data(model->index(start - 1, 0, c.parent)));
516 Q_ASSERT(c.next == model->data(model->index(start, 0, c.parent)));
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)