]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/tests/kitemlistcontrollertest.cpp
Merge remote-tracking branch 'origin/KDE/4.10'
[dolphin.git] / src / tests / kitemlistcontrollertest.cpp
index b446e84776428af37a61ae6b3dcddd5a4ce1e2e2..d8f838873766bd26aa64fd5a47e0399523394b98 100644 (file)
 #include <qtestmouse.h>
 #include <qtestkeyboard.h>
 
-#include <KDirLister>
 #include "kitemviews/kitemlistcontainer.h"
 #include "kitemviews/kfileitemlistview.h"
 #include "kitemviews/kfileitemmodel.h"
 #include "kitemviews/kitemlistcontroller.h"
 #include "kitemviews/kitemlistselectionmanager.h"
-#include "kitemviews/kitemlistviewlayouter_p.h"
+#include "kitemviews/private/kitemlistviewlayouter.h"
 #include "testdir.h"
 
+#include <KConfigGroup>
+#include <KGlobalSettings>
+
+#include <QGraphicsSceneMouseEvent>
+
 namespace {
     const int DefaultTimeout = 2000;
 };
 
-Q_DECLARE_METATYPE(KFileItemListView::Layout);
+Q_DECLARE_METATYPE(KFileItemListView::ItemLayout);
 Q_DECLARE_METATYPE(Qt::Orientation);
 Q_DECLARE_METATYPE(KItemListController::SelectionBehavior);
 Q_DECLARE_METATYPE(QSet<int>);
@@ -52,6 +56,7 @@ private slots:
 
     void testKeyboardNavigation_data();
     void testKeyboardNavigation();
