aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/controls/data/tst_tumbler.qml
blob: 5b3ef6e30f72f63ee81b17e17f2a4840b4a0bdaa (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
/****************************************************************************
**
** Copyright (C) 2019 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:BSD$
** 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.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.12
import QtTest 1.0
import QtQuick.Controls 2.12

TestCase {
    id: testCase
    width: 300
    height: 300
    visible: true
    when: windowShown
    name: "Tumbler"

    property var tumbler: null
    readonly property real implicitTumblerWidth: 60
    readonly property real implicitTumblerHeight: 200
    readonly property real defaultImplicitDelegateHeight: implicitTumblerHeight / 3
    readonly property real defaultListViewTumblerOffset: -defaultImplicitDelegateHeight
    readonly property real tumblerDelegateHeight: tumbler ? tumbler.availableHeight / tumbler.visibleItemCount : 0
    property Item tumblerView: null

    Component {
        id: tumblerComponent

        Tumbler {
            visibleItemCount: 3
        }
    }

    Component {
        id: itemComponent

        Item {
            anchors.fill: parent
        }
    }

    function createTumbler(args) {
        tumbler = createTemporaryObject(tumblerComponent, testCase, args);
        verify(tumbler, "Tumbler: failed to create an instance");
        tumblerView = findView(tumbler);
        verify(tumblerView);
    }

    function tumblerXCenter() {
        return tumbler.leftPadding + tumbler.width / 2;
    }

    function tumblerYCenter() {
        return tumbler.topPadding + tumbler.height / 2;
    }

    // visualItemIndex is from 0 to the amount of visible items.
    function itemCenterPos(visualItemIndex) {
        var halfDelegateHeight = tumblerDelegateHeight / 2;
        var yCenter = tumbler.y + tumbler.topPadding + halfDelegateHeight
            + (tumblerDelegateHeight * visualItemIndex);
        return Qt.point(tumblerXCenter(), yCenter);
    }

    function itemTopLeftPos(visualItemIndex) {
        return Qt.point(tumbler.leftPadding, tumbler.topPadding + (tumblerDelegateHeight * visualItemIndex));
    }

    function checkItemSizes() {
        var contentChildren = tumbler.wrap ? tumblerView.children : tumblerView.contentItem.children;
        verify(contentChildren.length >= tumbler.count);
        for (var i = 0; i < contentChildren.length; ++i) {
            compare(contentChildren[i].width, tumbler.availableWidth);
            compare(contentChildren[i].height, tumblerDelegateHeight);
        }
    }

    function findView(parent) {
        for (var i = 0; i < parent.children.length; ++i) {
            var child = parent.children[i];
            if (child.hasOwnProperty("currentIndex")) {
                return child;
            }

            var grandChild = findView(child);
            if (grandChild)
                return grandChild;
        }

        return null;
    }

    function findDelegateWithText(parent, text) {
        for (var i = 0; i < parent.children.length; ++i) {
            var child = parent.children[i];
            if (child.hasOwnProperty("text") && child.text === text) {
                return child;
            }

            var grandChild = findDelegateWithText(child, text);
            if (grandChild)
                return grandChild;
        }

        return null;
    }

    property Component noAttachedPropertiesDelegate: Text {
        text: modelData
    }

    function test_wrapWithoutAttachedProperties() {
        createTumbler();
        verify(tumbler.wrap);

        tumbler.delegate = noAttachedPropertiesDelegate;
        // Shouldn't assert.
        tumbler.wrap = false;
        verify(findView(tumbler));
    }

    // TODO: test that currentIndex is maintained between contentItem changes...
//    function tst_dynamicContentItemChange() {
//    }

    function test_currentIndex() {
        createTumbler();
        compare(tumbler.contentItem.parent, tumbler);

        tumbler.model = 5;

        compare(tumbler.currentIndex, 0);
        waitForRendering(tumbler);

        // Set it through user interaction.
        var pos = Qt.point(tumblerXCenter(), tumbler.height / 2);
        mouseDrag(tumbler, pos.x, pos.y, 0, tumbler.height / 3, Qt.LeftButton, Qt.NoModifier, 200);
        tryCompare(tumblerView, "offset", 1);
        compare(tumbler.currentIndex, 4);
        compare(tumblerView.currentIndex, 4);

        // Set it manually.
        tumbler.currentIndex = 2;
        tryCompare(tumbler, "currentIndex", 2);
        compare(tumblerView.currentIndex, 2);

        tumbler.model = null;
        tryCompare(tumbler, "currentIndex", -1);
        // PathView will only use 0 as the currentIndex when there are no items.
        compare(tumblerView.currentIndex, 0);

        tumbler.model = ["A", "B", "C"];
        tryCompare(tumbler, "currentIndex", 0);

        // Setting a negative current index should have no effect, because the model isn't empty.
        tumbler.currentIndex = -1;
        compare(tumbler.currentIndex, 0);

        tumbler.model = 1;
        compare(tumbler.currentIndex, 0);

        tumbler.model = 5;
        compare(tumbler.count, 5);
        tumblerView = findView(tumbler);
        tryCompare(tumblerView, "count", 5);
        tumbler.currentIndex = 4;
        compare(tumbler.currentIndex, 4);
        compare(tumblerView.currentIndex, 4);

        --tumbler.model;
        compare(tumbler.count, 4);
        compare(tumblerView.count, 4);
        // Removing an item from an integer-based model will cause views to reset their currentIndex to 0.
        compare(tumbler.currentIndex, 0);
        compare(tumblerView.currentIndex, 0);

        tumbler.model = 0;
        compare(tumbler.currentIndex, -1);
    }

    Component {
        id: currentIndexTumbler

        Tumbler {
            model: 5
            currentIndex: 2
            visibleItemCount: 3
        }
    }

    Component {
        id: currentIndexTumblerNoWrap

        Tumbler {
            model: 5
            currentIndex: 2
            wrap: false
            visibleItemCount: 3
        }
    }

    Component {
        id: currentIndexTumblerNoWrapReversedOrder

        Tumbler {
            model: 5
            wrap: false
            currentIndex: 2
            visibleItemCount: 3
        }
    }

    Component {
        id: negativeCurrentIndexTumblerNoWrap

        Tumbler {
            model: 5
            wrap: false
            currentIndex: -1
            visibleItemCount: 3
        }
    }

    Component {
        id: currentIndexTooLargeTumbler

        Tumbler {
            objectName: "currentIndexTooLargeTumbler"
            model: 10
            currentIndex: 10
        }
    }


    function test_currentIndexAtCreation_data() {
        return [
            { tag: "wrap: implicit, expected currentIndex: 2", currentIndex: 2, wrap: true, component: currentIndexTumbler },
            { tag: "wrap: false, expected currentIndex: 2", currentIndex: 2, wrap: false, component: currentIndexTumblerNoWrap },
            // Order of property assignments shouldn't matter
            { tag: "wrap: false, expected currentIndex: 2, reversed property assignment order",
                currentIndex: 2, wrap: false, component: currentIndexTumblerNoWrapReversedOrder },
            { tag: "wrap: false, expected currentIndex: 0", currentIndex: 0, wrap: false, component: negativeCurrentIndexTumblerNoWrap },
            { tag: "wrap: implicit, expected currentIndex: 0", currentIndex: 0, wrap: true, component: currentIndexTooLargeTumbler }
        ]
    }

    function test_currentIndexAtCreation(data) {
        // Test setting currentIndex at creation time
        tumbler = createTemporaryObject(data.component, testCase);
        verify(tumbler);
        // A "statically declared" currentIndex will be pending until the count has changed,
        // which happens when the model is set, which happens on the TumblerView's next polish.
        tryCompare(tumbler, "currentIndex", data.currentIndex);

        tumblerView = findView(tumbler);
        tryVerify(function() { return tumblerView.currentItem });
        compare(tumblerView.currentIndex, data.currentIndex);
        compare(tumblerView.currentItem.text, data.currentIndex.toString());

        if (data.wrap) {
            tryCompare(tumblerView, "offset", data.currentIndex > 0 ? tumblerView.count - data.currentIndex : 0);
        } else {
            tryCompare(tumblerView, "contentY", tumblerDelegateHeight * data.currentIndex - tumblerView.preferredHighlightBegin);
        }
    }

    function test_keyboardNavigation() {
        createTumbler();

        tumbler.model = 5;
        tumbler.forceActiveFocus();
        tumblerView.highlightMoveDuration = 0;

        // Navigate upwards through entire wheel.
        for (var j = 0; j < tumbler.count - 1; ++j) {
            keyClick(Qt.Key_Up, Qt.NoModifier);
            tryCompare(tumblerView, "offset", j + 1);
            compare(tumbler.currentIndex, tumbler.count - 1 - j);
        }

        keyClick(Qt.Key_Up, Qt.NoModifier);
        tryCompare(tumblerView, "offset", 0);
        compare(tumbler.currentIndex, 0);

        // Navigate downwards through entire wheel.
        for (j = 0; j < tumbler.count - 1; ++j) {
            keyClick(Qt.Key_Down, Qt.NoModifier);
            tryCompare(tumblerView, "offset", tumbler.count - 1 - j);
            compare(tumbler.currentIndex, j + 1);
        }

        keyClick(Qt.Key_Down, Qt.NoModifier);
        tryCompare(tumblerView, "offset", 0);
        compare(tumbler.currentIndex, 0);
    }

    function test_itemsCorrectlyPositioned() {
        createTumbler();

        tumbler.model = 4;
        tumbler.height = 120;
        compare(tumblerDelegateHeight, 40);
        checkItemSizes();

        wait(tumblerView.highlightMoveDuration);
        var firstItemCenterPos = itemCenterPos(1);
        var firstItem = tumblerView.itemAt(firstItemCenterPos.x, firstItemCenterPos.y);
        var actualPos = testCase.mapFromItem(firstItem, 0, 0);
        compare(actualPos.x, tumbler.leftPadding);
        compare(actualPos.y, tumbler.topPadding + 40);

        tumbler.forceActiveFocus();
        keyClick(Qt.Key_Down);
        tryCompare(tumblerView, "offset", 3.0);
        tryCompare(tumbler, "moving", false);
        firstItemCenterPos = itemCenterPos(0);
        firstItem = tumblerView.itemAt(firstItemCenterPos.x, firstItemCenterPos.y);
        verify(firstItem);
        // Test QTBUG-40298.
        actualPos = testCase.mapFromItem(firstItem, 0, 0);
        fuzzyCompare(actualPos.x, tumbler.leftPadding, 0.0001);
        fuzzyCompare(actualPos.y, tumbler.topPadding, 0.0001);

        var secondItemCenterPos = itemCenterPos(1);
        var secondItem = tumblerView.itemAt(secondItemCenterPos.x, secondItemCenterPos.y);
        verify(secondItem);
        verify(firstItem.y < secondItem.y);

        var thirdItemCenterPos = itemCenterPos(2);
        var thirdItem = tumblerView.itemAt(thirdItemCenterPos.x, thirdItemCenterPos.y);
        verify(thirdItem);
        verify(firstItem.y < thirdItem.y);
        verify(secondItem.y < thirdItem.y);
    }

    function test_focusPastTumbler() {
        tumbler = createTemporaryObject(tumblerComponent, testCase);
        verify(tumbler);

        var mouseArea = createTemporaryQmlObject(
            "import QtQuick 2.2; TextInput { activeFocusOnTab: true; width: 50; height: 50 }", testCase, "");

        tumbler.forceActiveFocus();
        verify(tumbler.activeFocus);

        keyClick(Qt.Key_Tab);
        verify(!tumbler.activeFocus);
        verify(mouseArea.activeFocus);
    }

    function test_datePicker() {
        var component = Qt.createComponent("TumblerDatePicker.qml");
        compare(component.status, Component.Ready, component.errorString());
        tumbler = createTemporaryObject(component, testCase);
        // Should not be any warnings.

        tryCompare(tumbler.dayTumbler, "currentIndex", 0);
        compare(tumbler.dayTumbler.count, 31);
        compare(tumbler.monthTumbler.currentIndex, 0);
        compare(tumbler.monthTumbler.count, 12);
        compare(tumbler.yearTumbler.currentIndex, 0);
        tryCompare(tumbler.yearTumbler, "count", 100);

        verify(findView(tumbler.dayTumbler).children.length >= tumbler.dayTumbler.visibleItemCount);
        verify(findView(tumbler.monthTumbler).children.length >= tumbler.monthTumbler.visibleItemCount);
        // TODO: do this properly somehow
        wait(100);
        verify(findView(tumbler.yearTumbler).children.length >= tumbler.yearTumbler.visibleItemCount);

        // March.
        tumbler.monthTumbler.currentIndex = 2;
        tryCompare(tumbler.monthTumbler, "currentIndex", 2);

        // 30th of March.
        tumbler.dayTumbler.currentIndex = 29;
        tryCompare(tumbler.dayTumbler, "currentIndex", 29);

        // February.
        tumbler.monthTumbler.currentIndex = 1;
        tryCompare(tumbler.monthTumbler, "currentIndex", 1);
        tryCompare(tumbler.dayTumbler, "currentIndex", 27);
    }

    Component {
        id: timePickerComponent

        Row {
            property alias minuteTumbler: minuteTumbler
            property alias amPmTumbler: amPmTumbler

            Tumbler {
                id: minuteTumbler
                currentIndex: 6
                model: 60
                width: 50
                height: 150
            }

            Tumbler {
                id: amPmTumbler
                model: ["AM", "PM"]
                width: 50
                height: 150
                contentItem: ListView {
                    anchors.fill: parent
                    model: amPmTumbler.model
                    delegate: amPmTumbler.delegate
                }
            }
        }
    }

    function test_listViewTimePicker() {
        var root = createTemporaryObject(timePickerComponent, testCase);
        verify(root);

        mouseDrag(root.minuteTumbler, root.minuteTumbler.width / 2, root.minuteTumbler.height / 2, 0, 50);
        // Shouldn't crash.
        mouseDrag(root.amPmTumbler, root.amPmTumbler.width / 2, root.amPmTumbler.height / 2, 0, 50);
    }

    function test_displacement_data() {
        var data = [
            // At 0 offset, the first item is current.
            { count: 6, index: 0, offset: 0, expectedDisplacement: 0 },
            { count: 6, index: 1, offset: 0, expectedDisplacement: -1 },
            { count: 6, index: 5, offset: 0, expectedDisplacement: 1 },
            // When we start to move the first item down, the second item above it starts to become current.
            { count: 6, index: 0, offset: 0.25, expectedDisplacement: -0.25 },
            { count: 6, index: 1, offset: 0.25, expectedDisplacement: -1.25 },
            { count: 6, index: 5, offset: 0.25, expectedDisplacement: 0.75 },
            { count: 6, index: 0, offset: 0.5, expectedDisplacement: -0.5 },
            { count: 6, index: 1, offset: 0.5, expectedDisplacement: -1.5 },
            { count: 6, index: 5, offset: 0.5, expectedDisplacement: 0.5 },
            // By this stage, the delegate at index 1 is destroyed, so we can't test its displacement.
            { count: 6, index: 0, offset: 0.75, expectedDisplacement: -0.75 },
            { count: 6, index: 5, offset: 0.75, expectedDisplacement: 0.25 },
            { count: 6, index: 0, offset: 4.75, expectedDisplacement: 1.25 },
            { count: 6, index: 1, offset: 4.75, expectedDisplacement: 0.25 },
            { count: 6, index: 0, offset: 4.5, expectedDisplacement: 1.5 },
            { count: 6, index: 1, offset: 4.5, expectedDisplacement: 0.5 },
            { count: 6, index: 0, offset: 4.25, expectedDisplacement: 1.75 },
            { count: 6, index: 1, offset: 4.25, expectedDisplacement: 0.75 },
            // count == visibleItemCount
            { count: 3, index: 0, offset: 0, expectedDisplacement: 0 },
            { count: 3, index: 1, offset: 0, expectedDisplacement: -1 },
            { count: 3, index: 2, offset: 0, expectedDisplacement: 1 },
            // count < visibleItemCount
            { count: 2, index: 0, offset: 0, expectedDisplacement: 0 },
            { count: 2, index: 1, offset: 0, expectedDisplacement: 1 },
            // count == 1
            { count: 1, index: 0, offset: 0, expectedDisplacement: 0 }
        ];
        for (var i = 0; i < data.length; ++i) {
            var row = data[i];
            row.tag = "delegate" + row.index + " offset=" + row.offset + " expectedDisplacement=" + row.expectedDisplacement;
        }
        return data;
    }

    property Component displacementDelegate: Text {
        objectName: "delegate" + index
        text: modelData
        opacity: 0.2 + Math.max(0, 1 - Math.abs(Tumbler.displacement)) * 0.8
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter

        Text {
            text: parent.displacement.toFixed(2)
            anchors.right: parent.right
            anchors.verticalCenter: parent.verticalCenter
        }

        property real displacement: Tumbler.displacement
    }

    function test_displacement(data) {
        createTumbler();

        // TODO: test setting these in the opposite order (delegate after model
        // doesn't seem to cause a change in delegates in PathView)
        tumbler.wrap = true;
        tumbler.delegate = displacementDelegate;
        tumbler.model = data.count;
        compare(tumbler.count, data.count);

        var delegate = findChild(tumblerView, "delegate" + data.index);
        verify(delegate);

        tumblerView.offset = data.offset;
        compare(delegate.displacement, data.expectedDisplacement);

        // test displacement after adding and removing items
    }

    function test_wrap() {
        createTumbler();

        tumbler.model = 5;
        compare(tumbler.count, 5);

        tumbler.currentIndex = 2;
        compare(tumblerView.currentIndex, 2);

        tumbler.wrap = false;
        tumblerView = findView(tumbler);
        compare(tumbler.count, 5);
        compare(tumbler.currentIndex, 2);
        // Tumbler's count hasn't changed (the model hasn't changed),
        // but the new view needs time to instantiate its items.
        tryCompare(tumblerView, "count", 5);
        compare(tumblerView.currentIndex, 2);
    }

    Component {
        id: twoItemTumbler

        Tumbler {
            model: 2
        }
    }

    Component {
        id: tenItemTumbler

        Tumbler {
            model: 10
        }
    }

    function test_countWrap() {
        tumbler = createTemporaryObject(tumblerComponent, testCase);
        verify(tumbler);

        // Check that a count that is less than visibleItemCount results in wrap being set to false.
        verify(2 < tumbler.visibleItemCount);
        tumbler.model = 2;
        compare(tumbler.count, 2);
        compare(tumbler.wrap, false);
    }

    function test_explicitlyNonwrapping() {
        // Check that explicitly setting wrap to false works even when it was implicitly false.
        var explicitlyNonWrapping = createTemporaryObject(twoItemTumbler, testCase);
        verify(explicitlyNonWrapping);
        tryCompare(explicitlyNonWrapping, "wrap", false);

        explicitlyNonWrapping.wrap = false;
        // wrap shouldn't be set to true now that there are more items than there are visible ones.
        verify(10 > explicitlyNonWrapping.visibleItemCount);
        explicitlyNonWrapping.model = 10;
        compare(explicitlyNonWrapping.wrap, false);

        // Test resetting wrap back to the default behavior.
        explicitlyNonWrapping.wrap = undefined;
        compare(explicitlyNonWrapping.wrap, true);
    }

    function test_explicitlyWrapping() {
        // Check that explicitly setting wrap to true works even when it was implicitly true.
        var explicitlyWrapping = createTemporaryObject(tenItemTumbler, testCase);
        verify(explicitlyWrapping);
        compare(explicitlyWrapping.wrap, true);

        explicitlyWrapping.wrap = true;
        // wrap shouldn't be set to false now that there are more items than there are visible ones.
        explicitlyWrapping.model = 2;
        compare(explicitlyWrapping.wrap, true);

        // Test resetting wrap back to the default behavior.
        explicitlyWrapping.wrap = undefined;
        compare(explicitlyWrapping.wrap, false);
    }

    Component {
        id: customListViewTumblerComponent

        Tumbler {
            id: listViewTumbler

            contentItem: ListView {
                anchors.fill: parent
                model: listViewTumbler.model
                delegate: listViewTumbler.delegate

                snapMode: ListView.SnapToItem
                highlightRangeMode: ListView.StrictlyEnforceRange
                preferredHighlightBegin: height / 2 - (height / listViewTumbler.visibleItemCount / 2)
                preferredHighlightEnd: height / 2 + (height / listViewTumbler.visibleItemCount / 2)
                clip: true
            }
        }
    }

    Component {
        id: customPathViewTumblerComponent

        Tumbler {
            id: pathViewTumbler

            contentItem: PathView {
                id: pathView
                model: pathViewTumbler.model
                delegate: pathViewTumbler.delegate
                clip: true
                pathItemCount: pathViewTumbler.visibleItemCount + 1
                preferredHighlightBegin: 0.5
                preferredHighlightEnd: 0.5
                dragMargin: width / 2

                path: Path {
                    startX: pathView.width / 2
                    startY: -pathView.delegateHeight / 2
                    PathLine {
                        x: pathView.width / 2
                        y: pathView.pathItemCount * pathView.delegateHeight - pathView.delegateHeight / 2
                    }
                }

                property real delegateHeight: pathViewTumbler.availableHeight / pathViewTumbler.visibleItemCount
            }
        }
    }

    function test_customContentItemAtConstruction_data() {
        return [
            { tag: "ListView", component: customListViewTumblerComponent },
            { tag: "PathView", component: customPathViewTumblerComponent }
        ];
    }

    function test_customContentItemAtConstruction(data) {
        var tumbler = createTemporaryObject(data.component, testCase);
        // Shouldn't assert.

        tumbler.model = 5;
        compare(tumbler.count, 5);

        tumbler.currentIndex = 2;
        var tumblerView = findView(tumbler);
        compare(tumblerView.currentIndex, 2);

        tumblerView.incrementCurrentIndex();
        compare(tumblerView.currentIndex, 3);
        compare(tumbler.currentIndex, 3);

        // Shouldn't have any affect.
        tumbler.wrap = false;
        compare(tumbler.count, 5);
        compare(tumblerView.currentIndex, 3);
        compare(tumbler.currentIndex, 3);
    }

    function findFirstDelegateWithText(view, text) {
        var delegate = null;
        var contentItem = view.hasOwnProperty("contentItem") ? view.contentItem : view;
        for (var i = 0; i < contentItem.children.length && !delegate; ++i) {
            var child = contentItem.children[i];
            if (child.hasOwnProperty("text") && child.text === text)
                delegate = child;
        }
        return delegate;
    }

    function test_customContentItemAfterConstruction_data() {
        return [
            { tag: "ListView", componentPath: "TumblerListView.qml" },
            { tag: "PathView", componentPath: "TumblerPathView.qml" }
        ];
    }

    function test_customContentItemAfterConstruction(data) {
        createTumbler();

        tumbler.model = 5;
        compare(tumbler.count, 5);

        tumbler.currentIndex = 2;
        compare(tumblerView.currentIndex, 2);

        var contentItemComponent = Qt.createComponent(data.componentPath);
        compare(contentItemComponent.status, Component.Ready);

        var customContentItem = createTemporaryObject(contentItemComponent, tumbler);
        tumbler.contentItem = customContentItem;
        compare(tumbler.count, 5);
        tumblerView = findView(tumbler);
        compare(tumblerView.currentIndex, 2);

        var delegate = findFirstDelegateWithText(tumblerView, "Custom2");
        verify(delegate);
        compare(delegate.height, defaultImplicitDelegateHeight);
        tryCompare(delegate.Tumbler, "displacement", 0);

        tumblerView.incrementCurrentIndex();
        compare(tumblerView.currentIndex, 3);
        compare(tumbler.currentIndex, 3);
    }

    function test_displacementListView_data() {
        var offset = defaultListViewTumblerOffset;

        var data = [
            // At 0 contentY, the first item is current.
            { contentY: offset, expectedDisplacements: [
                { index: 0, displacement: 0 },
                { index: 1, displacement: -1 },
                { index: 2, displacement: -2 } ]
            },
            // When we start to move the first item down, the second item above it starts to become current.
            { contentY: offset + defaultImplicitDelegateHeight * 0.25, expectedDisplacements: [
                { index: 0, displacement: 0.25 },
                { index: 1, displacement: -0.75 },
                { index: 2, displacement: -1.75 } ]
            },
            { contentY: offset + defaultImplicitDelegateHeight * 0.5, expectedDisplacements: [
                { index: 0, displacement: 0.5 },
                { index: 1, displacement: -0.5 },
                { index: 2, displacement: -1.5 } ]
            },
            { contentY: offset + defaultImplicitDelegateHeight * 0.75, expectedDisplacements: [
                { index: 0, displacement: 0.75 },
                { index: 1, displacement: -0.25 } ]
            },
            { contentY: offset + defaultImplicitDelegateHeight * 3.5, expectedDisplacements: [
                { index: 3, displacement: 0.5 },
                { index: 4, displacement: -0.5 } ]
            }
        ];
        for (var i = 0; i < data.length; ++i) {
            var row = data[i];
            row.tag = "contentY=" + row.contentY;
        }
        return data;
    }

    function test_displacementListView(data) {
        createTumbler();

        tumbler.wrap = false;
        tumbler.delegate = displacementDelegate;
        tumbler.model = 5;
        compare(tumbler.count, 5);
        // Ensure assumptions about the tumbler used in our data() function are correct.
        tumblerView = findView(tumbler);
        compare(tumblerView.contentY, -defaultImplicitDelegateHeight);
        var delegateCount = 0;
        var listView = tumblerView;
        var listViewContentItem = tumblerView.contentItem;

        // We use the mouse instead of setting contentY directly, otherwise the
        // items snap back into place. This doesn't seem to be an issue for
        // PathView for some reason.
        //
        // I tried lots of things to get this test to work with small changes
        // in ListView's contentY (to match the tests for a PathView-based Tumbler), but they didn't work:
        //
        // - Pressing once and then directly moving the mouse to the correct location
        // - Pressing once and interpolating the mouse position to the correct location
        // - Pressing once and doing some dragging up and down to trigger the
        //   overThreshold of QQuickFlickable
        //
        // Even after the last item above, QQuickFlickable wouldn't consider it a drag.
        // It seems that overThreshold is set too late, and because the drag distance is quite small
        // to begin with, nothing changes (the displacement was always very close to 0 in the end).

        // Ensure that we at least cover the distance required to reach the desired contentY.
        var distanceToReachContentY = data.contentY - defaultListViewTumblerOffset;
        var distance = Math.abs(distanceToReachContentY) + tumbler.height / 2;
        // If distanceToReachContentY is 0, we're testing 0 displacement, so we don't need to do anything.
        if (distanceToReachContentY != 0) {
            mousePress(tumbler, tumblerXCenter(), tumblerYCenter());

            var dragDirection = distanceToReachContentY > 0 ? -1 : 1;
            for (var i = 0; i < distance && Math.floor(listView.contentY) !== Math.floor(data.contentY); ++i) {
                mouseMove(tumbler, tumblerXCenter(), tumblerYCenter() + i * dragDirection);
                wait(1); // because Flickable pays attention to velocity, we need some time between movements (qtdeclarative ebf07c3)
            }
        }

        for (var i = 0; i < data.expectedDisplacements.length; ++i) {
            var delegate = findChild(listViewContentItem, "delegate" + data.expectedDisplacements[i].index);
            verify(delegate);
            compare(delegate.height, defaultImplicitDelegateHeight);
            // Due to the way we must perform this test, we can't expect high precision.
            var expectedDisplacement = data.expectedDisplacements[i].displacement;
            fuzzyCompare(delegate.displacement, expectedDisplacement, 0.1,
                "Delegate of ListView-based Tumbler at index " + data.expectedDisplacements[i].index
                    + " has displacement of " + delegate.displacement + " when it should be " + expectedDisplacement);
        }

        if (distanceToReachContentY != 0)
            mouseRelease(tumbler, tumblerXCenter(), itemCenterPos(1) + (data.contentY - defaultListViewTumblerOffset), Qt.LeftButton);
    }

    function test_listViewFlickAboveBounds_data() {
        // Tests that flicking above the bounds when already at the top of the
        // tumbler doesn't result in an incorrect displacement.
        var data = [];
        // Less than two items doesn't make sense. The default visibleItemCount
        // is 3, so we test a bit more than double that.
        for (var i = 2; i <= 7; ++i) {
            data.push({ tag: i + " items", model: i });
        }
        return data;
    }

    function test_listViewFlickAboveBounds(data) {
        createTumbler();

        tumbler.wrap = false;
        tumbler.delegate = displacementDelegate;
        tumbler.model = data.model;
        tumblerView = findView(tumbler);

        mousePress(tumbler, tumblerXCenter(), tumblerYCenter());

        // Ensure it's stationary.
        var listView = tumblerView;
        compare(listView.contentY, defaultListViewTumblerOffset);

        // We could just move up until the contentY changed, but this is safer.
        var distance = tumbler.height;
        var changed = false;

        for (var i = 0; i < distance && !changed; ++i) {
            mouseMove(tumbler, tumblerXCenter(), tumblerYCenter() + i, 10);

            // Don't test until the contentY has actually changed.
            if (Math.abs(listView.contentY) - listView.preferredHighlightBegin > 0.01) {

                for (var delegateIndex = 0; delegateIndex < Math.min(tumbler.count, tumbler.visibleItemCount); ++delegateIndex) {
                    var delegate = findChild(listView.contentItem, "delegate" + delegateIndex);
                    verify(delegate);

                    verify(delegate.displacement <= -delegateIndex, "Delegate at index " + delegateIndex + " has a displacement of "
                        + delegate.displacement + " when it should be less than or equal to " + -delegateIndex);
                    verify(delegate.displacement > -delegateIndex - 0.1, "Delegate at index 0 has a displacement of "
                        + delegate.displacement + " when it should be greater than ~ " + -delegateIndex - 0.1);
                }

                changed = true;
            }
        }

        // Sanity check that something was actually tested.
        verify(changed);

        mouseRelease(tumbler, tumblerXCenter(), tumbler.topPadding);
    }

    property Component objectNameDelegate: Text {
        objectName: "delegate" + index
        text: modelData
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
    }

    function test_visibleItemCount_data() {
        var data = [
            // e.g. {0: 2} = {delegate index: y pos / delegate height}
            // Skip item at index 3, because it's out of view.
            { model: 6, visibleItemCount: 5, expectedYPositions: {0: 2, 1: 3, 2: 4, 4: 0} },
            { model: 5, visibleItemCount: 3, expectedYPositions: {0: 1, 1: 2, 4: 0} },
            // Takes up the whole view.
            { model: 2, visibleItemCount: 1, expectedYPositions: {0: 0} },
        ];

        for (var i = 0; i < data.length; ++i) {
            data[i].tag = "items=" + data[i].model + ", visibleItemCount=" + data[i].visibleItemCount;
        }
        return data;
    }

    function test_visibleItemCount(data) {
        createTumbler();

        tumbler.delegate = objectNameDelegate;
        tumbler.visibleItemCount = data.visibleItemCount;

        tumbler.model = data.model;
        compare(tumbler.count, data.model);

        for (var delegateIndex = 0; delegateIndex < data.visibleItemCount; ++delegateIndex) {
            if (data.expectedYPositions.hasOwnProperty(delegateIndex)) {
                var delegate = findChild(tumblerView, "delegate" + delegateIndex);
                verify(delegate, "Delegate found at index " + delegateIndex);
                var expectedYPos = data.expectedYPositions[delegateIndex] * tumblerDelegateHeight;
                compare(delegate.mapToItem(tumbler.contentItem, 0, 0).y, expectedYPos);
            }
        }
    }

    property Component wrongDelegateTypeComponent: QtObject {
        property real displacement: Tumbler.displacement
    }

    property Component noParentDelegateComponent: Item {
        property real displacement: Tumbler.displacement
    }

    function test_attachedProperties() {
        tumbler = createTemporaryObject(tumblerComponent, testCase);
        verify(tumbler);

        // TODO: crashes somewhere in QML's guts
//        tumbler.model = 5;
//        tumbler.delegate = wrongDelegateTypeComponent;
//        ignoreWarning("Attached properties of Tumbler must be accessed from within a delegate item");
//        // Cause displacement to be changed. The warning isn't triggered if we don't do this.
//        tumbler.contentItem.offset += 1;

        ignoreWarning("Tumbler: attached properties must be accessed through a delegate item that has a parent");
        createTemporaryObject(noParentDelegateComponent, null);

        ignoreWarning("Tumbler: attempting to access attached property on item without an \"index\" property");
        var object = createTemporaryObject(noParentDelegateComponent, testCase);
        verify(object);
    }

    property Component paddingDelegate: Text {
        objectName: "delegate" + index
        text: modelData
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter

        Rectangle {
            anchors.fill: parent
            color: "transparent"
            border.color: "red"
            border.width: 1
        }
    }

    function test_padding_data() {
        var data = [];

        data.push({ padding: 0 });
        data.push({ padding: 10 });
        data.push({ left: 10, top: 10 });
        data.push({ right: 10, bottom: 10 });

        for (var i = 0; i < data.length; ++i) {
            var tag = "";

            if (data[i].padding !== undefined)
                tag += "padding: " + data[i].padding + " ";
            if (data[i].left !== undefined)
                tag += "left: " + data[i].left + " ";
            if (data[i].right !== undefined)
                tag += "right: " + data[i].right + " ";
            if (data[i].top !== undefined)
                tag += "top: " + data[i].top + " ";
            if (data[i].bottom !== undefined)
                tag += "bottom: " + data[i].bottom + " ";
            tag = tag.slice(0, -1);

            data[i].tag = tag;
        }

        return data;
    }

    function test_padding(data) {
        createTumbler();

        tumbler.delegate = paddingDelegate;
        tumbler.model = 5;
        compare(tumbler.padding, 0);
        compare(tumbler.leftPadding, 0);
        compare(tumbler.rightPadding, 0);
        compare(tumbler.topPadding, 0);
        compare(tumbler.bottomPadding, 0);
        compare(tumbler.contentItem.x, 0);
        compare(tumbler.contentItem.y, 0);

        if (data.padding !== undefined)
            tumbler.padding = data.padding;
        if (data.left !== undefined)
            tumbler.leftPadding = data.left;
        if (data.right !== undefined)
            tumbler.rightPadding = data.right;
        if (data.top !== undefined)
            tumbler.topPadding = data.top;
        if (data.bottom !== undefined)
            tumbler.bottomPadding = data.bottom;

        compare(tumbler.availableWidth, tumbler.implicitWidth - tumbler.leftPadding - tumbler.rightPadding);
        compare(tumbler.availableHeight, tumbler.implicitHeight - tumbler.topPadding - tumbler.bottomPadding);
        compare(tumbler.contentItem.x, tumbler.leftPadding);
        compare(tumbler.contentItem.y, tumbler.topPadding);

        var pathView = tumbler.contentItem;
        var expectedDelegateHeight = tumbler.availableHeight / tumbler.visibleItemCount;
        var itemIndicesInVisualOrder = [4, 0, 1];
        for (var i = 0; i < itemIndicesInVisualOrder.length; ++i) {
            var delegate = findChild(pathView, "delegate" + itemIndicesInVisualOrder[i]);
            verify(delegate, "Couldn't find delegate at index " + itemIndicesInVisualOrder[i]
                + " (iteration " + i + " out of " + (pathView.children.length - 1) + ")");

            compare(delegate.width, tumbler.availableWidth);
            compare(delegate.height, expectedDelegateHeight);

            var expectedY = tumbler.topPadding + i * expectedDelegateHeight;
            var mappedPos = delegate.mapToItem(null, delegate.width / 2, 0);
            fuzzyCompare(mappedPos.y, expectedY, 0.5,
                "Tumbler's PathView delegate at index " + itemIndicesInVisualOrder[i]
                    + " should have a y pos of " + expectedY + ", but it's actually " + mappedPos.y.toFixed(20));

            var expectedX = tumbler.leftPadding;
            compare(delegate.mapToItem(null, 0, 0).x, expectedX,
                "Tumbler's PathView delegate at index " + itemIndicesInVisualOrder[i]
                    + " should have a x pos of " + expectedX + ", but it's actually " + mappedPos.x.toFixed(20));
        }

        // Force new items to be created, as there was a bug where the path was correct until this happened.
        compare(tumblerView.offset, 0);
        ++tumbler.currentIndex;
        tryCompare(tumblerView, "offset", 4, tumblerView.highlightMoveDuration * 2);
    }

    function test_moving_data() {
        return [
            { tag: "wrap:true", wrap: true },
            { tag: "wrap:false", wrap: false }
        ]
    }

    function test_moving(data) {
        createTumbler({wrap: data.wrap, model: 5})
        compare(tumbler.wrap, data.wrap)
        compare(tumbler.moving, false)

        waitForRendering(tumbler)

        mousePress(tumbler, tumbler.width / 2, tumbler.height / 2, Qt.LeftButton)
        compare(tumbler.moving, false)

        for (var y = tumbler.height / 2; y >= tumbler.height / 4; y -= 10)
            mouseMove(tumbler, tumbler.width / 2, y, 1)
        compare(tumbler.moving, true)

        mouseRelease(tumbler, tumbler.width / 2, tumbler.height / 4, Qt.LeftButton)
        compare(tumbler.moving, true)
        tryCompare(tumbler, "moving", false)
    }

    Component {
        id: qtbug61374Component

        Row {
            property alias tumbler: tumbler
            property alias label: label

            Component.onCompleted: {
                tumbler.currentIndex = 2
            }

            Tumbler {
                id: tumbler
                model: 5
                // ...
            }

            Label {
                id: label
                text: tumbler.currentItem.text
            }
        }
    }

    function test_qtbug61374() {
        var row = createTemporaryObject(qtbug61374Component, testCase);
        verify(row);

        var tumbler = row.tumbler;
        tryCompare(tumbler, "currentIndex", 2);

        tumblerView = findView(tumbler);

        var label = row.label;
        compare(label.text, "2");
    }

    function test_positionViewAtIndex_data() {
        return [
            // Should be 20, 21, ... but there is a documented limitation for this in positionViewAtIndex()'s docs.
            { tag: "wrap=true, mode=Beginning", wrap: true, mode: Tumbler.Beginning, expectedVisibleIndices: [21, 22, 23, 24, 25] },
            { tag: "wrap=true, mode=Center", wrap: true, mode: Tumbler.Center, expectedVisibleIndices: [18, 19, 20, 21, 22] },
            { tag: "wrap=true, mode=End", wrap: true, mode: Tumbler.End, expectedVisibleIndices: [16, 17, 18, 19, 20] },
            // Same as Beginning; should start at 20.
            { tag: "wrap=true, mode=Contain", wrap: true, mode: Tumbler.Contain, expectedVisibleIndices: [21, 22, 23, 24, 25] },
            { tag: "wrap=true, mode=SnapPosition", wrap: true, mode: Tumbler.SnapPosition, expectedVisibleIndices: [18, 19, 20, 21, 22] },
            { tag: "wrap=false, mode=Beginning", wrap: false, mode: Tumbler.Beginning, expectedVisibleIndices: [20, 21, 22, 23, 24] },
            { tag: "wrap=false, mode=Center", wrap: false, mode: Tumbler.Center, expectedVisibleIndices: [18, 19, 20, 21, 22] },
            { tag: "wrap=false, mode=End", wrap: false, mode: Tumbler.End, expectedVisibleIndices: [16, 17, 18, 19, 20] },
            { tag: "wrap=false, mode=Visible", wrap: false, mode: Tumbler.Visible, expectedVisibleIndices: [16, 17, 18, 19, 20] },
            { tag: "wrap=false, mode=Contain", wrap: false, mode: Tumbler.Contain, expectedVisibleIndices: [16, 17, 18, 19, 20] },
            { tag: "wrap=false, mode=SnapPosition", wrap: false, mode: Tumbler.SnapPosition, expectedVisibleIndices: [18, 19, 20, 21, 22] }
        ]
    }

    function test_positionViewAtIndex(data) {
        createTumbler({ wrap: data.wrap, model: 40, visibleItemCount: 5 })
        compare(tumbler.wrap, data.wrap)

        waitForRendering(tumbler)

        tumbler.positionViewAtIndex(20, data.mode)
        tryCompare(tumbler, "moving", false)

        compare(tumbler.visibleItemCount, 5)
        for (var i = 0; i < 5; ++i) {
            // Find the item through its text, as that's easier than child/itemAt().
            var text = data.expectedVisibleIndices[i].toString()
            var item = findDelegateWithText(tumblerView, text)
            verify(item, "found no item with text \"" + text + "\"")
            compare(item.text, data.expectedVisibleIndices[i].toString())

            // Ensure that it's at the position we expect.
            var expectedPos = itemTopLeftPos(i)
            var actualPos = testCase.mapFromItem(item, 0, 0)
            compare(actualPos.x, expectedPos.x, "expected delegate with text " + item.text
                + " to have an x pos of " + expectedPos.x + " but it was " + actualPos.x)
            compare(actualPos.y, expectedPos.y, "expected delegate with text " + item.text
                + " to have an y pos of " + expectedPos.y + " but it was " + actualPos.y)
        }
    }

    Component {
        id: setCurrentIndexOnImperativeModelChangeComponent

        Tumbler {
            onModelChanged: currentIndex = model - 2
        }
    }

    function test_setCurrentIndexOnImperativeModelChange() {
        var tumbler = createTemporaryObject(setCurrentIndexOnImperativeModelChangeComponent, testCase);
        verify(tumbler);

        tumbler.model = 4
        compare(tumbler.count, 4);
        tumblerView = findView(tumbler);
        tryCompare(tumblerView, "count", 4);

        // 4 - 2 = 2
        compare(tumbler.currentIndex, 2);

        ++tumbler.model;
        compare(tumbler.count, 5);
        compare(tumbler.wrap, true);
        tumblerView = findView(tumbler);
        tryCompare(tumblerView, "count", 5);
        // 5 - 2 = 3
        compare(tumbler.currentIndex, 3);
    }

    Component {
        id: setCurrentIndexOnDeclarativeModelChangeComponent

        Item {
            property alias tumbler: tumbler

            property int setting: 4

            Tumbler {
                id: tumbler
                model: setting
                onModelChanged: currentIndex = model - 2
            }
        }
    }

    function test_setCurrentIndexOnDeclarativeModelChange() {
        var root = createTemporaryObject(setCurrentIndexOnDeclarativeModelChangeComponent, testCase);
        verify(root);

        var tumbler = root.tumbler;
        compare(tumbler.count, 4);
        compare(tumbler.wrap, false);
        tumblerView = findView(tumbler);
        tryCompare(tumblerView, "count", 4);
        // 4 - 2 = 2
        compare(tumbler.currentIndex, 2);

        ++root.setting;
        compare(tumbler.count, 5);
        compare(tumbler.wrap, true);
        tumblerView = findView(tumbler);
        tryCompare(tumblerView, "count", 5);
        // 5 - 2 = 3
        compare(tumbler.currentIndex, 3);
    }

    function test_displacementAfterResizing() {
        createTumbler({
            width: 200,
            wrap: false,
            delegate: displacementDelegate,
            model: 30,
            visibleItemCount: 7,
            currentIndex: 15
        })

        var delegate = findChild(tumblerView, "delegate15")
        verify(delegate)

        tryCompare(delegate, "displacement", 0)

        // Resizing the Tumbler shouldn't affect the displacement.
        tumbler.height *= 1.4
        tryCompare(delegate, "displacement", 0)
    }
}