summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/kernel/qitemmodel/tst_qitemmodel.cpp
blob: a4b1044cab1ecd1e281ef94c9a0246e54521fc68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest/QtTest>
#include <QtCore>
#include <qdebug.h>
#include "modelstotest.cpp"
#include <QMetaType>

Q_DECLARE_METATYPE(QModelIndex)

/*!
    See modelstotest.cpp for instructions on how to have your model tested with these tests.

    Each test such as rowCount have a _data() function which populate the QTest data with
    the tests specified by modelstotest.cpp and any extra data needed for that particular test.

    setupWithNoTestData() fills the QTest data with just the tests and is used by most tests.
 */
class tst_QItemModel : public QObject
{
    Q_OBJECT

public slots:
    void init();
    void cleanup();

private slots:
    void nonDestructiveBasicTest_data();
    void nonDestructiveBasicTest();

    void rowCount_data();
    void rowCount();
    void columnCount_data();
    void columnCount();

    void hasIndex_data();
    void hasIndex();
    void index_data();
    void index();

    void parent_data();
    void parent();

    void data_data();
    void data();

    void setData_data();
    void setData();

    void setHeaderData_data();
    void setHeaderData();

    void remove_data();
    void remove();

    void insert_data();
    void insert();

    void sort_data();
    void sort();

protected slots:
    void slot_rowsAboutToRemove(const QModelIndex &parent);
    void slot_rowsRemoved(const QModelIndex &parent);
    void slot_columnsAboutToRemove(const QModelIndex &parent);
    void slot_columnsRemoved(const QModelIndex &parent);

    void slot_rowsAboutToInserted(const QModelIndex &parent);
    void slot_rowsInserted(const QModelIndex &parent);
    void slot_columnsAboutToInserted(const QModelIndex &parent);
    void slot_columnsInserted(const QModelIndex &parent);
private:
    void setupWithNoTestData();
    QAbstractItemModel *currentModel;
    ModelsToTest *testModels;

    // used by remove()
    QPersistentModelIndex parentOfRemoved;
    int afterAboutToRemoveRowCount;
    int afterRemoveRowCount;
    int afterAboutToRemoveColumnCount;
    int afterRemoveColumnCount;

    // remove() recursive
    bool removeRecursively;

    // used by insert()
    QPersistentModelIndex parentOfInserted;
    int afterAboutToInsertRowCount;
    int afterInsertRowCount;
    int afterAboutToInsertColumnCount;
    int afterInsertColumnCount;

    // insert() recursive
    bool insertRecursively;
};

void tst_QItemModel::init()
{
    testModels = new ModelsToTest();
    removeRecursively = false;
    insertRecursively = false;
}

void tst_QItemModel::cleanup()
{
    testModels->cleanupTestArea(currentModel);
    delete testModels;
    delete currentModel;
    currentModel = 0;
}

void tst_QItemModel::setupWithNoTestData()
{
    ModelsToTest modelsToTest;
    QTest::addColumn<QString>("modelType");
    QTest::addColumn<bool>("readOnly");
    QTest::addColumn<bool>("isEmpty");
    for (int i = 0; i < modelsToTest.tests.size(); ++i) {
        ModelsToTest::test t = modelsToTest.tests.at(i);
        bool readOnly = (t.read == ModelsToTest::ReadOnly);
        bool isEmpty = (t.contains == ModelsToTest::Empty);
        QTest::newRow(t.modelType.toLatin1().data()) << t.modelType << readOnly << isEmpty;
    }
}

void tst_QItemModel::nonDestructiveBasicTest_data()
{
    setupWithNoTestData();
}

/*!
    nonDestructiveBasicTest tries to call a number of the basic functions (not all)
    to make sure the model doesn't segfault, testing the functions that makes sense.
 */
void tst_QItemModel::nonDestructiveBasicTest()
{
    QFETCH(QString, modelType);
    currentModel = testModels->createModel(modelType);
    QVERIFY(currentModel);

    QCOMPARE(currentModel->buddy(QModelIndex()), QModelIndex());
    currentModel->canFetchMore(QModelIndex());
    QVERIFY(currentModel->columnCount(QModelIndex()) >= 0);
    QCOMPARE(currentModel->data(QModelIndex()), QVariant());
    currentModel->fetchMore(QModelIndex());
    Qt::ItemFlags flags = currentModel->flags(QModelIndex());
    QVERIFY(flags == Qt::ItemIsDropEnabled || flags == 0);
    currentModel->hasChildren(QModelIndex());
    currentModel->hasIndex(0, 0);
    currentModel->headerData(0, Qt::Horizontal);
    currentModel->index(0,0), QModelIndex();
    currentModel->itemData(QModelIndex());
    QVariant cache;
    currentModel->match(QModelIndex(), -1, cache);
    currentModel->mimeTypes();
    QCOMPARE(currentModel->parent(QModelIndex()), QModelIndex());
    QVERIFY(currentModel->rowCount() >= 0);
    QVariant variant;
    currentModel->setData(QModelIndex(), variant, -1);
    currentModel->setHeaderData(-1, Qt::Horizontal, QVariant());
    currentModel->setHeaderData(0, Qt::Horizontal, QVariant());
    currentModel->setHeaderData(currentModel->columnCount() + 100, Qt::Horizontal, QVariant());
    QMap<int, QVariant> roles;
    currentModel->setItemData(QModelIndex(), roles);
    currentModel->sibling(0,0,QModelIndex());
    currentModel->span(QModelIndex());
    currentModel->supportedDropActions();
    currentModel->revert();
    currentModel->submit();
}


void tst_QItemModel::rowCount_data()
{
    setupWithNoTestData();
}

/*!
    Tests model's implimentation of QAbstractItemModel::rowCount() and hasChildren()
 */
void tst_QItemModel::rowCount()
{
    QFETCH(QString, modelType);
    currentModel = testModels->createModel(modelType);
    QVERIFY(currentModel);

    QFETCH(bool, isEmpty);
    if (isEmpty) {
        QCOMPARE(currentModel->rowCount(), 0);
        QCOMPARE(currentModel->hasChildren(), false);
    }
    else {
        QVERIFY(currentModel->rowCount() > 0);
        QCOMPARE(currentModel->hasChildren(), true);
    }

    // check top row
    QModelIndex topIndex = currentModel->index(0, 0, QModelIndex());
    int rows = currentModel->rowCount(topIndex);
    QVERIFY(rows >= 0);
    if (rows > 0)
        QCOMPARE(currentModel->hasChildren(topIndex), true);
    else
        QCOMPARE(currentModel->hasChildren(topIndex), false);

    QModelIndex secondLevelIndex = currentModel->index(0, 0, topIndex);
    if (secondLevelIndex.isValid()) { // not the top level
        // check a row count where parent is valid
        rows = currentModel->rowCount(secondLevelIndex);
        QVERIFY(rows >= 0);
        if (rows > 0)
            QCOMPARE(currentModel->hasChildren(secondLevelIndex), true);
        else
            QCOMPARE(currentModel->hasChildren(secondLevelIndex), false);
    }

    // rowCount is tested more extensivly more later in checkChildren(),
    // but this catches the big mistakes
}

