From b8b5e2f502ea20ec470f8d3d8f7b07acdf0e34e1 Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Tue, 4 Apr 2023 14:14:43 -0400
Subject: [PATCH] tests: fix some possible crashes

Prevents some uncaught exceptions that could occur depending on test implementation.

Gitlab: #899
Change-Id: I5b06c270c924071f331e20f3b894a4bb80228137
---
 src/app/calladapter.cpp |  6 +++--
 src/app/lrcinstance.cpp |  7 +++++-
 tests/qml/main.cpp      | 52 ++++++++++++++++++++---------------------
 3 files changed, 35 insertions(+), 30 deletions(-)

diff --git a/src/app/calladapter.cpp b/src/app/calladapter.cpp
index 07bf243df..9ef676e39 100644
--- a/src/app/calladapter.cpp
+++ b/src/app/calladapter.cpp
@@ -51,8 +51,7 @@ CallAdapter::CallAdapter(SystemTray* systemTray, LRCInstance* instance, QObject*
     QML_REGISTERSINGLETONTYPE_POBJECT(NS_MODELS, overlayModel_.get(), "CallOverlayModel");
 
     accountId_ = lrcInstance_->get_currentAccountId();
-    if (!accountId_.isEmpty())
-        connectCallModel(accountId_);
+    connectCallModel(accountId_);
 
     connect(&lrcInstance_->behaviorController(),
             &BehaviorController::showIncomingCallView,
@@ -471,6 +470,9 @@ CallAdapter::showNotification(const QString& accountId, const QString& convUid)
 void
 CallAdapter::connectCallModel(const QString& accountId)
 {
+    if (accountId.isEmpty())
+        return;
+
     auto& accInfo = lrcInstance_->accountModel().getAccountInfo(accountId);
     connect(accInfo.callModel.get(),
             &CallModel::callStarted,
diff --git a/src/app/lrcinstance.cpp b/src/app/lrcinstance.cpp
index 122fb1b05..f9d136929 100644
--- a/src/app/lrcinstance.cpp
+++ b/src/app/lrcinstance.cpp
@@ -44,7 +44,12 @@ LRCInstance::LRCInstance(migrateCallback willMigrateCb,
         accountModel().setTopAccount(currentAccountId_);
         Q_EMIT accountListChanged();
 
-        auto profileInfo = getCurrentAccountInfo().profileInfo;
+        profile::Info profileInfo;
+        try {
+            profileInfo = getCurrentAccountInfo().profileInfo;
+        } catch (...) {
+            return;
+        }
 
         // update type
         set_currentAccountType(profileInfo.type);
diff --git a/tests/qml/main.cpp b/tests/qml/main.cpp
index a8ab7be4d..d7c0165f5 100644
--- a/tests/qml/main.cpp
+++ b/tests/qml/main.cpp
@@ -49,7 +49,12 @@ public:
         : muteDring_(muteDring)
     {}
 
-    void init()
+public Q_SLOTS:
+
+    /*
+     * Called once before qmlEngineAvailable.
+     */
+    void applicationAvailable()
     {
         connectivityMonitor_.reset(new ConnectivityMonitor(this));
         settingsManager_.reset(new AppSettingsManager(this));
@@ -66,28 +71,6 @@ public:
         lrcInstance_->accountModel().downloadDirectory = downloadPath.toString() + "/";
     }
 
-    void registerQmlTypes(QQmlEngine* engine)
-    {
-        // Expose custom types to the QML engine.
-        Utils::registerTypes(engine,
-                             systemTray_.get(),
-                             lrcInstance_.get(),
-                             settingsManager_.get(),
-                             previewEngine_.get(),
-                             &screenInfo_,
-                             this);
-    }
-
-public Q_SLOTS:
-
-    /*
-     * Called once before qmlEngineAvailable.
-     */
-    void applicationAvailable()
-    {
-        init();
-    }
-
     /*
      * Called when the QML engine is available. Any import paths, plugin paths,
      * and extra file selectors will have been set on the engine by this point.
@@ -100,7 +83,17 @@ public Q_SLOTS:
      */
     void qmlEngineAvailable(QQmlEngine* engine)
     {
-        registerQmlTypes(engine);
+        lrcInstance_->set_currentAccountId();
+
+        // Expose custom types to the QML engine.
+        Utils::registerTypes(engine,
+                             systemTray_.get(),
+                             lrcInstance_.get(),
+                             settingsManager_.get(),
+                             previewEngine_.get(),
+                             &screenInfo_,
+                             this);
+
         auto videoProvider = new VideoProvider(lrcInstance_->avModel(), this);
         engine->rootContext()->setContextProperty("videoProvider", videoProvider);
 #ifdef WITH_WEBENGINE
@@ -133,9 +126,14 @@ main(int argc, char** argv)
 {
     QDir tempDir(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
 
-    auto jamiDataDir = tempDir.absolutePath() + "\\jami_test\\jami";
-    auto jamiConfigDir = tempDir.absolutePath() + "\\jami_test\\.config";
-    auto jamiCacheDir = tempDir.absolutePath() + "\\jami_test\\.cache";
+    auto jamiDataDir = tempDir.absolutePath() + "/jami_test/jami";
+    auto jamiConfigDir = tempDir.absolutePath() + "/jami_test/.config";
+    auto jamiCacheDir = tempDir.absolutePath() + "/jami_test/.cache";
+
+    // Clean up the temp directories.
+    QDir(jamiDataDir).removeRecursively();
+    QDir(jamiConfigDir).removeRecursively();
+    QDir(jamiCacheDir).removeRecursively();
 
     bool envSet = qputenv("JAMI_DATA_HOME", jamiDataDir.toLocal8Bit());
     envSet &= qputenv("JAMI_CONFIG_HOME", jamiConfigDir.toLocal8Bit());
-- 
GitLab