]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/tests/dolphinviewtest_allviewmodes.cpp
Details view: Fix jumping column-widths
[dolphin.git] / src / tests / dolphinviewtest_allviewmodes.cpp
index d9db5b141e82ca6ca69198f2803a318596df012f..b50d4b5df7ff812f486babcefe70a1f21de2d1e0 100644 (file)
@@ -57,7 +57,7 @@ DolphinViewTest_AllViewModes::DolphinViewTest_AllViewModes() {
 Q_DECLARE_METATYPE(KFileItemList)
 
 void DolphinViewTest_AllViewModes::testSelection() {
-    TestDir dir;    
+    TestDir dir;
     const int totalItems = 50;
     for (int i = 0; i < totalItems; i++) {
         dir.createFile(QString("%1").arg(i));
@@ -198,7 +198,7 @@ void DolphinViewTest_AllViewModes::testViewPropertySettings()
     // Show hidden files. This triggers the dir lister
     // -> we have to wait until loading the hidden files is finished
     view.setShowHiddenFiles(true);
-    QVERIFY(waitForFinishedPathLoading(&view));
+    waitForFinishedPathLoading(&view);
     QVERIFY(view.showHiddenFiles());
     QCOMPARE(viewItems(&view), QStringList() << ".f" << "a" << "b" << "c" << "d" << "e");
 
@@ -310,6 +310,15 @@ void DolphinViewTest_AllViewModes::testSaveAndRestoreState()
     view.setSortOrder(Qt::AscendingOrder);
     QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
 
+    // Make sure that previews are off and that the icon size does not depend on the preview setting.
+    // This is needed for the test for bug 270437, see below.
+    view.setShowPreview(false);
+    int zoomLevel = view.zoomLevel();
+    view.setShowPreview(true);
+    view.setZoomLevel(zoomLevel);
+    view.setShowPreview(false);
+
+    // Select item 45
     const QModelIndex index45 = itemView(&view)->model()->index(45, 0);
     itemView(&view)->scrollTo(index45);
     itemView(&view)->setCurrentIndex(index45);
@@ -323,12 +332,12 @@ void DolphinViewTest_AllViewModes::testSaveAndRestoreState()
 
     // Change the URL
     view.setUrl(dir.name() + "51");
-    QVERIFY(waitForFinishedPathLoading(&view));
+    waitForFinishedPathLoading(&view);
     qApp->sendPostedEvents();
 
     // Go back, but do not call DolphinView::restoreState()
     view.setUrl(dir.url());
-    QVERIFY(waitForFinishedPathLoading(&view));
+    waitForFinishedPathLoading(&view);
     qApp->sendPostedEvents();
 
     // Verify that the view is scrolled to top-left corner and that item 45 is not the current item.
@@ -342,7 +351,7 @@ void DolphinViewTest_AllViewModes::testSaveAndRestoreState()
 
     // Change the URL again
     view.setUrl(dir.name() + "51");
-    QVERIFY(waitForFinishedPathLoading(&view));
+    waitForFinishedPathLoading(&view);
     qApp->sendPostedEvents();
 
     // Check that the current item and scroll position are correct if DolphinView::restoreState()
@@ -350,9 +359,42 @@ void DolphinViewTest_AllViewModes::testSaveAndRestoreState()
     view.setUrl(dir.url());
     QDataStream restoreStream(viewState);
     view.restoreState(restoreStream);
-    QVERIFY(waitForFinishedPathLoading(&view));
+    waitForFinishedPathLoading(&view);
+    qApp->sendPostedEvents();
+
+    QCOMPARE(itemView(&view)->currentIndex(), index45);
+    QCOMPARE(itemView(&view)->horizontalScrollBar()->value(), scrollPosX);
+    QCOMPARE(itemView(&view)->verticalScrollBar()->value(), scrollPosY);
+
+    /**
+     * Additionally, we verify the fix for the bug https://bugs.kde.org/show_bug.cgi?id=270437
+     * Actually, it's a bug in KFilePreviewGenerator, but it is easier to test it here.
+     */
+
+    // Turn previews on.
+    view.setShowPreview(true);
+    QVERIFY(view.showPreview());
+
+    // We have to process all events in the queue to make sure that previews are really on.
     qApp->sendPostedEvents();
 
+    // Current item and scroll position should not change.
+    QCOMPARE(itemView(&view)->currentIndex(), index45);
+    QCOMPARE(itemView(&view)->horizontalScrollBar()->value(), scrollPosX);
+    QCOMPARE(itemView(&view)->verticalScrollBar()->value(), scrollPosY);
+
+    // Turn previews off again. Before bug 270437, this triggered the dir lister's openUrl() method
+    // -> we check that by listening to the view's startedPathLoading() signal and wait until the loading is finished in that case.
+    QSignalSpy spy(&view, SIGNAL(startedPathLoading(const KUrl&)));
+    view.setShowPreview(false);
+    QVERIFY(!view.showPreview());
+    qApp->sendPostedEvents();
+    if (!spy.isEmpty()) {
+        // The dir lister reloads the directory. We wait until the loading is finished.
+        waitForFinishedPathLoading(&view);
+    }
+
+    // Current item and scroll position should not change.
     QCOMPARE(itemView(&view)->currentIndex(), index45);
     QCOMPARE(itemView(&view)->horizontalScrollBar()->value(), scrollPosX);
     QCOMPARE(itemView(&view)->verticalScrollBar()->value(), scrollPosY);
@@ -389,13 +431,112 @@ void DolphinViewTest_AllViewModes::testKeyboardFocus()
     }
 }
 
+/**
+ * testCutCopyPaste() checks if cutting or copying items in one view and pasting
+ * them in another one works.
+ */
+
+void DolphinViewTest_AllViewModes::testCutCopyPaste()
+{
+    TestDir dir1;
+    dir1.createFiles(QStringList() << "a" << "b" << "c" << "d");
+    DolphinView view1(dir1.url(), 0);
+    QAbstractItemView* itemView1 = initView(&view1);
+
+    TestDir dir2;
+    dir2.createFiles(QStringList() << "1" << "2" << "3" << "4");
+    dir2.createDir("subfolder");
+    DolphinView view2(dir2.url(), 0);
+    QAbstractItemView* itemView2 = initView(&view2);
+
+    // Make sure that both views are sorted by name in ascending order
+    // TODO: Maybe that should be done in initView(), such all tests can rely on it...?
+    view1.setSorting(DolphinView::SortByName);
+    view1.setSortOrder(Qt::AscendingOrder);
+    view2.setSorting(DolphinView::SortByName);
+    view2.setSortOrder(Qt::AscendingOrder);
+    view2.setSortFoldersFirst(true);
+
+    QCOMPARE(viewItems(&view1), QStringList() << "a" << "b" << "c" << "d");
+    QCOMPARE(viewItems(&view2), QStringList() << "subfolder" << "1" << "2" << "3" << "4");
+
+    /** Copy and paste */
+    // Select an item ("d") n view1, copy it and paste it in view2.
+    // Note that we have to wait for view2's finishedPathLoading() signal because the pasting is done in the background.
+    QModelIndex index = itemView1->model()->index(3, 0);
+    itemView1->scrollTo(index);
+    QTest::mouseClick(itemView1->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView1->visualRect(index).center());
+    verifySelectedItemsCount(&view1, 1);
+    QCOMPARE(selectedItems(&view1), QStringList() << "d");
+    view1.copySelectedItems();
+    view2.paste();
+    waitForFinishedPathLoading(&view2);
+    QCOMPARE(viewItems(&view1), QStringList() << "a" << "b" << "c" << "d");
+    QCOMPARE(viewItems(&view2), QStringList() << "subfolder" << "1" << "2" << "3" << "4" << "d");
+    // The pasted item should be selected
+    QCOMPARE(selectedItems(&view2), QStringList() << "d");
+
+    /** Cut and paste */
+    // Select two items ("3", "4") in view2, cut and paste in view1.
+    view2.clearSelection();
+    index = itemView2->model()->index(3, 0);
+    itemView2->scrollTo(index);
+    QTest::mouseClick(itemView2->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView2->visualRect(index).center());
+    verifySelectedItemsCount(&view2, 1);
+    index = itemView2->model()->index(4, 0);
+    itemView2->scrollTo(index);
+    QTest::mouseClick(itemView2->viewport(), Qt::LeftButton, Qt::ShiftModifier, itemView2->visualRect(index).center());
+    verifySelectedItemsCount(&view2, 2);
+    QCOMPARE(selectedItems(&view2), QStringList() << "3" << "4");
+    view2.cutSelectedItems();
+    // In view1, "d" is still selected
+    QCOMPARE(selectedItems(&view1), QStringList() << "d");
+    // Paste "3" and "4"
+    view1.paste();
+    waitForFinishedPathLoading(&view1);
+    // In principle, KIO could implement copy&paste such that the pasted items are already there, but the cut items
+    // have not been removed yet. Therefore, we check the number of items in view2 and also wait for that view's
+    // finishedPathLoading() signal if the cut items are still there.
+    if (viewItems(&view2).count() > 4) {
+        waitForFinishedPathLoading(&view2);
+    }
+    QCOMPARE(viewItems(&view1), QStringList() << "3" << "4" << "a" << "b" << "c" << "d");
+    QCOMPARE(viewItems(&view2), QStringList() << "subfolder" << "1" << "2" << "d");
+    // The pasted items ("3", "4") should be selected now, and the previous selection ("d") should be cleared.
+    QCOMPARE(selectedItems(&view1), QStringList() << "3" << "4");
+
+    /** Copy and paste into subfolder */
+    view1.clearSelection();
+    index = itemView1->model()->index(3, 0);
+    itemView1->scrollTo(index);
+    QTest::mouseClick(itemView1->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView1->visualRect(index).center());
+    verifySelectedItemsCount(&view1, 1);
+    QCOMPARE(selectedItems(&view1), QStringList() << "b");
+    view1.copySelectedItems();
+    // Now we use view1 to display the subfolder, which is still empty.
+    view1.setUrl(dir2.name() + "subfolder");
+    waitForFinishedPathLoading(&view1);
+    QCOMPARE(viewItems(&view1), QStringList());
+    // Select the subfolder.in view2
+    view2.clearSelection();
+    index = itemView2->model()->index(0, 0);
+    itemView2->scrollTo(index);
+    QTest::mouseClick(itemView2->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView2->visualRect(index).center());
+    verifySelectedItemsCount(&view2, 1);
+    // Paste into the subfolder
+    view2.pasteIntoFolder();
+    waitForFinishedPathLoading(&view1);
+    QCOMPARE(viewItems(&view1), QStringList() << "b");
+    // The pasted items in view1 are *not* selected now (because the pasting was done indirectly using view2.pasteIntoFolder()).
+}
+
 // Private member functions which are used by the tests
 
 /**
  * initView(DolphinView*) sets the correct view mode, shows the view on the screen, and waits until loading the
  * folder in the view is finished.
  *
- * Many unit tests need access to DolphinVie's internal item view (icons, details, or columns).
+ * Many unit tests need access to DolphinView's internal item view (icons, details, or columns).
  * Therefore, a pointer to the item view is returned by initView(DolphinView*).
  */
 
@@ -414,7 +555,7 @@ QAbstractItemView* DolphinViewTest_AllViewModes::initView(DolphinView* view) con
     // while we were waiting in QTest::qWaitForWindowShown(view)
     // -> waitForFinishedPathLoading(view) would fail in that case.
     if (spyFinishedPathLoading.isEmpty()) {
-        Q_ASSERT(waitForFinishedPathLoading(view));
+        waitForFinishedPathLoading(view);
     }
 
     return itemView(view);