summaryrefslogtreecommitdiff
path: root/tests/auto/qquicktreemodeladaptor/tst_qquicktreemodeladaptor.cpp
blob: c3e77cf9b98307a1d875bb792e864afaba017b25 (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
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest/QtTest>
#include <QtCore>
#include <qquicktreemodeladaptor_p.h>
#include "../shared/testmodel.h"

class tst_QQuickTreeModelAdaptor : public QObject
{
    Q_OBJECT

public:
    void compareData(int row, QQuickTreeModelAdaptor1 &tma, const QModelIndex &idx, TestModel &model, bool expanded = false);
    void compareModels(QQuickTreeModelAdaptor1 &tma, TestModel &model);
    void expandAndTest(const QModelIndex &idx, QQuickTreeModelAdaptor1 &tma, bool expandable, int expectedRowCountDifference);
    void collapseAndTest(const QModelIndex &idx, QQuickTreeModelAdaptor1 &tma, bool expandable, int expectedRowCountDifference);
    bool rowRoleWasChanged(const QSignalSpy &captured, int row, int role);

private slots:
    void initTestCase();
    void cleanup();

    void setModel();
    void modelDestroyed();
    void modelReset();

    void rootIndex();

    void dataAccess();
    void dataChange();
    void groupedDataChange();

    void expandAndCollapse_data();
    void expandAndCollapse();
    void expandAndCollapse2ndLevel();

    void layoutChange();

    void removeRows_data();
    void removeRows();

    void removeRowsChildrenAndParent();
    void removeChildrenMoveParent();
    void removeChildrenRelayoutParent();

    void insertRows_data();
    void insertRows();

    void moveRows_data();
    void moveRows();
    void moveRowsDataChanged_data();
    void moveRowsDataChanged();
    void reparentOnSameRow();
    void moveAllChildren();

    void selectionForRowRange();

    void hasChildrenEmit();
};

void tst_QQuickTreeModelAdaptor::initTestCase()
{
}

void tst_QQuickTreeModelAdaptor::cleanup()
{
}

void tst_QQuickTreeModelAdaptor::compareData(int row, QQuickTreeModelAdaptor1 &tma, const QModelIndex &modelIdx, TestModel &model, bool expanded)
{
    const QModelIndex &tmaIdx = tma.index(row);
    const int indexDepth = model.level(modelIdx) - model.level(tma.rootIndex()) - 1;
    QCOMPARE(tma.mapToModel(tmaIdx), modelIdx);
    QCOMPARE(tma.data(tmaIdx, Qt::DisplayRole).toString(), model.displayData(modelIdx));
    QCOMPARE(tma.data(tmaIdx, QQuickTreeModelAdaptor1::DepthRole).toInt(), indexDepth);
    QCOMPARE(tma.data(tmaIdx, QQuickTreeModelAdaptor1::ExpandedRole).toBool(), expanded);
    QCOMPARE(tma.data(tmaIdx, QQuickTreeModelAdaptor1::HasChildrenRole).toBool(), model.hasChildren(modelIdx));
}

void tst_QQuickTreeModelAdaptor::expandAndTest(const QModelIndex &idx, QQuickTreeModelAdaptor1 &tma, bool expandable,
                                                  int expectedRowCountDifference)
{
    QSignalSpy rowsAboutToBeInsertedSpy(&tma, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)));
    QSignalSpy rowsInsertedSpy(&tma, SIGNAL(rowsInserted(QModelIndex,int,int)));
    QSignalSpy dataChangedSpy(&tma, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));

    int oldRowCount = tma.rowCount();
    tma.expand(idx);
    QCOMPARE(tma.isExpanded(idx), expandable);

    const QModelIndex &tmaIdx = tma.index(tma.itemIndex(idx));
    QCOMPARE(tma.data(tmaIdx, QQuickTreeModelAdaptor1::ExpandedRole).toBool(), expandable);

    if (expandable && expectedRowCountDifference != 0) {
        // Rows were added below the parent
        QCOMPARE(tma.rowCount(), oldRowCount + expectedRowCountDifference);
        QCOMPARE(rowsAboutToBeInsertedSpy.count(), rowsInsertedSpy.count());
        QVERIFY(rowsInsertedSpy.count() > 0);
        if (rowsInsertedSpy.count() == 1) {
            const QVariantList &rowsAboutToBeInsertedArgs = rowsAboutToBeInsertedSpy.takeFirst();
            const QVariantList &rowsInsertedArgs = rowsInsertedSpy.takeFirst();
            for (int i = 0; i < rowsInsertedArgs.count(); i++)
                QCOMPARE(rowsAboutToBeInsertedArgs.at(i), rowsInsertedArgs.at(i));
            QCOMPARE(rowsInsertedArgs.at(0).toModelIndex(), QModelIndex());
            QCOMPARE(rowsInsertedArgs.at(1).toInt(), tma.itemIndex(idx) + 1);
            QCOMPARE(rowsInsertedArgs.at(2).toInt(), tma.itemIndex(idx) + expectedRowCountDifference);
        }

        // Data changed for the parent's ExpandedRole (value checked above)
        QCOMPARE(dataChangedSpy.count(), 1);
        const QVariantList &dataChangedArgs = dataChangedSpy.first();
        QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tmaIdx);
        QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tmaIdx);
        QCOMPARE(dataChangedArgs.at(2).value<QVector<int> >(), QVector<int>(1, QQuickTreeModelAdaptor1::ExpandedRole));
    } else {
        QCOMPARE(tma.rowCount(), oldRowCount);
        QCOMPARE(rowsAboutToBeInsertedSpy.count(), 0);
        QCOMPARE(rowsInsertedSpy.count(), 0);
    }
}

void tst_QQuickTreeModelAdaptor::collapseAndTest(const QModelIndex &idx, QQuickTreeModelAdaptor1 &tma,
                                                 bool expandable, int expectedRowCountDifference)
{
    QSignalSpy rowsAboutToBeRemovedSpy(&tma, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)));
    QSignalSpy rowsRemovedSpy(&tma, SIGNAL(rowsRemoved(QModelIndex,int,int)));
    QSignalSpy dataChangedSpy(&tma, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));

    int oldRowCount = tma.rowCount();
    tma.collapse(idx);
    QVERIFY(!tma.isExpanded(idx));

    const QModelIndex &tmaIdx = tma.index(tma.itemIndex(idx));
    if (tmaIdx.isValid())
        QCOMPARE(tma.data(tmaIdx, QQuickTreeModelAdaptor1::ExpandedRole).toBool(), false);

    if (expandable && expectedRowCountDifference != 0) {
        // Rows were removed below the parent
        QCOMPARE(tma.rowCount(), oldRowCount - expectedRowCountDifference);
        QCOMPARE(rowsAboutToBeRemovedSpy.count(), 1);
        QCOMPARE(rowsRemovedSpy.count(), 1);
        const QVariantList &rowsAboutToBeRemovedArgs = rowsAboutToBeRemovedSpy.takeFirst();
        const QVariantList &rowsRemovedArgs = rowsRemovedSpy.takeFirst();
        for (int i = 0; i < rowsRemovedArgs.count(); i++)
            QCOMPARE(rowsAboutToBeRemovedArgs.at(i), rowsRemovedArgs.at(i));
        QCOMPARE(rowsRemovedArgs.at(0).toModelIndex(), QModelIndex());
        QCOMPARE(rowsRemovedArgs.at(1).toInt(), tma.itemIndex(idx) + 1);
        QCOMPARE(rowsRemovedArgs.at(2).toInt(), tma.itemIndex(idx) + expectedRowCountDifference);

        // Data changed for the parent's ExpandedRole (value checked above)
        QVERIFY(dataChangedSpy.count() >= 1);
        const QVariantList &dataChangedArgs = dataChangedSpy.first();
        QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tmaIdx);
        QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tmaIdx);
        QCOMPARE(dataChangedArgs.at(2).value<QVector<int> >(), QVector<int>(1, QQuickTreeModelAdaptor1::ExpandedRole));
    } else {
        QCOMPARE(tma.rowCount(), oldRowCount);
        QCOMPARE(rowsAboutToBeRemovedSpy.count(), 0);
        QCOMPARE(rowsRemovedSpy.count(), 0);
    }
}

bool tst_QQuickTreeModelAdaptor::rowRoleWasChanged(const QSignalSpy &captured, int row, int role)
{
    for (const QVariantList &args : captured) {
        const int startRow = args.at(0).toModelIndex().row();
        const int endRow = args.at(1).toModelIndex().row();
        if (row >= startRow && row <= endRow) {
            const QVector<int> roles = args.at(2).value<QVector<int>>();
            if (roles.contains(role))
                return true;
        }
    }
    return false;
}

void tst_QQuickTreeModelAdaptor::compareModels(QQuickTreeModelAdaptor1 &tma, TestModel &model)
{
    QModelIndex parent = tma.rootIndex();
    QStack<QModelIndex> parents;
    QModelIndex idx = model.index(0, 0, parent);
    int modelVisibleRows = model.rowCount(parent);
    for (int i = 0; i < tma.rowCount(); i++) {
        bool expanded = tma.isExpanded(i);
        compareData(i, tma, idx, model, expanded);
        if (expanded) {
            parents.push(parent);
            parent = idx;
            modelVisibleRows += model.rowCount(parent);
            idx = model.index(0, 0, parent);
        } else {
            while (idx.row() == model.rowCount(parent) - 1) {
                if (parents.isEmpty())
                    break;
                idx = parent;
                parent = parents.pop();
            }
            idx = model.index(idx.row() + 1, 0, parent);
        }
    }
    QCOMPARE(tma.rowCount(), modelVisibleRows);

    // Duplicates the model inspection above, but provides extra tests
    QVERIFY(tma.testConsistency());
}

