diff --git a/app/build.gradle b/app/build.gradle index 02928b761a..bd5b7e337a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -127,6 +127,10 @@ dependencies { testImplementation 'org.assertj:assertj-core:3.11.1' testImplementation "org.mockito:mockito-inline:4.10.0" testImplementation "org.mockito.kotlin:mockito-kotlin:$mockitoKotlinVersion" + + androidTestImplementation "com.google.dagger:hilt-android-testing:$daggerVersion" + kaptAndroidTest "com.google.dagger:hilt-android-compiler:$daggerVersion" + androidTestImplementation "org.mockito:mockito-android:4.10.0" androidTestImplementation "org.mockito.kotlin:mockito-kotlin:$mockitoKotlinVersion" testImplementation "androidx.test:core:$testCoreVersion" @@ -222,7 +226,7 @@ android { buildConfigField "int", "CANONICAL_VERSION_CODE", "$canonicalVersionCode" resConfigs autoResConfig() - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + testInstrumentationRunner "network.loki.messenger.util.HiltApplicationRunner" // The following argument makes the Android Test Orchestrator run its // "pm clear" command after each test invocation. This command ensures // that the app's state is completely cleared between tests. diff --git a/app/src/androidTest/java/network/loki/messenger/LibSessionTests.kt b/app/src/androidTest/java/network/loki/messenger/LibSessionTests.kt index a47175f5d7..0cf7ad399d 100644 --- a/app/src/androidTest/java/network/loki/messenger/LibSessionTests.kt +++ b/app/src/androidTest/java/network/loki/messenger/LibSessionTests.kt @@ -3,15 +3,21 @@ package network.loki.messenger import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.SmallTest import androidx.test.platform.app.InstrumentationRegistry +import dagger.hilt.android.testing.CustomTestApplication +import dagger.hilt.android.testing.HiltAndroidRule +import dagger.hilt.android.testing.HiltAndroidTest +import network.loki.messenger.libsession_util.ConfigBase import network.loki.messenger.libsession_util.Contacts import network.loki.messenger.libsession_util.util.Contact import network.loki.messenger.libsession_util.util.ExpiryMode import org.junit.Before +import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.mockito.kotlin.any import org.mockito.kotlin.spy import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever import org.session.libsession.messaging.MessagingModuleConfiguration import org.session.libsession.utilities.TextSecurePreferences import org.session.libsignal.utilities.KeyHelper @@ -20,14 +26,25 @@ import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.crypto.KeyPairUtilities import kotlin.random.Random +@CustomTestApplication(ApplicationContext::class) +interface HiltApplicationContext + @RunWith(AndroidJUnit4::class) @SmallTest +@HiltAndroidTest class LibSessionTests { + @get:Rule + val hiltRule = HiltAndroidRule(this) + private fun randomSeedBytes() = (0 until 16).map { Random.nextInt(UByte.MAX_VALUE.toInt()).toByte() } private fun randomKeyPair() = KeyPairUtilities.generate(randomSeedBytes().toByteArray()) private fun randomSessionId() = randomKeyPair().x25519KeyPair.hexEncodedPublicKey + private var fakeHashI = 0 + private val nextFakeHash: String + get() = "fakehash${fakeHashI++}" + private fun maybeGetUserInfo(): Pair? { val appContext = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as ApplicationContext val prefs = appContext.prefs @@ -46,7 +63,12 @@ class LibSessionTests { contactList.forEach { contact -> contacts.set(contact) } - return contacts.dump() + return contacts.push().config + } + + private fun fakePollNewConfig(configBase: ConfigBase, toMerge: ByteArray) { + configBase.merge(nextFakeHash to toMerge) + MessagingModuleConfiguration.shared.configFactory.persist(configBase) } @Before @@ -65,6 +87,7 @@ class LibSessionTests { @Test fun migration_one_to_ones() { val storageSpy = spy(MessagingModuleConfiguration.shared.storage) + whenever(MessagingModuleConfiguration.shared.storage).thenReturn(storageSpy) val newContactId = randomSessionId() val singleContact = Contact( @@ -73,8 +96,8 @@ class LibSessionTests { expiryMode = ExpiryMode.NONE ) val newContactMerge = buildContactMessage(listOf(singleContact)) - MessagingModuleConfiguration.shared.configFactory.contacts!!.merge("abc123" to newContactMerge) - + val contacts = MessagingModuleConfiguration.shared.configFactory.contacts!! + fakePollNewConfig(contacts, newContactMerge) verify(storageSpy).addLibSessionContacts(any()) } diff --git a/app/src/androidTest/java/network/loki/messenger/util/HiltApplicationRunner.kt b/app/src/androidTest/java/network/loki/messenger/util/HiltApplicationRunner.kt new file mode 100644 index 0000000000..39570c3d2e --- /dev/null +++ b/app/src/androidTest/java/network/loki/messenger/util/HiltApplicationRunner.kt @@ -0,0 +1,17 @@ +package network.loki.messenger.util + +import android.app.Application +import android.content.Context +import androidx.test.runner.AndroidJUnitRunner +import network.loki.messenger.HiltApplicationContext + +class HiltApplicationRunner: AndroidJUnitRunner() { + + override fun newApplication( + cl: ClassLoader?, + className: String?, + context: Context? + ): Application { + return super.newApplication(cl, HiltApplicationContext::class.java.name, context) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java index 8af45e7a92..a47871f59e 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java +++ b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java @@ -127,7 +127,7 @@ import network.loki.messenger.libsession_util.UserProfile; * @author Moxie Marlinspike */ @HiltAndroidApp -public class ApplicationContext extends Application implements DefaultLifecycleObserver, ConfigFactoryUpdateListener { +public class ApplicationContext extends BaseApplication implements DefaultLifecycleObserver, ConfigFactoryUpdateListener { public static final String PREFERENCES_NAME = "SecureSMS-Preferences"; diff --git a/app/src/main/java/org/thoughtcrime/securesms/BaseApplication.kt b/app/src/main/java/org/thoughtcrime/securesms/BaseApplication.kt new file mode 100644 index 0000000000..7bb711ab1d --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/BaseApplication.kt @@ -0,0 +1,19 @@ +package org.thoughtcrime.securesms + +import android.app.Application +import org.session.libsession.database.MessageDataProvider +import org.session.libsession.utilities.TextSecurePreferences +import org.thoughtcrime.securesms.database.JobDatabase +import org.thoughtcrime.securesms.database.LokiAPIDatabase +import org.thoughtcrime.securesms.database.Storage +import org.thoughtcrime.securesms.dependencies.ConfigFactory +import javax.inject.Inject + +abstract class BaseApplication: Application() { + lateinit var lokiAPIDatabase: LokiAPIDatabase + lateinit var storage: Storage + lateinit var messageDataProvider: MessageDataProvider + lateinit var jobDatabase: JobDatabase + lateinit var textSecurePreferences: TextSecurePreferences + lateinit var configFactory: ConfigFactory +} \ No newline at end of file diff --git a/app/src/main/java/org/thoughtcrime/securesms/home/HomeActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/home/HomeActivity.kt index 9bd49c4431..922e1f69e7 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/home/HomeActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/home/HomeActivity.kt @@ -360,7 +360,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity(), // TODO: remove this after enough updates that we can rely on ConfigBase.isNewConfigEnabled to always return true // This will only run if we aren't using new configs, as they are schedule to sync when there are changes applied - if (textSecurePreferences.getConfigurationMessageSynced() && !ConfigBase.isNewConfigEnabled) { + if (textSecurePreferences.getConfigurationMessageSynced()) { lifecycleScope.launch(Dispatchers.IO) { ConfigurationMessageUtilities.syncConfigurationIfNeeded(this@HomeActivity) } diff --git a/libsession-util/libsession-util b/libsession-util/libsession-util index 057318136c..2c18eced69 160000 --- a/libsession-util/libsession-util +++ b/libsession-util/libsession-util @@ -1 +1 @@ -Subproject commit 057318136c69f582b7fd78a39ee26686d1d5a3ec +Subproject commit 2c18eced69f464ed96aa5bb6ec7c4c6f815e17fe