]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/tests/kitemlistcontrollertest.cpp
Compile without foreach
[dolphin.git] / src / tests / kitemlistcontrollertest.cpp
index cd4d0a681b43dae9838f2f176def2ae5300442ff..80b88c03eea90e3a79f3fdefc18a381984beacd8 100644 (file)
@@ -1,21 +1,8 @@
-/***************************************************************************
- *   Copyright (C) 2012 by Frank Reininghaus <frank78ac@googlemail.com>    *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program; if not, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
- ***************************************************************************/
+/*
+ * SPDX-FileCopyrightText: 2012 Frank Reininghaus <frank78ac@googlemail.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
 
 #include "kitemviews/kitemlistcontainer.h"
 #include "kitemviews/kfileitemlistview.h"
 #include "kitemviews/private/kitemlistviewlayouter.h"
 #include "testdir.h"
 
-#include <KConfigGroup>
-#include <KGlobalSettings>
-
 #include <QTest>
 #include <QGraphicsSceneMouseEvent>
 #include <QSignalSpy>
+#include <QProxyStyle>
+
+/**
+ * \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);
+Q_DECLARE_METATYPE(KFileItemListView::ItemLayout)
+Q_DECLARE_METATYPE(Qt::Orientation)
+Q_DECLARE_METATYPE(KItemListController::SelectionBehavior)
+Q_DECLARE_METATYPE(KItemSet)
 
 class KItemListControllerTest : public QObject
 {
@@ -66,6 +93,7 @@ private:
     KFileItemModel* m_model;
     TestDir* m_testDir;
     KItemListContainer* m_container;
+    KItemListControllerTestStyle* m_testStyle;
 };
 
 /**
@@ -85,6 +113,8 @@ void KItemListControllerTest::initTestCase()
     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
@@ -96,20 +126,20 @@ void KItemListControllerTest::initTestCase()
 
     m_testDir->createFiles(files);
     m_model->loadDirectory(m_testDir->url());
-    QSignalSpy spyDirectoryLoadingCompleted(m_model, SIGNAL(directoryLoadingCompleted()));
+    QSignalSpy spyDirectoryLoadingCompleted(m_model, &KFileItemModel::directoryLoadingCompleted);
     QVERIFY(spyDirectoryLoadingCompleted.wait());
 
     m_container->show();
-    QTest::qWaitForWindowShown(m_container);
+    QVERIFY(QTest::qWaitForWindowExposed(m_container));
 }
 
 void KItemListControllerTest::cleanupTestCase()
 {
     delete m_container;
-    m_container = 0;
+    m_container = nullptr;
 
     delete m_testDir;
-    m_testDir = 0;
+    m_testDir = nullptr;
 }
 
 /** Before each test, the current item, selection, and item size are reset to the defaults. */