void tst_QQuickTreeModelAdaptor::setModel()
{
    TestModel model(5, 1);
    QQuickTreeModelAdaptor1 tma;

    QSignalSpy modelChangedSpy(&tma, SIGNAL(modelChanged(QAbstractItemModel*)));
    tma.setModel(&model);
    QCOMPARE(modelChangedSpy.count(), 1);
    QCOMPARE(tma.model(), &model);

    // Set same model twice
    tma.setModel(&model);
    QCOMPARE(modelChangedSpy.count(), 1);

    modelChangedSpy.clear();
    tma.setModel(0);
    QCOMPARE(modelChangedSpy.count(), 1);
    QCOMPARE(tma.model(), static_cast<QAbstractItemModel *>(0));
}

void tst_QQuickTreeModelAdaptor::modelDestroyed()
{
    TestModel *model = new TestModel(5, 1);
    QQuickTreeModelAdaptor1 tma;

    QSignalSpy modelChangedSpy(&tma, SIGNAL(modelChanged(QAbstractItemModel*)));
    tma.setModel(model);
    QCOMPARE(modelChangedSpy.count(), 1);
    QCOMPARE(tma.model(), model);

    QModelIndex idx = model->index(0, 0);
    modelChangedSpy.clear();
    delete model;
    QCOMPARE(modelChangedSpy.count(), 1);
    QCOMPARE(tma.model(), (QAbstractItemModel *)0);
    tma.expand(idx); // No crash, all fine
}

void tst_QQuickTreeModelAdaptor::modelReset()
{
    TestModel model(5, 1);
    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    QSignalSpy modelAboutToBeResetSpy(&tma, SIGNAL(modelAboutToBeReset()));
    QSignalSpy modelResetSpy(&tma, SIGNAL(modelReset()));

    // Nothing expanded
    model.resetModel();
    QCOMPARE(modelAboutToBeResetSpy.count(), 1);
    QCOMPARE(modelResetSpy.count(), 1);
    QCOMPARE(tma.rowCount(), model.rowCount());
    compareModels(tma, model);

    // Expanded items should not be anymore
    tma.expand(model.index(0, 0));
    tma.expand(model.index(2, 0));
    tma.expand(model.index(2, 0, model.index(2, 0)));
    modelAboutToBeResetSpy.clear();
    modelResetSpy.clear();
    model.resetModel();
    QCOMPARE(modelAboutToBeResetSpy.count(), 1);
    QCOMPARE(modelResetSpy.count(), 1);
    QCOMPARE(tma.rowCount(), model.rowCount());
    compareModels(tma, model);
}

void tst_QQuickTreeModelAdaptor::rootIndex()
{
    TestModel model(5, 1);

    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    QVERIFY(!tma.rootIndex().isValid());
    compareModels(tma, model);

    QSignalSpy rootIndexSpy(&tma, SIGNAL(rootIndexChanged()));
    QModelIndex rootIndex = model.index(0, 0);
    tma.setRootIndex(rootIndex);
    QCOMPARE(tma.rootIndex(), rootIndex);
    QCOMPARE(rootIndexSpy.count(), 1);
    compareModels(tma, model);

    rootIndexSpy.clear();
    rootIndex = model.index(2, 2, tma.rootIndex());
    tma.setRootIndex(rootIndex);
    QCOMPARE(tma.rootIndex(), rootIndex);
    QCOMPARE(rootIndexSpy.count(), 1);
    compareModels(tma, model);

    // Expand 1st visible item, business as usual
    expandAndTest(model.index(0, 0, rootIndex), tma, true /*expandable*/, 5);
    // Expand non root item descendant item, nothing should happen
    expandAndTest(model.index(0, 0), tma, true /*expandable*/, 0);
    // Collapse 1st visible item, business as usual
    collapseAndTest(model.index(0, 0, rootIndex), tma, true /*expandable*/, 5);
    // Collapse non root item descendant item, nothing should happen
    collapseAndTest(model.index(0, 0), tma, true /*expandable*/, 0);
}

void tst_QQuickTreeModelAdaptor::dataAccess()
{
    TestModel model(5, 1);

    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    QCOMPARE(tma.rowCount(), model.rowCount());
    compareModels(tma, model);

    QModelIndex parentIdx = model.index(2, 0);
    QVERIFY(model.hasChildren(parentIdx));
    tma.expand(parentIdx);
    QVERIFY(tma.isExpanded(parentIdx));
    QCOMPARE(tma.rowCount(), model.rowCount() + model.rowCount(parentIdx));
    compareModels(tma, model);

    tma.collapse(parentIdx);
    QCOMPARE(tma.rowCount(), model.rowCount());
    compareModels(tma, model);
}

void tst_QQuickTreeModelAdaptor::dataChange()
{
    TestModel model(5, 1);

    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    QSignalSpy dataChangedSpy(&tma, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
    const QModelIndex &idx = model.index(2, 0);
    model.setData(idx, QVariant(), Qt::DisplayRole);
    QCOMPARE(dataChangedSpy.count(), 1);
    const QVariantList &dataChangedArgs = dataChangedSpy.first();
    const QModelIndex &tmaIdx = tma.index(tma.itemIndex(idx));
    QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tmaIdx);
    QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tmaIdx);
    QCOMPARE(dataChangedArgs.at(2).value<QVector<int> >(), QVector<int>(1, Qt::DisplayRole));
    compareModels(tma, model);

    {
        // Non expanded children shouldn't emit any signal
        dataChangedSpy.clear();
        const QModelIndex &childIdx = model.index(4, 0, idx);
        model.setData(childIdx, QVariant(), Qt::DisplayRole);
        QCOMPARE(dataChangedSpy.count(), 0);
        compareModels(tma, model);

        // But expanded children should
        tma.expand(idx);
        QVERIFY(tma.isExpanded(idx));
        dataChangedSpy.clear(); // expand() emits dataChanged() with ExpandedRole
        model.setData(childIdx, QVariant(), Qt::DisplayRole);
        QCOMPARE(dataChangedSpy.count(), 1);
        const QVariantList &dataChangedArgs = dataChangedSpy.first();
        const QModelIndex &tmaIdx = tma.index(tma.itemIndex(childIdx));
        QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tmaIdx);
        QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tmaIdx);
        QCOMPARE(dataChangedArgs.at(2).value<QVector<int> >(), QVector<int>(1, Qt::DisplayRole));
        compareModels(tma, model);
    }
}

void tst_QQuickTreeModelAdaptor::groupedDataChange()
{
    TestModel model(10, 1);
    const QModelIndex &topLeftIdx = model.index(1, 0);
    const QModelIndex &bottomRightIdx = model.index(7, 0);

    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    QSignalSpy dataChangedSpy(&tma, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
    const QVector<int> roles(1, Qt::DisplayRole);

    {
        // No expanded items
        model.groupedSetData(topLeftIdx, bottomRightIdx, roles);
        QCOMPARE(dataChangedSpy.count(), 1);
        compareModels(tma, model);

        const QModelIndex &tmaTLIdx = tma.index(tma.itemIndex(topLeftIdx));
        const QModelIndex &tmaBRIdx = tma.index(tma.itemIndex(bottomRightIdx));
        const QVariantList &dataChangedArgs = dataChangedSpy.first();
        QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tmaTLIdx);
        QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tmaBRIdx);
        QCOMPARE(dataChangedArgs.at(2).value<QVector<int> >(), roles);
    }

    // One item expanded in the group range
    const QModelIndex &expandedIdx = model.index(4, 0);
    tma.expand(expandedIdx);
    QVERIFY(tma.isExpanded(expandedIdx));

    for (int i = 0; i < 2; i++) {
        const QModelIndex &tmaTLIdx = tma.index(tma.itemIndex(topLeftIdx));
        const QModelIndex &tmaExpandedIdx = tma.index(tma.itemIndex(expandedIdx));
        const QModelIndex &tmaExpandedSiblingIdx = tma.index(tma.itemIndex(expandedIdx.sibling(expandedIdx.row() + 1, 0)));
        const QModelIndex &tmaBRIdx = tma.index(tma.itemIndex(bottomRightIdx));

        dataChangedSpy.clear(); // expand() sends a dataChaned() signal
        model.groupedSetData(topLeftIdx, bottomRightIdx, roles);
        QCOMPARE(dataChangedSpy.count(), 2);
        compareModels(tma, model);

        QVariantList dataChangedArgs = dataChangedSpy.takeFirst();
        QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tmaTLIdx);
        QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tmaExpandedIdx);
        QCOMPARE(dataChangedArgs.at(2).value<QVector<int> >(), roles);

        dataChangedArgs = dataChangedSpy.takeFirst();
        QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tmaExpandedSiblingIdx);
        QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tmaBRIdx);
        QCOMPARE(dataChangedArgs.at(2).value<QVector<int> >(), roles);

        // Further expanded descendants should not change grouping
        tma.expand(model.index(0, 0, expandedIdx));
        QVERIFY(tma.isExpanded(expandedIdx));
    }
    tma.collapse(model.index(0, 0, expandedIdx));

    // Let's expand one more and see what happens...
    const QModelIndex &otherExpandedIdx = model.index(6, 0);
    tma.expand(otherExpandedIdx);
    QVERIFY(tma.isExpanded(otherExpandedIdx));

    for (int i = 0; i < 3; i++) {
        const QModelIndex &tmaTLIdx = tma.index(tma.itemIndex(topLeftIdx));
        const QModelIndex &tmaExpandedIdx = tma.index(tma.itemIndex(expandedIdx));
        const QModelIndex &tmaExpandedSiblingIdx = tma.index(tma.itemIndex(expandedIdx.sibling(expandedIdx.row() + 1, 0)));
        const QModelIndex &tmaOtherExpandedIdx = tma.index(tma.itemIndex(otherExpandedIdx));
        const QModelIndex &tmaOtherExpandedSiblingIdx = tma.index(tma.itemIndex(otherExpandedIdx.sibling(otherExpandedIdx.row() + 1, 0)));
        const QModelIndex &tmaBRIdx = tma.index(tma.itemIndex(bottomRightIdx));

        dataChangedSpy.clear(); // expand() sends a dataChaned() signal
        model.groupedSetData(topLeftIdx, bottomRightIdx, roles);
        QCOMPARE(dataChangedSpy.count(), 3);
        compareModels(tma, model);

        QVariantList dataChangedArgs = dataChangedSpy.takeFirst();
        QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tmaTLIdx);
        QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tmaExpandedIdx);
        QCOMPARE(dataChangedArgs.at(2).value<QVector<int> >(), roles);

        dataChangedArgs = dataChangedSpy.takeFirst();
        QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tmaExpandedSiblingIdx);
        QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tmaOtherExpandedIdx);
        QCOMPARE(dataChangedArgs.at(2).value<QVector<int> >(), roles);

        dataChangedArgs = dataChangedSpy.takeFirst();
        QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tmaOtherExpandedSiblingIdx);
        QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tmaBRIdx);
        QCOMPARE(dataChangedArgs.at(2).value<QVector<int> >(), roles);

        // Further expanded descendants should not change grouping
        if (i == 0) {
            tma.expand(model.index(0, 0, expandedIdx));
            QVERIFY(tma.isExpanded(expandedIdx));
        } else {
            tma.expand(model.index(0, 0, otherExpandedIdx));
            QVERIFY(tma.isExpanded(expandedIdx));
        }
    }
}