void tst_QItemModel::columnCount_data()
{
    setupWithNoTestData();
}

/*!
    Tests model's implimentation of QAbstractItemModel::columnCount() and hasChildren()
 */
void tst_QItemModel::columnCount()
{
    QFETCH(QString, modelType);
    currentModel = testModels->createModel(modelType);
    QVERIFY(currentModel);

    QFETCH(bool, isEmpty);
    if (isEmpty) {
        QCOMPARE(currentModel->hasChildren(), false);
    }
    else {
        QVERIFY(currentModel->columnCount() > 0);
        QCOMPARE(currentModel->hasChildren(), true);
    }

    // check top row
    QModelIndex topIndex = currentModel->index(0, 0, QModelIndex());
    int columns = currentModel->columnCount(topIndex);

    // check a row count where parent is valid
    columns = currentModel->columnCount(currentModel->index(0, 0, topIndex));
    QVERIFY(columns >= 0);

    // columnCount is tested more extensivly more later in checkChildren(),
    // but this catches the big mistakes
}

void tst_QItemModel::hasIndex_data()
{
    setupWithNoTestData();
}

/*!
    Tests model's implimentation of QAbstractItemModel::hasIndex()
 */
void tst_QItemModel::hasIndex()
{
    QFETCH(QString, modelType);
    currentModel = testModels->createModel(modelType);
    QVERIFY(currentModel);

    // Make sure that invalid values returns an invalid index
    QCOMPARE(currentModel->hasIndex(-2, -2), false);
    QCOMPARE(currentModel->hasIndex(-2, 0), false);
    QCOMPARE(currentModel->hasIndex(0, -2), false);

    int rows = currentModel->rowCount();
    int columns = currentModel->columnCount();

    QCOMPARE(currentModel->hasIndex(rows, columns), false);
    QCOMPARE(currentModel->hasIndex(rows+1, columns+1), false);

    QFETCH(bool, isEmpty);
    if (isEmpty)
        return;

    QCOMPARE(currentModel->hasIndex(0,0), true);

    // hasIndex is tested more extensivly more later in checkChildren(),
    // but this catches the big mistakes
}

void tst_QItemModel::index_data()
{
    setupWithNoTestData();
}

/*!
    Tests model's implimentation of QAbstractItemModel::index()
 */
void tst_QItemModel::index()
{
    QFETCH(QString, modelType);
    currentModel = testModels->createModel(modelType);
    QVERIFY(currentModel);

    // Make sure that invalid values returns an invalid index
    QCOMPARE(currentModel->index(-2, -2), QModelIndex());
    QCOMPARE(currentModel->index(-2, 0), QModelIndex());
    QCOMPARE(currentModel->index(0, -2), QModelIndex());

    QFETCH(bool, isEmpty);
    if (isEmpty)
        return;

    int rows = currentModel->rowCount();
    int columns = currentModel->columnCount();

    // Catch off by one errors
    QCOMPARE(currentModel->index(rows,columns), QModelIndex());
    QCOMPARE(currentModel->index(0,0).isValid(), true);

    // Make sure that the same index is always returned
    QModelIndex a = currentModel->index(0,0);
    QModelIndex b = currentModel->index(0,0);
    QVERIFY(a == b);

    // index is tested more extensivly more later in checkChildren(),
    // but this catches the big mistakes
}


void tst_QItemModel::parent_data()
{
    setupWithNoTestData();
}

/*!
    A model that returns an index of parent X should also return X when asking
    for the parent of the index.

    This recursive function does pretty extensive testing on the whole model in an
    effort to catch edge cases.

    This function assumes that rowCount(), columnCount() and index() work.  If they have
    a bug it will point it out, but the above tests should have already found the basic bugs
    because it is easier to figure out the problem in those tests then this one.
 */
void checkChildren(QAbstractItemModel *currentModel, const QModelIndex &parent, int currentDepth=0)
{
    QFETCH(bool, readOnly);

    if (currentModel->canFetchMore(parent))
        currentModel->fetchMore(parent);

    int rows = currentModel->rowCount(parent);
    int columns = currentModel->columnCount(parent);

    QCOMPARE(rows > 0, (currentModel->hasChildren(parent)));

    // Some reasuring testing against rows(),columns(), and hasChildren()
    QVERIFY(rows >= 0);
    QVERIFY(columns >= 0);
    if (rows > 0 || columns > 0)
        QCOMPARE(currentModel->hasChildren(parent), true);
    else
        QCOMPARE(currentModel->hasChildren(parent), false);

    QCOMPARE(currentModel->hasIndex(rows+1, 0, parent), false);
    for (int r = 0; r < rows; ++r) {
        if (currentModel->canFetchMore(parent))
            currentModel->fetchMore(parent);

        QCOMPARE(currentModel->hasIndex(r, columns+1, parent), false);
        for (int c = 0; c < columns; ++c) {
            QCOMPARE(currentModel->hasIndex(r, c, parent), true);
            QModelIndex index = currentModel->index(r, c, parent);
            QCOMPARE(index.isValid(), true);

            if (!readOnly)
                currentModel->setData(index, "I'm a little tea pot short and stout");
            QModelIndex modifiedIndex = currentModel->index(r, c, parent);
            QCOMPARE(index, modifiedIndex);

            // Make sure we get the same index if we request it twice in a row
            QModelIndex a = currentModel->index(r, c, parent);
            QModelIndex b = currentModel->index(r, c, parent);
            QVERIFY(a == b);

            // Some basic checking on the index that is returned
            QVERIFY(index.model() == currentModel);
            QCOMPARE(index.row(), r);
            QCOMPARE(index.column(), c);
            QCOMPARE(currentModel->data(index, Qt::DisplayRole).isValid(), true);

            // If the next test fails here is some somewhat useful debug you play with.
            /*
            if (currentModel->parent(index) != parent) {
                qDebug() << r << c << currentDepth << currentModel->data(index).toString()
                         << currentModel->data(parent).toString();
                qDebug() << index << parent << currentModel->parent(index);
                QTreeView view;
                view.setModel(currentModel);
                view.show();
                QTest::qWait(9000);
            }*/
            QCOMPARE(currentModel->parent(index), parent);

            // recursivly go down
            if (currentModel->hasChildren(index) && currentDepth < 5) {
                checkChildren(currentModel, index, ++currentDepth);
                // Because this is recursive we will return at the first failure rather then
                // reporting it over and over
                if (QTest::currentTestFailed())
                    return;
            }

            // make sure that after testing the children that the index pointer doesn't change.
            QModelIndex newerIndex = currentModel->index(r, c, parent);
            QCOMPARE(index, newerIndex);
        }
    }
}

/*!
    Tests model's implimentation of QAbstractItemModel::parent()
 */
