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
|
/*
* SPDX-FileCopyrightText: 2012 Frank Reininghaus <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kitemviews/kitemlistcontroller.h"
#include "kitemviews/kfileitemlistview.h"
#include "kitemviews/kfileitemmodel.h"
#include "kitemviews/kitemlistcontainer.h"
#include "kitemviews/kitemlistselectionmanager.h"
#include "kitemviews/private/kitemlistviewlayouter.h"
#include "testdir.h"
#include <QGraphicsSceneMouseEvent>
#include <QProxyStyle>
#include <QSignalSpy>
#include <QStandardPaths>
#include <QTest>
/**
* \class KItemListControllerTestStyle is a proxy style for testing the
* KItemListController with different style hint options, e.g. single/double
* click activation.
*/
class KItemListControllerTestStyle : public QProxyStyle
{
Q_OBJECT
public:
KItemListControllerTestStyle(QStyle *style)
: QProxyStyle(style)
, m_activateItemOnSingleClick((bool)style->styleHint(SH_ItemView_ActivateItemOnSingleClick))
{
}
void setActivateItemOnSingleClick(bool activateItemOnSingleClick)
{
m_activateItemOnSingleClick = activateItemOnSingleClick;
}
bool activateItemOnSingleClick() const
{
return m_activateItemOnSingleClick;
}
int styleHint(StyleHint hint, const QStyleOption *option = nullptr, const QWidget *widget = nullptr, QStyleHintReturn *returnData = nullptr) const override
{
switch (hint) {
case QStyle::SH_ItemView_ActivateItemOnSingleClick:
return (int)activateItemOnSingleClick();
default:
return QProxyStyle::styleHint(hint, option, widget, returnData);
}
}
private:
bool m_activateItemOnSingleClick;
};
Q_DECLARE_METATYPE(KFileItemListView::ItemLayout)
Q_DECLARE_METATYPE(Qt::Orientation)
Q_DECLARE_METATYPE(KItemListController::SelectionBehavior)
Q_DECLARE_METATYPE(KItemSet)
class KItemListControllerTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void init();
void cleanup();
void testKeyboardNavigationMultiSelection_data();
void testKeyboardNavigationMultiSelection();
void testKeyboardNavigationSingleSelectionNoSelection_data();
void testKeyboardNavigationSingleSelectionNoSelection();
void testMouseClickActivation();
void testKeyboardNavigationAfterMouseSelection();
private:
/**
* Make sure that the number of columns in the view is equal to \a count
* by changing the geometry of the container.
*/
void adjustGeometryForColumnCount(int count);
void simulateMouseClickOnItem(int index);
private:
KFileItemListView *m_view;
KItemListController *m_controller;
KItemListSelectionManager *m_selectionManager;
KFileItemModel *m_model;
TestDir *m_testDir;
KItemListContainer *m_container;
KItemListControllerTestStyle *m_testStyle;
};
/**
* This function initializes the member objects, creates the temporary files, and
* shows the view on the screen on startup. This could also be done before every
* single test, but this would make the time needed to run the test much larger.
*/
void KItemListControllerTest::initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
qRegisterMetaType<KItemSet>("KItemSet");
m_testDir = new TestDir();
m_model = new KFileItemModel();
m_view = new KFileItemListView();
m_controller = new KItemListController(m_model, m_view, this);
m_container = new KItemListContainer(m_controller);
#ifndef QT_NO_ACCESSIBILITY
m_view->setAccessibleParentsObject(m_container);
#endif
m_controller = m_container->controller();
m_controller->setSelectionBehavior(KItemListController::MultiSelection);
m_selectionManager = m_controller->selectionManager();
m_testStyle = new KItemListControllerTestStyle(m_view->style());
m_view->setStyle(m_testStyle);
QStringList files;
files << "a1"
<< "a2"
<< "a3"
<< "b1"
<< "c1"
<< "c2"
<< "c3"
<< "c4"
<< "c5"
<< "d1"
<< "d2"
<< "d3"
<< "d4"
<< "e"
<< "e 2"
<< "e 3"
<< "e 4"
<< "e 5"
<< "e 6"
<< "e 7";
m_testDir->createFiles(files);
m_model->loadDirectory(m_testDir->url());
QSignalSpy spyDirectoryLoadingCompleted(m_model, &KFileItemModel::directoryLoadingCompleted);
QVERIFY(spyDirectoryLoadingCompleted.wait());
m_container->show();
QVERIFY(QTest::qWaitForWindowExposed(m_container));
}
void KItemListControllerTest::cleanupTestCase()
{
delete m_container;
m_container = nullptr;
delete m_testDir;
m_testDir = nullptr;
}
/** Before each test, the current item, selection, and item size are reset to the defaults. */
void KItemListControllerTest::init()
{
m_selectionManager->setCurrentItem(0);
QCOMPARE(m_selectionManager->currentItem(), 0);
m_selectionManager->clearSelection();
QVERIFY(!m_selectionManager->hasSelection());
const QSizeF itemSize(50, 50);
m_view->setItemSize(itemSize);
QCOMPARE(m_view->itemSize(), itemSize);
m_controller->setSelectionBehavior(KItemListController::MultiSelection);
QCOMPARE(m_controller->selectionBehavior(), KItemListController::MultiSelection);
}
void KItemListControllerTest::cleanup()
{
m_controller->setSelectionModeEnabled(false);
}
/**
* \class KeyPress is a small helper struct that represents a key press event,
* including the key and the keyboard modifiers.
*/
struct KeyPress {
KeyPress(Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier)
: m_key(key)
, m_modifier(modifier)
{
}
Qt::Key m_key;
Qt::KeyboardModifiers m_modifier;
};
/**
* \class ViewState is a small helper struct that represents a certain state
* of the view, including the current item, the selected items in MultiSelection
* mode (in the other modes, the selection is either empty or equal to the
* current item), and the information whether items were activated by the last
* key press.
*/
struct ViewState {
ViewState(int current, const KItemSet &selection, bool activated = false)
: m_current(current)
, m_selection(selection)
, m_activated(activated)
{
}
int m_current;
KItemSet m_selection;
bool m_activated;
};
// We have to define a typedef for the pair in order to make the test compile.
typedef QPair<KeyPress, ViewState> keyPressViewStatePair;
Q_DECLARE_METATYPE(QList<keyPressViewStatePair>)
/**
* This function tests all possible combinations of view layouts, layout direction,
* and enabled/disabled groupings for different column counts, and
* provides a list of key presses and the states that the view should be in
* after the key press event.
*/
void KItemListControllerTest::testKeyboardNavigationMultiSelection_data()
{
QTest::addColumn<KFileItemListView::ItemLayout>("layout");
QTest::addColumn<Qt::Orientation>("scrollOrientation");
QTest::addColumn<int>("columnCount");
QTest::addColumn<bool>("groupingEnabled");
QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
QTest::addColumn<bool>(
"selectionModeEnabled"); // Don't confuse this with "selectionBehaviour". This is about changing controls for users to help multi-selecting.
QTest::addColumn<QList<QPair<KeyPress, ViewState>>>("testList");
QList<KFileItemListView::ItemLayout> layoutList;
QHash<KFileItemListView::ItemLayout, QString> layoutNames;
layoutList.append(KFileItemListView::IconsLayout);
layoutNames[KFileItemListView::IconsLayout] = "Icons";
layoutList.append(KFileItemListView::CompactLayout);
layoutNames[KFileItemListView::CompactLayout] = "Compact";
layoutList.append(KFileItemListView::DetailsLayout);
layoutNames[KFileItemListView::DetailsLayout] = "Details";
QList<bool> groupingEnabledList;
QHash<bool, QString> groupingEnabledNames;
groupingEnabledList.append(false);
groupingEnabledNames[false] = "ungrouped";
groupingEnabledList.append(true);
groupingEnabledNames[true] = "grouping enabled";
QList<Qt::LayoutDirection> layoutDirectionList;
QHash<Qt::LayoutDirection, QString> layoutDirectionNames;
layoutDirectionList.append(Qt::LeftToRight);
layoutDirectionNames[Qt::LeftToRight] = "Left-to-Right LayoutDirection";
layoutDirectionList.append(Qt::RightToLeft);
layoutDirectionNames[Qt::RightToLeft] = "Right-to-Left LayoutDirection";
bool selectionModeEnabled = false; // For most tests this is kept disabled because it is not really affected by all the other test conditions.
// We only enable it for a few separate tests at the end.
for (const KFileItemListView::ItemLayout &layout : layoutList) {
// The following settings depend on the layout.
// Note that 'columns' are actually 'rows' in
// Compact layout.
Qt::Orientation scrollOrientation;
QList<int> columnCountList;
Qt::Key nextItemKey = Qt::Key_Right;
Qt::Key previousItemKey = Qt::Key_Right;
Qt::Key nextRowKey = Qt::Key_Right;
Qt::Key previousRowKey = Qt::Key_Right;
switch (layout) {
case KFileItemListView::IconsLayout:
scrollOrientation = Qt::Vertical;
columnCountList << 1 << 3 << 5;
nextItemKey = Qt::Key_Right;
previousItemKey = Qt::Key_Left;
nextRowKey = Qt::Key_Down;
previousRowKey = Qt::Key_Up;
break;
case KFileItemListView::CompactLayout:
scrollOrientation = Qt::Horizontal;
columnCountList << 1 << 3 << 5;
nextItemKey = Qt::Key_Down;
previousItemKey = Qt::Key_Up;
nextRowKey = Qt::Key_Right;
previousRowKey = Qt::Key_Left;
break;
case KFileItemListView::DetailsLayout:
scrollOrientation = Qt::Vertical;
columnCountList << 1;
nextItemKey = Qt::Key_Down;
previousItemKey = Qt::Key_Up;
nextRowKey = Qt::Key_Down;
previousRowKey = Qt::Key_Up;
break;
}
for (auto layoutDirection : std::as_const(layoutDirectionList)) {
if (layoutDirection == Qt::RightToLeft) {
switch (layout) {
case KFileItemListView::IconsLayout:
std::swap(nextItemKey, previousItemKey);
break;
case KFileItemListView::CompactLayout:
std::swap(nextRowKey, previousRowKey);
break;
default:
break;
}
}
for (int columnCount : std::as_const(columnCountList)) {
for (bool groupingEnabled : std::as_const(groupingEnabledList)) {
QList<QPair<KeyPress, ViewState>> testList;
// First, key presses which should have the same effect
// for any layout and any number of columns.
testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
<< qMakePair(KeyPress(Qt::Key_Return), ViewState(1, KItemSet() << 1, true))
<< qMakePair(KeyPress(Qt::Key_Enter), ViewState(1, KItemSet() << 1, true))
<< qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet() << 2))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
<< qMakePair(KeyPress(Qt::Key_Return), ViewState(3, KItemSet() << 2 << 3, true))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 2))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 2 << 3))
<< qMakePair(KeyPress(Qt::Key_Return), ViewState(4, KItemSet() << 2 << 3, true))
<< qMakePair(KeyPress(previousItemKey), ViewState(3, KItemSet() << 3))
<< qMakePair(KeyPress(Qt::Key_Home, Qt::ShiftModifier), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
<< qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 2 << 3))
<< qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
<< qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 19))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(18, KItemSet() << 18 << 19))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0))
<< qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet()))
<< qMakePair(KeyPress(Qt::Key_Enter), ViewState(0, KItemSet(), true))
<< qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet() << 0))
<< qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet()))
<< qMakePair(KeyPress(Qt::Key_Space), ViewState(0, KItemSet() << 0))
<< qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
<< qMakePair(KeyPress(Qt::Key_Space), ViewState(14, KItemSet() << 14))
<< qMakePair(KeyPress(Qt::Key_3), ViewState(15, KItemSet() << 15))
<< qMakePair(KeyPress(Qt::Key_Escape), ViewState(15, KItemSet()))
<< qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
<< qMakePair(KeyPress(Qt::Key_E), ViewState(14, KItemSet() << 14))
<< qMakePair(KeyPress(previousItemKey), ViewState(13, KItemSet() << 13))
<< qMakePair(KeyPress(Qt::Key_E), ViewState(14, KItemSet() << 14))
<< qMakePair(KeyPress(Qt::Key_Escape), ViewState(14, KItemSet()))
<< qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0))
<< qMakePair(KeyPress(Qt::Key_Escape), ViewState(0, KItemSet()));
// Next, we test combinations of key presses which only work for a
// particular number of columns and either enabled or disabled grouping.
// One column.
if (columnCount == 1) {
testList << qMakePair(KeyPress(nextRowKey), ViewState(1, KItemSet() << 1))
<< qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 1 << 2))
<< qMakePair(KeyPress(nextRowKey, Qt::ControlModifier), ViewState(3, KItemSet() << 1 << 2))
<< qMakePair(KeyPress(previousRowKey), ViewState(2, KItemSet() << 2))
<< qMakePair(KeyPress(previousItemKey), ViewState(1, KItemSet() << 1))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
}
// Multiple columns: we test both 3 and 5 columns with grouping
// enabled or disabled. For each case, the layout of the items
// in the view is shown (both using file names and indices) to
// make it easier to understand what the tests do.
if (columnCount == 3 && !groupingEnabled) {
// 3 columns, no grouping:
//
// a1 a2 a3 | 0 1 2
// b1 c1 c2 | 3 4 5
// c3 c4 c5 | 6 7 8
// d1 d2 d3 | 9 10 11
// d4 e1 e2 | 12 13 14
// e3 e4 e5 | 15 16 17
// e6 e7 | 18 19
testList << qMakePair(KeyPress(nextRowKey), ViewState(3, KItemSet() << 3))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 3))
<< qMakePair(KeyPress(nextRowKey), ViewState(7, KItemSet() << 7))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 7 << 8))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(9, KItemSet() << 7 << 8 << 9))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 7 << 8))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 6 << 7))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 5 << 6 << 7))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 6 << 7))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7))
<< qMakePair(KeyPress(nextRowKey), ViewState(10, KItemSet() << 10))
<< qMakePair(KeyPress(nextItemKey), ViewState(11, KItemSet() << 11))
<< qMakePair(KeyPress(nextRowKey), ViewState(14, KItemSet() << 14))
<< qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 17))
<< qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
<< qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
<< qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 19))
<< qMakePair(KeyPress(previousRowKey), ViewState(16, KItemSet() << 16))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
}
if (columnCount == 5 && !groupingEnabled) {
// 5 columns, no grouping:
//
// a1 a2 a3 b1 c1 | 0 1 2 3 4
// c2 c3 c4 c5 d1 | 5 6 7 8 9
// d2 d3 d4 e1 e2 | 10 11 12 13 14
// e3 e4 e5 e6 e7 | 15 16 17 18 19
testList << qMakePair(KeyPress(nextRowKey), ViewState(5, KItemSet() << 5))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(6, KItemSet() << 5))
<< qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
<< qMakePair(KeyPress(nextItemKey), ViewState(12, KItemSet() << 12))
<< qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
<< qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
<< qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
<< qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
<< qMakePair(KeyPress(Qt::Key_End, Qt::ControlModifier), ViewState(19, KItemSet() << 12))
<< qMakePair(KeyPress(previousRowKey), ViewState(14, KItemSet() << 14))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
}
if (columnCount == 3 && groupingEnabled) {
// 3 columns, with grouping:
//
// a1 a2 a3 | 0 1 2
// b1 | 3
// c1 c2 c3 | 4 5 6
// c4 c5 | 7 8
// d1 d2 d3 | 9 10 11
// d4 | 12
// e1 e2 e3 | 13 14 15
// e4 e5 e6 | 16 17 18
// e7 | 19
testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
<< qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet() << 2))
<< qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
<< qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
<< qMakePair(KeyPress(nextRowKey), ViewState(8, KItemSet() << 8))
<< qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(12, KItemSet() << 11))
<< qMakePair(KeyPress(nextRowKey), ViewState(13, KItemSet() << 13))
<< qMakePair(KeyPress(nextRowKey), ViewState(16, KItemSet() << 16))
<< qMakePair(KeyPress(nextItemKey), ViewState(17, KItemSet() << 17))
<< qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
<< qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
}
if (columnCount == 5 && groupingEnabled) {
// 5 columns, with grouping:
//
// a1 a2 a3 | 0 1 2
// b1 | 3
// c1 c2 c3 c4 c5 | 4 5 6 7 8
// d1 d2 d3 d4 | 9 10 11 12
// e1 e2 e3 e4 e5 | 13 14 15 16 17
// e6 e7 | 18 19
testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
<< qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 1 << 2 << 3))
<< qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
<< qMakePair(KeyPress(nextItemKey), ViewState(6, KItemSet() << 6))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(7, KItemSet() << 6))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(8, KItemSet() << 6))
<< qMakePair(KeyPress(nextRowKey), ViewState(12, KItemSet() << 12))
<< qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 17))
<< qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
<< qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
<< qMakePair(KeyPress(Qt::Key_End, Qt::ShiftModifier), ViewState(19, KItemSet() << 17 << 18 << 19))
<< qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
}
const QString testName = layoutNames[layout] + ", " + QStringLiteral("%1 columns, ").arg(columnCount)
+ groupingEnabledNames[groupingEnabled] + ", " + layoutDirectionNames[layoutDirection];
const QByteArray testNameAscii = testName.toLatin1();
QTest::newRow(testNameAscii.data())
<< layout << scrollOrientation << columnCount << groupingEnabled << layoutDirection << selectionModeEnabled << testList;
}
}
}
}
/**
* Selection mode tests
* We only test for the default icon view mode with typical scrollOrientation, selectionBehaviour, no grouping, left-to-right layoutDirection because none
* of this should affect selection mode and special-casing selection mode within the above test would make the above code even more complex than it already
* is.
*/
selectionModeEnabled = true;
const KFileItemListView::ItemLayout layout = KFileItemListView::IconsLayout;
const Qt::Orientation scrollOrientation = Qt::Vertical;
const int columnCount = 3;
const Qt::Key nextItemKey = Qt::Key_Right;
const Qt::Key previousItemKey = Qt::Key_Left;
const Qt::Key nextRowKey = Qt::Key_Down;
const Qt::Key previousRowKey = Qt::Key_Up;
const Qt::LayoutDirection layoutDirection = Qt::LeftToRight;
const bool groupingEnabled = false;
QList<QPair<KeyPress, ViewState>> testList;
testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet())) // In selection mode nothing is selected simply by moving with arrow keys.
<< qMakePair(KeyPress(Qt::Key_Return), ViewState(1, KItemSet() << 1)) // Pressing Return toggles the selection but does not activate.
<< qMakePair(KeyPress(Qt::Key_Enter), ViewState(1, KItemSet())) // Pressing Enter toggles the selection but does not activate.
<< qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet()))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3)) // Shift+Arrow key still selects in selection mode.
<< qMakePair(KeyPress(Qt::Key_Return), ViewState(3, KItemSet() << 2))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 2 << 3))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2)) // Shift+Left and then Shift+Right cancel each other out.
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 2))
<< qMakePair(KeyPress(Qt::Key_Return), ViewState(4, KItemSet() << 2 << 4))
<< qMakePair(KeyPress(previousItemKey), ViewState(3, KItemSet() << 2 << 4))
<< qMakePair(KeyPress(Qt::Key_Home, Qt::ShiftModifier), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3 << 4))
<< qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 2 << 3 << 4))
<< qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3 << 4))
<< qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 0 << 1 << 2 << 3 << 4))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(18, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
<< qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19))
<< qMakePair(KeyPress(Qt::Key_Enter), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
<< qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19))
<< qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
<< qMakePair(KeyPress(Qt::Key_Space), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19)) // Space toggles selection in selection mode.
<< qMakePair(KeyPress(Qt::Key_D), ViewState(9, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19)) // No selection change by type-ahead.
<< qMakePair(KeyPress(Qt::Key_Space), ViewState(9, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19)) // Space is not added to type-ahead.
<< qMakePair(KeyPress(Qt::Key_4), ViewState(12, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19)) // No selection change by type-ahead.
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
// The following tests assume a columnCount of three and no grouping enabled.
<< qMakePair(KeyPress(nextRowKey), ViewState(3, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
<< qMakePair(KeyPress(nextRowKey), ViewState(7, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 1 << 2 << 3 << 4 << 7 << 8 << 9 << 18 << 19))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(9, KItemSet() << 1 << 2 << 3 << 4 << 7 << 8 << 9 << 18 << 19))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 1 << 2 << 3 << 4 << 7 << 8 << 9 << 18 << 19))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 1 << 2 << 3 << 4 << 6 << 7 << 9 << 18 << 19))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 9 << 18 << 19))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 1 << 2 << 3 << 4 << 6 << 7 << 9 << 18 << 19))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
<< qMakePair(KeyPress(nextRowKey), ViewState(10, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
<< qMakePair(KeyPress(nextItemKey), ViewState(11, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
<< qMakePair(KeyPress(nextRowKey), ViewState(14, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
<< qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
<< qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
<< qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
<< qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
<< qMakePair(KeyPress(previousRowKey), ViewState(16, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19));
const QString testName = "Selection Mode: " + layoutNames[layout] + ", " + QStringLiteral("%1 columns, ").arg(columnCount)
+ groupingEnabledNames[groupingEnabled] + ", " + layoutDirectionNames[layoutDirection];
const QByteArray testNameAscii = testName.toLatin1();
QTest::newRow(testNameAscii.data()) << layout << scrollOrientation << columnCount << groupingEnabled << layoutDirection << selectionModeEnabled << testList;
}
/**
* This function sets the view's properties according to the data provided.
*
* The list \a testList contains pairs of key presses, which are sent to the
* container, and expected view states, which are verified then.
*/
void KItemListControllerTest::testKeyboardNavigationMultiSelection()
{
QFETCH(KFileItemListView::ItemLayout, layout);
QFETCH(Qt::Orientation, scrollOrientation);
QFETCH(int, columnCount);
QFETCH(bool, groupingEnabled);
QFETCH(Qt::LayoutDirection, layoutDirection);
QFETCH(bool, selectionModeEnabled);
QFETCH(QList<keyPressViewStatePair>, testList);
QApplication::setLayoutDirection(layoutDirection);
m_view->setLayoutDirection(layoutDirection);
m_view->setItemLayout(layout);
QCOMPARE(m_view->itemLayout(), layout);
m_view->setScrollOrientation(scrollOrientation);
QCOMPARE(m_view->scrollOrientation(), scrollOrientation);
m_controller->setSelectionBehavior(KItemListController::MultiSelection);
QCOMPARE(m_controller->selectionBehavior(), KItemListController::MultiSelection);
m_model->setGroupedSorting(groupingEnabled);
QCOMPARE(m_model->groupedSorting(), groupingEnabled);
m_controller->setSelectionModeEnabled(selectionModeEnabled);
QCOMPARE(m_controller->selectionMode(), selectionModeEnabled);
adjustGeometryForColumnCount(columnCount);
QCOMPARE(m_view->m_layouter->m_columnCount, columnCount);
QSignalSpy spySingleItemActivated(m_controller, &KItemListController::itemActivated);
QSignalSpy spyMultipleItemsActivated(m_controller, &KItemListController::itemsActivated);
int rowCount = 0;
while (!testList.isEmpty()) {
++rowCount;
const QPair<KeyPress, ViewState> test = testList.takeFirst();
const Qt::Key key = test.first.m_key;
const Qt::KeyboardModifiers modifier = test.first.m_modifier;
const int current = test.second.m_current;
const KItemSet selection = test.second.m_selection;
const bool activated = test.second.m_activated;
QTest::keyClick(m_container, key, modifier);
QVERIFY2(
m_selectionManager->currentItem() == current,
qPrintable(QStringLiteral("currentItem() returns index %1 but %2 would be expected. Before this, key \"%3\" was pressed. This test case is defined "
"in row %4 of the testList from KItemListControllerTest::testKeyboardNavigationMultiSelection_data().")
.arg(m_selectionManager->currentItem())
.arg(current)
.arg(QKeySequence(key).toString())
.arg(rowCount)));
QCOMPARE(m_selectionManager->selectedItems(), selection);
if (activated) {
if (!selection.isEmpty()) {
// The selected items should be activated.
if (selection.count() == 1) {
QVERIFY(!spySingleItemActivated.isEmpty());
QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), selection.first());
QVERIFY(spyMultipleItemsActivated.isEmpty());
} else {
QVERIFY(spySingleItemActivated.isEmpty());
QVERIFY(!spyMultipleItemsActivated.isEmpty());
QCOMPARE(qvariant_cast<KItemSet>(spyMultipleItemsActivated.takeFirst().at(0)), selection);
}
} else {
QVERIFY(!spySingleItemActivated.isEmpty());
QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), current);
QVERIFY(spyMultipleItemsActivated.isEmpty());
}
}
}
}
/**
* This function tests all possible combinations of view layouts, layout direction,
* and enabled/disabled groupings for different column counts, and
* provides a list of key presses and the states that the view should be in
* after the key press event.
*/
void KItemListControllerTest::testKeyboardNavigationSingleSelectionNoSelection_data()
{
QTest::addColumn<KFileItemListView::ItemLayout>("layout");
QTest::addColumn<Qt::Orientation>("scrollOrientation");
QTest::addColumn<int>("columnCount");
QTest::addColumn<KItemListController::SelectionBehavior>("selectionBehavior"); // Defines how many items can be selected at the same time.
QTest::addColumn<bool>("groupingEnabled");
QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
QTest::addColumn<bool>(
"selectionModeEnabled"); // Don't confuse this with "selectionBehaviour". This is about changing controls for users to help multi-selecting.
QTest::addColumn<QList<QPair<KeyPress, ViewState>>>("testList");
QList<KFileItemListView::ItemLayout> layoutList;
QHash<KFileItemListView::ItemLayout, QString> layoutNames;
layoutList.append(KFileItemListView::IconsLayout);
layoutNames[KFileItemListView::IconsLayout] = "Icons";
layoutList.append(KFileItemListView::CompactLayout);
layoutNames[KFileItemListView::CompactLayout] = "Compact";
layoutList.append(KFileItemListView::DetailsLayout);
layoutNames[KFileItemListView::DetailsLayout] = "Details";
QList<KItemListController::SelectionBehavior> selectionBehaviorList;
QHash<KItemListController::SelectionBehavior, QString> selectionBehaviorNames;
selectionBehaviorList.append(KItemListController::NoSelection);
selectionBehaviorNames[KItemListController::NoSelection] = "NoSelection";
selectionBehaviorList.append(KItemListController::SingleSelection);
selectionBehaviorNames[KItemListController::SingleSelection] = "SingleSelection";
QList<bool> groupingEnabledList;
QHash<bool, QString> groupingEnabledNames;
groupingEnabledList.append(false);
groupingEnabledNames[false] = "ungrouped";
groupingEnabledList.append(true);
groupingEnabledNames[true] = "grouping enabled";
QList<Qt::LayoutDirection> layoutDirectionList;
QHash<Qt::LayoutDirection, QString> layoutDirectionNames;
layoutDirectionList.append(Qt::LeftToRight);
layoutDirectionNames[Qt::LeftToRight] = "Left-to-Right LayoutDirection";
layoutDirectionList.append(Qt::RightToLeft);
layoutDirectionNames[Qt::RightToLeft] = "Right-to-Left LayoutDirection";
bool selectionModeEnabled = false; // For most tests this is kept disabled because it is not really affected by all the other test conditions.
// We only enable it for a few separate tests at the end.
for (const KFileItemListView::ItemLayout &layout : layoutList) {
// The following settings depend on the layout.
// Note that 'columns' are actually 'rows' in
// Compact layout.
Qt::Orientation scrollOrientation;
QList<int> columnCountList;
Qt::Key nextItemKey = Qt::Key_Right;
Qt::Key previousItemKey = Qt::Key_Right;
Qt::Key nextRowKey = Qt::Key_Right;
Qt::Key previousRowKey = Qt::Key_Right;
switch (layout) {
case KFileItemListView::IconsLayout:
scrollOrientation = Qt::Vertical;
columnCountList << 1 << 3 << 5;
nextItemKey = Qt::Key_Right;
previousItemKey = Qt::Key_Left;
nextRowKey = Qt::Key_Down;
previousRowKey = Qt::Key_Up;
break;
case KFileItemListView::CompactLayout:
scrollOrientation = Qt::Horizontal;
columnCountList << 1 << 3 << 5;
nextItemKey = Qt::Key_Down;
previousItemKey = Qt::Key_Up;
nextRowKey = Qt::Key_Right;
previousRowKey = Qt::Key_Left;
break;
case KFileItemListView::DetailsLayout:
scrollOrientation = Qt::Vertical;
columnCountList << 1;
nextItemKey = Qt::Key_Down;
previousItemKey = Qt::Key_Up;
nextRowKey = Qt::Key_Down;
previousRowKey = Qt::Key_Up;
break;
}
for (auto layoutDirection : std::as_const(layoutDirectionList)) {
if (layoutDirection == Qt::RightToLeft) {
switch (layout) {
case KFileItemListView::IconsLayout:
std::swap(nextItemKey, previousItemKey);
break;
case KFileItemListView::CompactLayout:
std::swap(nextRowKey, previousRowKey);
break;
default:
break;
}
}
for (int columnCount : std::as_const(columnCountList)) {
for (const KItemListController::SelectionBehavior &selectionBehavior : std::as_const(selectionBehaviorList)) {
for (bool groupingEnabled : std::as_const(groupingEnabledList)) {
QList<QPair<KeyPress, ViewState>> testList;
// First, key presses which should have the same effect
// for any layout and any number of columns.
testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
<< qMakePair(KeyPress(Qt::Key_Return), ViewState(1, KItemSet() << 1, true))
<< qMakePair(KeyPress(Qt::Key_Enter), ViewState(1, KItemSet() << 1, true))
<< qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet() << 2))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
<< qMakePair(KeyPress(Qt::Key_Return), ViewState(3, KItemSet() << 2 << 3, true))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 2))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 2 << 3))
<< qMakePair(KeyPress(Qt::Key_Return), ViewState(4, KItemSet() << 2 << 3, true))
<< qMakePair(KeyPress(previousItemKey), ViewState(3, KItemSet() << 3))
<< qMakePair(KeyPress(Qt::Key_Home, Qt::ShiftModifier), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
<< qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 2 << 3))
<< qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
<< qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 19))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(18, KItemSet() << 18 << 19))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0))
<< qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet()))
<< qMakePair(KeyPress(Qt::Key_Enter), ViewState(0, KItemSet(), true))
<< qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet() << 0))
<< qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet()))
<< qMakePair(KeyPress(Qt::Key_Space), ViewState(0, KItemSet() << 0))
<< qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
<< qMakePair(KeyPress(Qt::Key_Space), ViewState(14, KItemSet() << 14))
<< qMakePair(KeyPress(Qt::Key_3), ViewState(15, KItemSet() << 15))
<< qMakePair(KeyPress(Qt::Key_Escape), ViewState(15, KItemSet()))
<< qMakePair(KeyPress(Qt::Key_E), ViewState(16, KItemSet() << 16))
<< qMakePair(KeyPress(Qt::Key_E), ViewState(17, KItemSet() << 17))
<< qMakePair(KeyPress(previousItemKey), ViewState(16, KItemSet() << 16))
<< qMakePair(KeyPress(Qt::Key_E), ViewState(17, KItemSet() << 17))
<< qMakePair(KeyPress(Qt::Key_Escape), ViewState(17, KItemSet()))
<< qMakePair(KeyPress(Qt::Key_E), ViewState(18, KItemSet() << 18))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0))
<< qMakePair(KeyPress(Qt::Key_Escape), ViewState(0, KItemSet()));
// Next, we test combinations of key presses which only work for a
// particular number of columns and either enabled or disabled grouping.
// One column.
if (columnCount == 1) {
testList << qMakePair(KeyPress(nextRowKey), ViewState(1, KItemSet() << 1))
<< qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 1 << 2))
<< qMakePair(KeyPress(nextRowKey, Qt::ControlModifier), ViewState(3, KItemSet() << 1 << 2))
<< qMakePair(KeyPress(previousRowKey), ViewState(2, KItemSet() << 2))
<< qMakePair(KeyPress(previousItemKey), ViewState(1, KItemSet() << 1))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
}
// Multiple columns: we test both 3 and 5 columns with grouping
// enabled or disabled. For each case, the layout of the items
// in the view is shown (both using file names and indices) to
// make it easier to understand what the tests do.
if (columnCount == 3 && !groupingEnabled) {
// 3 columns, no grouping:
//
// a1 a2 a3 | 0 1 2
// b1 c1 c2 | 3 4 5
// c3 c4 c5 | 6 7 8
// d1 d2 d3 | 9 10 11
// d4 e1 e2 | 12 13 14
// e3 e4 e5 | 15 16 17
// e6 e7 | 18 19
testList << qMakePair(KeyPress(nextRowKey), ViewState(3, KItemSet() << 3))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 3))
<< qMakePair(KeyPress(nextRowKey), ViewState(7, KItemSet() << 7))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 7 << 8))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(9, KItemSet() << 7 << 8 << 9))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 7 << 8))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 6 << 7))
<< qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 5 << 6 << 7))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 6 << 7))
<< qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7))
<< qMakePair(KeyPress(nextRowKey), ViewState(10, KItemSet() << 10))
<< qMakePair(KeyPress(nextItemKey), ViewState(11, KItemSet() << 11))
<< qMakePair(KeyPress(nextRowKey), ViewState(14, KItemSet() << 14))
<< qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 17))
<< qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
<< qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
<< qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 19))
<< qMakePair(KeyPress(previousRowKey), ViewState(16, KItemSet() << 16))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
}
if (columnCount == 5 && !groupingEnabled) {
// 5 columns, no grouping:
//
// a1 a2 a3 b1 c1 | 0 1 2 3 4
// c2 c3 c4 c5 d1 | 5 6 7 8 9
// d2 d3 d4 e1 e2 | 10 11 12 13 14
// e3 e4 e5 e6 e7 | 15 16 17 18 19
testList << qMakePair(KeyPress(nextRowKey), ViewState(5, KItemSet() << 5))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(6, KItemSet() << 5))
<< qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
<< qMakePair(KeyPress(nextItemKey), ViewState(12, KItemSet() << 12))
<< qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
<< qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
<< qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
<< qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
<< qMakePair(KeyPress(Qt::Key_End, Qt::ControlModifier), ViewState(19, KItemSet() << 12))
<< qMakePair(KeyPress(previousRowKey), ViewState(14, KItemSet() << 14))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
}
if (columnCount == 3 && groupingEnabled) {
// 3 columns, with grouping:
//
// a1 a2 a3 | 0 1 2
// b1 | 3
// c1 c2 c3 | 4 5 6
// c4 c5 | 7 8
// d1 d2 d3 | 9 10 11
// d4 | 12
// e1 e2 e3 | 13 14 15
// e4 e5 e6 | 16 17 18
// e7 | 19
testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
<< qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet() << 2))
<< qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
<< qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
<< qMakePair(KeyPress(nextRowKey), ViewState(8, KItemSet() << 8))
<< qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(12, KItemSet() << 11))
<< qMakePair(KeyPress(nextRowKey), ViewState(13, KItemSet() << 13))
<< qMakePair(KeyPress(nextRowKey), ViewState(16, KItemSet() << 16))
<< qMakePair(KeyPress(nextItemKey), ViewState(17, KItemSet() << 17))
<< qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
<< qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
}
if (columnCount == 5 && groupingEnabled) {
// 5 columns, with grouping:
//
// a1 a2 a3 | 0 1 2
// b1 | 3
// c1 c2 c3 c4 c5 | 4 5 6 7 8
// d1 d2 d3 d4 | 9 10 11 12
// e1 e2 e3 e4 e5 | 13 14 15 16 17
// e6 e7 | 18 19
testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
<< qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 1 << 2 << 3))
<< qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
<< qMakePair(KeyPress(nextItemKey), ViewState(6, KItemSet() << 6))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(7, KItemSet() << 6))
<< qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(8, KItemSet() << 6))
<< qMakePair(KeyPress(nextRowKey), ViewState(12, KItemSet() << 12))
<< qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 17))
<< qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
<< qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
<< qMakePair(KeyPress(Qt::Key_End, Qt::ShiftModifier), ViewState(19, KItemSet() << 17 << 18 << 19))
<< qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
<< qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
}
const QString testName = layoutNames[layout] + ", " + QStringLiteral("%1 columns, ").arg(columnCount)
+ selectionBehaviorNames[selectionBehavior] + ", " + groupingEnabledNames[groupingEnabled] + ", "
+ layoutDirectionNames[layoutDirection];
const QByteArray testNameAscii = testName.toLatin1();
QTest::newRow(testNameAscii.data()) << layout << scrollOrientation << columnCount << selectionBehavior << groupingEnabled
<< layoutDirection << selectionModeEnabled << testList;
}
}
}
}
}
}
/**
* This function sets the view's properties according to the data provided.
*
* The list \a testList contains pairs of key presses, which are sent to the
* container, and expected view states, which are verified then.
*/
void KItemListControllerTest::testKeyboardNavigationSingleSelectionNoSelection()
{
QFETCH(KFileItemListView::ItemLayout, layout);
QFETCH(Qt::Orientation, scrollOrientation);
QFETCH(int, columnCount);
QFETCH(KItemListController::SelectionBehavior, selectionBehavior);
QFETCH(bool, groupingEnabled);
QFETCH(Qt::LayoutDirection, layoutDirection);
QFETCH(bool, selectionModeEnabled);
QFETCH(QList<keyPressViewStatePair>, testList);
QApplication::setLayoutDirection(layoutDirection);
m_view->setLayoutDirection(layoutDirection);
m_view->setItemLayout(layout);
QCOMPARE(m_view->itemLayout(), layout);
m_view->setScrollOrientation(scrollOrientation);
QCOMPARE(m_view->scrollOrientation(), scrollOrientation);
m_controller->setSelectionBehavior(selectionBehavior);
QCOMPARE(m_controller->selectionBehavior(), selectionBehavior);
m_model->setGroupedSorting(groupingEnabled);
QCOMPARE(m_model->groupedSorting(), groupingEnabled);
m_controller->setSelectionModeEnabled(selectionModeEnabled);
QCOMPARE(m_controller->selectionMode(), selectionModeEnabled);
adjustGeometryForColumnCount(columnCount);
QCOMPARE(m_view->m_layouter->m_columnCount, columnCount);
QSignalSpy spySingleItemActivated(m_controller, &KItemListController::itemActivated);
QSignalSpy spyMultipleItemsActivated(m_controller, &KItemListController::itemsActivated);
int rowCount = 0;
while (!testList.isEmpty()) {
++rowCount;
const QPair<KeyPress, ViewState> test = testList.takeFirst();
const Qt::Key key = test.first.m_key;
const Qt::KeyboardModifiers modifier = test.first.m_modifier;
const int current = test.second.m_current;
const KItemSet selection = test.second.m_selection;
const bool activated = test.second.m_activated;
QTest::keyClick(m_container, key, modifier);
QVERIFY2(
m_selectionManager->currentItem() == current,
qPrintable(QStringLiteral("currentItem() returns index %1 but %2 would be expected. Before this, key \"%3\" was pressed. This test case is defined "
"in row %4 of the testList from KItemListControllerTest::testKeyboardNavigationSingleSelectionNoSelection_data().")
.arg(m_selectionManager->currentItem())
.arg(current)
.arg(QKeySequence(key).toString())
.arg(rowCount)));
switch (selectionBehavior) {
case KItemListController::NoSelection:
QVERIFY(m_selectionManager->selectedItems().isEmpty());
break;
case KItemListController::SingleSelection:
QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << current);
break;
default:
Q_UNREACHABLE();
}
if (activated) {
switch (selectionBehavior) {
case KItemListController::NoSelection:
case KItemListController::SingleSelection:
// In NoSelection and SingleSelection mode, the current item should be activated.
QVERIFY(!spySingleItemActivated.isEmpty());
QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), current);
QVERIFY(spyMultipleItemsActivated.isEmpty());
break;
default:
Q_UNREACHABLE();
}
}
}
}
void KItemListControllerTest::testMouseClickActivation()
{
m_view->setItemLayout(KFileItemListView::IconsLayout);
// Make sure that we have a large window, such that
// the items are visible and clickable.
adjustGeometryForColumnCount(5);
// Make sure that the first item is visible in the view.
m_view->setScrollOffset(0);
QCOMPARE(m_view->firstVisibleIndex(), 0);
const QPointF pos = m_view->itemContextRect(0).center();
// Save the "single click" setting.
const bool restoreSettingsSingleClick = m_testStyle->activateItemOnSingleClick();
QGraphicsSceneMouseEvent mousePressEvent(QEvent::GraphicsSceneMousePress);
mousePressEvent.setPos(pos);
mousePressEvent.setButton(Qt::LeftButton);
mousePressEvent.setButtons(Qt::LeftButton);
QGraphicsSceneMouseEvent mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease);
mouseReleaseEvent.setPos(pos);
mouseReleaseEvent.setButton(Qt::LeftButton);
mouseReleaseEvent.setButtons(Qt::NoButton);
QGraphicsSceneMouseEvent mouseDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick);
mouseDoubleClickEvent.setPos(pos);
mouseDoubleClickEvent.setButton(Qt::LeftButton);
mouseDoubleClickEvent.setButtons(Qt::LeftButton);
QGraphicsSceneMouseEvent mouseRightPressEvent(QEvent::GraphicsSceneMousePress);
mouseRightPressEvent.setPos(pos);
mouseRightPressEvent.setButton(Qt::RightButton);
mouseRightPressEvent.setButtons(Qt::RightButton);
QGraphicsSceneMouseEvent mouseRightReleaseEvent(QEvent::GraphicsSceneMouseRelease);
mouseRightReleaseEvent.setPos(pos);
mouseRightReleaseEvent.setButton(Qt::RightButton);
mouseRightReleaseEvent.setButtons(Qt::NoButton);
QGraphicsSceneMouseEvent mouseRightDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick);
mouseRightDoubleClickEvent.setPos(pos);
mouseRightDoubleClickEvent.setButton(Qt::RightButton);
mouseRightDoubleClickEvent.setButtons(Qt::RightButton);
QGraphicsSceneMouseEvent mouseBackPressEvent(QEvent::GraphicsSceneMousePress);
mouseBackPressEvent.setPos(pos);
mouseBackPressEvent.setButton(Qt::BackButton);
mouseBackPressEvent.setButtons(Qt::BackButton);
QGraphicsSceneMouseEvent mouseBackReleaseEvent(QEvent::GraphicsSceneMouseRelease);
mouseBackReleaseEvent.setPos(pos);
mouseBackReleaseEvent.setButton(Qt::BackButton);
mouseBackReleaseEvent.setButtons(Qt::NoButton);
QGraphicsSceneMouseEvent mouseBackDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick);
mouseBackDoubleClickEvent.setPos(pos);
mouseBackDoubleClickEvent.setButton(Qt::BackButton);
mouseBackDoubleClickEvent.setButtons(Qt::BackButton);
QSignalSpy spyItemActivated(m_controller, &KItemListController::itemActivated);
// Default setting: single click activation.
m_testStyle->setActivateItemOnSingleClick(true);
m_view->event(&mousePressEvent);
m_view->event(&mouseReleaseEvent);
QCOMPARE(spyItemActivated.count(), 1);
spyItemActivated.clear();
// Set the global setting to "double click activation".
m_testStyle->setActivateItemOnSingleClick(false);
m_view->event(&mousePressEvent);
m_view->event(&mouseReleaseEvent);
QCOMPARE(spyItemActivated.count(), 0);
spyItemActivated.clear();
// emulation of double click according to https://doc.qt.io/qt-6/qgraphicsscene.html#mouseDoubleClickEvent
m_view->event(&mousePressEvent);
m_view->event(&mouseReleaseEvent);
m_view->event(&mouseDoubleClickEvent);
m_view->event(&mouseReleaseEvent);
QCOMPARE(spyItemActivated.count(), 1);
spyItemActivated.clear();
// right mouse button should not trigger activation
m_view->event(&mouseRightPressEvent);
m_view->event(&mouseRightReleaseEvent);
m_view->event(&mouseRightDoubleClickEvent);
m_view->event(&mouseRightReleaseEvent);
QCOMPARE(spyItemActivated.count(), 0);
// back mouse button should not trigger activation
m_view->event(&mouseBackPressEvent);
m_view->event(&mouseBackReleaseEvent);
m_view->event(&mouseBackDoubleClickEvent);
m_view->event(&mouseBackReleaseEvent);
QCOMPARE(spyItemActivated.count(), 0);
// Enforce single click activation in the controller.
m_controller->setSingleClickActivationEnforced(true);
m_view->event(&mousePressEvent);
m_view->event(&mouseReleaseEvent);
QCOMPARE(spyItemActivated.count(), 1);
spyItemActivated.clear();
// Do not enforce single click activation in the controller.
m_controller->setSingleClickActivationEnforced(false);
m_view->event(&mousePressEvent);
m_view->event(&mouseReleaseEvent);
QCOMPARE(spyItemActivated.count(), 0);
spyItemActivated.clear();
// Set the global setting back to "single click activation".
m_testStyle->setActivateItemOnSingleClick(true);
m_view->event(&mousePressEvent);
m_view->event(&mouseReleaseEvent);
QCOMPARE(spyItemActivated.count(), 1);
spyItemActivated.clear();
// Enforce single click activation in the controller.
m_controller->setSingleClickActivationEnforced(true);
m_view->event(&mousePressEvent);
m_view->event(&mouseReleaseEvent);
QCOMPARE(spyItemActivated.count(), 1);
spyItemActivated.clear();
// Restore previous settings.
m_controller->setSingleClickActivationEnforced(true);
m_testStyle->setActivateItemOnSingleClick(restoreSettingsSingleClick);
}
/**
* This function simulates a mouse click on an item under a given index.
*/
void KItemListControllerTest::simulateMouseClickOnItem(int index)
{
const QPointF pos = m_view->itemContextRect(index).center();
QGraphicsSceneMouseEvent mousePressEvent(QEvent::GraphicsSceneMousePress);
mousePressEvent.setPos(pos);
mousePressEvent.setButton(Qt::LeftButton);
mousePressEvent.setButtons(Qt::LeftButton);
QGraphicsSceneMouseEvent mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease);
mouseReleaseEvent.setPos(pos);
mouseReleaseEvent.setButton(Qt::LeftButton);
mouseReleaseEvent.setButtons(Qt::NoButton);
m_view->event(&mousePressEvent);
m_view->event(&mouseReleaseEvent);
}
void KItemListControllerTest::testKeyboardNavigationAfterMouseSelection()
{
QApplication::setLayoutDirection(Qt::LeftToRight);
m_view->setLayoutDirection(Qt::LeftToRight);
m_view->setItemLayout(KFileItemListView::IconsLayout);
m_model->setGroupedSorting(false);
adjustGeometryForColumnCount(3);
QCOMPARE(m_view->m_layouter->m_columnCount, 3);
m_view->setScrollOffset(0);
QCOMPARE(m_view->firstVisibleIndex(), 0);
simulateMouseClickOnItem(0);
QCOMPARE(m_selectionManager->currentItem(), 0);
QTest::keyClick(m_container, Qt::Key_Down);
QCOMPARE(m_selectionManager->currentItem(), 3);
QTest::keyClick(m_container, Qt::Key_Down);
QCOMPARE(m_selectionManager->currentItem(), 6);
simulateMouseClickOnItem(1);
QCOMPARE(m_selectionManager->currentItem(), 1);
QTest::keyClick(m_container, Qt::Key_Down);
QCOMPARE(m_selectionManager->currentItem(), 4);
QTest::keyClick(m_container, Qt::Key_Down);
QCOMPARE(m_selectionManager->currentItem(), 7);
simulateMouseClickOnItem(2);
QCOMPARE(m_selectionManager->currentItem(), 2);
QTest::keyClick(m_container, Qt::Key_Down);
QCOMPARE(m_selectionManager->currentItem(), 5);
QTest::keyClick(m_container, Qt::Key_Down);
QCOMPARE(m_selectionManager->currentItem(), 8);
simulateMouseClickOnItem(10);
QCOMPARE(m_selectionManager->currentItem(), 10);
QTest::keyClick(m_container, Qt::Key_Up);
QCOMPARE(m_selectionManager->currentItem(), 7);
QTest::keyClick(m_container, Qt::Key_Up);
QCOMPARE(m_selectionManager->currentItem(), 4);
}
void KItemListControllerTest::adjustGeometryForColumnCount(int count)
{
const QSize size = m_view->itemSize().toSize();
QRect rect = m_container->geometry();
rect.setSize(size * count);
m_container->setGeometry(rect);
// Increase the size of the container until the correct column count is reached.
while (m_view->m_layouter->m_columnCount < count) {
rect = m_container->geometry();
rect.setSize(rect.size() + size);
m_container->setGeometry(rect);
}
}
QTEST_MAIN(KItemListControllerTest)
#include "kitemlistcontrollertest.moc"
|