void tst_QQuickTreeModelAdaptor::expandAndCollapse_data()
{
    QTest::addColumn<int>("parentRow");
    QTest::newRow("First") << 0;
    QTest::newRow("Middle") << 2;
    QTest::newRow("Last") << 4;
    QTest::newRow("Non expandable") << 3;
}

void tst_QQuickTreeModelAdaptor::expandAndCollapse()
{
    QFETCH(int, parentRow);
    TestModel model(5, 1);
    const QModelIndex &parentIdx = model.index(parentRow, 0);
    bool expandable = model.hasChildren(parentIdx);

    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    expandAndTest(parentIdx, tma, expandable, model.rowCount(parentIdx));
    compareModels(tma, model);

    collapseAndTest(parentIdx, tma, expandable, model.rowCount(parentIdx));
    compareModels(tma, model);
}

void tst_QQuickTreeModelAdaptor::expandAndCollapse2ndLevel()
{
    const int expandRows[] = { 0, 2, 4, 3 };
    const int expandRowsCount = sizeof(expandRows) / sizeof(expandRows[0]);
    for (int i = 0; i < expandRowsCount - 1; i++) { // Skip last non-expandable row
        TestModel model(5, 1);
        const QModelIndex &parentIdx = model.index(expandRows[i], 0);
        QVERIFY(model.hasChildren(parentIdx));

        QQuickTreeModelAdaptor1 tma;
        tma.setModel(&model);

        tma.expand(parentIdx);
        QVERIFY(tma.isExpanded(parentIdx));
        QCOMPARE(tma.rowCount(), model.rowCount() + model.rowCount(parentIdx));

        for (int j = 0; j < expandRowsCount; j++) {
            const QModelIndex &childIdx = model.index(expandRows[j], 0, parentIdx);
            bool expandable = model.hasChildren(childIdx);

            // Expand child
            expandAndTest(childIdx, tma, expandable, model.rowCount(childIdx));
            compareModels(tma, model);
            // Collapse child
            collapseAndTest(childIdx, tma, expandable, model.rowCount(childIdx));
            compareModels(tma, model);

            // Expand child again
            expandAndTest(childIdx, tma, expandable, model.rowCount(childIdx));
            compareModels(tma, model);
            // Collapse parent -> child node invisible, but expanded
            collapseAndTest(parentIdx, tma, true, model.rowCount(parentIdx) + model.rowCount(childIdx));
            compareModels(tma, model);
            QCOMPARE(tma.isExpanded(childIdx), expandable);
            // Expand parent again
            expandAndTest(parentIdx, tma, true, model.rowCount(parentIdx) + model.rowCount(childIdx));
            compareModels(tma, model);

            // Collapse parent -> child node invisible, but expanded
            collapseAndTest(parentIdx, tma, true, model.rowCount(parentIdx) + model.rowCount(childIdx));
            compareModels(tma, model);
            QCOMPARE(tma.isExpanded(childIdx), expandable);
            // Collapse child -> nothing should change
            collapseAndTest(childIdx, tma, false, 0);
            compareModels(tma, model);
            // Expand parent again
            expandAndTest(parentIdx, tma, true, model.rowCount(parentIdx));
            compareModels(tma, model);

            // Expand child, one last time
            expandAndTest(childIdx, tma, expandable, model.rowCount(childIdx));
            compareModels(tma, model);
            // Collapse child, and done
            collapseAndTest(childIdx, tma, expandable, model.rowCount(childIdx));
            compareModels(tma, model);
        }
    }
}

void tst_QQuickTreeModelAdaptor::layoutChange()
{
    TestModel model(5, 1);
    const QModelIndex &idx = model.index(0, 0);
    const QModelIndex &idx2 = model.index(2, 0);

    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    // Nothing expanded
    QSignalSpy dataChangedSpy(&tma, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
    model.changeLayout();
    QCOMPARE(dataChangedSpy.count(), 1);
    QVariantList dataChangedArgs = dataChangedSpy.takeFirst();
    QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tma.index(0));
    QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tma.index(tma.rowCount() - 1));
    QVERIFY(dataChangedArgs.at(2).value<QVector<int> >().isEmpty());
    compareModels(tma, model);

    // One item expanded
    tma.expand(idx);
    QVERIFY(tma.isExpanded(idx));
    dataChangedSpy.clear();
    model.changeLayout();
    QCOMPARE(dataChangedSpy.count(), 1);
    dataChangedArgs = dataChangedSpy.takeFirst();
    QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tma.index(0));
    QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tma.index(tma.rowCount() - 1));
    QVERIFY(dataChangedArgs.at(2).value<QVector<int> >().isEmpty());
    compareModels(tma, model);

    // One parent layout change, expanded
    dataChangedSpy.clear();
    QList<QPersistentModelIndex> parents;
    parents << idx;
    model.changeLayout(parents);
    QCOMPARE(dataChangedSpy.count(), 1);
    dataChangedArgs = dataChangedSpy.takeFirst();
    QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tma.index(tma.itemIndex(model.index(0, 0, idx))));
    QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tma.index(tma.itemIndex(model.index(model.rowCount(idx) - 1, 0, idx))));
    QVERIFY(dataChangedArgs.at(2).value<QVector<int> >().isEmpty());
    compareModels(tma, model);

    // One parent layout change, collapsed
    tma.collapse(idx);
    dataChangedSpy.clear();
    model.changeLayout(parents);
    QCOMPARE(dataChangedSpy.count(), 0);
    compareModels(tma, model);

    // Two-parent layout change, both collapsed
    parents << idx2;
    dataChangedSpy.clear();
    model.changeLayout(parents);
    QCOMPARE(dataChangedSpy.count(), 0);
    compareModels(tma, model);

    // Two-parent layout change, only one expanded
    tma.expand(idx2);
    QVERIFY(tma.isExpanded(idx2));
    dataChangedSpy.clear();
    model.changeLayout(parents);
    QCOMPARE(dataChangedSpy.count(), 1);
    dataChangedArgs = dataChangedSpy.takeFirst();
    QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tma.index(tma.itemIndex(model.index(0, 0, idx2))));
    QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tma.index(tma.itemIndex(model.index(model.rowCount(idx2) - 1, 0, idx2))));
    QVERIFY(dataChangedArgs.at(2).value<QVector<int> >().isEmpty());
    compareModels(tma, model);

    // Two-parent layout change, both expanded
    tma.expand(idx);
    QVERIFY(tma.isExpanded(idx));
    dataChangedSpy.clear();
    model.changeLayout(parents);
    QCOMPARE(dataChangedSpy.count(), 2);
    dataChangedArgs = dataChangedSpy.takeFirst();
    QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tma.index(tma.itemIndex(model.index(0, 0, idx))));
    QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tma.index(tma.itemIndex(model.index(model.rowCount(idx) - 1, 0, idx))));
    QVERIFY(dataChangedArgs.at(2).value<QVector<int> >().isEmpty());
    dataChangedArgs = dataChangedSpy.takeFirst();
    QCOMPARE(dataChangedArgs.at(0).toModelIndex(), tma.index(tma.itemIndex(model.index(0, 0, idx2))));
    QCOMPARE(dataChangedArgs.at(1).toModelIndex(), tma.index(tma.itemIndex(model.index(model.rowCount(idx2) - 1, 0, idx2))));
    QVERIFY(dataChangedArgs.at(2).value<QVector<int> >().isEmpty());
    compareModels(tma, model);
}

static const int ModelRowCount = 9;