void tst_QItemModel::parent()
{
    QFETCH(QString, modelType);
    currentModel = testModels->createModel(modelType);
    QVERIFY(currentModel);

    // Make sure the model wont crash and will return an invalid QModelIndex
    // when asked for the parent of an invalid index.
    QCOMPARE(currentModel->parent(QModelIndex()), QModelIndex());

    QFETCH(bool, isEmpty);
    if (isEmpty)
        return;

    // Common error test #1, make sure that a top level index has a parent
    // that is a invalid QModelIndex.  You model
    QModelIndex topIndex = currentModel->index(0, 0, QModelIndex());
    QCOMPARE(currentModel->parent(topIndex), QModelIndex());

    // Common error test #2, make sure that a second level index has a parent
    // that is the top level index.
    if (currentModel->rowCount(topIndex) > 0) {
        QModelIndex childIndex = currentModel->index(0, 0, topIndex);
        QCOMPARE(currentModel->parent(childIndex), topIndex);
    }

    // Common error test #3, the second column has the same children
    // as the first column in a row.
    QModelIndex topIndex1 = currentModel->index(0, 1, QModelIndex());
    if (currentModel->rowCount(topIndex1) > 0) {
        QModelIndex childIndex = currentModel->index(0, 0, topIndex);
        QModelIndex childIndex1 = currentModel->index(0, 0, topIndex1);
        QVERIFY(childIndex != childIndex1);
    }

    // Full test, walk 10 levels deap through the model making sure that all
    // parents's children correctly specify their parent
    QModelIndex top = QModelIndex();
    checkChildren(currentModel, top);
}


void tst_QItemModel::data_data()
{
    setupWithNoTestData();
}

/*!
    Tests model's implimentation of QAbstractItemModel::data()
 */
void tst_QItemModel::data()
{
    QFETCH(QString, modelType);
    currentModel = testModels->createModel(modelType);
    QVERIFY(currentModel);

    // Invalid index should return an invalid qvariant
    QVERIFY(!currentModel->data(QModelIndex()).isValid());

    QFETCH(bool, isEmpty);
    if (isEmpty)
        return;

    // A valid index should have a valid qvariant data
    QVERIFY(currentModel->index(0,0).isValid());

    // shouldn't be able to set data on an invalid index
    QCOMPARE(currentModel->setData(QModelIndex(), "foo", Qt::DisplayRole), false);

    // General Purpose roles
    QVariant variant = currentModel->data(currentModel->index(0,0), Qt::ToolTipRole);
    if (variant.isValid()) {
        QVERIFY(qVariantCanConvert<QString>(variant));
    }
    variant = currentModel->data(currentModel->index(0,0), Qt::StatusTipRole);
    if (variant.isValid()) {
        QVERIFY(qVariantCanConvert<QString>(variant));
    }
    variant = currentModel->data(currentModel->index(0,0), Qt::WhatsThisRole);
    if (variant.isValid()) {
        QVERIFY(qVariantCanConvert<QString>(variant));
    }

    variant = currentModel->data(currentModel->index(0,0), Qt::SizeHintRole);
    if (variant.isValid()) {
        QVERIFY(qVariantCanConvert<QSize>(variant));
    }


    // Appearance roles
    QVariant fontVariant = currentModel->data(currentModel->index(0,0), Qt::FontRole);
    if (fontVariant.isValid()) {
        QVERIFY(qVariantCanConvert<QFont>(fontVariant));
    }

    QVariant textAlignmentVariant = currentModel->data(currentModel->index(0,0), Qt::TextAlignmentRole);
    if (textAlignmentVariant.isValid()) {
        int alignment = textAlignmentVariant.toInt();
        QVERIFY(alignment == Qt::AlignLeft ||
                alignment == Qt::AlignRight ||
                alignment == Qt::AlignHCenter ||
                alignment == Qt::AlignJustify);
    }

    QVariant colorVariant = currentModel->data(currentModel->index(0,0), Qt::BackgroundColorRole);
    if (colorVariant.isValid()) {
        QVERIFY(qVariantCanConvert<QColor>(colorVariant));
    }

    colorVariant = currentModel->data(currentModel->index(0,0), Qt::TextColorRole);
    if (colorVariant.isValid()) {
        QVERIFY(qVariantCanConvert<QColor>(colorVariant));
    }

    QVariant checkStateVariant = currentModel->data(currentModel->index(0,0), Qt::CheckStateRole);
    if (checkStateVariant.isValid()) {
        int state = checkStateVariant.toInt();
        QVERIFY(state == Qt::Unchecked ||
                state == Qt::PartiallyChecked ||
                state == Qt::Checked);
    }
}

void tst_QItemModel::setData_data()
{
    setupWithNoTestData();
}

/*!
    Tests model's implimentation of QAbstractItemModel::setData()
 */
void tst_QItemModel::setData()
{
    QFETCH(QString, modelType);
    currentModel = testModels->createModel(modelType);
    QVERIFY(currentModel);
    qRegisterMetaType<QModelIndex>("QModelIndex");
    QSignalSpy spy(currentModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)));
    QCOMPARE(currentModel->setData(QModelIndex(), QVariant()), false);
    QCOMPARE(spy.count(), 0);

    QFETCH(bool, isEmpty);
    if (isEmpty)
        return;

    QFETCH(bool, readOnly);
    if (readOnly)
        return;

    // Populate the test area so we can chage stuff.  See: cleanup()
    QModelIndex topIndex = testModels->populateTestArea(currentModel);
    QVERIFY(currentModel->hasChildren(topIndex));
    QModelIndex index = currentModel->index(0, 0, topIndex);
    QVERIFY(index.isValid());

    spy.clear();
    QString text = "Index private pointers should always be the same";
    QCOMPARE(currentModel->setData(index, text, Qt::EditRole), true);
    QCOMPARE(index.data(Qt::EditRole).toString(), text);

    // Changing the text shouldn't change the layout, parent, pointer etc.
    QModelIndex changedIndex = currentModel->index(0, 0, topIndex);
    QCOMPARE(changedIndex, index);
    QCOMPARE(spy.count(), 1);
}

void tst_QItemModel::setHeaderData_data()
{
    setupWithNoTestData();
}

/*!
    Tests model's implimentation of QAbstractItemModel::setHeaderData()
 */
void tst_QItemModel::setHeaderData()
{
    QFETCH(QString, modelType);
    currentModel = testModels->createModel(modelType);
    QVERIFY(currentModel);

    QCOMPARE(currentModel->setHeaderData(-1, Qt::Horizontal, QVariant()), false);
    QCOMPARE(currentModel->setHeaderData(-1, Qt::Vertical, QVariant()), false);

    QFETCH(bool, isEmpty);
    if (isEmpty)
        return;

    QFETCH(bool, readOnly);
    if (readOnly)
        return;

    // Populate the test area so we can change stuff.  See: cleanup()
    QModelIndex topIndex = testModels->populateTestArea(currentModel);
    QVERIFY(currentModel->hasChildren(topIndex));
    QModelIndex index = currentModel->index(0, 0, topIndex);
    QVERIFY(index.isValid());

    qRegisterMetaType<Qt::Orientation>("Qt::Orientation");
    QSignalSpy spy(currentModel, SIGNAL(headerDataChanged( Qt::Orientation, int , int )));

    QString text = "Index private pointers should always be the same";
    int signalCount = 0;
    for (int i = 0; i < 4; ++i){
        if(currentModel->setHeaderData(i, Qt::Horizontal, text)) {
            QCOMPARE(currentModel->headerData(i, Qt::Horizontal).toString(), text);
            ++signalCount;
        }
        if(currentModel->setHeaderData(i, Qt::Vertical, text)) {
            QCOMPARE(currentModel->headerData(i, Qt::Vertical).toString(), text);
            ++signalCount;
        }
    }
    QCOMPARE(spy.count(), signalCount);
}

