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
|
/*
* SPDX-FileCopyrightText: 2026 Sebastian Englbrecht
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "panels/places/placespanel.h"
#include <KFilePlacesModel>
#include <KLocalizedString>
#include <QApplication>
#include <QContextMenuEvent>
#include <QItemSelectionModel>
#include <QMenu>
#include <QSignalSpy>
#include <QStandardPaths>
#include <QTemporaryDir>
#include <QTest>
#include <QTimer>
// Select the given index and open context menu via keyboard event.
static void triggerContextMenuForIndex(KFilePlacesView &view, const QModelIndex &index)
{
view.selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
QContextMenuEvent event(QContextMenuEvent::Keyboard, QPoint(), QPoint());
QApplication::sendEvent(&view, &event);
}
// Open context menu with no item selected (empty-area behaviour).
static void triggerContextMenuEmptyArea(KFilePlacesView &view)
{
view.selectionModel()->clearCurrentIndex();
view.selectionModel()->clearSelection();
QContextMenuEvent event(QContextMenuEvent::Keyboard, QPoint(), QPoint());
QApplication::sendEvent(&view, &event);
}
// Collect visible action texts from the menu (top-level, non-separator, visible only).
static QStringList visibleMenuActionTexts(QMenu *menu)
{
QStringList texts;
for (QAction *action : menu->actions()) {
if (!action->isSeparator() && action->isVisible()) {
texts << action->text();
}
}
return texts;
}
class PlacesPanelTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void testOpenInSplitViewVisibleForPlace();
void testOpenInSplitViewHiddenForEmptyArea();
void testOpenInSplitViewEmitsSignal();
void testConfigureTrashVisibleForTrash();
void testConfigureTrashHiddenForOtherPlaces();
void testCustomContextMenuActionsInEmptyArea();
void testCustomContextMenuActionsHiddenForItems();
void testCustomContextMenuActionsRoundTrip();
private:
// Find the first model index whose URL matches the given scheme.
QModelIndex indexForScheme(KFilePlacesModel *model, const QString &scheme) const;
QTemporaryDir m_tmpHome;
};
void PlacesPanelTest::initTestCase()
{
QVERIFY(m_tmpHome.isValid());
qputenv("HOME", m_tmpHome.path().toUtf8());
QStandardPaths::setTestModeEnabled(true);
}
void PlacesPanelTest::cleanupTestCase()
{
const QString bookmarks = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + u"/user-places.xbel";
QFile::remove(bookmarks);
}
QModelIndex PlacesPanelTest::indexForScheme(KFilePlacesModel *model, const QString &scheme) const
{
for (int row = 0; row < model->rowCount(); ++row) {
const QModelIndex idx = model->index(row, 0);
if (model->url(idx).scheme() == scheme) {
return idx;
}
}
return {};
}
void PlacesPanelTest::testOpenInSplitViewVisibleForPlace()
{
PlacesPanel panel(nullptr);
auto *model = qobject_cast<KFilePlacesModel *>(panel.model());
QVERIFY(model);
// Home is the first place and always has a valid URL.
const QModelIndex homeIndex = model->index(0, 0);
QVERIFY(homeIndex.isValid());
QVERIFY(model->url(homeIndex).isValid());
QStringList visibleActions;
connect(
&panel,
&KFilePlacesView::contextMenuAboutToShow,
this,
[&visibleActions](const QModelIndex &, QMenu *menu) {
QTimer::singleShot(0, menu, [menu, &visibleActions]() {
visibleActions = visibleMenuActionTexts(menu);
menu->close();
});
},
Qt::SingleShotConnection);
triggerContextMenuForIndex(panel, homeIndex);
QVERIFY(visibleActions.contains(i18nc("@action:inmenu", "Open in Split View")));
}
void PlacesPanelTest::testOpenInSplitViewHiddenForEmptyArea()
{
PlacesPanel panel(nullptr);
QStringList visibleActions;
connect(
&panel,
&KFilePlacesView::contextMenuAboutToShow,
this,
[&visibleActions](const QModelIndex &, QMenu *menu) {
QTimer::singleShot(0, menu, [menu, &visibleActions]() {
visibleActions = visibleMenuActionTexts(menu);
menu->close();
});
},
Qt::SingleShotConnection);
triggerContextMenuEmptyArea(panel);
QVERIFY(!visibleActions.contains(i18nc("@action:inmenu", "Open in Split View")));
}
void PlacesPanelTest::testOpenInSplitViewEmitsSignal()
{
PlacesPanel panel(nullptr);
auto *model = qobject_cast<KFilePlacesModel *>(panel.model());
QVERIFY(model);
const QModelIndex homeIndex = model->index(0, 0);
QVERIFY(homeIndex.isValid());
const QUrl homeUrl = model->url(homeIndex);
QSignalSpy splitSpy(&panel, &PlacesPanel::openInSplitViewRequested);
connect(
&panel,
&KFilePlacesView::contextMenuAboutToShow,
this,
[](const QModelIndex &, QMenu *menu) {
QTimer::singleShot(0, menu, [menu]() {
for (QAction *action : menu->actions()) {
if (action->isVisible() && action->text() == i18nc("@action:inmenu", "Open in Split View")) {
menu->setActiveAction(action);
QTest::keyClick(menu, Qt::Key_Return);
return;
}
}
menu->close();
});
},
Qt::SingleShotConnection);
triggerContextMenuForIndex(panel, homeIndex);
QCOMPARE(splitSpy.count(), 1);
QCOMPARE(splitSpy.first().first().toUrl(), homeUrl);
}
void PlacesPanelTest::testConfigureTrashVisibleForTrash()
{
PlacesPanel panel(nullptr);
auto *model = qobject_cast<KFilePlacesModel *>(panel.model());
QVERIFY(model);
const QModelIndex trashIndex = indexForScheme(model, QStringLiteral("trash"));
if (!trashIndex.isValid()) {
QSKIP("No trash place in model");
}
QStringList visibleActions;
connect(
&panel,
&KFilePlacesView::contextMenuAboutToShow,
this,
[&visibleActions](const QModelIndex &, QMenu *menu) {
QTimer::singleShot(0, menu, [menu, &visibleActions]() {
visibleActions = visibleMenuActionTexts(menu);
menu->close();
});
},
Qt::SingleShotConnection);
triggerContextMenuForIndex(panel, trashIndex);
QVERIFY(visibleActions.contains(i18nc("@action:inmenu", "Configure Trash…")));
}
void PlacesPanelTest::testConfigureTrashHiddenForOtherPlaces()
{
PlacesPanel panel(nullptr);
auto *model = qobject_cast<KFilePlacesModel *>(panel.model());
QVERIFY(model);
const QModelIndex homeIndex = model->index(0, 0);
QVERIFY(homeIndex.isValid());
QCOMPARE(model->url(homeIndex).scheme(), QStringLiteral("file"));
QStringList visibleActions;
connect(
&panel,
&KFilePlacesView::contextMenuAboutToShow,
this,
[&visibleActions](const QModelIndex &, QMenu *menu) {
QTimer::singleShot(0, menu, [menu, &visibleActions]() {
visibleActions = visibleMenuActionTexts(menu);
menu->close();
});
},
Qt::SingleShotConnection);
triggerContextMenuForIndex(panel, homeIndex);
QVERIFY(!visibleActions.contains(i18nc("@action:inmenu", "Configure Trash…")));
}
void PlacesPanelTest::testCustomContextMenuActionsInEmptyArea()
{
PlacesPanel panel(nullptr);
QAction customAction(QStringLiteral("TestCustomAction"));
panel.setCustomContextMenuActions({&customAction});
QStringList allActions;
connect(
&panel,
&KFilePlacesView::contextMenuAboutToShow,
this,
[&allActions](const QModelIndex &, QMenu *menu) {
QTimer::singleShot(0, menu, [menu, &allActions]() {
for (QAction *action : menu->actions()) {
if (!action->isSeparator()) {
allActions << action->text();
}
}
menu->close();
});
},
Qt::SingleShotConnection);
triggerContextMenuEmptyArea(panel);
QVERIFY(allActions.contains(QStringLiteral("TestCustomAction")));
}
void PlacesPanelTest::testCustomContextMenuActionsHiddenForItems()
{
PlacesPanel panel(nullptr);
auto *model = qobject_cast<KFilePlacesModel *>(panel.model());
QVERIFY(model);
QAction customAction(QStringLiteral("TestCustomActionForItem"));
panel.setCustomContextMenuActions({&customAction});
const QModelIndex homeIndex = model->index(0, 0);
QVERIFY(homeIndex.isValid());
QStringList allActions;
connect(
&panel,
&KFilePlacesView::contextMenuAboutToShow,
this,
[&allActions](const QModelIndex &, QMenu *menu) {
QTimer::singleShot(0, menu, [menu, &allActions]() {
for (QAction *action : menu->actions()) {
if (!action->isSeparator()) {
allActions << action->text();
}
}
menu->close();
});
},
Qt::SingleShotConnection);
triggerContextMenuForIndex(panel, homeIndex);
QVERIFY(!allActions.contains(QStringLiteral("TestCustomActionForItem")));
}
void PlacesPanelTest::testCustomContextMenuActionsRoundTrip()
{
PlacesPanel panel(nullptr);
auto *model = qobject_cast<KFilePlacesModel *>(panel.model());
QVERIFY(model);
QAction customAction(QStringLiteral("TestRoundTripAction"));
panel.setCustomContextMenuActions({&customAction});
const QModelIndex homeIndex = model->index(0, 0);
QVERIFY(homeIndex.isValid());
// First: open item menu — custom action must not appear.
QStringList itemMenuActions;
connect(
&panel,
&KFilePlacesView::contextMenuAboutToShow,
this,
[&itemMenuActions](const QModelIndex &, QMenu *menu) {
QTimer::singleShot(0, menu, [menu, &itemMenuActions]() {
for (QAction *action : menu->actions()) {
if (!action->isSeparator()) {
itemMenuActions << action->text();
}
}
menu->close();
});
},
Qt::SingleShotConnection);
triggerContextMenuForIndex(panel, homeIndex);
QVERIFY(!itemMenuActions.contains(QStringLiteral("TestRoundTripAction")));
// Second: open empty-area menu — custom action must be re-added.
QStringList emptyAreaActions;
connect(
&panel,
&KFilePlacesView::contextMenuAboutToShow,
this,
[&emptyAreaActions](const QModelIndex &, QMenu *menu) {
QTimer::singleShot(0, menu, [menu, &emptyAreaActions]() {
for (QAction *action : menu->actions()) {
if (!action->isSeparator()) {
emptyAreaActions << action->text();
}
}
menu->close();
});
},
Qt::SingleShotConnection);
triggerContextMenuEmptyArea(panel);
QVERIFY(emptyAreaActions.contains(QStringLiteral("TestRoundTripAction")));
}
QTEST_MAIN(PlacesPanelTest)
#include "placespaneltest.moc"
|