void tst_QQuickTreeModelAdaptor::removeRows_data()
{
    QTest::addColumn<int>("removeFromRow");
    QTest::addColumn<int>("removeCount");
    QTest::addColumn<int>("removeParentRow");
    QTest::addColumn<int>("expandRow");
    QTest::addColumn<int>("expandParentRow");
    QTest::addColumn<int>("expectedRemovedCount");

    QTest::newRow("Nothing expanded, remove 1st row") << 0 << 1 << -1 << -1 << -1 << 1;
    QTest::newRow("Expand 1st row, remove 1st row") << 0 << 1 << -1 << 0 << -1 << 1 + ModelRowCount;
    QTest::newRow("Expand last row, remove 1st row") << 0 << 1 << -1 << ModelRowCount - 1 << -1 << 1;
    QTest::newRow("Nothing expanded, remove last row") << ModelRowCount - 1 << 1 << -1 << -1 << -1 << 1;
    QTest::newRow("Expand 1st row, remove last row") << ModelRowCount - 1 << 1 << -1 << 0 << -1 << 1;
    QTest::newRow("Expand last row, remove last row") << ModelRowCount - 1 << 1 << -1 << ModelRowCount - 1 << -1 << 1 + ModelRowCount;
    QTest::newRow("Remove child row, parent collapsed") << 2 << 1 << 0 << -1 << -1 << 0;
    QTest::newRow("Remove child row, parent expanded") << 2 << 1 << 0 << 0 << -1 << 1;
    QTest::newRow("Remove several rows, nothing expanded") << 2 << 5 << -1 << -1 << -1 << 5;
    QTest::newRow("Remove several rows, 1st row expanded") << 2 << 5 << -1 << 0 << -1 << 5;
    QTest::newRow("Remove several rows, last row expanded") << 2 << 5 << -1 << ModelRowCount - 1 << -1 << 5;
    QTest::newRow("Remove several rows, one of them expanded") << 2 << 5 << -1 << 4 << -1 << 5 + ModelRowCount;
    QTest::newRow("Remove all rows, nothing expanded") << 0 << ModelRowCount << -1 << -1 << -1 << ModelRowCount;
    QTest::newRow("Remove all rows, 1st row expanded") << 0 << ModelRowCount << -1 << 0 << -1 << ModelRowCount * 2;
    QTest::newRow("Remove all rows, last row expanded") << 0 << ModelRowCount << -1 << ModelRowCount - 1 << -1 << ModelRowCount * 2;
    QTest::newRow("Remove all rows, random one expanded") << 0 << ModelRowCount << -1 << 4 << -1 << ModelRowCount * 2;
}

void tst_QQuickTreeModelAdaptor::removeRows()
{
    QFETCH(int, removeFromRow);
    QFETCH(int, removeCount);
    QFETCH(int, removeParentRow);
    QFETCH(int, expandRow);
    QFETCH(int, expandParentRow);
    QFETCH(int, expectedRemovedCount);

    TestModel model(ModelRowCount, 1);
    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    const QModelIndex &expandParentIdx = expandParentRow == -1 ? QModelIndex() : model.index(expandParentRow, 0);
    if (expandParentIdx.isValid()) {
        tma.expand(expandParentIdx);
        QVERIFY(tma.isExpanded(expandParentIdx));
    }
    const QModelIndex &expandIdx = model.index(expandRow, 0, expandParentIdx);
    if (expandIdx.isValid()) {
        tma.expand(expandIdx);
        QVERIFY(tma.isExpanded(expandIdx));
    }

    const QModelIndex &removeParentIdx = removeParentRow == -1 ? QModelIndex() : model.index(removeParentRow, 0);
    const QModelIndex &removeIdx = model.index(removeFromRow, 0, removeParentIdx);
    int tmaItemIdx = tma.itemIndex(removeIdx);

    QSignalSpy rowsAboutToBeRemovedSpy(&tma, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int, int)));
    QSignalSpy rowsRemovedSpy(&tma, SIGNAL(rowsRemoved(const QModelIndex&, int, int)));
    model.removeRows(removeFromRow, removeCount, removeParentIdx);
    if (expectedRemovedCount == 0) {
        QCOMPARE(rowsAboutToBeRemovedSpy.count(), 0);
        QCOMPARE(rowsRemovedSpy.count(), 0);
    } else {
        QCOMPARE(rowsAboutToBeRemovedSpy.count(), 1);
        QCOMPARE(rowsRemovedSpy.count(), 1);
        QVariantList rowsAboutToBeRemovedArgs = rowsAboutToBeRemovedSpy.first();
        QVariantList rowsRemovedArgs = rowsRemovedSpy.first();
        QCOMPARE(rowsAboutToBeRemovedArgs, rowsRemovedArgs);
        QCOMPARE(rowsAboutToBeRemovedArgs.at(0).toModelIndex(), QModelIndex());
        QCOMPARE(rowsAboutToBeRemovedArgs.at(1).toInt(), tmaItemIdx);
        QCOMPARE(rowsAboutToBeRemovedArgs.at(2).toInt(), tmaItemIdx + expectedRemovedCount - 1);
    }
}

void tst_QQuickTreeModelAdaptor::removeRowsChildrenAndParent()
{
    TestModel model(ModelRowCount, 1);
    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    // Expand the first node
    const QModelIndex &parent = model.index(0, 0);
    tma.expand(parent);
    QVERIFY(tma.isExpanded(parent));

    QSignalSpy rowsAboutToBeRemovedSpy(&tma, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int, int)));
    QSignalSpy rowsRemovedSpy(&tma, SIGNAL(rowsRemoved(const QModelIndex&, int, int)));

    // Remove the first node children
    int expectedRemovedCount = model.rowCount(parent);
    int tmaItemIdx = tma.itemIndex(model.index(0, 0, parent));
    QCOMPARE(tmaItemIdx, tma.itemIndex(parent) + 1);
    model.removeRows(0, expectedRemovedCount, parent);
    QCOMPARE(rowsAboutToBeRemovedSpy.count(), 1);
    QCOMPARE(rowsRemovedSpy.count(), 1);
    QVariantList rowsAboutToBeRemovedArgs = rowsAboutToBeRemovedSpy.first();
    QVariantList rowsRemovedArgs = rowsRemovedSpy.first();
    QCOMPARE(rowsAboutToBeRemovedArgs, rowsRemovedArgs);
    QCOMPARE(rowsAboutToBeRemovedArgs.at(0).toModelIndex(), QModelIndex());
    QCOMPARE(rowsAboutToBeRemovedArgs.at(1).toInt(), tmaItemIdx);
    QCOMPARE(rowsAboutToBeRemovedArgs.at(2).toInt(), tmaItemIdx + expectedRemovedCount - 1);

    // Remove the first node
    rowsAboutToBeRemovedSpy.clear();
    rowsRemovedSpy.clear();
    model.removeRows(0, 1, QModelIndex());
    QCOMPARE(rowsAboutToBeRemovedSpy.count(), 1);
    QCOMPARE(rowsRemovedSpy.count(), 1);
    rowsAboutToBeRemovedArgs = rowsAboutToBeRemovedSpy.first();
    rowsRemovedArgs = rowsRemovedSpy.first();
    QCOMPARE(rowsAboutToBeRemovedArgs, rowsRemovedArgs);
    QCOMPARE(rowsAboutToBeRemovedArgs.at(0).toModelIndex(), QModelIndex());
    QCOMPARE(rowsAboutToBeRemovedArgs.at(1).toInt(), 0);
    QCOMPARE(rowsAboutToBeRemovedArgs.at(2).toInt(), 0);
}

void tst_QQuickTreeModelAdaptor::removeChildrenMoveParent()
{
    TestModel model(ModelRowCount, 1);
    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    // Expand the first node
    const QModelIndex &parent = model.index(0, 0);
    tma.expand(parent);
    QVERIFY(tma.isExpanded(parent));

    // Remove the first node children
    QSignalSpy rowsAboutToBeRemovedSpy(&tma, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int, int)));
    QSignalSpy rowsRemovedSpy(&tma, SIGNAL(rowsRemoved(const QModelIndex&, int, int)));
    int expectedRemovedCount = model.rowCount(parent);
    int tmaItemIdx = tma.itemIndex(model.index(0, 0, parent));
    QCOMPARE(tmaItemIdx, tma.itemIndex(parent) + 1);
    model.removeRows(0, expectedRemovedCount, parent);
    QCOMPARE(rowsAboutToBeRemovedSpy.count(), 1);
    QCOMPARE(rowsRemovedSpy.count(), 1);
    QVariantList rowsAboutToBeRemovedArgs = rowsAboutToBeRemovedSpy.first();
    QVariantList rowsRemovedArgs = rowsRemovedSpy.first();
    QCOMPARE(rowsAboutToBeRemovedArgs, rowsRemovedArgs);
    QCOMPARE(rowsAboutToBeRemovedArgs.at(0).toModelIndex(), QModelIndex());
    QCOMPARE(rowsAboutToBeRemovedArgs.at(1).toInt(), tmaItemIdx);
    QCOMPARE(rowsAboutToBeRemovedArgs.at(2).toInt(), tmaItemIdx + expectedRemovedCount - 1);

    // Move the first node
    QSignalSpy rowsAboutToBeMovedSpy(&tma, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
    QSignalSpy rowsMovedSpy(&tma, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)));
    model.moveRows(QModelIndex(), 0, 1, QModelIndex(), 3);
    QCOMPARE(rowsAboutToBeMovedSpy.count(), 1);
    QCOMPARE(rowsRemovedSpy.count(), 1);
    QVariantList rowsAboutToBeMovedArgs = rowsAboutToBeMovedSpy.first();
    QVariantList rowsMovedArgs = rowsMovedSpy.first();
    QCOMPARE(rowsAboutToBeMovedArgs, rowsMovedArgs);
    QCOMPARE(rowsAboutToBeMovedArgs.at(0).toModelIndex(), QModelIndex());
    QCOMPARE(rowsAboutToBeMovedArgs.at(1).toInt(), 0);
    QCOMPARE(rowsAboutToBeMovedArgs.at(2).toInt(), 0);
    QCOMPARE(rowsAboutToBeMovedArgs.at(3).toModelIndex(), QModelIndex());
    QCOMPARE(rowsAboutToBeMovedArgs.at(4).toInt(), 3);
}

