]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/tests/dolphinviewtest_allviewmodes.cpp
Use capitalized KDE includes
[dolphin.git] / src / tests / dolphinviewtest_allviewmodes.cpp
index 3eb78c0d256430146644f269454d063c7585ac91..9d4876fd8f15a8689700fe6d4684b8346e274f3f 100644 (file)
@@ -27,6 +27,9 @@
 #include "views/dolphinmodel.h"
 #include "views/dolphindirlister.h"
 #include "views/dolphinsortfilterproxymodel.h"
+#include "views/zoomlevelinfo.h"
+
+#include <QScrollBar>
 
 #include <qtestmouse.h>
 #include <qtestkeyboard.h>
@@ -37,6 +40,13 @@ DolphinViewTest_AllViewModes::DolphinViewTest_AllViewModes() {
 }
 
 void DolphinViewTest_AllViewModes::init() {
+    if (mode() == DolphinView::ColumnView) {
+        // In Columns View mode, we need to create a new DolphinView after each
+        // test to make the file-related tests after the first one pass.
+        // TODO: Try to find out if there is a hidden bug in the Columns View that causes this.
+        delete m_view;
+        m_view = new DolphinView(KUrl(m_path), 0);
+    }
     m_view->setMode(mode());
     QVERIFY(verifyCorrectViewMode());
     m_view->resize(200, 300);
@@ -98,15 +108,22 @@ void DolphinViewTest_AllViewModes::testSelection() {
     QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView()->visualRect(index).center());
     verifySelectedItemsCount(1);
 
-    index = itemView()->model()->index(45, 0);
+    index = itemView()->model()->index(totalItems - 5, 0);
     itemView()->scrollTo(index);
     QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView()->visualRect(index).center());
     verifySelectedItemsCount(2);
 
-    index = itemView()->model()->index(48, 0);
+    index = itemView()->model()->index(totalItems - 2, 0);
     itemView()->scrollTo(index);
     QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ShiftModifier, itemView()->visualRect(index).center());
     verifySelectedItemsCount(5);
+
+    m_view->invertSelection();
+    verifySelectedItemsCount(totalItems - 5);
+
+    // Pressing Esc should clear the selection
+    QTest::keyClick(itemView()->viewport(), Qt::Key_Escape);
+    verifySelectedItemsCount(0);
 }
 
 /**
@@ -143,13 +160,6 @@ void DolphinViewTest_AllViewModes::testViewPropertySettings()
     m_view->setShowHiddenFiles(false);
     QVERIFY(!m_view->showHiddenFiles());
 
-    if (mode() == DolphinView::ColumnView) {
-        // If the columns view is used, the view is empty before calling qApp->sendPostedEvents.
-        // It seems that some event needs to be processed to see the items in the view.
-        // TODO: Find out why this is needed for the columns view, but not for the other view modes.
-        qApp->sendPostedEvents();
-    }
-
     /** Check that the sort order is correct for different kinds of settings */
 
     // Sort by Name, ascending
@@ -213,18 +223,170 @@ void DolphinViewTest_AllViewModes::testViewPropertySettings()
     // TODO: Check that the view properties are restored correctly when changing the folder and then going back.
 }
 
