Skip to content
Snippets Groups Projects
  • Matthias Urhahn's avatar
    b4f32951
    Introduce VM injection for home screen and enable screen testing... · b4f32951
    Matthias Urhahn authored
    Introduce VM injection for home screen and enable screen testing (EXPOSUREAPP-2946, EXPOSUREAPP-2948) (#1320)
    
    * Rename "MainFragment" to "HomeFragment"
    Add VM injection.
    Move initial logic into VM.
    
    TODO: Move more code, write more tests.
    
    * Additional refactoring, moving logic out of the Fragment.
    Moving code into their own encapsulated routines/components.
    Use composition to reuse TracingViewModel, SettingsViewModel and SubmissionViewModel
    
    * Introduce @AppContext annotation to prevent anyone mistaking it for something else.
    
    * Add VM to RiskDetailsFragment
    
    * Simplify nested viewmodels, here: TracingViewModel.kt
    
    * Add skeletons instrumentation test skeleton for single fragments with injection and mocking.
    
    * Address PR comments.
    
    * Introduce sealed events class, + some refactoring.
    
    * Make the linter happy.
    Introduce VM injection for home screen and enable screen testing...
    Matthias Urhahn authored
    Introduce VM injection for home screen and enable screen testing (EXPOSUREAPP-2946, EXPOSUREAPP-2948) (#1320)
    
    * Rename "MainFragment" to "HomeFragment"
    Add VM injection.
    Move initial logic into VM.
    
    TODO: Move more code, write more tests.
    
    * Additional refactoring, moving logic out of the Fragment.
    Moving code into their own encapsulated routines/components.
    Use composition to reuse TracingViewModel, SettingsViewModel and SubmissionViewModel
    
    * Introduce @AppContext annotation to prevent anyone mistaking it for something else.
    
    * Add VM to RiskDetailsFragment
    
    * Simplify nested viewmodels, here: TracingViewModel.kt
    
    * Add skeletons instrumentation test skeleton for single fragments with injection and mocking.
    
    * Address PR comments.
    
    * Introduce sealed events class, + some refactoring.
    
    * Make the linter happy.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
TestApplication.kt 2.58 KiB
package testhelpers

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.HasAndroidInjector
import dagger.android.support.AndroidSupportInjection
import de.rki.coronawarnapp.util.di.AutoInject
import timber.log.Timber
import javax.inject.Inject

class TestApplication : Application(), HasAndroidInjector {
    @Inject lateinit var androidInjector: DispatchingAndroidInjector<Any>
    override fun androidInjector(): AndroidInjector<Any> = androidInjector

    private lateinit var component: TestAppComponent

    override fun onCreate() {
        super.onCreate()
        component = DaggerTestAppComponent.factory().create(this)
        component.inject(this)
        setupActivityHook()
    }

    private fun setupActivityHook() {
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityPreCreated(activity: Activity, savedInstanceState: Bundle?) {
                setupFragmentHook(activity)
            }

            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) = Unit

            override fun onActivityStarted(activity: Activity) = Unit

            override fun onActivityResumed(activity: Activity) = Unit

            override fun onActivityPaused(activity: Activity) = Unit

            override fun onActivityStopped(activity: Activity) = Unit

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) = Unit

            override fun onActivityDestroyed(activity: Activity) = Unit
        })
    }

    private fun setupFragmentHook(activity: Activity) {
        if (activity is FragmentActivity) {
            activity.supportFragmentManager
                .registerFragmentLifecycleCallbacks(object :
                    FragmentManager.FragmentLifecycleCallbacks() {
                    override fun onFragmentPreAttached(
                        fm: FragmentManager,
                        f: Fragment,
                        context: Context
                    ) {
                        if (f is AutoInject) {
                            Timber.d("Injecting %s", f)
                            AndroidSupportInjection.inject(f)
                        }
                        super.onFragmentPreAttached(fm, f, context)
                    }
                }, true)
        }
    }
}