void tst_QQuickTreeModelAdaptor::removeChildrenRelayoutParent()
{
    TestModel model(ModelRowCount, 1);
    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    // Expand the first node
    const QModelIndex &parent = model.index(0, 0);
    tma.expand(parent);
    QVERIFY(tma.isExpanded(parent));

    QSignalSpy rowsAboutToBeRemovedSpy(&tma, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int, int)));
    QSignalSpy rowsRemovedSpy(&tma, SIGNAL(rowsRemoved(const QModelIndex&, int, int)));

    // Remove the first node children
    int expectedRemovedCount = model.rowCount(parent);
    int tmaItemIdx = tma.itemIndex(model.index(0, 0, parent));
    QCOMPARE(tmaItemIdx, tma.itemIndex(parent) + 1);
    model.removeRows(0, expectedRemovedCount, parent);
    QCOMPARE(rowsAboutToBeRemovedSpy.count(), 1);
    QCOMPARE(rowsRemovedSpy.count(), 1);
    QVariantList rowsAboutToBeRemovedArgs = rowsAboutToBeRemovedSpy.first();
    QVariantList rowsRemovedArgs = rowsRemovedSpy.first();
    QCOMPARE(rowsAboutToBeRemovedArgs, rowsRemovedArgs);
    QCOMPARE(rowsAboutToBeRemovedArgs.at(0).toModelIndex(), QModelIndex());
    QCOMPARE(rowsAboutToBeRemovedArgs.at(1).toInt(), tmaItemIdx);
    QCOMPARE(rowsAboutToBeRemovedArgs.at(2).toInt(), tmaItemIdx + expectedRemovedCount - 1);

    // Relayout the first node
    QSignalSpy dataChanged(&tma, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
    QList<QPersistentModelIndex> parents;
    parents << parent;
    model.changeLayout(parents);
    QCOMPARE(dataChanged.count(), 0);
}

void tst_QQuickTreeModelAdaptor::insertRows_data()
{
    QTest::addColumn<int>("insertFromRow");
    QTest::addColumn<int>("insertCount");
    QTest::addColumn<int>("insertParentRow");
    QTest::addColumn<int>("expandRow");
    QTest::addColumn<int>("expandParentRow");
    QTest::addColumn<int>("expectedInsertedCount");

    QTest::newRow("Nothing expanded, insert 1st row") << 0 << 1 << -1 << -1 << -1 << 1;
    QTest::newRow("Expand 1st row, insert 1st row") << 0 << 1 << -1 << 0 << -1 << 1;
    QTest::newRow("Expand last row, insert 1st row") << 0 << 1 << -1 << ModelRowCount - 1 << -1 << 1;
    QTest::newRow("Nothing expanded, insert before the last row") << ModelRowCount - 1 << 1 << -1 << -1 << -1 << 1;
    QTest::newRow("Nothing expanded, insert after the last row") << ModelRowCount << 1 << -1 << -1 << -1 << 1;
    QTest::newRow("Expand 1st row, insert before the last row") << ModelRowCount - 1 << 1 << -1 << 0 << -1 << 1;
    QTest::newRow("Expand 1st row, insert after the last row") << ModelRowCount << 1 << -1 << 0 << -1 << 1;
    QTest::newRow("Expand last row, insert before the last row") << ModelRowCount - 1 << 1 << -1 << ModelRowCount - 1 << -1 << 1;
    QTest::newRow("Expand last row, insert after the last row") << ModelRowCount << 1 << -1 << ModelRowCount - 1 << -1 << 1;
    QTest::newRow("Insert child row, parent collapsed") << 2 << 1 << 0 << -1 << -1 << 0;
    QTest::newRow("Insert child row, parent expanded") << 2 << 1 << 0 << 0 << -1 << 1;
    QTest::newRow("Insert several rows, nothing expanded") << 2 << 5 << -1 << -1 << -1 << 5;
    QTest::newRow("Insert several rows, 1st row expanded") << 2 << 5 << -1 << 0 << -1 << 5;
    QTest::newRow("Insert several rows, last row expanded") << 2 << 5 << -1 << ModelRowCount - 1 << -1 << 5;
}

void tst_QQuickTreeModelAdaptor::insertRows()
{
    QFETCH(int, insertFromRow);
    QFETCH(int, insertCount);
    QFETCH(int, insertParentRow);
    QFETCH(int, expandRow);
    QFETCH(int, expandParentRow);
    QFETCH(int, expectedInsertedCount);

    TestModel model(ModelRowCount, 1);
    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    const QModelIndex &expandParentIdx = expandParentRow == -1 ? QModelIndex() : model.index(expandParentRow, 0);
    if (expandParentIdx.isValid()) {
        tma.expand(expandParentIdx);
        QVERIFY(tma.isExpanded(expandParentIdx));
    }
    const QModelIndex &expandIdx = model.index(expandRow, 0, expandParentIdx);
    if (expandIdx.isValid()) {
        tma.expand(expandIdx);
        QVERIFY(tma.isExpanded(expandIdx));
    }

    const QModelIndex &insertParentIdx = insertParentRow == -1 ? QModelIndex() : model.index(insertParentRow, 0);
    const QModelIndex &insertIdx = model.index(insertFromRow, 0, insertParentIdx);
    int tmaItemIdx = insertFromRow == model.rowCount(insertParentIdx) ? tma.rowCount() : tma.itemIndex(insertIdx);

    QSignalSpy rowsAboutToBeInsertedSpy(&tma, SIGNAL(rowsAboutToBeInserted(const QModelIndex&, int, int)));
    QSignalSpy rowsInsertedSpy(&tma, SIGNAL(rowsInserted(const QModelIndex&, int, int)));
    model.insertRows(insertFromRow, insertCount, insertParentIdx);
    if (expectedInsertedCount == 0) {
        QCOMPARE(rowsAboutToBeInsertedSpy.count(), 0);
        QCOMPARE(rowsInsertedSpy.count(), 0);
    } else {
        QCOMPARE(rowsAboutToBeInsertedSpy.count(), 1);
        QCOMPARE(rowsInsertedSpy.count(), 1);
        QVariantList rowsAboutToBeInsertedArgs = rowsAboutToBeInsertedSpy.first();
        QVariantList rowsInsertedArgs = rowsInsertedSpy.first();
        QCOMPARE(rowsAboutToBeInsertedArgs, rowsInsertedArgs);
        QCOMPARE(rowsAboutToBeInsertedArgs.at(0).toModelIndex(), QModelIndex());
        QCOMPARE(rowsAboutToBeInsertedArgs.at(1).toInt(), tmaItemIdx);
        QCOMPARE(rowsAboutToBeInsertedArgs.at(2).toInt(), tmaItemIdx + expectedInsertedCount - 1);
        QCOMPARE(tma.itemIndex(model.index(insertFromRow, 0, insertParentIdx)), tmaItemIdx);
    }
}

enum MoveSignalType {
    RowsMoved = 0, RowsInserted, RowsRemoved
};

void tst_QQuickTreeModelAdaptor::moveRows_data()
{
    QTest::addColumn<int>("sourceRow");
    QTest::addColumn<bool>("expandSource");
    QTest::addColumn<int>("moveCount");
    QTest::addColumn<int>("sourceParentRow");
    QTest::addColumn<bool>("expandSourceParent");
    QTest::addColumn<int>("destRow");
    QTest::addColumn<bool>("expandDest");
    QTest::addColumn<int>("destParentRow");
    QTest::addColumn<bool>("expandDestParent");
    QTest::addColumn<int>("expandRow");
    QTest::addColumn<int>("expandParentRow");
    QTest::addColumn<int>("signalType");
    QTest::addColumn<int>("expectedMovedCount");

    QTest::newRow("From and to top-level parent")
            << 0 << false << 1 << -1 << false
            << 3 << false << -1 << false
            << -1 << -1 << (int)RowsMoved << 1;
    QTest::newRow("From and to top-level parent, expanded")
            << 0 << true << 1 << -1 << false
            << 3 << false << -1 << false
            << -1 << -1 << (int)RowsMoved << ModelRowCount + 1;
    QTest::newRow("From and to top-level parent, backwards")
            << 4 << false << 1 << -1 << false
            << 0 << false << -1 << false
            << -1 << -1 << (int)RowsMoved << 1;
    QTest::newRow("From and to top-level parent, expanded, backwards")
            << 4 << true << 1 << -1 << false
            << 0 << false << -1 << false
            << -1 << -1 << (int)RowsMoved << ModelRowCount + 1;
    QTest::newRow("Moving between collapsed parents")
            << 0 << false << 1 << 0 << false
            << 0 << false << 2 << false
            << -1 << -1 << (int)RowsMoved << 0;
    QTest::newRow("From expanded parent to collapsed parent")
            << 0 << false << 1 << 0 << true
            << 0 << false << 2 << false
            << -1 << -1 << (int)RowsRemoved << 1;
    QTest::newRow("From collapsed parent to expanded parent")
            << 0 << false << 1 << 0 << false
            << 0 << false << 2 << true
            << -1 << -1 << (int)RowsInserted << 1;
    QTest::newRow("From and to same expanded parent")
            << 0 << false << 1 << 0 << true
            << 2 << false << 0 << false
            << -1 << -1 << (int)RowsMoved << 1;
    QTest::newRow("From expanded parent to collapsed parent, expanded row")
            << 0 << true << 1 << 0 << true
            << 0 << false << 2 << false
            << -1 << -1 << (int)RowsRemoved << ModelRowCount + 1;
    QTest::newRow("From collapsed parent to expanded parent, expanded row")
            << 0 << true << 1 << 0 << false
            << 0 << false << 2 << true
            << -1 << -1 << (int)RowsInserted << ModelRowCount + 1;
    QTest::newRow("From and to same expanded parent, expanded row, forward")
            << 0 << true << 1 << 0 << true
            << 5 << false << 0 << false
            << -1 << -1 << (int)RowsMoved << ModelRowCount + 1;
    QTest::newRow("From and to same expanded parent, expanded row, last row")
            << 0 << true << 1 << 0 << true
            << ModelRowCount << false << 0 << false
            << -1 << -1 << (int)RowsMoved << ModelRowCount + 1;
    QTest::newRow("From and to same expanded parent, expanded row, several")
            << 0 << true << 3 << 0 << true
            << 5 << false << 0 << false
            << -1 << -1 << (int)RowsMoved << ModelRowCount + 3;
    QTest::newRow("From and to same expanded parent, expanded row, backward")
            << 6 << true << 1 << 0 << true
            << 0 << false << 0 << false
            << -1 << -1 << (int)RowsMoved << ModelRowCount + 1;
    QTest::newRow("From and to same expanded parent, expanded row, several, backward")
            << 6 << true << 2 << 0 << true
            << 0 << false << 0 << false
            << -1 << -1 << (int)RowsMoved << ModelRowCount + 2;
    QTest::newRow("From and to different expanded parents")
            << 0 << false << 1 << 0 << true
            << 1 << false << 4 << true
            << -1 << -1 << (int)RowsMoved << 1;
    QTest::newRow("From and to different expanded parents, backward")
            << 0 << false << 1 << 4 << true
            << 2 << false << 0 << true
            << -1 << -1 << (int)RowsMoved << 1;
    QTest::newRow("From and to different expanded parents, up in level")
            << 0 << false << 1 << 0 << true
            << 5 << true << -1 << true
            << -1 << -1 << (int)RowsMoved << 1;
    QTest::newRow("From and to different expanded parents, up in level, backwards")
            << 0 << false << 1 << 4 << true
            << 1 << false << -1 << true
            << -1 << -1 << (int)RowsMoved << 1;
    QTest::newRow("From and to different expanded parents, up in level, as 1st item")
            << 0 << false << 1 << 0 << true
            << 0 << false << -1 << true
            << -1 << -1 << (int)RowsMoved << 1;
    QTest::newRow("From and to different expanded parents, backward, up in level")
            << 0 << false << 1 << 4 << true
            << 2 << false << 0 << true
            << -1 << -1 << (int)RowsMoved << 1;
}