void tst_QItemModel::sort_data()
{
    setupWithNoTestData();
}

/*!
    Tests model's implimentation of QAbstractItemModel::sort()
 */
void tst_QItemModel::sort()
{
    QFETCH(QString, modelType);
    currentModel = testModels->createModel(modelType);
    QVERIFY(currentModel);

    QFETCH(bool, isEmpty);
    if (isEmpty)
        return;

    // Populate the test area so we can chage stuff.  See: cleanup()
    QPersistentModelIndex topIndex = testModels->populateTestArea(currentModel);
    QVERIFY(currentModel->hasChildren(topIndex));
    QModelIndex index = currentModel->index(0, 0, topIndex);
    QVERIFY(index.isValid());
    QSignalSpy spy(currentModel, SIGNAL(layoutChanged()));
    for (int i=-1; i < 10; ++i){
        currentModel->sort(i);
        if (index != currentModel->index(0, 0, topIndex)){
            QVERIFY(spy.count() > 0);
            index = currentModel->index(0, 0, topIndex);
            spy.clear();
        }
    }
    currentModel->sort(99999);
}

/*!
    Tests model's implimentation of QAbstractItemModel::removeRow() and QAbstractItemModel::removeColumn()
 */
#define START 0
#define MIDDLE 6
#define END -1
#define MANY 9
#define ALL -1
#define NOSIGNALS 0
#define DEFAULTCOUNT 1
#define DNS 1 // DefaultNumberOfSignals
#define RECURSIVE true
#define SUCCESS true
#define FAIL false
void tst_QItemModel::remove_data()
{
    ModelsToTest modelsToTest;
    QTest::addColumn<QString>("modelType");
    QTest::addColumn<bool>("readOnly");
    QTest::addColumn<bool>("isEmpty");

    QTest::addColumn<int>("start");
    QTest::addColumn<int>("count");

    QTest::addColumn<int>("numberOfRowsAboutToBeRemovedSignals");
    QTest::addColumn<int>("numberOfColumnsAboutToBeRemovedSignals");
    QTest::addColumn<int>("numberOfRowsRemovedSignals");
    QTest::addColumn<int>("numberOfColumnsRemovedSignals");

    QTest::addColumn<bool>("recursive");
    QTest::addColumn<int>("recursiveRow");
    QTest::addColumn<int>("recursiveCount");

    QTest::addColumn<bool>("shouldSucceed");

#define makeTestRow(testName, start, count, sar, srr, sac, src, r, rr, rc, s) \
        QTest::newRow((t.modelType + testName).toLatin1().data()) << t.modelType << readOnly << isEmpty << \
        start << count << \
        sar << srr << sac << src << \
        r << rr << rc << \
        s;

    for (int i = 0; i < modelsToTest.tests.size(); ++i) {
        ModelsToTest::test t = modelsToTest.tests.at(i);
        QString name = t.modelType;
        bool readOnly = (t.read == ModelsToTest::ReadOnly);
        bool isEmpty = (t.contains == ModelsToTest::Empty);

        // half these
        makeTestRow(":one at the start",  START,  DEFAULTCOUNT, DNS, DNS, DNS, DNS, !RECURSIVE, 0, 0, SUCCESS);
        makeTestRow(":one at the middle", MIDDLE, DEFAULTCOUNT, DNS, DNS, DNS, DNS, !RECURSIVE, 0, 0, SUCCESS);
        makeTestRow(":one at the end",    END,    DEFAULTCOUNT, DNS, DNS, DNS, DNS, !RECURSIVE, 0, 0, SUCCESS);

        makeTestRow(":many at the start",  START,  MANY, DNS, DNS, DNS, DNS, !RECURSIVE, 0, 0, SUCCESS);
        makeTestRow(":many at the middle", MIDDLE, MANY, DNS, DNS, DNS, DNS, !RECURSIVE, 0, 0, SUCCESS);
        makeTestRow(":many at the end",    END,    MANY, DNS, DNS, DNS, DNS, !RECURSIVE, 0, 0, SUCCESS);

        makeTestRow(":remove all",        START,  ALL,  DNS, DNS, DNS, DNS, !RECURSIVE, 0, 0, SUCCESS);

        makeTestRow(":none at the start",  START,  0, NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":none at the middle", MIDDLE, 0, NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":none at the end",    END,    0, NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);

        makeTestRow(":invalid start, valid count", -99,  0,    NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":invalid start, valid count", 9999, 0,    NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":invalid start, valid count", -99,  1,    NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":invalid start, valid count", 9999, 1,    NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":invalid start, valid count", -99,  MANY, NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":invalid start, valid count", 9999, MANY, NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);

        makeTestRow(":valid start, invalid count",  START,  -2,   NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":valid start, invalid count",  MIDDLE, -2,   NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":valid start, invalid count",  END,    -2,   NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":valid start, invalid count",  START,  9999, NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":valid start, invalid count",  MIDDLE, 9999, NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":valid start, invalid count",  END,    9999, NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);

        // Recursive remove's might assert, haven't decided yet...
        //makeTestRow(":one at the start recursivly",  START,  DEFAULTCOUNT, 2, DNS, 2, DNS, RECURSIVE, START, DEFAULTCOUNT, FAIL);
        //makeTestRow(":one at the middle recursivly", MIDDLE, DEFAULTCOUNT, 2, DNS, 2, DNS, RECURSIVE, START, DEFAULTCOUNT, SUCCESS);
        //makeTestRow(":one at the end recursivly",    END,    DEFAULTCOUNT, 2, DNS, 2, DNS, RECURSIVE, START, DEFAULTCOUNT, SUCCESS);
    }
}