+/**
+ * testZoomLevel() checks that setting the zoom level works, both using DolphinView's API and using Ctrl+mouse wheel.
+ */
+
+void DolphinViewTest_AllViewModes::testZoomLevel()
+{
+    createFiles(QStringList() << "a" << "b");
+    reloadViewAndWait();
+
+    m_view->setShowPreview(false);
+    QVERIFY(!m_view->showPreview());
+
+    int zoomLevelBackup = m_view->zoomLevel();
+
+    int zoomLevel = ZoomLevelInfo::minimumLevel();
+    m_view->setZoomLevel(zoomLevel);
+    QCOMPARE(m_view->zoomLevel(), zoomLevel);
+
+    // Increase the zoom level successively to the maximum.
+    while(zoomLevel < ZoomLevelInfo::maximumLevel()) {
+        zoomLevel++;
+        m_view->setZoomLevel(zoomLevel);
+        QCOMPARE(m_view->zoomLevel(), zoomLevel);
+    }
+
+    // Try setting a zoom level larger than the maximum
+    m_view->setZoomLevel(ZoomLevelInfo::maximumLevel() + 1);
+    QCOMPARE(m_view->zoomLevel(), ZoomLevelInfo::maximumLevel());
+
+    // Turn previews on and try setting a zoom level smaller than the minimum
+    m_view->setShowPreview(true);
+    QVERIFY(m_view->showPreview());
+    m_view->setZoomLevel(ZoomLevelInfo::minimumLevel() - 1);
+    QCOMPARE(m_view->zoomLevel(), ZoomLevelInfo::minimumLevel());
+
+    // Turn previews off again and check that the zoom level is restored
+    m_view->setShowPreview(false);
+    QVERIFY(!m_view->showPreview());
+    QCOMPARE(m_view->zoomLevel(), ZoomLevelInfo::maximumLevel());
+
+    // Change the zoom level using Ctrl+mouse wheel
+    QModelIndex index = itemView()->model()->index(0, 0);
+    itemView()->scrollTo(index);
+
+    while (m_view->zoomLevel() > ZoomLevelInfo::minimumLevel()) {
+        int oldZoomLevel = m_view->zoomLevel();
+        QWheelEvent wheelEvent(itemView()->visualRect(index).center(), -1, Qt::NoButton, Qt::ControlModifier);
+        bool wheelEventReceived = qApp->notify(itemView()->viewport(), &wheelEvent);
+        QVERIFY(wheelEventReceived);
+        QVERIFY(m_view->zoomLevel() < oldZoomLevel);
+    }
+    QCOMPARE(m_view->zoomLevel(), ZoomLevelInfo::minimumLevel());
+
+    while (m_view->zoomLevel() < ZoomLevelInfo::maximumLevel()) {
+        int oldZoomLevel = m_view->zoomLevel();
+        QWheelEvent wheelEvent(itemView()->visualRect(index).center(), 1, Qt::NoButton, Qt::ControlModifier);
+        bool wheelEventReceived = qApp->notify(itemView()->viewport(), &wheelEvent);
+        QVERIFY(wheelEventReceived);
+        QVERIFY(m_view->zoomLevel() > oldZoomLevel);
+    }
+    QCOMPARE(m_view->zoomLevel(), ZoomLevelInfo::maximumLevel());
+
+    // Turn previews on again and check that the zoom level is restored
+    m_view->setShowPreview(true);
+    QVERIFY(m_view->showPreview());
+    QCOMPARE(m_view->zoomLevel(), ZoomLevelInfo::minimumLevel());
+
+    // Restore the initial state
+    m_view->setZoomLevel(zoomLevelBackup);
+    m_view->setShowPreview(false);
+    m_view->setZoomLevel(zoomLevelBackup);
+}
+
+/**
+ * testSaveAndRestoreState() checks if saving and restoring the view state (current item, scroll position).
+ *
+ * Note that we call qApp->sendPostedEvents() every time the view's finishedPathLoading(const KUrl&) signal
+ * is received. The reason is that the scroll position is restored in the slot restoreContentsPosition(),
+ * which is been invoked using a queued connection in DolphinView::slotLoadingCompleted(). To make sure
+ * that this slot is really executed before we proceed, we have to empty the event queue using qApp->sendPostedEvents().
+ */
+
+void DolphinViewTest_AllViewModes::testSaveAndRestoreState()
+{
+    const int totalItems = 50;
+
+    for (int i = 0; i < totalItems; i++) {
+        createFile(QString("%1").arg(i));
+    }
+    createDir("51");
+    reloadViewAndWait();
+
+    // Set sorting settings to the default to make sure that the item positions are reproducible.
+    m_view->setSorting(DolphinView::SortByName);
+    QCOMPARE(m_view->sorting(), DolphinView::SortByName);
+    m_view->setSortOrder(Qt::AscendingOrder);
+    QCOMPARE(m_view->sortOrder(), Qt::AscendingOrder);
+
+    const QModelIndex index45 = itemView()->model()->index(45, 0);
+    itemView()->scrollTo(index45);
+    itemView()->setCurrentIndex(index45);
+    const int scrollPosX = itemView()->horizontalScrollBar()->value();
+    const int scrollPosY = itemView()->verticalScrollBar()->value();
+
+    // Save the view state
+    QByteArray viewState;
+    QDataStream saveStream(&viewState, QIODevice::WriteOnly);
+    m_view->saveState(saveStream);
+
+    // Change the URL and then go back
+    m_view->setUrl(m_path + "/51");
+    QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
+    qApp->sendPostedEvents();
+
+    m_view->setUrl(m_path);
+    QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
+    qApp->sendPostedEvents();
+
+    // Verify that the view is scrolled to top and that item 45 is not the current item
+    QVERIFY(itemView()->currentIndex() != index45);
+    QCOMPARE(itemView()->horizontalScrollBar()->value(), 0);
+    QCOMPARE(itemView()->verticalScrollBar()->value(), 0);
+
+    // Change the URL again
+    m_view->setUrl(m_path + "/51");
+    QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
+    qApp->sendPostedEvents();
+
+    // Check that the current item and scroll position are correct if DolphinView::restoreState()
+    // is called after the URL change
+    m_view->setUrl(m_path);
+    QDataStream restoreStream(viewState);
+    m_view->restoreState(restoreStream);
+    QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
+    qApp->sendPostedEvents();
+
+    QCOMPARE(itemView()->currentIndex(), index45);
+    QCOMPARE(itemView()->horizontalScrollBar()->value(), scrollPosX);
+    QCOMPARE(itemView()->verticalScrollBar()->value(), scrollPosY);
+}
+
 /**
  * testKeyboardFocus() checks whether a view grabs the keyboard focus.
  *
  * A view may never grab the keyboard focus itself and must respect the focus-state
- * when switching the view mode.
+ * when switching the view mode, see
+ *
+ * https://bugs.kde.org/show_bug.cgi?id=261147
  */
 
 void DolphinViewTest_AllViewModes::testKeyboardFocus()
 {
     const DolphinView::Mode mode = m_view->mode();
 
+    // Move keyboard focus to another widget. To see that this is needed, run only this test,
+    // i.e., pass 'testKeyboardFocus' as a parameter on the command line.
+    QWidget widget;
+    widget.show();
+    QTest::qWaitForWindowShown(&widget);
+    widget.setFocus();
+
     QVERIFY(!m_view->hasFocus());
+
+    // Switch view modes and verify that the view does not get the focus back
     for (int i = 0; i <= DolphinView::MaxModeEnum; ++i) {
         m_view->setMode(static_cast<DolphinView::Mode>(i));
         QVERIFY(!m_view->hasFocus());