void tst_QQuickTreeModelAdaptor::moveRows()
{
    QFETCH(int, sourceRow);
    QFETCH(bool, expandSource);
    QFETCH(int, moveCount);
    QFETCH(int, sourceParentRow);
    QFETCH(bool, expandSourceParent);
    QFETCH(int, destRow);
    QFETCH(bool, expandDest);
    QFETCH(int, destParentRow);
    QFETCH(bool, expandDestParent);
    QFETCH(int, expandRow);
    QFETCH(int, expandParentRow);
    QFETCH(int, signalType);
    QFETCH(int, expectedMovedCount);

    TestModel model(ModelRowCount, 1);
    model.alternateChildlessRows = false;
    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    const QModelIndex &expandParentIdx = expandParentRow == -1 ? QModelIndex() : model.index(expandParentRow, 0);
    if (expandParentIdx.isValid()) {
        tma.expand(expandParentIdx);
        QVERIFY(tma.isExpanded(expandParentIdx));
    }
    const QModelIndex &expandIdx = model.index(expandRow, 0, expandParentIdx);
    if (expandIdx.isValid()) {
        tma.expand(expandIdx);
        QVERIFY(tma.isExpanded(expandIdx));
    }

    const QModelIndex &sourceParentIdx = sourceParentRow == -1 ? QModelIndex() : model.index(sourceParentRow, 0);
    if (expandSourceParent && sourceParentIdx.isValid()) {
        tma.expand(sourceParentIdx);
        QVERIFY(tma.isExpanded(sourceParentIdx));
    }
    const QModelIndex &sourceIdx = model.index(sourceRow, 0, sourceParentIdx);
    if (expandSource) {
        tma.expand(sourceIdx);
        QVERIFY(tma.isExpanded(sourceIdx));
    }

    const QModelIndex &destParentIdx = destParentRow == -1 ? QModelIndex() : model.index(destParentRow, 0);
    if (expandDestParent && destParentIdx.isValid()) {
        tma.expand(destParentIdx);
        QVERIFY(tma.isExpanded(destParentIdx));
    }
    const QModelIndex &destIdx = model.index(destRow, 0, destParentIdx);
    if (expandDest) {
        tma.expand(destIdx);
        QVERIFY(tma.isExpanded(destIdx));
    }

    int tmaSourceItemIdx = signalType == RowsInserted ? -1 // Not tested if RowsInserted
                           : tma.itemIndex(sourceIdx);
    int tmaDestItemIdx = signalType == RowsRemoved ? -1 : // Not tested if RowsRemoved
                         destRow == model.rowCount(destParentIdx) ? -1  /* FIXME */ : tma.itemIndex(destIdx);

    QSignalSpy rowsAboutToBeMovedSpy(&tma, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
    QSignalSpy rowsMovedSpy(&tma, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)));
    QSignalSpy rowsAboutToBeInsertedSpy(&tma, SIGNAL(rowsAboutToBeInserted(const QModelIndex&, int, int)));
    QSignalSpy rowsInsertedSpy(&tma, SIGNAL(rowsInserted(const QModelIndex&, int, int)));
    QSignalSpy rowsAboutToBeRemovedSpy(&tma, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int, int)));
    QSignalSpy rowsRemovedSpy(&tma, SIGNAL(rowsRemoved(const QModelIndex&, int, int)));

    QVERIFY(model.moveRows(sourceParentIdx, sourceRow, moveCount, destParentIdx, destRow));

    if (signalType != RowsMoved || expectedMovedCount == 0) {
        QCOMPARE(rowsAboutToBeMovedSpy.count(), 0);
        QCOMPARE(rowsMovedSpy.count(), 0);
    }
    if (signalType != RowsInserted || expectedMovedCount == 0) {
        QCOMPARE(rowsAboutToBeInsertedSpy.count(), 0);
        QCOMPARE(rowsInsertedSpy.count(), 0);
    }
    if (signalType != RowsRemoved || expectedMovedCount == 0) {
        QCOMPARE(rowsAboutToBeRemovedSpy.count(), 0);
        QCOMPARE(rowsRemovedSpy.count(), 0);
    }

    if (expectedMovedCount != 0) {
        if (signalType == RowsMoved) {
            QCOMPARE(rowsAboutToBeMovedSpy.count(), 1);
            QCOMPARE(rowsMovedSpy.count(), 1);
            QVariantList rowsAboutToBeMovedArgs = rowsAboutToBeMovedSpy.first();
            QVariantList rowsMovedArgs = rowsMovedSpy.first();
            QCOMPARE(rowsAboutToBeMovedArgs, rowsMovedArgs);
            QCOMPARE(rowsAboutToBeMovedArgs.at(0).toModelIndex(), QModelIndex());
            QCOMPARE(rowsAboutToBeMovedArgs.at(1).toInt(), tmaSourceItemIdx);
            QCOMPARE(rowsAboutToBeMovedArgs.at(2).toInt(), tmaSourceItemIdx + expectedMovedCount - 1);
            QCOMPARE(rowsAboutToBeMovedArgs.at(3).toModelIndex(), QModelIndex());
            if (tmaDestItemIdx != -1)
                QCOMPARE(rowsAboutToBeMovedArgs.at(4).toInt(), tmaDestItemIdx);
        } else if (signalType == RowsInserted) {
            // We only test with one level of expanded children here, so we can do
            // exhaustive testing depending on whether the moved row is expanded.
            int signalCount = expandSource ? 2 : 1;
            QCOMPARE(rowsAboutToBeInsertedSpy.count(), signalCount);
            QCOMPARE(rowsInsertedSpy.count(), signalCount);
            QVariantList rowsAboutToBeInsertedArgs = rowsAboutToBeInsertedSpy.takeFirst();
            QVariantList rowsInsertedArgs = rowsInsertedSpy.takeFirst();
            QCOMPARE(rowsAboutToBeInsertedArgs, rowsInsertedArgs);
            QCOMPARE(rowsAboutToBeInsertedArgs.at(0).toModelIndex(), QModelIndex());
            QCOMPARE(rowsAboutToBeInsertedArgs.at(1).toInt(), tmaDestItemIdx);
            if (expandSource) {
                QCOMPARE(rowsAboutToBeInsertedArgs.at(2).toInt(), tmaDestItemIdx);
                rowsAboutToBeInsertedArgs = rowsAboutToBeInsertedSpy.first();
                rowsInsertedArgs = rowsInsertedSpy.first();
                QCOMPARE(rowsAboutToBeInsertedArgs, rowsInsertedArgs);
                QCOMPARE(rowsAboutToBeInsertedArgs.at(0).toModelIndex(), QModelIndex());
                QCOMPARE(rowsAboutToBeInsertedArgs.at(1).toInt(), tmaDestItemIdx + 1);
            }
            QCOMPARE(rowsAboutToBeInsertedArgs.at(2).toInt(), tmaDestItemIdx + expectedMovedCount - 1);
            QCOMPARE(tma.itemIndex(model.index(destRow, 0, destParentIdx)), tmaDestItemIdx);
        } else if (signalType == RowsRemoved) {
            QCOMPARE(rowsAboutToBeRemovedSpy.count(), 1);
            QCOMPARE(rowsRemovedSpy.count(), 1);
            QVariantList rowsAboutToBeRemovedArgs = rowsAboutToBeRemovedSpy.first();
            QVariantList rowsRemovedArgs = rowsRemovedSpy.first();
            QCOMPARE(rowsAboutToBeRemovedArgs, rowsRemovedArgs);
            QCOMPARE(rowsAboutToBeRemovedArgs.at(0).toModelIndex(), QModelIndex());
            QCOMPARE(rowsAboutToBeRemovedArgs.at(1).toInt(), tmaSourceItemIdx);
            QCOMPARE(rowsAboutToBeRemovedArgs.at(2).toInt(), tmaSourceItemIdx + expectedMovedCount - 1);
        }
    }
    QVERIFY(tma.testConsistency());
    compareModels(tma, model);
}

