Use a TestApp for UnitTests so we can use different modules for injection

This commit is contained in:
Torsten Grote 2021-02-15 12:16:07 -03:00 committed by Chirayu Desai
parent e8fbc3fbe7
commit 6a5eaea2fd
5 changed files with 79 additions and 20 deletions

View file

@ -34,7 +34,7 @@ import org.koin.dsl.module
* @author Steve Soltys
* @author Torsten Grote
*/
class App : Application() {
open class App : Application() {
private val appModule = module {
single { SettingsManager(this@App) }
@ -52,22 +52,7 @@ class App : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
androidLogger()
androidContext(this@App)
modules(
listOf(
cryptoModule,
headerModule,
metadataModule,
documentsProviderModule, // storage plugin
backupModule,
restoreModule,
installModule,
appModule
)
)
}
startKoin()
if (isDebugBuild()) {
StrictMode.setThreadPolicy(
StrictMode.ThreadPolicy.Builder()
@ -88,6 +73,23 @@ class App : Application() {
}
}
protected open fun startKoin() = startKoin {
androidLogger()
androidContext(this@App)
modules(
listOf(
cryptoModule,
headerModule,
metadataModule,
documentsProviderModule, // storage plugin
backupModule,
restoreModule,
installModule,
appModule
)
)
}
private val settingsManager: SettingsManager by inject()
private val metadataManager: MetadataManager by inject()

View file

@ -0,0 +1,45 @@
package com.stevesoltys.seedvault
import com.stevesoltys.seedvault.crypto.CipherFactory
import com.stevesoltys.seedvault.crypto.CipherFactoryImpl
import com.stevesoltys.seedvault.crypto.Crypto
import com.stevesoltys.seedvault.crypto.CryptoImpl
import com.stevesoltys.seedvault.crypto.KeyManager
import com.stevesoltys.seedvault.crypto.KeyManagerTestImpl
import com.stevesoltys.seedvault.header.headerModule
import com.stevesoltys.seedvault.metadata.metadataModule
import com.stevesoltys.seedvault.plugins.saf.documentsProviderModule
import com.stevesoltys.seedvault.restore.install.installModule
import com.stevesoltys.seedvault.transport.backup.backupModule
import com.stevesoltys.seedvault.transport.restore.restoreModule
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin
import org.koin.dsl.module
class TestApp : App() {
private val testCryptoModule = module {
factory<CipherFactory> { CipherFactoryImpl(get()) }
single<KeyManager> { KeyManagerTestImpl() }
single<Crypto> { CryptoImpl(get(), get(), get()) }
}
private val appModule = module {
single { Clock() }
}
override fun startKoin() = startKoin {
androidContext(this@TestApp)
modules(
listOf(
testCryptoModule,
headerModule,
metadataModule,
documentsProviderModule, // storage plugin
backupModule,
restoreModule,
installModule,
appModule
)
)
}
}

View file

@ -8,6 +8,7 @@ import android.content.pm.ApplicationInfo.FLAG_SYSTEM
import android.content.pm.PackageInfo
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.stevesoltys.seedvault.Clock
import com.stevesoltys.seedvault.TestApp
import com.stevesoltys.seedvault.getRandomByteArray
import com.stevesoltys.seedvault.getRandomString
import com.stevesoltys.seedvault.metadata.PackageState.APK_AND_DATA
@ -37,7 +38,10 @@ import kotlin.random.Random
@Suppress("DEPRECATION")
@RunWith(AndroidJUnit4::class)
@Config(sdk = [29]) // robolectric does not support 30, yet
@Config(
sdk = [29], // robolectric does not support 30, yet
application = TestApp::class
)
class MetadataManagerTest {
private val context: Context = mockk()

View file

@ -5,6 +5,7 @@ import android.net.Uri
import android.provider.DocumentsContract
import androidx.documentfile.provider.DocumentFile
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.stevesoltys.seedvault.TestApp
import io.mockk.mockk
import org.junit.After
import org.junit.Assert.assertEquals
@ -15,7 +16,10 @@ import org.koin.core.context.stopKoin
import org.robolectric.annotation.Config
@RunWith(AndroidJUnit4::class)
@Config(sdk = [29]) // robolectric does not support 30, yet
@Config(
sdk = [29], // robolectric does not support 30, yet
application = TestApp::class
)
internal class DocumentFileTest {
private val context: Context = mockk()

View file

@ -6,6 +6,7 @@ import android.util.DisplayMetrics
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.stevesoltys.seedvault.R
import com.stevesoltys.seedvault.TestApp
import com.stevesoltys.seedvault.getRandomString
import io.mockk.every
import io.mockk.mockk
@ -20,7 +21,10 @@ import org.robolectric.annotation.Config
import kotlin.random.Random
@RunWith(AndroidJUnit4::class)
@Config(sdk = [29]) // robolectric does not support 30, yet
@Config(
sdk = [29], // robolectric does not support 30, yet
application = TestApp::class
)
internal class DeviceInfoTest {
@After