@@ -154,7 +184,7 @@ struct KeyPress {
  */
 struct ViewState {
 
-    ViewState(int current, const KItemSet selection, bool activated = false) :
+    ViewState(int current, const KItemSet &selection, bool activated = false) :
         m_current(current),
         m_selection(selection),
         m_activated(activated)
@@ -167,7 +197,7 @@ struct ViewState {
 
 // 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>);
+Q_DECLARE_METATYPE(QList<keyPressViewStatePair>)
 
 /**
  * This function provides the data for the actual test function
@@ -211,7 +241,7 @@ void KItemListControllerTest::testKeyboardNavigation_data()
     groupingEnabledList.append(true);
     groupingEnabledNames[true] = "grouping enabled";
 
-    foreach (const KFileItemListView::ItemLayout& layout, layoutList) {
+    for (const KFileItemListView::ItemLayout& layout : layoutList) {
         // The following settings depend on the layout.
         // Note that 'columns' are actually 'rows' in
         // Compact layout.
@@ -249,9 +279,9 @@ void KItemListControllerTest::testKeyboardNavigation_data()
             break;
         }
 
-        foreach (int columnCount, columnCountList) {
-            foreach (const KItemListController::SelectionBehavior& selectionBehavior, selectionBehaviorList) {
-                foreach (bool groupingEnabled, groupingEnabledList) { // krazy:exclude=foreach
+        for (int columnCount : qAsConst(columnCountList)) {
+            for (const KItemListController::SelectionBehavior& selectionBehavior : qAsConst(selectionBehaviorList)) {
+                for (bool groupingEnabled : qAsConst(groupingEnabledList)) {
                     QList<QPair<KeyPress, ViewState> > testList;
 
                     // First, key presses which should have the same effect
@@ -283,6 +313,12 @@ void KItemListControllerTest::testKeyboardNavigation_data()
                         << 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(Qt::Key_E), 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_Home), ViewState(0, KItemSet() << 0))
                         << qMakePair(KeyPress(Qt::Key_Escape), ViewState(0, KItemSet()));
 
@@ -418,7 +454,7 @@ void KItemListControllerTest::testKeyboardNavigation_data()
                         selectionBehaviorNames[selectionBehavior] + ", " +
                         groupingEnabledNames[groupingEnabled];
 
-                    const QByteArray testNameAscii = testName.toAscii();
+                    const QByteArray testNameAscii = testName.toLatin1();
 
                     QTest::newRow(testNameAscii.data())
                         << layout
@@ -464,8 +500,8 @@ void KItemListControllerTest::testKeyboardNavigation()
     adjustGeometryForColumnCount(columnCount);
     QCOMPARE(m_view->m_layouter->m_columnCount, columnCount);
 
-    QSignalSpy spySingleItemActivated(m_controller, SIGNAL(itemActivated(int)));
-    QSignalSpy spyMultipleItemsActivated(m_controller, SIGNAL(itemsActivated(KItemSet)));
+    QSignalSpy spySingleItemActivated(m_controller, &KItemListController::itemActivated);
+    QSignalSpy spyMultipleItemsActivated(m_controller, &KItemListController::itemsActivated);
 
     while (!testList.isEmpty()) {
         const QPair<KeyPress, ViewState> test = testList.takeFirst();
@@ -502,6 +538,7 @@ void KItemListControllerTest::testKeyboardNavigation()
                 }
                 // No items are selected. Therefore, the current item should be activated.
                 // This is handled by falling through to the NoSelection/SingleSelection case.
+                Q_FALLTHROUGH();
             case KItemListController::NoSelection:
             case KItemListController::SingleSelection:
                 // In NoSelection and SingleSelection mode, the current item should be activated.
@@ -529,10 +566,7 @@ void KItemListControllerTest::testMouseClickActivation()
     const QPointF pos = m_view->itemContextRect(0).center();
 
     // Save the "single click" setting.
-    const bool restoreKGlobalSettingsSingleClick = KGlobalSettings::singleClick();
-
-    KConfig config("kcminputrc");
-    KConfigGroup group = config.group("KDE");
+    const bool restoreSettingsSingleClick = m_testStyle->activateItemOnSingleClick();
 
     QGraphicsSceneMouseEvent mousePressEvent(QEvent::GraphicsSceneMousePress);
     mousePressEvent.setPos(pos);
@@ -544,46 +578,17 @@ void KItemListControllerTest::testMouseClickActivation()
     mouseReleaseEvent.setButton(Qt::LeftButton);
     mouseReleaseEvent.setButtons(Qt::NoButton);
 
-    QSignalSpy spyItemActivated(m_controller, SIGNAL(itemActivated(int)));
+    QSignalSpy spyItemActivated(m_controller, &KItemListController::itemActivated);
 
     // Default setting: single click activation.
-    group.writeEntry("SingleClick", true, KConfig::Persistent|KConfig::Global);
-    config.sync();
-    KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged, KGlobalSettings::SETTINGS_MOUSE);
-
-    int iterations = 0;
-    const int maxIterations = 20;
-    while (!KGlobalSettings::singleClick() && iterations < maxIterations) {
-        QTest::qWait(50);
-        ++iterations;
-    }
-
-    if (!KGlobalSettings::singleClick()) {
-        // TODO: Try to find a way to make sure that changing the global setting works.
-        QSKIP("Failed to change the KGlobalSettings::singleClick() setting!");
-    }
-
+    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".
-    group.writeEntry("SingleClick", false, KConfig::Persistent|KConfig::Global);
-    config.sync();
-    KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged, KGlobalSettings::SETTINGS_MOUSE);
-
-    iterations = 0;
-    while (KGlobalSettings::singleClick() && iterations < maxIterations) {
-        QTest::qWait(50);
-        ++iterations;
-    }
-
-    if (KGlobalSettings::singleClick()) {
-        // TODO: Try to find a way to make sure that changing the global setting works.
-        QSKIP("Failed to change the KGlobalSettings::singleClick() setting!");
-    }
-
+    m_testStyle->setActivateItemOnSingleClick(false);
     m_view->event(&mousePressEvent);
     m_view->event(&mouseReleaseEvent);
     QCOMPARE(spyItemActivated.count(), 0);
@@ -604,21 +609,7 @@ void KItemListControllerTest::testMouseClickActivation()
     spyItemActivated.clear();
 
     // Set the global setting back to "single click activation".
-    group.writeEntry("SingleClick", true, KConfig::Persistent|KConfig::Global);
-    config.sync();
-    KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged, KGlobalSettings::SETTINGS_MOUSE);
-
-    iterations = 0;
-    while (!KGlobalSettings::singleClick() && iterations < maxIterations) {
-        QTest::qWait(50);
-        ++iterations;
-    }
-
-    if (!KGlobalSettings::singleClick()) {
-        // TODO: Try to find a way to make sure that changing the global setting works.
-        QSKIP("Failed to change the KGlobalSettings::singleClick() setting!");
-    }
-
+    m_testStyle->setActivateItemOnSingleClick(true);
     m_view->event(&mousePressEvent);
     m_view->event(&mouseReleaseEvent);
     QCOMPARE(spyItemActivated.count(), 1);
@@ -633,20 +624,7 @@ void KItemListControllerTest::testMouseClickActivation()
 
     // Restore previous settings.
     m_controller->setSingleClickActivationEnforced(true);
-    group.writeEntry("SingleClick", restoreKGlobalSettingsSingleClick, KConfig::Persistent|KConfig::Global);
-    config.sync();
-    KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged, KGlobalSettings::SETTINGS_MOUSE);
-
-    iterations = 0;
-    while (KGlobalSettings::singleClick() != restoreKGlobalSettingsSingleClick && iterations < maxIterations) {
-        QTest::qWait(50);
-        ++iterations;
-    }
-
-    if (KGlobalSettings::singleClick() != restoreKGlobalSettingsSingleClick) {
-        // TODO: Try to find a way to make sure that changing the global setting works.
-        QSKIP("Failed to change the KGlobalSettings::singleClick() setting!");
-    }
+    m_testStyle->setActivateItemOnSingleClick(restoreSettingsSingleClick);
 }
 
 void KItemListControllerTest::adjustGeometryForColumnCount(int count)