void tst_QQuickTreeModelAdaptor::moveRowsDataChanged_data()
{
    /* This is the list of rows in the *list* model which are expanded. */
    QTest::addColumn<QVector<int>>("expandedRows");

    /* Here's the row to be moved (always just one) and the destination
     * position. We use a QVector<int> to identify a single row of the tree
     * model: the array value at index n represents the row number at the depth
     * n.
     */
    QTest::addColumn<QVector<int>>("sourcePath");
    QTest::addColumn<QVector<int>>("destPath");

    /* This is the list of expected changed rows in the *list* model. */
    QTest::addColumn<QSet<int>>("expectedRowChanges");

    QTest::newRow("From and to top-level parent")
            << QVector<int> {}
            << QVector<int> { 4 }
            << QVector<int> { 3 }
            << QSet<int> { 3, 4 };

    QTest::newRow("From and to top-level parent, expanded")
            << QVector<int> { 3, 5 }
            << QVector<int> { 4 }
            << QVector<int> { 3 }
            << QSet<int> { 3, 4, 5, 6, 7, 8, 9 };

    QTest::newRow("From and to top-level parent, expanded, down")
            << QVector<int> { 3, 5 }
            << QVector<int> { 3 }
            << QVector<int> { 5 }
            << QSet<int> { 3, 4, 5, 6, 7, 8, 9 };

    /* Expected visible result:
     * A      A
     * D      D
     * `-E    |-E
     * F      `-H
     * G   -> F
     * |-H    G
     * |-I    |-I
     * `-L    `-L
     * M      M
     */
    QTest::newRow("Visible move, different parents")
            << QVector<int> { 3, 1 }
            << QVector<int> { 3, 0 }
            << QVector<int> { 1, 1 }
            << QSet<int> { 3, 4, 5, 6, 7 };

    QTest::newRow("Move to non expanded parent")
            << QVector<int> {}
            << QVector<int> { 3 }
            << QVector<int> { 1, 0 }
            << QSet<int> { 3 };
}

void tst_QQuickTreeModelAdaptor::moveRowsDataChanged()
{
    QFETCH(QVector<int>, expandedRows);
    QFETCH(QVector<int>, sourcePath);
    QFETCH(QVector<int>, destPath);
    QFETCH(QSet<int>, expectedRowChanges);

    TestModel model(0, 1);
    model.alternateChildlessRows = false;
    model.fetchMore(QModelIndex());
    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    /* Build this model:
     *   A
     *   |-B
     *   `-C
     *   D
     *   `-E
     *   F
     *   G
     *   |-H
     *   |-I
     *   | |-J
     *   | `-K
     *   `-L
     *   M
     */
    model.insertRows(0, 5, QModelIndex());
    QModelIndex index = model.index(0, 0);
    model.insertRows(0, 2, index);
    index = model.index(1, 0);
    model.insertRows(0, 1, index);
    index = model.index(3, 0);
    model.insertRows(0, 3, index);
    index = model.index(1, 0, index);
    model.insertRows(0, 2, index);

    for (int row : expandedRows) {
        tma.expandRow(row);
    }

    /* Find the source index */
    int sourceRow = sourcePath.takeLast();
    QModelIndex sourceIndex;
    for (int row : sourcePath) {
        sourceIndex = model.index(row, 0, sourceIndex);
    }

    /* Find the destination index */
    int destRow = destPath.takeLast();
    QModelIndex destIndex;
    for (int row : destPath) {
        destIndex = model.index(row, 0, destIndex);
    }

    QSignalSpy dataChangedSpy(&tma, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
    QVERIFY(dataChangedSpy.isValid());

    QVERIFY(model.moveRows(sourceIndex, sourceRow, 1, destIndex, destRow));

    QSet<int> emittedChanges;
    for (int i = 0; i < dataChangedSpy.count(); i++) {
        QVariantList args = dataChangedSpy.at(i);
        QVector<int> roles = args.at(2).value<QVector<int>>();
        if (!roles.isEmpty() &&
            !roles.contains(QQuickTreeModelAdaptor1::ModelIndexRole))
            continue;

        const QModelIndex topLeft = args.at(0).value<QModelIndex>();
        const QModelIndex bottomRight = args.at(1).value<QModelIndex>();
        for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
            emittedChanges.insert(row);
        }
    }

    QCOMPARE(emittedChanges, expectedRowChanges);
}

void tst_QQuickTreeModelAdaptor::reparentOnSameRow()
{
    TestModel model(2, 1);
    model.alternateChildlessRows = false;
    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    const QModelIndex &destParent = model.index(0, 0);
    const QModelIndex &sourceParent = QModelIndex();
    QVERIFY(destParent.isValid());
    tma.expand(destParent);
    QVERIFY(tma.isExpanded(destParent));

    QSignalSpy dataChangedSpy(&tma, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
    QSignalSpy rowsMovedSpy(&tma, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)));
    QVERIFY(rowsMovedSpy.isValid());
    QVERIFY(dataChangedSpy.isValid());

    QVERIFY(model.moveRows(sourceParent, 1, 1, destParent, 2));

    QModelIndex movedIndex = tma.index(3, 0, QModelIndex());
    QVERIFY(movedIndex.isValid());
    QCOMPARE(movedIndex.data(QQuickTreeModelAdaptor1::DepthRole).toInt(), 1);
    QCOMPARE(tma.data(movedIndex, QQuickTreeModelAdaptor1::ModelIndexRole).toModelIndex(), model.index(2, 0, destParent));

    // at least DepthRole and ModeIndexRole changes should have happened for the affected row
    bool depthChanged = false;
    bool modelIndexChanged = false;
    const QList<QList<QVariant> > &changes = dataChangedSpy;
    for (const QList<QVariant> &change : changes) {
        if (change.at(0) == movedIndex) {
            if (change.at(2).value<QVector<int> >().contains(QQuickTreeModelAdaptor1::DepthRole))
                depthChanged = true;
            if (change.at(2).value<QVector<int> >().contains(QQuickTreeModelAdaptor1::ModelIndexRole))
                modelIndexChanged = true;
        }
    }

    QCOMPARE(depthChanged, true);
    QCOMPARE(modelIndexChanged, true);

    QCOMPARE(rowsMovedSpy.count(), 0);

    model.moveRow(destParent, 2, QModelIndex(), 1);

    QCOMPARE(rowsMovedSpy.count(), 0);
    QVERIFY(tma.testConsistency());
    compareModels(tma, model);
}