+    void testMouseClickActivation();
 
 private:
     /**
@@ -65,7 +70,6 @@ private:
     KItemListController* m_controller;
     KItemListSelectionManager* m_selectionManager;
     KFileItemModel* m_model;
-    KDirLister* m_dirLister;
     TestDir* m_testDir;
     KItemListContainer* m_container;
 };
@@ -80,17 +84,14 @@ void KItemListControllerTest::initTestCase()
     qRegisterMetaType<QSet<int> >("QSet<int>");
 
     m_testDir = new TestDir();
-    m_dirLister = new KDirLister();
-    m_model = new KFileItemModel(m_dirLister);
-    m_container = new KItemListContainer();
+    m_model = new KFileItemModel();
+    m_view = new KFileItemListView();
+    m_controller = new KItemListController(m_model, m_view, this);
+    m_container = new KItemListContainer(m_controller);
     m_controller = m_container->controller();
     m_controller->setSelectionBehavior(KItemListController::MultiSelection);
     m_selectionManager = m_controller->selectionManager();
 
-    m_view = new KFileItemListView();
-    m_controller->setView(m_view);
-    m_controller->setModel(m_model);
-
     QStringList files;
     files
         << "a1" << "a2" << "a3"
@@ -100,8 +101,8 @@ void KItemListControllerTest::initTestCase()
         << "e1" << "e2" << "e3" << "e4" << "e5" << "e6" << "e7";
 
     m_testDir->createFiles(files);
-    m_dirLister->openUrl(m_testDir->url());
-    QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(loadingCompleted()), DefaultTimeout));
+    m_model->loadDirectory(m_testDir->url());
+    QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
 
     m_container->show();
     QTest::qWaitForWindowShown(m_container);
@@ -109,18 +110,8 @@ void KItemListControllerTest::initTestCase()
 
 void KItemListControllerTest::cleanupTestCase()
 {
-    delete m_view;
-    m_view = 0;
-
     delete m_container;
     m_container = 0;
-    m_controller = 0;
-
-    delete m_model;
-    m_model = 0;
-
-    delete m_dirLister;
-    m_dirLister = 0;
 
     delete m_testDir;
     m_testDir = 0;
@@ -193,15 +184,15 @@ Q_DECLARE_METATYPE(QList<keyPressViewStatePair>);
  */
 void KItemListControllerTest::testKeyboardNavigation_data()
 {
-    QTest::addColumn<KFileItemListView::Layout>("layout");
+    QTest::addColumn<KFileItemListView::ItemLayout>("layout");
     QTest::addColumn<Qt::Orientation>("scrollOrientation");
     QTest::addColumn<int>("columnCount");
     QTest::addColumn<KItemListController::SelectionBehavior>("selectionBehavior");
     QTest::addColumn<bool>("groupingEnabled");
     QTest::addColumn<QList<QPair<KeyPress, ViewState> > >("testList");
 
-    QList<KFileItemListView::Layout> layoutList;
-    QHash<KFileItemListView::Layout, QString> layoutNames;
+    QList<KFileItemListView::ItemLayout> layoutList;
+    QHash<KFileItemListView::ItemLayout, QString> layoutNames;
     layoutList.append(KFileItemListView::IconsLayout);
     layoutNames[KFileItemListView::IconsLayout] = "Icons";
     layoutList.append(KFileItemListView::CompactLayout);
@@ -225,7 +216,7 @@ void KItemListControllerTest::testKeyboardNavigation_data()
     groupingEnabledList.append(true);
     groupingEnabledNames[true] = "grouping enabled";
 
-    foreach (KFileItemListView::Layout layout, layoutList) {
+    foreach (const KFileItemListView::ItemLayout& layout, layoutList) {
         // The following settings depend on the layout.
         // Note that 'columns' are actually 'rows' in
         // Compact layout.
@@ -264,8 +255,8 @@ void KItemListControllerTest::testKeyboardNavigation_data()
         }
 
         foreach (int columnCount, columnCountList) {
-            foreach (KItemListController::SelectionBehavior selectionBehavior, selectionBehaviorList) {
-                foreach (bool groupingEnabled, groupingEnabledList) {
+            foreach (const KItemListController::SelectionBehavior& selectionBehavior, selectionBehaviorList) {
+                foreach (bool groupingEnabled, groupingEnabledList) { // krazy:exclude=foreach
                     QList<QPair<KeyPress, ViewState> > testList;
 
                     // First, key presses which should have the same effect
@@ -449,7 +440,7 @@ void KItemListControllerTest::testKeyboardNavigation_data()
  */
 void KItemListControllerTest::testKeyboardNavigation()
 {
-    QFETCH(KFileItemListView::Layout, layout);
+    QFETCH(KFileItemListView::ItemLayout, layout);
     QFETCH(Qt::Orientation, scrollOrientation);
     QFETCH(int, columnCount);
     QFETCH(KItemListController::SelectionBehavior, selectionBehavior);
@@ -521,6 +512,141 @@ void KItemListControllerTest::testKeyboardNavigation()
     }
 }
 
+void KItemListControllerTest::testMouseClickActivation()
+{
+    m_view->setItemLayout(KFileItemListView::IconsLayout);
+
+    // Make sure that we have a large window, such that
+    // the items are visible and clickable.
+    adjustGeometryForColumnCount(5);
+
+    // Make sure that the first item is visible in the view.
+    m_view->setScrollOffset(0);
+    QCOMPARE(m_view->firstVisibleIndex(), 0);
+
+    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");
+
+    QGraphicsSceneMouseEvent mousePressEvent(QEvent::GraphicsSceneMousePress);
+    mousePressEvent.setPos(pos);
+    mousePressEvent.setButton(Qt::LeftButton);
+    mousePressEvent.setButtons(Qt::LeftButton);
+
+    QGraphicsSceneMouseEvent mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease);
+    mouseReleaseEvent.setPos(pos);
+    mouseReleaseEvent.setButton(Qt::LeftButton);
+    mouseReleaseEvent.setButtons(Qt::NoButton);
+
+    QSignalSpy spyItemActivated(m_controller, SIGNAL(itemActivated(int)));
+
+    // 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!", SkipSingle);
+    }
+
+    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!", SkipSingle);
+    }
+
+    m_view->event(&mousePressEvent);
+    m_view->event(&mouseReleaseEvent);
+    QCOMPARE(spyItemActivated.count(), 0);
+    spyItemActivated.clear();
+
+    // Enforce single click activation in the controller.
+    m_controller->setSingleClickActivationEnforced(true);
+    m_view->event(&mousePressEvent);
+    m_view->event(&mouseReleaseEvent);
+    QCOMPARE(spyItemActivated.count(), 1);
+    spyItemActivated.clear();
+
+    // Do not enforce single click activation in the controller.
+    m_controller->setSingleClickActivationEnforced(false);
+    m_view->event(&mousePressEvent);
+    m_view->event(&mouseReleaseEvent);
+    QCOMPARE(spyItemActivated.count(), 0);
+    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!", SkipSingle);
+    }
+
+    m_view->event(&mousePressEvent);
+    m_view->event(&mouseReleaseEvent);
+    QCOMPARE(spyItemActivated.count(), 1);
+    spyItemActivated.clear();
+
+    // Enforce single click activation in the controller.
+    m_controller->setSingleClickActivationEnforced(true);
+    m_view->event(&mousePressEvent);
+    m_view->event(&mouseReleaseEvent);
+    QCOMPARE(spyItemActivated.count(), 1);
+    spyItemActivated.clear();
+
+    // 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!", SkipSingle);
+    }
+}
+
 void KItemListControllerTest::adjustGeometryForColumnCount(int count)
 {
     const QSize size = m_view->itemSize().toSize();