]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/tests/kitemlistcontrollertest.cpp
Add unit test for bug 313342
[dolphin.git] / src / tests / kitemlistcontrollertest.cpp
index 47b2b8b7593a344cfb63345a5e358b982ab697b8..8044f9ac946dda66821170d21f9ee4401ecb1e55 100644 (file)
 #include "kitemviews/private/kitemlistviewlayouter.h"
 #include "testdir.h"
 
+#include <KConfigGroup>
+#include <KGlobalSettings>
+
+#include <QGraphicsSceneMouseEvent>
+
 namespace {
     const int DefaultTimeout = 2000;
 };
@@ -51,6 +56,7 @@ private slots:
 
     void testKeyboardNavigation_data();
     void testKeyboardNavigation();
+    void testMouseClickActivation();
 
 private:
     /**
@@ -210,7 +216,7 @@ void KItemListControllerTest::testKeyboardNavigation_data()
     groupingEnabledList.append(true);
     groupingEnabledNames[true] = "grouping enabled";
 
-    foreach (KFileItemListView::ItemLayout layout, layoutList) {
+    foreach (const KFileItemListView::ItemLayout& layout, layoutList) {
         // The following settings depend on the layout.
         // Note that 'columns' are actually 'rows' in
         // Compact layout.
@@ -249,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
@@ -506,6 +512,108 @@ 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.
+    QTest::keyClick(m_container, Qt::Key_End, Qt::NoModifier);
+    QTest::keyClick(m_container, Qt::Key_Home, Qt::NoModifier);
+    while (m_view->firstVisibleIndex() > 0) {
+        QTest::qWait(50);
+    }
+    
+    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);
+    while (!KGlobalSettings::singleClick()) {
+        QTest::qWait(50);
+    }
+    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);
+    while (KGlobalSettings::singleClick()) {
+        QTest::qWait(50);
+    }
+    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);
+    while (!KGlobalSettings::singleClick()) {
+        QTest::qWait(50);
+    }
+    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);
+    while (KGlobalSettings::singleClick() != restoreKGlobalSettingsSingleClick) {
+        QTest::qWait(50);
+    }
+}
+
 void KItemListControllerTest::adjustGeometryForColumnCount(int count)
 {
     const QSize size = m_view->itemSize().toSize();