]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/tests/dolphinmainwindowtest.cpp
DolphinMainWindowTest: Add unit test for autosave session feature
[dolphin.git] / src / tests / dolphinmainwindowtest.cpp
index 009008002df723bbca3e3e4ee733a184e233961f..60d46518a0262c43dabffc2d18a64e33288aa4cf 100644 (file)
 #include "testdir.h"
 
 #include <KActionCollection>
+#include <KConfig>
+#include <KConfigGui>
 
+#include <QAccessible>
+#include <QFileSystemWatcher>
 #include <QScopedPointer>
 #include <QSignalSpy>
 #include <QStandardPaths>
 #include <QTest>
 
+#include <set>
+
 class DolphinMainWindowTest : public QObject
 {
     Q_OBJECT
@@ -39,6 +45,8 @@ private Q_SLOTS:
     void testPlacesPanelWidthResistance();
     void testGoActions();
     void testOpenFiles();
+    void testAccessibilityAncestorTree();
+    void testAutoSaveSession();
     void cleanupTestCase();
 
 private:
@@ -524,6 +532,74 @@ void DolphinMainWindowTest::testOpenFiles()
     QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
 }
 
+void DolphinMainWindowTest::testAccessibilityAncestorTree()
+{
+    m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
+    m_mainWindow->show();
+    QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
+    QVERIFY(m_mainWindow->isVisible());
+
+    std::set<const QObject *> testedObjects; // Makes sure we stop testing if we arrive at an item that was already tested.
+    QAccessibleInterface *accessibleInterfaceOfMainWindow = QAccessible::queryAccessibleInterface(m_mainWindow.get());
+    Q_CHECK_PTR(accessibleInterfaceOfMainWindow);
+
+    // We will do accessibility checks for every object that gets focus. Focus will be changed using the Tab key.
+    while (qApp->focusObject() && !testedObjects.count(qApp->focusObject())) {
+        const auto currentlyFocusedObject = qApp->focusObject();
+        QAccessibleInterface *accessibleInterface = QAccessible::queryAccessibleInterface(currentlyFocusedObject);
+
+        // The accessibleInterfaces of focused objects might themselves have children.
+        // We go down that hierarchy as far as possible and then test the ancestor tree from there.
+        while (accessibleInterface->childCount() > 0) {
+            accessibleInterface = accessibleInterface->child(0);
+        }
+        while (accessibleInterface != accessibleInterfaceOfMainWindow) {
+            QVERIFY2(accessibleInterface,
+                     qPrintable(QString("%1's accessibleInterface or one of its accessible children doesn't have the main window as an ancestor.")
+                                    .arg(currentlyFocusedObject->metaObject()->className())));
+            accessibleInterface = accessibleInterface->parent();
+        }
+
+        testedObjects.insert(currentlyFocusedObject); // Add it to testedObjects so we won't test it again later.
+        QTest::keyClick(m_mainWindow.get(), Qt::Key::Key_Tab, Qt::ShiftModifier); // ShiftModifier because the Tab cycle is currently broken going forward.
+    }
+}
+
+void DolphinMainWindowTest::testAutoSaveSession()
+{
+    m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
+    m_mainWindow->show();
+    QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
+    QVERIFY(m_mainWindow->isVisible());
+
+    // Create config file
+    KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
+    KConfig *config = KConfigGui::sessionConfig();
+    m_mainWindow->saveGlobalProperties(config);
+    m_mainWindow->savePropertiesInternal(config, 1);
+    config->sync();
+
+    // Setup watcher for config file changes
+    const QString configFileName = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/" + KConfigGui::sessionConfig()->name();
+    QFileSystemWatcher *configWatcher = new QFileSystemWatcher({configFileName}, this);
+    QSignalSpy spySessionSaved(configWatcher, &QFileSystemWatcher::fileChanged);
+
+    // Enable session autosave.
+    m_mainWindow->setSessionAutoSaveEnabled(true);
+
+    // Open a new tab
+    auto tabWidget = m_mainWindow->findChild<DolphinTabWidget *>("tabWidget");
+    QVERIFY(tabWidget);
+    tabWidget->openNewActivatedTab(QUrl::fromLocalFile(QDir::tempPath()));
+    QCOMPARE(tabWidget->count(), 2);
+
+    // Wait till a session save occurs
+    QVERIFY(spySessionSaved.wait(60000));
+
+    // Disable session autosave.
+    m_mainWindow->setSessionAutoSaveEnabled(false);
+}
+
 void DolphinMainWindowTest::cleanupTestCase()
 {
     m_mainWindow->showNormal();