24 #ifdef KLF_DEBUG_USE_MODELTEST
26 #include <QtGui/QtGui>
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()));
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(layoutAboutToBeChanged()),
65 this, SLOT(layoutAboutToBeChanged()));
66 connect(model, SIGNAL(layoutChanged()),
67 this, SLOT(layoutChanged()));
69 connect(model, SIGNAL(rowsAboutToBeInserted(
const QModelIndex &,
int,
int)),
70 this, SLOT(rowsAboutToBeInserted(
const QModelIndex &,
int,
int)));
71 connect(model, SIGNAL(rowsAboutToBeRemoved(
const QModelIndex &,
int,
int)),
72 this, SLOT(rowsAboutToBeRemoved(
const QModelIndex &,
int,
int)));
73 connect(model, SIGNAL(rowsInserted(
const QModelIndex &,
int,
int)),
74 this, SLOT(rowsInserted(
const QModelIndex &,
int,
int)));
75 connect(model, SIGNAL(rowsRemoved(
const QModelIndex &,
int,
int)),
76 this, SLOT(rowsRemoved(
const QModelIndex &,
int,
int)));
81 void ModelTest::runAllTests()
85 nonDestructiveBasicTest();
98 void ModelTest::nonDestructiveBasicTest()
106 fetchingMore =
false;
108 Q_ASSERT(flags == Qt::ItemIsDropEnabled || flags == 0);
110 model->hasIndex(0, 0);
111 model->headerData(0, Qt::Horizontal);
119 Q_ASSERT(model->rowCount() >= 0);
122 model->setHeaderData(-1, Qt::Horizontal,
QVariant());
123 model->setHeaderData(0, Qt::Horizontal,
QVariant());
124 model->setHeaderData(999999, Qt::Horizontal,
QVariant());
128 model->supportedDropActions();
136 void ModelTest::rowCount()
140 int rows = model->rowCount(topIndex);
143 Q_ASSERT(model->hasChildren(topIndex) ==
true);
145 QModelIndex secondLevelIndex = model->index(0, 0, topIndex);
146 if (secondLevelIndex.isValid()) {
148 rows = model->rowCount(secondLevelIndex);
151 Q_ASSERT(model->hasChildren(secondLevelIndex) ==
true);
161 void ModelTest::columnCount()
165 Q_ASSERT(model->columnCount(topIndex) >= 0);
168 QModelIndex childIndex = model->index(0, 0, topIndex);
169 if (childIndex.isValid())
170 Q_ASSERT(model->columnCount(childIndex) >= 0);
179 void ModelTest::hasIndex()
182 Q_ASSERT(model->hasIndex(-2, -2) ==
false);
183 Q_ASSERT(model->hasIndex(-2, 0) ==
false);
184 Q_ASSERT(model->hasIndex(0, -2) ==
false);
186 int rows = model->rowCount();
187 int columns = model->columnCount();
190 Q_ASSERT(model->hasIndex(rows, columns) ==
false);
191 Q_ASSERT(model->hasIndex(rows + 1, columns + 1) ==
false);
194 Q_ASSERT(model->hasIndex(0, 0) ==
true);
203 void ModelTest::index()
210 int rows = model->rowCount();
211 int columns = model->columnCount();
217 Q_ASSERT(model->index(rows, columns) ==
QModelIndex());
218 Q_ASSERT(model->index(0, 0).isValid() ==
true);
232 void ModelTest::parent()
238 if (model->rowCount() == 0)
249 Q_ASSERT(model->parent(topIndex) ==
QModelIndex());
253 if (model->rowCount(topIndex) > 0) {
254 QModelIndex childIndex = model->index(0, 0, topIndex);
255 Q_ASSERT(model->parent(childIndex) == topIndex);
262 if (model->rowCount(topIndex1) > 0) {
263 QModelIndex childIndex = model->index(0, 0, topIndex);
264 QModelIndex childIndex1 = model->index(0, 0, topIndex1);
265 Q_ASSERT(childIndex != childIndex1);
287 void ModelTest::checkChildren(
const QModelIndex &parent,
int currentDepth)
295 if (model->canFetchMore(parent)) {
297 model->fetchMore(parent);
298 fetchingMore =
false;
301 int rows = model->rowCount(parent);
302 int columns = model->columnCount(parent);
305 Q_ASSERT(model->hasChildren(parent));
309 Q_ASSERT(columns >= 0);
311 Q_ASSERT(model->hasChildren(parent) ==
true);
316 Q_ASSERT(model->hasIndex(rows + 1, 0, parent) ==
false);
317 for (
int r = 0; r < rows; ++r) {
318 if (model->canFetchMore(parent)) {
320 model->fetchMore(parent);
321 fetchingMore =
false;
323 Q_ASSERT(model->hasIndex(r, columns + 1, parent) ==
false);
324 for (
int c = 0; c < columns; ++c) {
325 qDebug()<<
"index info is r="<<r<<
" c="<<c<<
"; parent="<<parent<<
"; model->index(...)="<<model->index(r,c,parent);
326 Q_ASSERT(model->hasIndex(r, c, parent) ==
true);
329 Q_ASSERT(index.isValid() ==
true);
332 QModelIndex modifiedIndex = model->index(r, c, parent);
333 Q_ASSERT(index == modifiedIndex);
341 Q_ASSERT(index.model() == model);
342 Q_ASSERT(index.row() == r);
343 Q_ASSERT(index.column() == c);
350 if (model->parent(index) != parent) {
351 qDebug() <<
"Bad parent!";
352 qDebug() << r << c << currentDepth << model->data(index).toString()
353 << model->data(parent).toString();
354 qDebug() << index << parent << model->
parent(index);
366 Q_ASSERT(model->parent(index) == parent);
369 if (model->hasChildren(index) && currentDepth < 10 ) {
371 checkChildren(index, ++currentDepth);
375 QModelIndex newerIndex = model->index(r, c, parent);
376 Q_ASSERT(index == newerIndex);
384 void ModelTest::data()
389 if (model->rowCount() == 0)
393 Q_ASSERT(model->index(0, 0).isValid());
399 QVariant variant = model->data(model->index(0, 0), Qt::ToolTipRole);
400 if (variant.isValid()) {
401 Q_ASSERT(qVariantCanConvert<QString>(variant));
403 variant = model->data(model->index(0, 0), Qt::StatusTipRole);
404 if (variant.isValid()) {
405 Q_ASSERT(qVariantCanConvert<QString>(variant));
407 variant = model->data(model->index(0, 0), Qt::WhatsThisRole);
408 if (variant.isValid()) {
409 Q_ASSERT(qVariantCanConvert<QString>(variant));
413 variant = model->data(model->index(0, 0), Qt::SizeHintRole);
414 if (variant.isValid()) {
415 Q_ASSERT(qVariantCanConvert<QSize>(variant));
419 QVariant fontVariant = model->data(model->index(0, 0), Qt::FontRole);
421 Q_ASSERT(qVariantCanConvert<QFont>(fontVariant));
425 QVariant textAlignmentVariant = model->data(model->index(0, 0), Qt::TextAlignmentRole);
426 if (textAlignmentVariant.
isValid()) {
427 int alignment = textAlignmentVariant.
toInt();
428 Q_ASSERT(alignment == Qt::AlignLeft ||
429 alignment == Qt::AlignRight ||
430 alignment == Qt::AlignHCenter ||
431 alignment == Qt::AlignJustify ||
432 alignment == Qt::AlignTop ||
433 alignment == Qt::AlignBottom ||
434 alignment == Qt::AlignVCenter ||
435 alignment == Qt::AlignCenter ||
436 alignment == Qt::AlignAbsolute ||
437 alignment == Qt::AlignLeading ||
438 alignment == Qt::AlignTrailing);
442 QVariant colorVariant = model->data(model->index(0, 0), Qt::BackgroundColorRole);
444 Q_ASSERT(qVariantCanConvert<QColor>(colorVariant));
447 colorVariant = model->data(model->index(0, 0), Qt::TextColorRole);
449 Q_ASSERT(qVariantCanConvert<QColor>(colorVariant));
453 QVariant checkStateVariant = model->data(model->index(0, 0), Qt::CheckStateRole);
454 if (checkStateVariant.
isValid()) {
455 int state = checkStateVariant.
toInt();
456 Q_ASSERT(state == Qt::Unchecked ||
457 state == Qt::PartiallyChecked ||
458 state == Qt::Checked);
467 void ModelTest::rowsAboutToBeInserted(
const QModelIndex &parent,
int start,
int end)
472 c.oldSize = model->rowCount(parent);
473 c.last = model->
data(model->index(start - 1, 0, parent));
474 c.next = model->data(model->index(start, 0, parent));
483 void ModelTest::rowsInserted(
const QModelIndex & parent,
int start,
int end)
485 Changing c = insert.pop();
486 Q_ASSERT(c.parent == parent);
487 Q_ASSERT(c.oldSize + (end - start + 1) == model->rowCount(parent));
488 Q_ASSERT(c.last == model->data(model->index(start - 1, 0, c.parent)));
497 Q_ASSERT(c.next == model->data(model->index(end + 1, 0, c.parent)));
500 void ModelTest::layoutAboutToBeChanged()
502 for (
int i = 0; i < qBound(0, model->rowCount(), 100); ++i)
506 void ModelTest::layoutChanged()
508 for (
int i = 0; i < changing.count(); ++i) {
520 void ModelTest::rowsAboutToBeRemoved(
const QModelIndex &parent,
int start,
int end)
524 c.oldSize = model->rowCount(parent);
525 c.last = model->
data(model->index(start - 1, 0, parent));
526 c.next = model->data(model->index(end + 1, 0, parent));
535 void ModelTest::rowsRemoved(
const QModelIndex & parent,
int start,
int end)
537 Changing c =
remove.pop();
538 Q_ASSERT(c.parent == parent);
539 Q_ASSERT(c.oldSize - (end - start + 1) == model->rowCount(parent));
540 Q_ASSERT(c.last == model->data(model->index(start - 1, 0, c.parent)));
541 Q_ASSERT(c.next == model->data(model->index(start, 0, c.parent)));
data(int role=Qt::DisplayRole)