Skip to content

Commit

Permalink
implement home module unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume M committed Jun 24, 2019
1 parent 0ef2a2a commit bc9ef93
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 7 deletions.
16 changes: 10 additions & 6 deletions app/src/main/java/io/freshdroid/mymonzo/MyMonzoApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.multidex.MultiDexApplication
import io.freshdroid.core.di.CoreComponent
import io.freshdroid.core.di.CoreModule
import io.freshdroid.core.di.DaggerCoreComponent
import io.freshdroid.core.ui.BaseActivity
import io.freshdroid.core.ui.BaseFragment
import io.freshdroid.mymonzo.core.di.CoreComponent
import io.freshdroid.mymonzo.core.di.CoreModule
import io.freshdroid.mymonzo.core.di.DaggerCoreComponent
import io.freshdroid.mymonzo.core.ui.BaseActivity
import io.freshdroid.mymonzo.core.ui.BaseFragment
import timber.log.Timber

class MyMonzoApplication : MultiDexApplication(), LifecycleObserver {
open class MyMonzoApplication : MultiDexApplication(), LifecycleObserver {

private val coreComponent: CoreComponent by lazy {
DaggerCoreComponent.builder()
Expand All @@ -31,6 +31,10 @@ class MyMonzoApplication : MultiDexApplication(), LifecycleObserver {

fun coreComponent(): CoreComponent = coreComponent

protected open fun isInUnitTests(): Boolean {
return false
}

override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ class HomeActivity : BaseActivity() {

}

enum class FragmentState(tag: String) {
enum class FragmentState(
val tag: String
) {
FEED("tab_feed"),
SUMMARY("tab_summary"),
ACCOUNT("tab_account"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.freshdroid.mymonzo.home

import io.freshdroid.mymonzo.MyMonzoApplication

internal class MyMonzoApplicationTest : MyMonzoApplication() {

override fun isInUnitTests(): Boolean = true

override fun onCreate() {
super.onCreate()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.freshdroid.mymonzo.home

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider
import com.uber.autodispose.android.lifecycle.test.TestLifecycleOwner
import io.freshdroid.mymonzo.core.di.CoreComponent
import junit.framework.TestCase
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(application = MyMonzoApplicationTest::class)
internal abstract class MyMonzoRobolectricTestCase : TestCase() {

private var mApplication: MyMonzoApplicationTest? = null

protected fun application(): MyMonzoApplicationTest {
if (mApplication != null) {
return mApplication as MyMonzoApplicationTest
}

mApplication = ApplicationProvider.getApplicationContext() as MyMonzoApplicationTest
return mApplication as MyMonzoApplicationTest
}

protected fun context(): Context {
return application().applicationContext
}

protected fun coreComponent(): CoreComponent {
return application().coreComponent()
}

protected fun scopeProvider(): AndroidLifecycleScopeProvider {
val testLifecycleOwner = TestLifecycleOwner.create()
return AndroidLifecycleScopeProvider.from(testLifecycleOwner)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package io.freshdroid.mymonzo.home.viewmodels

import android.content.Intent
import io.freshdroid.mymonzo.home.MyMonzoRobolectricTestCase
import io.freshdroid.mymonzo.home.views.FragmentState
import io.freshdroid.mymonzo.navigation.Activities
import io.reactivex.subscribers.TestSubscriber
import org.junit.Test

internal class HomeViewModelTest : MyMonzoRobolectricTestCase() {

@Test
fun testDisplaySelectedFragment_feed() {
val vm = HomeViewModel(scopeProvider())
val displaySelectedFragment = TestSubscriber<FragmentState>()
vm.outputs.displaySelectedFragment().subscribe(displaySelectedFragment::onNext)

vm.inputs.onFeedClick()
displaySelectedFragment.assertValue { it == FragmentState.FEED }
}

@Test
fun testDisplaySelectedFragment_summary() {
val vm = HomeViewModel(scopeProvider())
val displaySelectedFragment = TestSubscriber<FragmentState>()
vm.outputs.displaySelectedFragment().subscribe(displaySelectedFragment::onNext)

vm.inputs.onSummaryClick()
displaySelectedFragment.assertValue { it == FragmentState.SUMMARY }
}

@Test
fun testDisplaySelectedFragment_account() {
val vm = HomeViewModel(scopeProvider())
val displaySelectedFragment = TestSubscriber<FragmentState>()
vm.outputs.displaySelectedFragment().subscribe(displaySelectedFragment::onNext)

vm.inputs.onAccountClick()
displaySelectedFragment.assertValue { it == FragmentState.ACCOUNT }
}

@Test
fun testDisplaySelectedFragment_help() {
val vm = HomeViewModel(scopeProvider())
val displaySelectedFragment = TestSubscriber<FragmentState>()
vm.outputs.displaySelectedFragment().subscribe(displaySelectedFragment::onNext)

vm.inputs.onHelpClick()
displaySelectedFragment.assertValue { it == FragmentState.HELP }
}

@Test
fun testInitBottomNavigationBar_feed() {
val vm = HomeViewModel(scopeProvider())
val initBottomNavigationBar = TestSubscriber<FragmentState>()
vm.outputs.initBottomNavigationBar().subscribe(initBottomNavigationBar::onNext)

val homeIntent = Intent()
homeIntent.putExtra(Activities.Home.KEY_EXTRA_HOME_ACTIVE_TAB, FragmentState.FEED.tag)
vm.intent(homeIntent)

initBottomNavigationBar.assertValue { it == FragmentState.FEED }
}

@Test
fun testInitBottomNavigationBar_summary() {
val vm = HomeViewModel(scopeProvider())
val initBottomNavigationBar = TestSubscriber<FragmentState>()
vm.outputs.initBottomNavigationBar().subscribe(initBottomNavigationBar::onNext)

val homeIntent = Intent()
homeIntent.putExtra(Activities.Home.KEY_EXTRA_HOME_ACTIVE_TAB, FragmentState.SUMMARY.tag)
vm.intent(homeIntent)

initBottomNavigationBar.assertValue { it == FragmentState.SUMMARY }
}

@Test
fun testInitBottomNavigationBar_account() {
val vm = HomeViewModel(scopeProvider())
val initBottomNavigationBar = TestSubscriber<FragmentState>()
vm.outputs.initBottomNavigationBar().subscribe(initBottomNavigationBar::onNext)

val homeIntent = Intent()
homeIntent.putExtra(Activities.Home.KEY_EXTRA_HOME_ACTIVE_TAB, FragmentState.ACCOUNT.tag)
vm.intent(homeIntent)

initBottomNavigationBar.assertValue { it == FragmentState.ACCOUNT }
}

@Test
fun testInitBottomNavigationBar_help() {
val vm = HomeViewModel(scopeProvider())
val initBottomNavigationBar = TestSubscriber<FragmentState>()
vm.outputs.initBottomNavigationBar().subscribe(initBottomNavigationBar::onNext)

val homeIntent = Intent()
homeIntent.putExtra(Activities.Home.KEY_EXTRA_HOME_ACTIVE_TAB, FragmentState.HELP.tag)
vm.intent(homeIntent)

initBottomNavigationBar.assertValue { it == FragmentState.HELP }
}

}
1 change: 1 addition & 0 deletions home/src/test/resources/robolectric.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application=io.freshdroid.mymonzo.home.MyMonzoApplicationTest

0 comments on commit bc9ef93

Please sign in to comment.