void tst_QItemModel::remove()
{
    QFETCH(QString, modelType);

    currentModel = testModels->createModel(modelType);
    QVERIFY(currentModel);

    QFETCH(bool, readOnly);
    if (readOnly)
        return;

    QFETCH(int, start);
    QFETCH(int, count);

    QFETCH(bool, recursive);
    removeRecursively = recursive;

/*!
    Removes count number of rows starting at start
    if count is -1 it removes all rows
    if start is -1 then it starts at the last row - count
 */
    QFETCH(bool, shouldSucceed);

    // Populate the test area so we can remove something.  See: cleanup()
    // parentOfRemoved is stored so that the slots can make sure parentOfRemoved is the index that is emitted.
    parentOfRemoved = testModels->populateTestArea(currentModel);

    if (count == -1)
        count = currentModel->rowCount(parentOfRemoved);
    if (start == -1)
        start = currentModel->rowCount(parentOfRemoved)-count;

    if (currentModel->rowCount(parentOfRemoved) == 0 ||
        currentModel->columnCount(parentOfRemoved) == 0) {
        qWarning() << "model test area doesn't have any rows or columns, can't fully test remove(). Skipping";
        return;
    }

    // When a row or column is removed there should be two signals.
    // Watch to make sure they are emitted and get the row/column count when they do get emitted by connecting them to a slot
    qRegisterMetaType<QModelIndex>("QModelIndex");
    QSignalSpy columnsAboutToBeRemovedSpy(currentModel, SIGNAL(columnsAboutToBeRemoved( const QModelIndex &, int , int )));
    QSignalSpy rowsAboutToBeRemovedSpy(currentModel, SIGNAL(rowsAboutToBeRemoved( const QModelIndex &, int , int )));
    QSignalSpy columnsRemovedSpy(currentModel, SIGNAL(columnsRemoved( const QModelIndex &, int, int )));
    QSignalSpy rowsRemovedSpy(currentModel, SIGNAL(rowsRemoved( const QModelIndex &, int, int )));
    QSignalSpy modelResetSpy(currentModel, SIGNAL(modelReset()));
    QSignalSpy modelLayoutChangedSpy(currentModel, SIGNAL(layoutChanged()));

    QFETCH(int, numberOfRowsAboutToBeRemovedSignals);
    QFETCH(int, numberOfColumnsAboutToBeRemovedSignals);
    QFETCH(int, numberOfRowsRemovedSignals);
    QFETCH(int, numberOfColumnsRemovedSignals);

    //
    // test removeRow()
    //
    connect(currentModel, SIGNAL(rowsAboutToBeRemoved( const QModelIndex &, int , int )),
            this, SLOT(slot_rowsAboutToRemove(const QModelIndex &)));
    connect(currentModel, SIGNAL(rowsRemoved( const QModelIndex &, int , int )),
            this, SLOT(slot_rowsRemoved(const QModelIndex &)));
    int beforeRemoveRowCount = currentModel->rowCount(parentOfRemoved);
    QPersistentModelIndex dyingIndex = currentModel->index(start + count + 1, 0, parentOfRemoved);
    QCOMPARE(currentModel->removeRows(start, count, parentOfRemoved), shouldSucceed);
    currentModel->submit();
    if (shouldSucceed && dyingIndex.isValid())
        QCOMPARE(dyingIndex.row(), start + 1);

    if (rowsAboutToBeRemovedSpy.count() > 0){
        QList<QVariant> arguments = rowsAboutToBeRemovedSpy.at(0);
        QModelIndex parent = (qvariant_cast<QModelIndex>(arguments.at(0)));
        int first = arguments.at(1).toInt();
        int last = arguments.at(2).toInt();
        QCOMPARE(first, start);
        QCOMPARE(last,  start + count - 1);
        QVERIFY(parentOfRemoved == parent);
    }

    if (rowsRemovedSpy.count() > 0){
        QList<QVariant> arguments = rowsRemovedSpy.at(0);
        QModelIndex parent = (qvariant_cast<QModelIndex>(arguments.at(0)));
        int first = arguments.at(1).toInt();
        int last = arguments.at(2).toInt();
        QCOMPARE(first, start);
        QCOMPARE(last,  start + count - 1);
        QVERIFY(parentOfRemoved == parent);
    }

    // Only the row signals should have been emitted
    if (modelResetSpy.count() >= 1 || modelLayoutChangedSpy.count() >=1 ){
        QCOMPARE(columnsAboutToBeRemovedSpy.count(), 0);
        QCOMPARE(rowsAboutToBeRemovedSpy.count(), 0);
        QCOMPARE(columnsRemovedSpy.count(), 0);
        QCOMPARE(rowsRemovedSpy.count(), 0);
    }
    else {
        QCOMPARE(columnsAboutToBeRemovedSpy.count(), 0);
        QCOMPARE(rowsAboutToBeRemovedSpy.count(), numberOfRowsAboutToBeRemovedSignals);
        QCOMPARE(columnsRemovedSpy.count(), 0);
        QCOMPARE(rowsRemovedSpy.count(), numberOfRowsRemovedSignals);
    }

    // The row count should only change *after* rowsAboutToBeRemoved has been emitted
    if (shouldSucceed) {
        if (modelResetSpy.count() == 0 && modelLayoutChangedSpy.count() == 0){
            QCOMPARE(afterAboutToRemoveRowCount, beforeRemoveRowCount);
            QCOMPARE(afterRemoveRowCount, beforeRemoveRowCount-count-(numberOfRowsRemovedSignals-1));
        }
        if (modelResetSpy.count() == 0 )
            QCOMPARE(currentModel->rowCount(parentOfRemoved), beforeRemoveRowCount-count-(numberOfRowsRemovedSignals-1));
    }
    else {
        if (recursive)
            QCOMPARE(currentModel->rowCount(parentOfRemoved), beforeRemoveRowCount-1);
        else
            QCOMPARE(currentModel->rowCount(parentOfRemoved), beforeRemoveRowCount);

    }
    disconnect(currentModel, SIGNAL(rowsAboutToBeRemoved( const QModelIndex &, int , int )),
            this, SLOT(slot_rowsAboutToRemove(const QModelIndex &)));
    disconnect(currentModel, SIGNAL(rowsRemoved( const QModelIndex &, int , int )),
            this, SLOT(slot_rowsRemoved(const QModelIndex &)));
    modelResetSpy.clear();
    QCOMPARE(modelResetSpy.count(), 0);

    //
    // Test remove column
    //
    connect(currentModel, SIGNAL(columnsAboutToBeRemoved( const QModelIndex &, int , int )),
            this, SLOT(slot_columnsAboutToRemove(const QModelIndex &)));
    connect(currentModel, SIGNAL(columnsRemoved( const QModelIndex &, int , int )),
            this, SLOT(slot_columnsRemoved(const QModelIndex &)));
    int beforeRemoveColumnCount = currentModel->columnCount(parentOfRemoved);

    // Some models don't let you remove the column, only row
    if (currentModel->removeColumns(start, count, parentOfRemoved)) {
        currentModel->submit();
        // Didn't reset the rows, so they should still be at the same value
        if (modelResetSpy.count() >= 1 || modelLayoutChangedSpy.count() >= 1){
            QCOMPARE(columnsAboutToBeRemovedSpy.count(), 0);
            //QCOMPARE(rowsAboutToBeRemovedSpy.count(), numberOfRowsAboutToBeRemovedSignals);
            QCOMPARE(columnsRemovedSpy.count(), 0);
            //QCOMPARE(rowsRemovedSpy.count(), numberOfRowsRemovedSignals);
        }
        else {
            QCOMPARE(columnsAboutToBeRemovedSpy.count(), numberOfColumnsAboutToBeRemovedSignals);
            QCOMPARE(rowsAboutToBeRemovedSpy.count(), numberOfRowsAboutToBeRemovedSignals);
            QCOMPARE(columnsRemovedSpy.count(), numberOfColumnsRemovedSignals);
            QCOMPARE(rowsRemovedSpy.count(), numberOfRowsRemovedSignals);
        }

        // The column count should only change *after* rowsAboutToBeRemoved has been emitted
        if (shouldSucceed) {
            if (modelResetSpy.count() == 0 && modelLayoutChangedSpy.count() == 0){
                QCOMPARE(afterAboutToRemoveColumnCount, beforeRemoveColumnCount);
                QCOMPARE(afterRemoveColumnCount, beforeRemoveColumnCount-count-(numberOfColumnsRemovedSignals-1));
            }
            if (modelResetSpy.count() == 0)
                QCOMPARE(currentModel->columnCount(parentOfRemoved), beforeRemoveColumnCount-count-(numberOfColumnsRemovedSignals-1));
        }
        else
            QCOMPARE(currentModel->rowCount(parentOfRemoved), beforeRemoveRowCount);
    }
    disconnect(currentModel, SIGNAL(columnsAboutToBeRemoved( const QModelIndex &, int , int )),
            this, SLOT(slot_columnsAboutToRemove(const QModelIndex &)));
    disconnect(currentModel, SIGNAL(columnsRemoved( const QModelIndex &, int , int )),
            this, SLOT(slot_columnsRemoved(const QModelIndex &)));

    if (columnsAboutToBeRemovedSpy.count() > 0){
        QList<QVariant> arguments = columnsAboutToBeRemovedSpy.at(0);
        QModelIndex parent = (qvariant_cast<QModelIndex>(arguments.at(0)));
        int first = arguments.at(1).toInt();
        int last = arguments.at(2).toInt();
        QCOMPARE(first, start);
        QCOMPARE(last,  start + count - 1);
        QVERIFY(parentOfRemoved == parent);
    }

    if (columnsRemovedSpy.count() > 0){
        QList<QVariant> arguments = columnsRemovedSpy.at(0);
        QModelIndex parent = (qvariant_cast<QModelIndex>(arguments.at(0)));
        int first = arguments.at(1).toInt();
        int last = arguments.at(2).toInt();
        QCOMPARE(first, start);
        QCOMPARE(last,  start + count - 1);
        QVERIFY(parentOfRemoved == parent);
    }

    // Cleanup the test area because remove::() is called multiple times in a test
    testModels->cleanupTestArea(currentModel);
}

