feat: adding dependencies to try and get android tests working, fixing bug with initial config not syncing properly

This commit is contained in:
0x330a 2023-04-18 22:50:06 +10:00
parent fdff11c535
commit c445267ed9
7 changed files with 70 additions and 7 deletions

View File

@ -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.

View File

@ -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<ByteArray, String>? {
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())
}

View File

@ -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)
}
}

View File

@ -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";

View File

@ -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
}

View File

@ -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)
}

@ -1 +1 @@
Subproject commit 057318136c69f582b7fd78a39ee26686d1d5a3ec
Subproject commit 2c18eced69f464ed96aa5bb6ec7c4c6f815e17fe