56ModelTest::
ModelTest(QAbstractItemModel* _model, QObject* parent) : QObject(parent), model(_model), fetchingMore(false)
60 connect(model, &QAbstractItemModel::columnsAboutToBeInserted,
62 connect(model, &QAbstractItemModel::columnsAboutToBeRemoved,
64 connect(model, &QAbstractItemModel::columnsInserted,
66 connect(model, &QAbstractItemModel::columnsRemoved,
68 connect(model, &QAbstractItemModel::dataChanged,
70 connect(model, &QAbstractItemModel::headerDataChanged,
75 connect(model, &QAbstractItemModel::rowsAboutToBeInserted,
77 connect(model, &QAbstractItemModel::rowsAboutToBeRemoved,
79 connect(model, &QAbstractItemModel::rowsInserted,
81 connect(model, &QAbstractItemModel::rowsRemoved,
85 connect(model, &QAbstractItemModel::layoutAboutToBeChanged,
87 connect(model, &QAbstractItemModel::layoutChanged,
90 connect(model, &QAbstractItemModel::rowsAboutToBeInserted,
92 connect(model, &QAbstractItemModel::rowsAboutToBeRemoved,
94 connect(model, &QAbstractItemModel::rowsInserted,
96 connect(model, &QAbstractItemModel::rowsRemoved,
107 nonDestructiveBasicTest();
120void ModelTest::nonDestructiveBasicTest()
122 Q_ASSERT(model->buddy(QModelIndex()) == QModelIndex());
123 model->canFetchMore(QModelIndex());
124 Q_ASSERT(model->columnCount(QModelIndex()) >= 0);
125 Q_ASSERT(model->data(QModelIndex()) == QVariant());
127 model->fetchMore(QModelIndex());
128 fetchingMore =
false;
129 Qt::ItemFlags flags = model->flags(QModelIndex());
130 Q_ASSERT(flags == Qt::ItemIsDropEnabled || flags == 0);
131 model->hasChildren(QModelIndex());
132 model->hasIndex(0, 0);
133 model->headerData(0, Qt::Horizontal);
135 model->itemData(QModelIndex());
137 model->match(QModelIndex(), -1, cache);
139 Q_ASSERT(model->parent(QModelIndex()) == QModelIndex());
140 Q_ASSERT(model->rowCount() >= 0);
142 model->setData(QModelIndex(), variant, -1);
143 model->setHeaderData(-1, Qt::Horizontal, QVariant());
144 model->setHeaderData(999999, Qt::Horizontal, QVariant());
145 QMap<int, QVariant> roles;
146 model->sibling(0, 0, QModelIndex());
147 model->span(QModelIndex());
148 model->supportedDropActions();
156void ModelTest::rowCount()
160 QModelIndex topIndex = model->index(0, 0, QModelIndex());
161 int rows = model->rowCount(topIndex);
164 Q_ASSERT(model->hasChildren(topIndex) ==
true);
167 QModelIndex secondLevelIndex = model->index(0, 0, topIndex);
168 if (secondLevelIndex.isValid()) {
170 rows = model->rowCount(secondLevelIndex);
173 Q_ASSERT(model->hasChildren(secondLevelIndex) ==
true);
184void ModelTest::columnCount()
187 QModelIndex topIndex = model->index(0, 0, QModelIndex());
188 Q_ASSERT(model->columnCount(topIndex) >= 0);
191 QModelIndex childIndex = model->index(0, 0, topIndex);
192 if (childIndex.isValid()) {
193 Q_ASSERT(model->columnCount(childIndex) >= 0);
203void ModelTest::hasIndex()
207 Q_ASSERT(model->hasIndex(-2, -2) ==
false);
208 Q_ASSERT(model->hasIndex(-2, 0) ==
false);
209 Q_ASSERT(model->hasIndex(0, -2) ==
false);
211 int rows = model->rowCount();
212 int columns = model->columnCount();
215 Q_ASSERT(model->hasIndex(rows, columns) ==
false);
216 Q_ASSERT(model->hasIndex(rows + 1, columns + 1) ==
false);
219 Q_ASSERT(model->hasIndex(0, 0) ==
true);
229void ModelTest::index()
233 Q_ASSERT(model->index(-2, -2) == QModelIndex());
234 Q_ASSERT(model->index(-2, 0) == QModelIndex());
235 Q_ASSERT(model->index(0, -2) == QModelIndex());
237 int rows = model->rowCount();
238 int columns = model->columnCount();
245 Q_ASSERT(model->index(rows, columns) == QModelIndex());
246 Q_ASSERT(model->index(0, 0).isValid() ==
true);
249 QModelIndex a = model->index(0, 0);
250 QModelIndex b = model->index(0, 0);
260void ModelTest::parent()
265 Q_ASSERT(model->parent(QModelIndex()) == QModelIndex());
267 if (model->rowCount() == 0) {
278 QModelIndex topIndex = model->index(0, 0, QModelIndex());
279 Q_ASSERT(model->parent(topIndex) == QModelIndex());
283 if (model->rowCount(topIndex) > 0) {
284 QModelIndex childIndex = model->index(0, 0, topIndex);
285 Q_ASSERT(model->parent(childIndex) == topIndex);
291 QModelIndex topIndex1 = model->index(0, 1, QModelIndex());
292 if (model->rowCount(topIndex1) > 0) {
293 QModelIndex childIndex = model->index(0, 0, topIndex);
294 QModelIndex childIndex1 = model->index(0, 0, topIndex1);
295 Q_ASSERT(childIndex != childIndex1);
300 checkChildren(QModelIndex());
317void ModelTest::checkChildren(
const QModelIndex &parent,
int currentDepth)
320 QModelIndex p = parent;
321 while (p.isValid()) {
326 if (model->canFetchMore(parent)) {
328 model->fetchMore(parent);
329 fetchingMore =
false;
332 int rows = model->rowCount(parent);
333 int columns = model->columnCount(parent);
336 Q_ASSERT(model->hasChildren(parent));
341 Q_ASSERT(columns >= 0);
343 Q_ASSERT(model->hasChildren(parent) ==
true);
349 Q_ASSERT(model->hasIndex(rows + 1, 0, parent) ==
false);
350 for (
int r = 0; r < rows; ++r) {
351 if (model->canFetchMore(parent)) {
353 model->fetchMore(parent);
354 fetchingMore =
false;
356 Q_ASSERT(model->hasIndex(r, columns + 1, parent) ==
false);
357 for (
int c = 0; c < columns; ++c) {
358 Q_ASSERT(model->hasIndex(r, c, parent) ==
true);
359 QModelIndex index = model->index(r, c, parent);
361 Q_ASSERT(index.isValid() ==
true);
364 QModelIndex modifiedIndex = model->index(r, c, parent);
365 Q_ASSERT(index == modifiedIndex);
368 QModelIndex a = model->index(r, c, parent);
369 QModelIndex b = model->index(r, c, parent);
373 Q_ASSERT(index.model() == model);
374 Q_ASSERT(index.row() == r);
375 Q_ASSERT(index.column() == c);
378 Q_ASSERT ( model->data ( index, Qt::DisplayRole ).isValid() ==
true );
382 if (model->parent(index) != parent) {
383 qDebug() << r << c << currentDepth << model->data(index).toString()
384 << model->data(parent).toString();
385 qDebug() << index << parent << model->parent(index);
395 Q_ASSERT(model->parent(index) == parent);
398 if (model->hasChildren(index) && currentDepth < 10) {
400 checkChildren(index, ++currentDepth);
404 QModelIndex newerIndex = model->index(r, c, parent);
405 Q_ASSERT(index == newerIndex);
413void ModelTest::data()
416 Q_ASSERT(!model->data(QModelIndex()).isValid());
418 if (model->rowCount() == 0) {
423 Q_ASSERT(model->index(0, 0).isValid());
426 Q_ASSERT(model->setData(QModelIndex(), QLatin1String(
"foo"), Qt::DisplayRole) ==
false);
429 QVariant variant = model->data(model->index(0, 0), Qt::ToolTipRole);
430 if (variant.isValid()) {
431 Q_ASSERT(variant.canConvert<QString>());
433 variant = model->data(model->index(0, 0), Qt::StatusTipRole);
434 if (variant.isValid()) {
435 Q_ASSERT(variant.canConvert<QString>());
437 variant = model->data(model->index(0, 0), Qt::WhatsThisRole);
438 if (variant.isValid()) {
439 Q_ASSERT(variant.canConvert<QString>());
443 variant = model->data(model->index(0, 0), Qt::SizeHintRole);
444 if (variant.isValid()) {
445 Q_ASSERT(variant.canConvert<QSize>());
449 QVariant fontVariant = model->data(model->index(0, 0), Qt::FontRole);
450 if (fontVariant.isValid()) {
451 Q_ASSERT(fontVariant.canConvert<QFont>());
455 QVariant textAlignmentVariant = model->data(model->index(0, 0), Qt::TextAlignmentRole);
456 if (textAlignmentVariant.isValid()) {
457 int alignment = textAlignmentVariant.toInt();
458 Q_ASSERT(alignment ==
int(alignment & (Qt::AlignHorizontal_Mask | Qt::AlignVertical_Mask)));
462 QVariant colorVariant = model->data(model->index(0, 0), Qt::BackgroundRole);
463 if (colorVariant.isValid()) {
464 Q_ASSERT(colorVariant.canConvert<QColor>());
467 colorVariant = model->data(model->index(0, 0), Qt::ForegroundRole);
468 if (colorVariant.isValid()) {
469 Q_ASSERT(colorVariant.canConvert<QColor>());
473 QVariant checkStateVariant = model->data(model->index(0, 0), Qt::CheckStateRole);
474 if (checkStateVariant.isValid()) {
475 int state = checkStateVariant.toInt();
476 Q_ASSERT(
state == Qt::Unchecked ||
477 state == Qt::PartiallyChecked ||
478 state == Qt::Checked);
495 c.oldSize = model->rowCount(parent);
496 c.last = model->data(model->index(start - 1, 0, parent));
497 c.next = model->data(model->index(start, 0, parent));
508 Changing c = insert.pop();
509 Q_ASSERT(c.parent == parent);
519 Q_ASSERT(c.oldSize + (end - start + 1) == model->rowCount(parent));
520 Q_ASSERT(c.last == model->data(model->index(start - 1, 0, c.parent)));
522 if (c.next != model->data(model->index(end + 1, 0, c.parent))) {
523 qDebug() << start << end;
524 for (
int i = 0;
i < model->rowCount(); ++
i) {
525 qDebug() << model->index(
i, 0).data().toString();
527 qDebug() << c.next << model->data(model->index(end + 1, 0, c.parent));
530 Q_ASSERT(c.next == model->data(model->index(end + 1, 0, c.parent)));
535 for (
int i = 0;
i < qBound(0, model->rowCount(), 100); ++
i) {
536 changing.append(QPersistentModelIndex(model->index(
i, 0)));
542 for (
int i = 0;
i < changing.count(); ++
i) {
543 QPersistentModelIndex p = changing[
i];
544 Q_ASSERT(p == model->index(p.row(), p.column(), p.parent()));
556 qDebug() <<
"ratbr" << parent << start << end;
559 c.oldSize = model->rowCount(parent);
560 c.last = model->data(model->index(start - 1, 0, parent));
561 c.next = model->data(model->index(end + 1, 0, parent));
572 qDebug() <<
"rr" << parent << start << end;
573 Changing c = remove.pop();
574 Q_ASSERT(c.parent == parent);
575 Q_ASSERT(c.oldSize - (end - start + 1) == model->rowCount(parent));
576 Q_ASSERT(c.last == model->data(model->index(start - 1, 0, c.parent)));
577 Q_ASSERT(c.next == model->data(model->index(start, 0, c.parent)));
void rowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
void layoutAboutToBeChanged()
void rowsRemoved(const QModelIndex &parent, int start, int end)
void rowsInserted(const QModelIndex &parent, int start, int end)
Q_DECLARE_METATYPE(FlashCookie)