/*!
    Developers like to use the slots to then *do* something on the model so it needs to be
    in a working state.
 */
void verifyState(QAbstractItemModel *currentModel) {
    // Make sure the model isn't confused right now and still knows what is root
    if (currentModel->hasChildren()) {
        QCOMPARE(currentModel->hasIndex(0, 0), true);
        QModelIndex index = currentModel->index(0,0);
        QCOMPARE(index.isValid(), true);
        QCOMPARE(currentModel->parent(index).isValid(), false);
    } else {
        QModelIndex index = currentModel->index(0,0);
        QCOMPARE(index.isValid(), false);
    }
}

void tst_QItemModel::slot_rowsAboutToRemove(const QModelIndex &parent)
{
    QVERIFY(parentOfRemoved == parent);
    afterAboutToRemoveRowCount = currentModel->rowCount(parent);
    // hasChildren() should still work
    if (afterAboutToRemoveRowCount > 0)
        QCOMPARE(currentModel->hasChildren(parent), true);
    else
        QCOMPARE(currentModel->hasChildren(parent), false);

    verifyState(currentModel);

    // This does happen
    if (removeRecursively) {
        QFETCH(int, recursiveRow);
        QFETCH(int, recursiveCount);
        removeRecursively = false;
        QCOMPARE(currentModel->removeRows(recursiveRow, recursiveCount, parent), true);
    }
}

void tst_QItemModel::slot_rowsRemoved(const QModelIndex &parent)
{
    QVERIFY(parentOfRemoved == parent);
    afterRemoveRowCount = currentModel->rowCount(parent);
    if (afterRemoveRowCount > 0)
        QCOMPARE(currentModel->hasChildren(parent), true);
    else
        QCOMPARE(currentModel->hasChildren(parent), false);

    verifyState(currentModel);
}

void tst_QItemModel::slot_columnsAboutToRemove(const QModelIndex &parent)
{
    QVERIFY(parentOfRemoved == parent);
    afterAboutToRemoveColumnCount = currentModel->columnCount(parent);
    // hasChildren() should still work
    if (afterAboutToRemoveColumnCount > 0 && currentModel->rowCount(parent) > 0)
        QCOMPARE(currentModel->hasChildren(parent), true);
    else
        QCOMPARE(currentModel->hasChildren(parent), false);

    verifyState(currentModel);
}

void tst_QItemModel::slot_columnsRemoved(const QModelIndex &parent)
{
    QVERIFY(parentOfRemoved == parent);
    afterRemoveColumnCount = currentModel->columnCount(parent);
    if (afterRemoveColumnCount > 0)
        QCOMPARE(currentModel->hasChildren(parent), true);
    else
        QCOMPARE(currentModel->hasChildren(parent), false);

    verifyState(currentModel);
}

/*!
    Tests the model's insertRow/Column()
 */
void tst_QItemModel::insert_data()
{
    ModelsToTest modelsToTest;
    QTest::addColumn<QString>("modelType");
    QTest::addColumn<bool>("readOnly");
    QTest::addColumn<bool>("isEmpty");

    QTest::addColumn<int>("start");
    QTest::addColumn<int>("count");

    QTest::addColumn<int>("numberOfRowsAboutToBeInsertedSignals");
    QTest::addColumn<int>("numberOfColumnsAboutToBeInsertedSignals");
    QTest::addColumn<int>("numberOfRowsInsertedSignals");
    QTest::addColumn<int>("numberOfColumnsInsertedSignals");

    QTest::addColumn<bool>("recursive");
    QTest::addColumn<int>("recursiveRow");
    QTest::addColumn<int>("recursiveCount");

    QTest::addColumn<bool>("shouldSucceed");

#define makeTestRow(testName, start, count, sar, srr, sac, src, r, rr, rc, s) \
        QTest::newRow((t.modelType + testName).toLatin1().data()) << t.modelType << readOnly << isEmpty << \
        start << count << \
        sar << srr << sac << src << \
        r << rr << rc << \
        s;

    for (int i = 0; i < modelsToTest.tests.size(); ++i) {
        ModelsToTest::test t = modelsToTest.tests.at(i);
        QString name = t.modelType;
        bool readOnly = (t.read == ModelsToTest::ReadOnly);
        bool isEmpty = (t.contains == ModelsToTest::Empty);

        // half these
        makeTestRow(":one at the start",  START,  DEFAULTCOUNT, DNS, DNS, DNS, DNS, !RECURSIVE, 0, 0, SUCCESS);
        makeTestRow(":one at the middle", MIDDLE, DEFAULTCOUNT, DNS, DNS, DNS, DNS, !RECURSIVE, 0, 0, SUCCESS);
        makeTestRow(":one at the end",    END,    DEFAULTCOUNT, DNS, DNS, DNS, DNS, !RECURSIVE, 0, 0, SUCCESS);

        makeTestRow(":many at the start",  START,  MANY, DNS, DNS, DNS, DNS, !RECURSIVE, 0, 0, SUCCESS);
        makeTestRow(":many at the middle", MIDDLE, MANY, DNS, DNS, DNS, DNS, !RECURSIVE, 0, 0, SUCCESS);
        makeTestRow(":many at the end",    END,    MANY, DNS, DNS, DNS, DNS, !RECURSIVE, 0, 0, SUCCESS);

        makeTestRow(":add row count",        START,  ALL,  DNS, DNS, DNS, DNS, !RECURSIVE, 0, 0, SUCCESS);

        makeTestRow(":none at the start",  START,  0, NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":none at the middle", MIDDLE, 0, NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":none at the end",    END,    0, NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);

        makeTestRow(":invalid start, valid count", -99,  0,    NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":invalid start, valid count", 9999, 0,    NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":invalid start, valid count", -99,  1,    NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":invalid start, valid count", 9999, 1,    NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":invalid start, valid count", -99,  MANY, NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":invalid start, valid count", 9999, MANY, NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);

        makeTestRow(":valid start, invalid count",  START,  -2,   NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":valid start, invalid count",  MIDDLE, -2,   NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);
        makeTestRow(":valid start, invalid count",  END,    -2,   NOSIGNALS, NOSIGNALS, NOSIGNALS, NOSIGNALS, !RECURSIVE, 0, 0, FAIL);

        // Recursive insert's might assert, haven't decided yet...
        //makeTestRow(":one at the start recursivly",  START,  DEFAULTCOUNT, 2, DNS, 2, DNS, RECURSIVE, START, DEFAULTCOUNT, FAIL);
        //makeTestRow(":one at the middle recursivly", MIDDLE, DEFAULTCOUNT, 2, DNS, 2, DNS, RECURSIVE, START, DEFAULTCOUNT, SUCCESS);
        //makeTestRow(":one at the end recursivly",    END,    DEFAULTCOUNT, 2, DNS, 2, DNS, RECURSIVE, START, DEFAULTCOUNT, SUCCESS);
    }
}