void tst_QQuickTreeModelAdaptor::moveAllChildren()
{
    TestModel model(0, 1);
    model.alternateChildlessRows = false;
    model.fetchMore(QModelIndex());
    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    /* Build this model:
     *   A
     *   B
     *   `-C
     */
    model.insertRows(0, 2, QModelIndex());
    QPersistentModelIndex aIndex(model.index(0, 0));
    QPersistentModelIndex bIndex(model.index(1, 0));
    model.insertRows(0, 1, bIndex);

    QCOMPARE(model.rowCount(QModelIndex()), 2);
    /* Expand B, then move C under A */
    tma.expandRow(1);


    QSignalSpy dataChangedSpy(&tma, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
    QVERIFY(dataChangedSpy.isValid());

    QVERIFY(model.moveRows(bIndex, 0, 1, aIndex, 0));

    /* Check the outcome */
    QCOMPARE(tma.data(aIndex, QQuickTreeModelAdaptor1::HasChildrenRole).toBool(), true);
    QCOMPARE(tma.data(bIndex, QQuickTreeModelAdaptor1::HasChildrenRole).toBool(), false);
    QVERIFY(rowRoleWasChanged(dataChangedSpy, 0, QQuickTreeModelAdaptor1::HasChildrenRole));
    QVERIFY(rowRoleWasChanged(dataChangedSpy, 1, QQuickTreeModelAdaptor1::HasChildrenRole));
    dataChangedSpy.clear();

    /* Move C back into under B */
    QVERIFY(model.moveRows(aIndex, 0, 1, bIndex, 0));

    QCOMPARE(tma.data(aIndex, QQuickTreeModelAdaptor1::HasChildrenRole).toBool(), false);
    QCOMPARE(tma.data(bIndex, QQuickTreeModelAdaptor1::HasChildrenRole).toBool(), true);
    QVERIFY(rowRoleWasChanged(dataChangedSpy, 0, QQuickTreeModelAdaptor1::HasChildrenRole));
    QVERIFY(rowRoleWasChanged(dataChangedSpy, 1, QQuickTreeModelAdaptor1::HasChildrenRole));
    /* B must not be in expanded state, since it previously lost all its children */
    QCOMPARE(tma.data(bIndex, QQuickTreeModelAdaptor1::ExpandedRole).toBool(), false);

    dataChangedSpy.clear();
}

void tst_QQuickTreeModelAdaptor::selectionForRowRange()
{
    const int ModelRowCount = 9;
    const int ModelRowCountLoopStep = 4;

    TestModel model(ModelRowCount, 1);
    model.alternateChildlessRows = false;
    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    // NOTE: Some selections may look a bit cryptic. Insert a call to
    // tma.dump() before each block if you need to see what's going on.

    for (int i = 0; i < ModelRowCount; i += ModelRowCountLoopStep) {
        // Single row selection
        const QModelIndex &idx = model.index(i, 0);
        const QItemSelection &sel = tma.selectionForRowRange(idx, idx);
        QCOMPARE(sel.count(), 1);
        const QItemSelectionRange &range = sel.first();
        QCOMPARE(QModelIndex(range.topLeft()), model.index(i, 0));
        QCOMPARE(QModelIndex(range.bottomRight()), model.index(i, 0));
    }

    for (int i = 0; i < ModelRowCount - ModelRowCountLoopStep; i += ModelRowCountLoopStep) {
        // Single range selection
        const QModelIndex &from = model.index(i, 0);
        const QModelIndex &to = model.index(i + ModelRowCountLoopStep, 0);
        const QItemSelection &sel = tma.selectionForRowRange(from, to);
        QCOMPARE(sel.count(), 1);
        const QItemSelectionRange &range = sel.first();
        QCOMPARE(QModelIndex(range.topLeft()), model.index(i, 0));
        QCOMPARE(QModelIndex(range.bottomRight()), model.index(i + ModelRowCountLoopStep, 0));
    }

    {   // Select all, no branch expanded
        const QModelIndex &from = model.index(0, 0);
        const QModelIndex &to = model.index(ModelRowCount - 1, 0);
        const QItemSelection &sel = tma.selectionForRowRange(from, to);
        QCOMPARE(sel.count(), 1);
        const QItemSelectionRange &range = sel.first();
        QCOMPARE(QModelIndex(range.topLeft()), model.index(0, 0));
        QCOMPARE(QModelIndex(range.bottomRight()), model.index(ModelRowCount - 1, 0));
    }

    // Expand 1st top-level item
    const QModelIndex &parent = model.index(0, 0);
    tma.expand(parent);

    {   // 1st item expanded, select first 5 rows
        const QModelIndex &from = tma.mapRowToModelIndex(0);
        const QModelIndex &to = tma.mapRowToModelIndex(4);
        const QItemSelection &sel = tma.selectionForRowRange(from, to);
        QCOMPARE(sel.count(), 2);
        // We don't know in which order the selection ranges are
        // being added, so we iterate and try to find what we expect.
        for (const QItemSelectionRange &range : sel) {
            if (range.topLeft() ==  model.index(0, 0))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(0, 0));
            else if (range.topLeft() ==  model.index(0, 0, parent))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(3, 0, parent));
            else
                QFAIL("Unexpected selection range");
        }
    }

    {   // 1st item expanded, select first 5 top-level items
        const QModelIndex &from = tma.mapRowToModelIndex(0);
        const QModelIndex &to = tma.mapRowToModelIndex(4 + ModelRowCount);
        const QItemSelection &sel = tma.selectionForRowRange(from, to);
        QCOMPARE(sel.count(), 2);
        // We don't know in which order the selection ranges are
        // being added, so we iterate and try to find what we expect.
        for (const QItemSelectionRange &range : sel) {
            if (range.topLeft() ==  model.index(0, 0))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(4, 0));
            else if (range.topLeft() ==  model.index(0, 0, parent))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(ModelRowCount - 1, 0, parent));
            else
                QFAIL("Unexpected selection range");
        }
    }

    // Expand 2nd top-level item
    const QModelIndex &parent2 = model.index(1, 0);
    tma.expand(parent2);

    {   // 1st two items expanded, select first 5 top-level items
        const QModelIndex &from = tma.mapRowToModelIndex(0);
        const QModelIndex &to = tma.mapRowToModelIndex(4 + 2 * ModelRowCount);
        const QItemSelection &sel = tma.selectionForRowRange(from, to);
        QCOMPARE(sel.count(), 3);
        // We don't know in which order the selection ranges are
        // being added, so we iterate and try to find what we expect.
        for (const QItemSelectionRange &range : sel) {
            if (range.topLeft() ==  model.index(0, 0))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(4, 0));
            else if (range.topLeft() ==  model.index(0, 0, parent))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(ModelRowCount - 1, 0, parent));
            else if (range.topLeft() ==  model.index(0, 0, parent2))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(ModelRowCount - 1, 0, parent2));
            else
                QFAIL("Unexpected selection range");
        }
    }

    // Expand 1st child of 1st top-level item
    const QModelIndex &parent3 = model.index(0, 0, parent);
    tma.expand(parent3);

    {   // 1st two items, and 1st child of 1st item expanded, select first 5 rows
        const QModelIndex &from = tma.mapRowToModelIndex(0);
        const QModelIndex &to = tma.mapRowToModelIndex(4);
        const QItemSelection &sel = tma.selectionForRowRange(from, to);
        QCOMPARE(sel.count(), 3);
        // We don't know in which order the selection ranges are
        // being added, so we iterate and try to find what we expect.
        for (const QItemSelectionRange &range : sel) {
            if (range.topLeft() ==  model.index(0, 0))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(0, 0));
            else if (range.topLeft() ==  model.index(0, 0, parent))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(0, 0, parent));
            else if (range.topLeft() ==  model.index(0, 0, parent3))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(2, 0, parent3));
            else
                QFAIL("Unexpected selection range");
        }
    }

    {   // 1st two items, and 1st child of 1st item expanded, select all
        const QModelIndex &from = tma.mapRowToModelIndex(0);
        const QModelIndex &to = tma.mapRowToModelIndex(4 * ModelRowCount - 1);
        const QItemSelection &sel = tma.selectionForRowRange(from, to);
        QCOMPARE(sel.count(), 4);
        // We don't know in which order the selection ranges are
        // being added, so we iterate and try to find what we expect.
        for (const QItemSelectionRange &range : sel) {
            if (range.topLeft() ==  model.index(0, 0))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(ModelRowCount - 1, 0));
            else if (range.topLeft() ==  model.index(0, 0, parent))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(ModelRowCount - 1, 0, parent));
            else if (range.topLeft() ==  model.index(0, 0, parent2))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(ModelRowCount - 1, 0, parent2));
            else if (range.topLeft() ==  model.index(0, 0, parent3))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(ModelRowCount - 1, 0, parent3));
            else
                QFAIL("Unexpected selection range");
        }
    }

    {   // 1st two items, and 1st child of 1st item expanded, select rows across branches
        const QModelIndex &from = tma.mapRowToModelIndex(8);
        const QModelIndex &to = tma.mapRowToModelIndex(23);
        const QItemSelection &sel = tma.selectionForRowRange(from, to);
        QCOMPARE(sel.count(), 4);
        // We don't know in which order the selection ranges are
        // being added, so we iterate and try to find what we expect.
        for (const QItemSelectionRange &range : sel) {
            if (range.topLeft() ==  model.index(1, 0))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(1, 0));
            else if (range.topLeft() ==  model.index(1, 0, parent))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(ModelRowCount - 1, 0, parent));
            else if (range.topLeft() ==  model.index(0, 0, parent2))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(3, 0, parent2));
            else if (range.topLeft() ==  model.index(6, 0, parent3))
                QCOMPARE(QModelIndex(range.bottomRight()), model.index(ModelRowCount - 1, 0, parent3));
            else
                QFAIL("Unexpected selection range");
        }
    }
}

void tst_QQuickTreeModelAdaptor::hasChildrenEmit()
{
    TestModel model(1, 1);
    model.alternateChildlessRows = false;
    QQuickTreeModelAdaptor1 tma;
    tma.setModel(&model);

    QModelIndex root = model.index(0,0);
    QVERIFY(root.isValid());

    QModelIndex child = model.index(0, 0, root);
    QVERIFY(child.isValid());

    // Root not expanded , child not expanded, insert in child, expect no datachanged
    {
        QVERIFY(!tma.isExpanded(root));
        QVERIFY(!tma.isExpanded(child));
        QSignalSpy dataChangedSpy(&tma, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
        QCOMPARE(dataChangedSpy.count(), 0);
        QCOMPARE(model.rowCount(child), 1);
        model.insertRow(1, child);
        QCOMPARE(model.rowCount(child), 2);
        QCOMPARE(dataChangedSpy.count(), 0);
    }

    // Root not expanded, insert in root, expect datachanged
    {
        QVERIFY(!tma.isExpanded(root));
        QSignalSpy dataChangedSpy(&tma, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
        QCOMPARE(dataChangedSpy.count(), 0);
        QCOMPARE(model.rowCount(root), 1);
        model.insertRow(1, root);
        QCOMPARE(model.rowCount(root), 2);
        QCOMPARE(dataChangedSpy.count(), 1);
    }

    // Root not expanded, child not expanded, remove in child, expect no datachanged
    {
        QVERIFY(!tma.isExpanded(root));
        QVERIFY(!tma.isExpanded(child));
        QSignalSpy dataChangedSpy(&tma, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
        QCOMPARE(dataChangedSpy.count(), 0);
        QCOMPARE(model.rowCount(child), 2);
        model.removeRow(1, child);
        QCOMPARE(model.rowCount(child), 1);
        QCOMPARE(dataChangedSpy.count(), 0);
    }

    // Root not expanded, remove in root, expected datachanged on hasChildren
    {
        QVERIFY(!tma.isExpanded(root));
        QSignalSpy dataChangedSpy(&tma, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
        QCOMPARE(dataChangedSpy.count(), 0);
        QCOMPARE(model.rowCount(root), 2);
        model.removeRow(1, root);
        QCOMPARE(model.rowCount(root), 1);
        QCOMPARE(dataChangedSpy.count(), 1);
    }
}

QTEST_MAIN(tst_QQuickTreeModelAdaptor)
#include "tst_qquicktreemodeladaptor.moc"