From 604ec58b34d6450bc1daac62e664a1a85265d6da Mon Sep 17 00:00:00 2001
From: ialokim <ialokim@mailbox.org>
Date: Fri, 16 Sep 2022 21:04:35 +0200
Subject: [PATCH] use separate Preference node for test builds
 (UnencryptedSettings)

---
 .../briar/desktop/DesktopCoreModule.kt        | 26 +----------
 .../desktop/settings/DesktopSettingsModule.kt | 46 +++++++++++++++++++
 .../settings/UnencryptedSettingsImpl.kt       | 11 +++--
 .../RunWithMultipleTemporaryAccounts.kt       |  8 ++--
 .../briar/desktop/RunWithTemporaryAccount.kt  |  8 ++--
 5 files changed, 66 insertions(+), 33 deletions(-)
 create mode 100644 briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/settings/DesktopSettingsModule.kt

diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/DesktopCoreModule.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/DesktopCoreModule.kt
index 5464f843ee..21e278a761 100644
--- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/DesktopCoreModule.kt
+++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/DesktopCoreModule.kt
@@ -60,12 +60,7 @@ import org.briarproject.briar.desktop.notification.linux.LibnotifyNotificationPr
 import org.briarproject.briar.desktop.notification.windows.Toast4jNotificationProvider
 import org.briarproject.briar.desktop.settings.Configuration
 import org.briarproject.briar.desktop.settings.ConfigurationImpl
-import org.briarproject.briar.desktop.settings.EncryptedSettings
-import org.briarproject.briar.desktop.settings.EncryptedSettingsImpl
-import org.briarproject.briar.desktop.settings.EncryptedSettingsReadOnly
-import org.briarproject.briar.desktop.settings.UnencryptedSettings
-import org.briarproject.briar.desktop.settings.UnencryptedSettingsImpl
-import org.briarproject.briar.desktop.settings.UnencryptedSettingsReadOnly
+import org.briarproject.briar.desktop.settings.DesktopSettingsModule
 import org.briarproject.briar.desktop.threading.BriarExecutors
 import org.briarproject.briar.desktop.threading.BriarExecutorsImpl
 import org.briarproject.briar.desktop.threading.UiExecutor
@@ -95,6 +90,7 @@ import javax.swing.SwingUtilities.isEventDispatchThread
         DefaultTaskSchedulerModule::class,
         DefaultWakefulIoExecutorModule::class,
         DesktopSecureRandomModule::class,
+        DesktopSettingsModule::class,
         JavaNetworkModule::class,
         JavaSystemModule::class,
         SocksModule::class,
@@ -124,24 +120,6 @@ internal class DesktopCoreModule(
         return DesktopDatabaseConfig(dbDir, keyDir)
     }
 
-    @Provides
-    @Singleton
-    fun provideUnencryptedSettings(settings: UnencryptedSettingsImpl): UnencryptedSettings = settings
-
-    @Provides
-    @Singleton
-    // provide [UnencryptedSettings] singleton itself as provided above to use same object
-    fun provideUnencryptedSettingsReadOnly(settings: UnencryptedSettings): UnencryptedSettingsReadOnly = settings
-
-    @Provides
-    @Singleton
-    fun provideEncryptedSettings(settings: EncryptedSettingsImpl): EncryptedSettings = settings
-
-    @Provides
-    @Singleton
-    // provide [EncryptedSettings] singleton itself as provided above to use same object
-    fun provideEncryptedSettingsReadOnly(settings: EncryptedSettings): EncryptedSettingsReadOnly = settings
-
     @Provides
     @Singleton
     @EventExecutor
diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/settings/DesktopSettingsModule.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/settings/DesktopSettingsModule.kt
new file mode 100644
index 0000000000..cf44d023f3
--- /dev/null
+++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/settings/DesktopSettingsModule.kt
@@ -0,0 +1,46 @@
+/*
+ * Briar Desktop
+ * Copyright (C) 2021-2022 The Briar Project
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package org.briarproject.briar.desktop.settings
+
+import dagger.Module
+import dagger.Provides
+import javax.inject.Singleton
+
+@Module
+class DesktopSettingsModule(
+    private val unencryptedSettingsPostfix: String? = null,
+) {
+    @Provides
+    @Singleton
+    fun provideUnencryptedSettings(): UnencryptedSettings = UnencryptedSettingsImpl(unencryptedSettingsPostfix)
+
+    @Provides
+    @Singleton
+    // provide [UnencryptedSettings] singleton itself as provided above to use same object
+    fun provideUnencryptedSettingsReadOnly(settings: UnencryptedSettings): UnencryptedSettingsReadOnly = settings
+
+    @Provides
+    @Singleton
+    fun provideEncryptedSettings(settings: EncryptedSettingsImpl): EncryptedSettings = settings
+
+    @Provides
+    @Singleton
+    // provide [EncryptedSettings] singleton itself as provided above to use same object
+    fun provideEncryptedSettingsReadOnly(settings: EncryptedSettings): EncryptedSettingsReadOnly = settings
+}
diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/settings/UnencryptedSettingsImpl.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/settings/UnencryptedSettingsImpl.kt
index a3c9afcfd0..5987373807 100644
--- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/settings/UnencryptedSettingsImpl.kt
+++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/settings/UnencryptedSettingsImpl.kt
@@ -28,21 +28,26 @@ import org.briarproject.briar.desktop.utils.InternationalizationUtils
 import org.briarproject.briar.desktop.utils.KLoggerUtils.e
 import org.briarproject.briar.desktop.viewmodel.SingleStateEvent
 import java.util.prefs.Preferences
-import javax.inject.Inject
 import kotlin.reflect.KProperty
 
 const val PREF_THEME = "theme" // NON-NLS
 const val PREF_LANG = "language" // NON-NLS
 const val PREF_UI_SCALE = "uiScale" // NON-NLS
 
-class UnencryptedSettingsImpl @Inject internal constructor() : UnencryptedSettings {
+class UnencryptedSettingsImpl(
+    unencryptedSettingsPostfix: String? = null,
+) : UnencryptedSettings {
 
     companion object {
         private val LOG = KotlinLogging.logger {}
     }
 
     // used for unencrypted settings, namely theme, language and UI scale factor
-    private val prefs = Preferences.userNodeForPackage(this::class.java)
+    private val prefs = Preferences.userNodeForPackage(this::class.java).let { node ->
+        if (unencryptedSettingsPostfix != null)
+            node.node(unencryptedSettingsPostfix)
+        else node
+    }
 
     override val invalidateScreen = SingleStateEvent<Unit>()
 
diff --git a/briar-desktop/src/test/kotlin/org/briarproject/briar/desktop/RunWithMultipleTemporaryAccounts.kt b/briar-desktop/src/test/kotlin/org/briarproject/briar/desktop/RunWithMultipleTemporaryAccounts.kt
index eaeb3f74fb..6a8fbe6d02 100644
--- a/briar-desktop/src/test/kotlin/org/briarproject/briar/desktop/RunWithMultipleTemporaryAccounts.kt
+++ b/briar-desktop/src/test/kotlin/org/briarproject/briar/desktop/RunWithMultipleTemporaryAccounts.kt
@@ -28,6 +28,7 @@ import org.briarproject.bramble.api.plugin.TorConstants.DEFAULT_CONTROL_PORT
 import org.briarproject.bramble.api.plugin.TorConstants.DEFAULT_SOCKS_PORT
 import org.briarproject.briar.BriarCoreEagerSingletons
 import org.briarproject.briar.desktop.TestUtils.getDataDir
+import org.briarproject.briar.desktop.settings.DesktopSettingsModule
 import org.briarproject.briar.desktop.utils.KLoggerUtils.i
 import org.briarproject.briar.desktop.utils.KLoggerUtils.w
 import org.briarproject.briar.desktop.utils.LogUtils
@@ -78,9 +79,10 @@ internal class RunWithMultipleTemporaryAccounts(
         LOG.i { "Using data directory '$dataDir'" }
 
         val app =
-            DaggerBriarDesktopTestApp.builder().desktopCoreModule(
-                DesktopCoreModule(dataDir, socksPort, controlPort)
-            ).build()
+            DaggerBriarDesktopTestApp.builder()
+                .desktopCoreModule(DesktopCoreModule(dataDir, socksPort, controlPort))
+                .desktopSettingsModule(DesktopSettingsModule("test"))
+                .build()
 
         app.getShutdownManager().addShutdownHook {
             LOG.i { "deleting temporary account at $dataDir" }
diff --git a/briar-desktop/src/test/kotlin/org/briarproject/briar/desktop/RunWithTemporaryAccount.kt b/briar-desktop/src/test/kotlin/org/briarproject/briar/desktop/RunWithTemporaryAccount.kt
index 7b50b7493b..7d37c4c0a0 100644
--- a/briar-desktop/src/test/kotlin/org/briarproject/briar/desktop/RunWithTemporaryAccount.kt
+++ b/briar-desktop/src/test/kotlin/org/briarproject/briar/desktop/RunWithTemporaryAccount.kt
@@ -29,6 +29,7 @@ import org.briarproject.bramble.api.lifecycle.Service
 import org.briarproject.bramble.api.lifecycle.ServiceException
 import org.briarproject.briar.BriarCoreEagerSingletons
 import org.briarproject.briar.desktop.TestUtils.getDataDir
+import org.briarproject.briar.desktop.settings.DesktopSettingsModule
 import org.briarproject.briar.desktop.utils.KLoggerUtils.i
 import org.briarproject.briar.desktop.utils.KLoggerUtils.w
 import org.briarproject.briar.desktop.utils.LogUtils
@@ -61,9 +62,10 @@ internal class RunWithTemporaryAccount(
         }
 
         val app =
-            DaggerBriarDesktopTestApp.builder().desktopCoreModule(
-                DesktopCoreModule(dataDir)
-            ).build()
+            DaggerBriarDesktopTestApp.builder()
+                .desktopCoreModule(DesktopCoreModule(dataDir))
+                .desktopSettingsModule(DesktopSettingsModule("test"))
+                .build()
 
         app.getShutdownManager().addShutdownHook {
             LOG.i { "deleting temporary account at $dataDir" }
-- 
GitLab