void tst_QItemModel::insert()
{
    QFETCH(QString, modelType);
    currentModel = testModels->createModel(modelType);
    QVERIFY(currentModel);

    QFETCH(bool, readOnly);
    if (readOnly)
        return;

    QFETCH(int, start);
    QFETCH(int, count);

    QFETCH(bool, recursive);
    insertRecursively = recursive;

/*!
    Inserts count number of rows starting at start
    if count is -1 it inserts all rows
    if start is -1 then it starts at the last row - count
 */
    QFETCH(bool, shouldSucceed);

    // Populate the test area so we can insert something.  See: cleanup()
    // parentOfInserted is stored so that the slots can make sure parentOfInserted is the index that is emitted.
    parentOfInserted = testModels->populateTestArea(currentModel);

    if (count == -1)
        count = currentModel->rowCount(parentOfInserted);
    if (start == -1)
        start = currentModel->rowCount(parentOfInserted)-count;

    if (currentModel->rowCount(parentOfInserted) == 0 ||
        currentModel->columnCount(parentOfInserted) == 0) {
        qWarning() << "model test area doesn't have any rows, can't fully test insert(). Skipping";
        return;
    }

    // When a row or column is inserted there should be two signals.
    // Watch to make sure they are emitted and get the row/column count when they do get emitted by connecting them to a slot
    qRegisterMetaType<QModelIndex>("QModelIndex");
    QSignalSpy columnsAboutToBeInsertedSpy(currentModel, SIGNAL(columnsAboutToBeInserted( const QModelIndex &, int , int )));
    QSignalSpy rowsAboutToBeInsertedSpy(currentModel, SIGNAL(rowsAboutToBeInserted( const QModelIndex &, int , int )));
    QSignalSpy columnsInsertedSpy(currentModel, SIGNAL(columnsInserted( const QModelIndex &, int, int )));
    QSignalSpy rowsInsertedSpy(currentModel, SIGNAL(rowsInserted( const QModelIndex &, int, int )));
    QSignalSpy modelResetSpy(currentModel, SIGNAL(modelReset()));
    QSignalSpy modelLayoutChangedSpy(currentModel, SIGNAL(layoutChanged()));

    QFETCH(int, numberOfRowsAboutToBeInsertedSignals);
    QFETCH(int, numberOfColumnsAboutToBeInsertedSignals);
    QFETCH(int, numberOfRowsInsertedSignals);
    QFETCH(int, numberOfColumnsInsertedSignals);

    //
    // test insertRow()
    //
    connect(currentModel, SIGNAL(rowsAboutToBeInserted( const QModelIndex &, int , int )),
            this, SLOT(slot_rowsAboutToInserted(const QModelIndex &)));
    connect(currentModel, SIGNAL(rowsInserted( const QModelIndex &, int , int )),
            this, SLOT(slot_rowsInserted(const QModelIndex &)));
    int beforeInsertRowCount = currentModel->rowCount(parentOfInserted);
    QCOMPARE(currentModel->insertRows(start, count, parentOfInserted), shouldSucceed);
    currentModel->submit();

    if (rowsAboutToBeInsertedSpy.count() > 0){
        QList<QVariant> arguments = rowsAboutToBeInsertedSpy.at(0);
        QModelIndex parent = (qvariant_cast<QModelIndex>(arguments.at(0)));
        int first = arguments.at(1).toInt();
        int last = arguments.at(2).toInt();
        QCOMPARE(first, start);
        QCOMPARE(last,  start + count - 1);
        QVERIFY(parentOfInserted == parent);
    }

    if (rowsInsertedSpy.count() > 0){
        QList<QVariant> arguments = rowsInsertedSpy.at(0);
        QModelIndex parent = (qvariant_cast<QModelIndex>(arguments.at(0)));
        int first = arguments.at(1).toInt();
        int last = arguments.at(2).toInt();
        QCOMPARE(first, start);
        QCOMPARE(last,  start + count - 1);
        QVERIFY(parentOfInserted == parent);
    }

    // Only the row signals should have been emitted
    if (modelResetSpy.count() >= 1 || modelLayoutChangedSpy.count() >= 1) {
        QCOMPARE(columnsAboutToBeInsertedSpy.count(), 0);
        QCOMPARE(rowsAboutToBeInsertedSpy.count(), 0);
        QCOMPARE(columnsInsertedSpy.count(), 0);
        QCOMPARE(rowsInsertedSpy.count(), 0);
    }
    else {
        QCOMPARE(columnsAboutToBeInsertedSpy.count(), 0);
        QCOMPARE(rowsAboutToBeInsertedSpy.count(), numberOfRowsAboutToBeInsertedSignals);
        QCOMPARE(columnsInsertedSpy.count(), 0);
        QCOMPARE(rowsInsertedSpy.count(), numberOfRowsInsertedSignals);
    }
    // The row count should only change *after* rowsAboutToBeInserted has been emitted
    if (shouldSucceed) {
        if (modelResetSpy.count() == 0 && modelLayoutChangedSpy.count() == 0) {
            QCOMPARE(afterAboutToInsertRowCount, beforeInsertRowCount);
            QCOMPARE(afterInsertRowCount, beforeInsertRowCount+count+(numberOfRowsInsertedSignals-1));
        }
        if (modelResetSpy.count() == 0)
            QCOMPARE(currentModel->rowCount(parentOfInserted), beforeInsertRowCount+count+(numberOfRowsInsertedSignals-1));
    }
    else {
        if (recursive)
            QCOMPARE(currentModel->rowCount(parentOfInserted), beforeInsertRowCount+1);
        else
            QCOMPARE(currentModel->rowCount(parentOfInserted), beforeInsertRowCount);

    }
    disconnect(currentModel, SIGNAL(rowsAboutToBeInserted( const QModelIndex &, int , int )),
            this, SLOT(slot_rowsAboutToInserted(const QModelIndex &)));
    disconnect(currentModel, SIGNAL(rowsInserted( const QModelIndex &, int , int )),
            this, SLOT(slot_rowsInserted(const QModelIndex &)));
    modelResetSpy.clear();

    //
    // Test insertColumn()
    //
    connect(currentModel, SIGNAL(columnsAboutToBeInserted( const QModelIndex &, int , int )),
            this, SLOT(slot_columnsAboutToInserted(const QModelIndex &)));
    connect(currentModel, SIGNAL(columnsInserted( const QModelIndex &, int , int )),
            this, SLOT(slot_columnsInserted(const QModelIndex &)));
    int beforeInsertColumnCount = currentModel->columnCount(parentOfInserted);

    // Some models don't let you insert the column, only row
    if (currentModel->insertColumns(start, count, parentOfInserted)) {
        currentModel->submit();
        if (modelResetSpy.count() >= 1 || modelLayoutChangedSpy.count() >= 1) {
            // Didn't reset the rows, so they should still be at the same value
            QCOMPARE(columnsAboutToBeInsertedSpy.count(), 0);
            //QCOMPARE(rowsAboutToBeInsertedSpy.count(), numberOfRowsAboutToBeInsertedSignals);
            QCOMPARE(columnsInsertedSpy.count(), 0);
            //QCOMPARE(rowsInsertedSpy.count(), numberOfRowsInsertedSignals);
        }
        else {
            // Didn't reset the rows, so they should still be at the same value
            QCOMPARE(columnsAboutToBeInsertedSpy.count(), numberOfColumnsAboutToBeInsertedSignals);
            QCOMPARE(rowsAboutToBeInsertedSpy.count(), numberOfRowsAboutToBeInsertedSignals);
            QCOMPARE(columnsInsertedSpy.count(), numberOfColumnsInsertedSignals);
            QCOMPARE(rowsInsertedSpy.count(), numberOfRowsInsertedSignals);
        }
        // The column count should only change *after* rowsAboutToBeInserted has been emitted
        if (shouldSucceed) {
            if (modelResetSpy.count() == 0 &&  modelLayoutChangedSpy.count() == 0) {
                QCOMPARE(afterAboutToInsertColumnCount, beforeInsertColumnCount);
                QCOMPARE(afterInsertColumnCount, beforeInsertColumnCount+count+(numberOfColumnsInsertedSignals-1));
            }
            if (modelResetSpy.count() == 0)
                QCOMPARE(currentModel->columnCount(parentOfInserted), beforeInsertColumnCount+count+(numberOfColumnsInsertedSignals-1));
        }
        else
            QCOMPARE(currentModel->rowCount(parentOfInserted), beforeInsertRowCount);
    }
    disconnect(currentModel, SIGNAL(columnsAboutToBeInserted( const QModelIndex &, int , int )),
            this, SLOT(slot_columnsAboutToInserted(const QModelIndex &)));
    disconnect(currentModel, SIGNAL(columnsInserted( const QModelIndex &, int , int )),
            this, SLOT(slot_columnsInserted(const QModelIndex &)));

    if (columnsAboutToBeInsertedSpy.count() > 0){
        QList<QVariant> arguments = columnsAboutToBeInsertedSpy.at(0);
        QModelIndex parent = (qvariant_cast<QModelIndex>(arguments.at(0)));
        int first = arguments.at(1).toInt();
        int last = arguments.at(2).toInt();
        QCOMPARE(first, start);
        QCOMPARE(last,  start + count - 1);
        QVERIFY(parentOfInserted == parent);
    }

    if (columnsInsertedSpy.count() > 0){
        QList<QVariant> arguments = columnsInsertedSpy.at(0);
        QModelIndex parent = (qvariant_cast<QModelIndex>(arguments.at(0)));
        int first = arguments.at(1).toInt();
        int last = arguments.at(2).toInt();
        QCOMPARE(first, start);
        QCOMPARE(last,  start + count - 1);
        QVERIFY(parentOfInserted == parent);
    }

    // Cleanup the test area because insert::() is called multiple times in a test
    testModels->cleanupTestArea(currentModel);
}

