]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/placesitemmodeltest.cpp
Follow-up of commit 9760f9607d
[dolphin.git] / src / tests / placesitemmodeltest.cpp
1 /***************************************************************************
2 * Copyright (C) 2017 by Renato Araujo Oliveira <renato.araujo@kdab.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include <QTest>
21 #include <QSignalSpy>
22 #include <QStandardPaths>
23 #include <QAction>
24 #include <QDBusInterface>
25
26 #include <KBookmarkManager>
27 #include <KConfig>
28 #include <KConfigGroup>
29 #include <KAboutData>
30 #include <KFilePlacesModel>
31
32 #include "panels/places/placesitemmodel.h"
33 #include "panels/places/placesitem.h"
34 #include "views/viewproperties.h"
35
36 Q_DECLARE_METATYPE(KItemRangeList)
37 Q_DECLARE_METATYPE(KItemRange)
38
39 #ifdef Q_OS_WIN
40 //c:\ as root for windows
41 #define KDE_ROOT_PATH "C:\\"
42 #else
43 #define KDE_ROOT_PATH "/"
44 #endif
45
46 static QString bookmarksFile()
47 {
48 return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/user-places.xbel";
49 }
50
51 class PlacesItemModelTest : public QObject
52 {
53 Q_OBJECT
54
55 private slots:
56 void init();
57 void cleanup();
58
59 void initTestCase();
60 void cleanupTestCase();
61
62 void testModelSort();
63 void testGroups();
64 void testDeletePlace();
65 void testPlaceItem_data();
66 void testPlaceItem();
67 void testTearDownDevice();
68 void testDefaultViewProperties_data();
69 void testDefaultViewProperties();
70 void testClear();
71 void testHideItem();
72 void testSystemItems();
73 void testEditBookmark();
74 void testEditAfterCreation();
75 void testEditMetadata();
76 void testRefresh();
77 void testIcons_data();
78 void testIcons();
79 void testDragAndDrop();
80 void testHideDevices();
81 void testDuplicatedEntries();
82 void renameAfterCreation();
83
84 private:
85 PlacesItemModel* m_model;
86 QSet<int> m_tobeRemoved;
87 QMap<QString, QDBusInterface *> m_interfacesMap;
88 int m_expectedModelCount = 15;
89 bool m_hasDesktopFolder = false;
90 bool m_hasDownloadsFolder = false;
91
92 void setBalooEnabled(bool enabled);
93 int indexOf(const QUrl &url);
94 QDBusInterface *fakeManager();
95 QDBusInterface *fakeDevice(const QString &udi);
96 QStringList placesUrls(PlacesItemModel *model = nullptr) const;
97 QStringList initialUrls() const;
98 void createPlaceItem(const QString &text, const QUrl &url, const QString &icon);
99 void removePlaceAfter(int index);
100 void cancelPlaceRemoval(int index);
101 void removeTestUserData();
102 QMimeData *createMimeData(const QList<int> &indexes) const;
103 };
104
105 #define CHECK_PLACES_URLS(urls) \
106 { \
107 QStringList places = placesUrls(); \
108 if (places != urls) { \
109 qWarning() << "Expected:" << urls; \
110 qWarning() << "Got:" << places; \
111 QCOMPARE(places, urls); \
112 } \
113 }
114
115 void PlacesItemModelTest::setBalooEnabled(bool enabled)
116 {
117 KConfig config(QStringLiteral("baloofilerc"));
118 KConfigGroup basicSettings = config.group("Basic Settings");
119 basicSettings.writeEntry("Indexing-Enabled", enabled);
120 config.sync();
121 }
122
123 int PlacesItemModelTest::indexOf(const QUrl &url)
124 {
125 for (int r = 0; r < m_model->count(); r++) {
126 if (m_model->placesItem(r)->url() == url) {
127 return r;
128 }
129 }
130 return -1;
131 }
132
133 QDBusInterface *PlacesItemModelTest::fakeManager()
134 {
135 return fakeDevice(QStringLiteral("/org/kde/solid/fakehw"));
136 }
137
138 QDBusInterface *PlacesItemModelTest::fakeDevice(const QString &udi)
139 {
140 if (m_interfacesMap.contains(udi)) {
141 return m_interfacesMap[udi];
142 }
143
144 QDBusInterface *iface = new QDBusInterface(QDBusConnection::sessionBus().baseService(), udi);
145 m_interfacesMap[udi] = iface;
146
147 return iface;
148 }
149
150 QStringList PlacesItemModelTest::placesUrls(PlacesItemModel *model) const
151 {
152 QStringList urls;
153 if (!model) {
154 model = m_model;
155 }
156
157 for (int row = 0; row < model->count(); ++row) {
158 urls << model->placesItem(row)->url().toDisplayString(QUrl::PreferLocalFile);
159 }
160 return urls;
161 }
162
163 QStringList PlacesItemModelTest::initialUrls() const
164 {
165 static QStringList urls;
166 if (urls.isEmpty()) {
167 urls << QDir::homePath();
168
169 if (m_hasDesktopFolder) {
170 urls << QDir::homePath() + QStringLiteral("/Desktop");
171 }
172
173 if (m_hasDownloadsFolder) {
174 urls << QDir::homePath() + QStringLiteral("/Downloads");
175 }
176
177 urls << QStringLiteral(KDE_ROOT_PATH) << QStringLiteral("trash:/")
178 << QStringLiteral("remote:/")
179 << QStringLiteral("/media/nfs")
180 << QStringLiteral("timeline:/today") << QStringLiteral("timeline:/yesterday")
181 << QStringLiteral("search:/documents") << QStringLiteral("search:/images") << QStringLiteral("search:/audio") << QStringLiteral("search:/videos")
182 << QStringLiteral("/foreign")
183 << QStringLiteral("/media/floppy0") << QStringLiteral("/media/XO-Y4") << QStringLiteral("/media/cdrom");
184 }
185 return urls;
186 }
187
188 void PlacesItemModelTest::createPlaceItem(const QString &text, const QUrl &url, const QString &icon)
189 {
190 m_model->createPlacesItem(text, url, icon);
191 }
192
193 void PlacesItemModelTest::removePlaceAfter(int index)
194 {
195 m_tobeRemoved.insert(index);
196 }
197
198 void PlacesItemModelTest::cancelPlaceRemoval(int index)
199 {
200 m_tobeRemoved.remove(index);
201 }
202
203 void PlacesItemModelTest::removeTestUserData()
204 {
205 // user hardcoded path to avoid removal of any user personal data
206 QDir dir(QStringLiteral("/home/renato/.qttest/share/placesitemmodeltest"));
207 if (dir.exists()) {
208 QVERIFY(dir.removeRecursively());
209 }
210 }
211
212 QMimeData *PlacesItemModelTest::createMimeData(const QList<int> &indexes) const
213 {
214 QByteArray itemData;
215 QDataStream stream(&itemData, QIODevice::WriteOnly);
216 QList<QUrl> urls;
217
218 for (int index : indexes) {
219 const QUrl itemUrl = m_model->placesItem(index)->url();
220 if (itemUrl.isValid()) {
221 urls << itemUrl;
222 }
223 stream << index;
224 }
225
226 QMimeData* mimeData = new QMimeData();
227 mimeData->setUrls(urls);
228 // copied from PlacesItemModel::internalMimeType()
229 const QString internalMimeType = "application/x-dolphinplacesmodel-" +
230 QString::number((qptrdiff)m_model);
231 mimeData->setData(internalMimeType, itemData);
232 return mimeData;
233 }
234
235 void PlacesItemModelTest::init()
236 {
237 m_model = new PlacesItemModel();
238 // WORKAROUND: need to wait for bookmark to load, check: PlacesItemModel::updateBookmarks
239 QTest::qWait(300);
240 QCOMPARE(m_model->count(), m_expectedModelCount);
241 }
242
243 void PlacesItemModelTest::cleanup()
244 {
245 const auto tobeRemoved = m_tobeRemoved;
246 for (const int i : tobeRemoved) {
247 int before = m_model->count();
248 m_model->deleteItem(i);
249 QTRY_COMPARE(m_model->count(), before - 1);
250 }
251 m_tobeRemoved.clear();
252 delete m_model;
253 m_model = nullptr;
254 removeTestUserData();
255 }
256
257 void PlacesItemModelTest::initTestCase()
258 {
259 QStandardPaths::setTestModeEnabled(true);
260 // remove test user data
261 removeTestUserData();
262
263 const QString fakeHw = QFINDTESTDATA("data/fakecomputer.xml");
264 QVERIFY(!fakeHw.isEmpty());
265 qputenv("SOLID_FAKEHW", QFile::encodeName(fakeHw));
266
267 setBalooEnabled(true);
268 const QString bookmarsFileName = bookmarksFile();
269 if (QFileInfo::exists(bookmarsFileName)) {
270 // Ensure we'll have a clean bookmark file to start
271 QVERIFY(QFile::remove(bookmarsFileName));
272 }
273
274 if (QDir(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)).exists()) {
275 m_hasDesktopFolder = true;
276 m_expectedModelCount++;
277 }
278
279 if (QDir(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)).exists()) {
280 m_hasDownloadsFolder = true;
281 m_expectedModelCount++;
282 }
283
284 qRegisterMetaType<KItemRangeList>();
285 qRegisterMetaType<KItemRange>();
286 }
287
288 void PlacesItemModelTest::cleanupTestCase()
289 {
290 qDeleteAll(m_interfacesMap);
291 QFile::remove(bookmarksFile());
292
293 // Remove any previous properties file
294 removeTestUserData();
295 }
296
297 void PlacesItemModelTest::testModelSort()
298 {
299 CHECK_PLACES_URLS(initialUrls());
300 }
301
302 void PlacesItemModelTest::testGroups()
303 {
304 const auto groups = m_model->groups();
305
306
307 QCOMPARE(groups.size(), 6);
308
309 QCOMPARE(groups.at(0).first, 0);
310 QCOMPARE(groups.at(0).second.toString(), QStringLiteral("Places"));
311
312 QCOMPARE(groups.at(1).first, 5);
313 QCOMPARE(groups.at(1).second.toString(), QStringLiteral("Remote"));
314
315 QCOMPARE(groups.at(2).first, 7);
316 QCOMPARE(groups.at(2).second.toString(), QStringLiteral("Recently Saved"));
317
318 QCOMPARE(groups.at(3).first, 9);
319 QCOMPARE(groups.at(3).second.toString(), QStringLiteral("Search For"));
320
321 QCOMPARE(groups.at(4).first, 13);
322 QCOMPARE(groups.at(4).second.toString(), QStringLiteral("Devices"));
323
324 QCOMPARE(groups.at(5).first, 14);
325 QCOMPARE(groups.at(5).second.toString(), QStringLiteral("Removable Devices"));
326 }
327
328 void PlacesItemModelTest::testPlaceItem_data()
329 {
330 QTest::addColumn<QUrl>("url");
331 QTest::addColumn<bool>("expectedIsHidden");
332 QTest::addColumn<bool>("expectedIsSystemItem");
333 QTest::addColumn<QString>("expectedGroup");
334 QTest::addColumn<bool>("expectedStorageSetupNeeded");
335
336 // places
337 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << false << true << QStringLiteral("Places") << false;
338
339 // baloo -search
340 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << false << true << QStringLiteral("Search For") << false;
341
342 // baloo - timeline
343 QTest::newRow("Baloo - Today") << QUrl("timeline:/today") << false << true << QStringLiteral("Recently Saved") << false;
344
345 // devices
346 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << false << false << QStringLiteral("Removable Devices") << false;
347 }
348
349 void PlacesItemModelTest::testPlaceItem()
350 {
351 QFETCH(QUrl, url);
352 QFETCH(bool, expectedIsHidden);
353 QFETCH(bool, expectedIsSystemItem);
354 QFETCH(QString, expectedGroup);
355 QFETCH(bool, expectedStorageSetupNeeded);
356
357 const int index = indexOf(url);
358 PlacesItem *item = m_model->placesItem(index);
359 QCOMPARE(item->url(), url);
360 QCOMPARE(item->isHidden(), expectedIsHidden);
361 QCOMPARE(item->isSystemItem(), expectedIsSystemItem);
362 QCOMPARE(item->group(), expectedGroup);
363 QCOMPARE(item->storageSetupNeeded(), expectedStorageSetupNeeded);
364 }
365
366 void PlacesItemModelTest::testDeletePlace()
367 {
368 const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
369 QStringList urls = initialUrls();
370 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
371 QSignalSpy itemsRemovedSpy(m_model, &PlacesItemModel::itemsRemoved);
372
373 PlacesItemModel *model = new PlacesItemModel();
374
375 // create a new place
376 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
377 urls.insert(5, tempUrl.toLocalFile());
378
379 // check if the new entry was created
380 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
381 CHECK_PLACES_URLS(urls);
382 QTRY_COMPARE(model->count(), m_model->count());
383
384 // delete item
385 m_model->deleteItem(5);
386
387 // make sure that the new item is removed
388 QTRY_COMPARE(itemsRemovedSpy.count(), 1);
389 QTRY_COMPARE(m_model->count(), m_expectedModelCount);
390 CHECK_PLACES_URLS(initialUrls());
391 QTRY_COMPARE(model->count(), m_model->count());
392 }
393
394 void PlacesItemModelTest::testTearDownDevice()
395 {
396 const QUrl mediaUrl = QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
397 int index = indexOf(mediaUrl);
398 QVERIFY(index != -1);
399
400 auto ejectAction = m_model->ejectAction(index);
401 QVERIFY(!ejectAction);
402
403 auto teardownAction = m_model->teardownAction(index);
404 QVERIFY(teardownAction);
405
406 QCOMPARE(m_model->count(), m_expectedModelCount);
407
408 QSignalSpy spyItemsRemoved(m_model, &PlacesItemModel::itemsRemoved);
409 fakeManager()->call(QStringLiteral("unplug"), "/org/kde/solid/fakehw/volume_part1_size_993284096");
410 QTRY_COMPARE(m_model->count(), 16);
411 QCOMPARE(spyItemsRemoved.count(), 1);
412 const QList<QVariant> spyItemsRemovedArgs = spyItemsRemoved.takeFirst();
413 const KItemRangeList removedRange = spyItemsRemovedArgs.at(0).value<KItemRangeList>();
414 QCOMPARE(removedRange.size(), 1);
415 QCOMPARE(removedRange.first().index, index);
416 QCOMPARE(removedRange.first().count, 1);
417
418 QCOMPARE(indexOf(mediaUrl), -1);
419
420 QSignalSpy spyItemsInserted(m_model, &PlacesItemModel::itemsInserted);
421 fakeManager()->call(QStringLiteral("plug"), "/org/kde/solid/fakehw/volume_part1_size_993284096");
422 QTRY_COMPARE(m_model->count(), m_expectedModelCount);
423 QCOMPARE(spyItemsInserted.count(), 1);
424 index = indexOf(mediaUrl);
425
426 const QList<QVariant> args = spyItemsInserted.takeFirst();
427 const KItemRangeList insertedRange = args.at(0).value<KItemRangeList>();
428 QCOMPARE(insertedRange.size(), 1);
429 QCOMPARE(insertedRange.first().index, index);
430 QCOMPARE(insertedRange.first().count, 1);
431 }
432
433 void PlacesItemModelTest::testDefaultViewProperties_data()
434 {
435 QTest::addColumn<QUrl>("url");
436 QTest::addColumn<DolphinView::Mode>("expectedViewMode");
437 QTest::addColumn<bool>("expectedPreviewShow");
438 QTest::addColumn<QList<QByteArray> >("expectedVisibleRole");
439
440 // places
441 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << DolphinView::IconsView << true << QList<QByteArray>({"text"});
442
443 // baloo -search
444 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << DolphinView::DetailsView << false << QList<QByteArray>({"text", "path"});
445
446 // audio files
447 QTest::newRow("Places - Audio") << QUrl("search:/audio") << DolphinView::DetailsView << false << QList<QByteArray>({"text", "artist", "album"});
448
449 // baloo - timeline
450 QTest::newRow("Baloo - Today") << QUrl("timeline:/today") << DolphinView::DetailsView << true << QList<QByteArray>({"text", "modificationtime"});
451
452 // devices
453 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << DolphinView::IconsView << true << QList<QByteArray>({"text"});
454
455 }
456
457 void PlacesItemModelTest::testDefaultViewProperties()
458 {
459 QFETCH(QUrl, url);
460 QFETCH(DolphinView::Mode, expectedViewMode);
461 QFETCH(bool, expectedPreviewShow);
462 QFETCH(QList<QByteArray>, expectedVisibleRole);
463
464 ViewProperties properties(KFilePlacesModel::convertedUrl(url));
465 QCOMPARE(properties.viewMode(), expectedViewMode);
466 QCOMPARE(properties.previewsShown(), expectedPreviewShow);
467 QCOMPARE(properties.visibleRoles(), expectedVisibleRole);
468 }
469
470 void PlacesItemModelTest::testClear()
471 {
472 QCOMPARE(m_model->count(), m_expectedModelCount);
473 m_model->clear();
474 QCOMPARE(m_model->count(), 0);
475 QCOMPARE(m_model->hiddenCount(), 0);
476 m_model->refresh();
477 QTRY_COMPARE(m_model->count(), m_expectedModelCount);
478 }
479
480 void PlacesItemModelTest::testHideItem()
481 {
482 const QUrl mediaUrl = QUrl::fromLocalFile(QStringLiteral("/media/XO-Y4"));
483 const int index = indexOf(mediaUrl);
484
485 PlacesItem *item = m_model->placesItem(index);
486
487 QSignalSpy spyItemsRemoved(m_model, &PlacesItemModel::itemsRemoved);
488 QList<QVariant> spyItemsRemovedArgs;
489 KItemRangeList removedRange;
490
491 QSignalSpy spyItemsInserted(m_model, &PlacesItemModel::itemsInserted);
492 QList<QVariant> spyItemsInsertedArgs;
493 KItemRangeList insertedRange;
494 QVERIFY(item);
495
496 // hide an item
497 item->setHidden(true);
498
499 // check if items removed was fired
500 QTRY_COMPARE(m_model->count(), 16);
501 QCOMPARE(spyItemsRemoved.count(), 1);
502 spyItemsRemovedArgs = spyItemsRemoved.takeFirst();
503 removedRange = spyItemsRemovedArgs.at(0).value<KItemRangeList>();
504 QCOMPARE(removedRange.size(), 1);
505 QCOMPARE(removedRange.first().index, index);
506 QCOMPARE(removedRange.first().count, 1);
507
508 // allow model to show hidden items
509 m_model->setHiddenItemsShown(true);
510
511 // check if the items inserted was fired
512 spyItemsInsertedArgs = spyItemsInserted.takeFirst();
513 insertedRange = spyItemsInsertedArgs.at(0).value<KItemRangeList>();
514 QCOMPARE(insertedRange.size(), 1);
515 QCOMPARE(insertedRange.first().index, index);
516 QCOMPARE(insertedRange.first().count, 1);
517
518 // mark item as visible
519 item = m_model->placesItem(index);
520 item->setHidden(false);
521
522 // mark model to hide invisible items
523 m_model->setHiddenItemsShown(true);
524
525 QTRY_COMPARE(m_model->count(), m_expectedModelCount);
526 }
527
528 void PlacesItemModelTest::testSystemItems()
529 {
530 QCOMPARE(m_model->count(), m_expectedModelCount);
531 for (int r = 0; r < m_model->count(); r++) {
532 QCOMPARE(m_model->placesItem(r)->isSystemItem(), !m_model->placesItem(r)->device().isValid());
533 }
534
535 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
536
537 // create a new entry (non system item)
538 createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation)), QString());
539
540 // check if the new entry was created
541 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
542
543 // make sure the new place get removed
544 removePlaceAfter(5);
545
546 QList<QVariant> args = itemsInsertedSpy.takeFirst();
547 KItemRangeList range = args.at(0).value<KItemRangeList>();
548 QCOMPARE(range.first().index, 5);
549 QCOMPARE(range.first().count, 1);
550 QVERIFY(!m_model->placesItem(5)->isSystemItem());
551 QCOMPARE(m_model->count(), 18);
552
553 QTest::qWait(300);
554 // check if the removal signal is correct
555 QSignalSpy itemsRemovedSpy(m_model, &PlacesItemModel::itemsRemoved);
556 m_model->deleteItem(5);
557 QTRY_COMPARE(itemsRemovedSpy.count(), 1);
558 args = itemsRemovedSpy.takeFirst();
559 range = args.at(0).value<KItemRangeList>();
560 QCOMPARE(range.first().index, 5);
561 QCOMPARE(range.first().count, 1);
562 QTRY_COMPARE(m_model->count(), m_expectedModelCount);
563
564 //cancel removal (it was removed above)
565 cancelPlaceRemoval(5);
566 }
567
568 void PlacesItemModelTest::testEditBookmark()
569 {
570 QScopedPointer<PlacesItemModel> other(new PlacesItemModel());
571
572 createPlaceItem(QStringLiteral("Temporary Dir"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation)), QString());
573
574 // make sure that the new item will be removed later
575 removePlaceAfter(5);
576
577 QSignalSpy itemsChangedSply(m_model, &PlacesItemModel::itemsChanged);
578
579 // modify place text
580 m_model->item(3)->setText(QStringLiteral("Renamed place"));
581 m_model->refresh();
582
583 // check if the correct signal was fired
584 QTRY_COMPARE(itemsChangedSply.count(), 1);
585 QList<QVariant> args = itemsChangedSply.takeFirst();
586 KItemRangeList range = args.at(0).value<KItemRangeList>();
587 QCOMPARE(range.first().index, 3);
588 QCOMPARE(range.first().count, 1);
589 QSet<QByteArray> roles = args.at(1).value<QSet<QByteArray> >();
590 QCOMPARE(roles.size(), 1);
591 QCOMPARE(*roles.begin(), QByteArrayLiteral("text"));
592 QCOMPARE(m_model->item(3)->text(), QStringLiteral("Renamed place"));
593
594 // check if the item was updated in the other model
595 QTRY_COMPARE(other->item(3)->text(), QStringLiteral("Renamed place"));
596 }
597
598 void PlacesItemModelTest::testEditAfterCreation()
599 {
600 const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
601 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
602
603 // create a new place
604 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
605 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
606
607 PlacesItemModel *model = new PlacesItemModel();
608 QTRY_COMPARE(model->count(), m_model->count());
609
610 // make sure that the new item will be removed later
611 removePlaceAfter(5);
612
613 // modify place text
614 PlacesItem *item = m_model->placesItem(3);
615 item->setText(QStringLiteral("Renamed place"));
616 m_model->refresh();
617
618 // check if the second model got the changes
619 QTRY_COMPARE(model->count(), m_model->count());
620 QTRY_COMPARE(model->placesItem(3)->text(), m_model->placesItem(3)->text());
621 QTRY_COMPARE(model->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
622 m_model->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")));
623 QTRY_COMPARE(model->placesItem(3)->icon(), m_model->placesItem(3)->icon());
624 QTRY_COMPARE(model->placesItem(3)->url(), m_model->placesItem(3)->url());
625 }
626
627 void PlacesItemModelTest::testEditMetadata()
628 {
629 const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
630 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
631
632 // create a new place
633 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
634 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
635
636 // check if the new entry was created
637 PlacesItemModel *model = new PlacesItemModel();
638 QTRY_COMPARE(model->count(), m_model->count());
639
640 // make sure that the new item will be removed later
641 removePlaceAfter(5);
642
643 // modify place metadata
644 PlacesItem *item = m_model->placesItem(3);
645 item->bookmark().setMetaDataItem(QStringLiteral("OnlyInApp"), KAboutData::applicationData().componentName());
646 m_model->refresh();
647
648 // check if the place was modified in both models
649 QTRY_COMPARE(model->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
650 KAboutData::applicationData().componentName());
651 QTRY_COMPARE(model->placesItem(3)->text(), m_model->placesItem(3)->text());
652 QTRY_COMPARE(model->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")),
653 m_model->placesItem(3)->bookmark().metaDataItem(QStringLiteral("OnlyInApp")));
654 QTRY_COMPARE(model->placesItem(3)->icon(), m_model->placesItem(3)->icon());
655 QTRY_COMPARE(model->placesItem(3)->url(), m_model->placesItem(3)->url());
656 }
657
658 void PlacesItemModelTest::testRefresh()
659 {
660 const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
661 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
662
663 // create a new place
664 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
665 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
666
667 PlacesItemModel *model = new PlacesItemModel();
668 QTRY_COMPARE(model->count(), m_model->count());
669
670 // make sure that the new item will be removed later
671 removePlaceAfter(5);
672
673 PlacesItem *item = m_model->placesItem(5);
674 PlacesItem *sameItem = model->placesItem(5);
675 QCOMPARE(item->text(), sameItem->text());
676
677 // modify place text
678 item->setText(QStringLiteral("Renamed place"));
679
680 // item from another model is not affected at the moment
681 QVERIFY(item->text() != sameItem->text());
682
683 // propagate change
684 m_model->refresh();
685
686 // item must be equal
687 QTRY_COMPARE(item->text(), sameItem->text());
688 }
689
690 void PlacesItemModelTest::testIcons_data()
691 {
692 QTest::addColumn<QUrl>("url");
693 QTest::addColumn<QString>("expectedIconName");
694
695 // places
696 QTest::newRow("Places - Home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("user-home");
697
698 // baloo -search
699 QTest::newRow("Baloo - Documents") << QUrl("search:/documents") << QStringLiteral("folder-text");
700
701 // baloo - timeline
702 QTest::newRow("Baloo - Today") << QUrl("timeline:/today") << QStringLiteral("go-jump-today");
703
704 // devices
705 QTest::newRow("Devices - Floppy") << QUrl("file:///media/floppy0") << QStringLiteral("blockdevice");
706 }
707
708 void PlacesItemModelTest::testIcons()
709 {
710 QFETCH(QUrl, url);
711 QFETCH(QString, expectedIconName);
712
713 PlacesItem *item = m_model->placesItem(indexOf(url));
714 QCOMPARE(item->icon(), expectedIconName);
715
716 for (int r = 0; r < m_model->count(); r++) {
717 QVERIFY(!m_model->placesItem(r)->icon().isEmpty());
718 }
719 }
720
721 void PlacesItemModelTest::testDragAndDrop()
722 {
723 QList<QVariant> args;
724 KItemRangeList range;
725 QStringList urls = initialUrls();
726 QSignalSpy itemsInsertedSpy(m_model, &PlacesItemModel::itemsInserted);
727 QSignalSpy itemsRemovedSpy(m_model, &PlacesItemModel::itemsRemoved);
728
729 CHECK_PLACES_URLS(initialUrls());
730 // Move the home directory to the end of the places group
731 QMimeData *dropData = createMimeData(QList<int>() << 0);
732 m_model->dropMimeDataBefore(m_model->count() - 1, dropData);
733 urls.move(0, 4);
734 delete dropData;
735
736 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
737 QTRY_COMPARE(itemsRemovedSpy.count(), 1);
738
739 // remove item from actual position
740 args = itemsRemovedSpy.takeFirst();
741 range = args.at(0).value<KItemRangeList>();
742 QCOMPARE(range.size(), 1);
743 QCOMPARE(range.at(0).count, 1);
744 QCOMPARE(range.at(0).index, 0);
745
746 // insert intem in his group
747 args = itemsInsertedSpy.takeFirst();
748 range = args.at(0).value<KItemRangeList>();
749 QCOMPARE(range.size(), 1);
750 QCOMPARE(range.at(0).count, 1);
751 QCOMPARE(range.at(0).index, 4);
752
753 CHECK_PLACES_URLS(urls);
754
755 itemsInsertedSpy.clear();
756 itemsRemovedSpy.clear();
757
758 // Move home directory item back to its original position
759 dropData = createMimeData(QList<int>() << 4);
760 m_model->dropMimeDataBefore(0, dropData);
761 urls.move(4, 0);
762 delete dropData;
763
764 QTRY_COMPARE(itemsInsertedSpy.count(), 1);
765 QTRY_COMPARE(itemsRemovedSpy.count(), 1);
766
767 // remove item from actual position
768 args = itemsRemovedSpy.takeFirst();
769 range = args.at(0).value<KItemRangeList>();
770 QCOMPARE(range.size(), 1);
771 QCOMPARE(range.at(0).count, 1);
772 QCOMPARE(range.at(0).index, 4);
773
774 // insert intem in the requested position
775 args = itemsInsertedSpy.takeFirst();
776 range = args.at(0).value<KItemRangeList>();
777 QCOMPARE(range.size(), 1);
778 QCOMPARE(range.at(0).count, 1);
779 QCOMPARE(range.at(0).index, 0);
780
781 CHECK_PLACES_URLS(urls);
782 }
783
784 void PlacesItemModelTest::testHideDevices()
785 {
786 QSignalSpy itemsRemoved(m_model, &PlacesItemModel::itemsRemoved);
787 QStringList urls = initialUrls();
788
789 m_model->setGroupHidden(KFilePlacesModel::RemovableDevicesType, true);
790 QTRY_VERIFY(m_model->isGroupHidden(KFilePlacesModel::RemovableDevicesType));
791 QTRY_COMPARE(itemsRemoved.count(), 3);
792
793 // remove removable-devices
794 urls.removeOne(QStringLiteral("/media/floppy0"));
795 urls.removeOne(QStringLiteral("/media/XO-Y4"));
796 urls.removeOne(QStringLiteral("/media/cdrom"));
797
798 // check if the correct urls was removed
799 CHECK_PLACES_URLS(urls);
800
801 delete m_model;
802 m_model = new PlacesItemModel();
803 QTRY_COMPARE(m_model->count(), urls.count());
804 CHECK_PLACES_URLS(urls);
805
806 // revert changes
807 m_model->setGroupHidden(KFilePlacesModel::RemovableDevicesType, false);
808 urls = initialUrls();
809 QTRY_COMPARE(m_model->count(), urls.count());
810 CHECK_PLACES_URLS(urls);
811 }
812
813 void PlacesItemModelTest::testDuplicatedEntries()
814 {
815 QStringList urls = initialUrls();
816 // create a duplicated entry on bookmark
817 KBookmarkManager *bookmarkManager = KBookmarkManager::managerForFile(bookmarksFile(), QStringLiteral("kfilePlaces"));
818 KBookmarkGroup root = bookmarkManager->root();
819 KBookmark bookmark = root.addBookmark(QStringLiteral("Duplicated Search Videos"), QUrl("search:/videos"), {});
820
821 const QString id = QUuid::createUuid().toString();
822 bookmark.setMetaDataItem(QStringLiteral("ID"), id);
823 bookmark.setMetaDataItem(QStringLiteral("OnlyInApp"), KAboutData::applicationData().componentName());
824 bookmarkManager->emitChanged(bookmarkManager->root());
825
826 PlacesItemModel *newModel = new PlacesItemModel();
827 QTRY_COMPARE(placesUrls(newModel).count(QStringLiteral("search:/videos")), 1);
828 QTRY_COMPARE(urls, placesUrls(newModel));
829 delete newModel;
830 }
831
832 void PlacesItemModelTest::renameAfterCreation()
833 {
834 const QUrl tempUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
835 QStringList urls = initialUrls();
836 PlacesItemModel *model = new PlacesItemModel();
837
838 CHECK_PLACES_URLS(urls);
839 QTRY_COMPARE(model->count(), m_model->count());
840
841 // create a new place
842 createPlaceItem(QStringLiteral("Temporary Dir"), tempUrl, QString());
843 urls.insert(5, tempUrl.toLocalFile());
844
845 // make sure that the new item will be removed later
846 removePlaceAfter(5);
847
848 CHECK_PLACES_URLS(urls);
849 QCOMPARE(model->count(), m_model->count());
850
851
852 // modify place text
853 QSignalSpy changedSpy(m_model, &PlacesItemModel::itemsChanged);
854
855 PlacesItem *item = m_model->placesItem(3);
856 item->setText(QStringLiteral("New Temporary Dir"));
857 item->setUrl(item->url());
858 item->setIcon(item->icon());
859 m_model->refresh();
860
861 QTRY_COMPARE(changedSpy.count(), 1);
862
863 // check if the place was modified in both models
864 QTRY_COMPARE(m_model->placesItem(3)->text(), QStringLiteral("New Temporary Dir"));
865 QTRY_COMPARE(model->placesItem(3)->text(), QStringLiteral("New Temporary Dir"));
866 }
867
868 QTEST_MAIN(PlacesItemModelTest)
869
870 #include "placesitemmodeltest.moc"