void tst_QItemModel::slot_rowsAboutToInserted(const QModelIndex &parent)
{
    QVERIFY(parentOfInserted == parent);
    afterAboutToInsertRowCount = currentModel->rowCount(parent);
    bool hasChildren = currentModel->hasChildren(parent);
    bool hasDimensions = currentModel->columnCount(parent) > 0 && currentModel->rowCount(parent) > 0;
    QCOMPARE(hasChildren, hasDimensions);
    verifyState(currentModel);

    // This does happen
    if (insertRecursively) {
        QFETCH(int, recursiveRow);
        QFETCH(int, recursiveCount);
        insertRecursively = false;
        QCOMPARE(currentModel->insertRows(recursiveRow, recursiveCount, parent), true);
    }
}

void tst_QItemModel::slot_rowsInserted(const QModelIndex &parent)
{
    QVERIFY(parentOfInserted == parent);
    afterInsertRowCount = currentModel->rowCount(parent);
    bool hasChildren = currentModel->hasChildren(parent);
    bool hasDimensions = currentModel->columnCount(parent) > 0 && currentModel->rowCount(parent) > 0;
    QCOMPARE(hasChildren, hasDimensions);
    verifyState(currentModel);
}

void tst_QItemModel::slot_columnsAboutToInserted(const QModelIndex &parent)
{
    QVERIFY(parentOfInserted == parent);
    afterAboutToInsertColumnCount = currentModel->columnCount(parent);
    bool hasChildren = currentModel->hasChildren(parent);
    bool hasDimensions = currentModel->columnCount(parent) > 0 && currentModel->rowCount(parent) > 0;
    QCOMPARE(hasChildren, hasDimensions);
    verifyState(currentModel);
}

void tst_QItemModel::slot_columnsInserted(const QModelIndex &parent)
{
    QVERIFY(parentOfInserted == parent);
    afterInsertColumnCount = currentModel->columnCount(parent);
    bool hasChildren = currentModel->hasChildren(parent);
    bool hasDimensions = currentModel->columnCount(parent) > 0 && currentModel->rowCount(parent) > 0;
    QCOMPARE(hasChildren, hasDimensions);
    verifyState(currentModel);
}

QTEST_MAIN(tst_QItemModel)
#include "tst_qitemmodel.moc"