Skip to content

Commit

Permalink
Fix navigation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
albodelu committed Mar 14, 2018
1 parent 6265424 commit 8e2bcc7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import android.content.Intent
import com.fernandocejas.sample.BaseActivity

class LoginActivity : BaseActivity() {

companion object {
fun callingIntent(context: Context) = Intent(context, LoginActivity::class.java)
@JvmStatic
fun callingIntent(context: Context): Intent {
val intent = Intent(context, LoginActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
return intent
}
}

override fun fragment() = LoginFragment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import com.fernandocejas.sample.BaseActivity
class MoviesActivity : BaseActivity() {

companion object {
fun callingIntent(context: Context) = Intent(context, MoviesActivity::class.java)
@JvmStatic
fun callingIntent(context: Context): Intent {
val intent = Intent(context, MoviesActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
return intent
}
}

override fun fragment() = MoviesFragment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ package com.fernandocejas.sample

import android.support.v7.app.AppCompatActivity
import org.amshove.kluent.shouldEqual
import org.amshove.kluent.shouldNotEqual
import org.robolectric.Robolectric
import org.robolectric.Shadows
import kotlin.reflect.KClass

infix fun KClass<out AppCompatActivity>.`shouldNavigateTo`(nextActivity: KClass<out AppCompatActivity>) = {
infix fun KClass<out AppCompatActivity>.`shouldNavigateTo`(nextActivity: KClass<out AppCompatActivity>) {
val originActivity = Robolectric.buildActivity(this.java).get()
val shadowActivity = Shadows.shadowOf(originActivity)
val nextIntent = shadowActivity.peekNextStartedActivity()

nextIntent.component.className shouldEqual nextActivity.java.canonicalName
}

infix fun KClass<out AppCompatActivity>.`shouldNotNavigateTo`(nextActivity: KClass<out AppCompatActivity>) {
val originActivity = Robolectric.buildActivity(this.java).get()
val shadowActivity = Shadows.shadowOf(originActivity)
val nextIntent = shadowActivity.peekNextStartedActivity()

nextIntent.component.className shouldNotEqual nextActivity.java.canonicalName
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.fernandocejas.sample.features.login.Authenticator
import com.fernandocejas.sample.features.login.LoginActivity
import com.fernandocejas.sample.features.movies.MoviesActivity
import com.fernandocejas.sample.shouldNavigateTo
import com.fernandocejas.sample.shouldNotNavigateTo
import com.nhaarman.mockito_kotlin.whenever
import org.junit.Before
import org.junit.Test
Expand All @@ -22,21 +23,30 @@ class NavigatorTest : AndroidTest() {
navigator = Navigator(authenticator)
}

@Test fun `should forward user to login screen`() {
@Test fun `should forward unauthenticated user to login screen`() {
whenever(authenticator.userLoggedIn()).thenReturn(false)

navigator.showMain(activityContext())
navigator.showMain(context())

verify(authenticator).userLoggedIn()
RouteActivity::class shouldNavigateTo LoginActivity::class
}

@Test fun `should forward user to movies screen`() {
@Test fun `should forward authenticated user to movies screen`() {
whenever(authenticator.userLoggedIn()).thenReturn(true)

navigator.showMain(activityContext())
navigator.showMain(context())

verify(authenticator).userLoggedIn()
RouteActivity::class shouldNavigateTo MoviesActivity::class
}

@Test fun `should not forward unauthenticated user to movies screen`() {
whenever(authenticator.userLoggedIn()).thenReturn(false)

navigator.showMain(context())

verify(authenticator).userLoggedIn()
RouteActivity::class shouldNotNavigateTo MoviesActivity::class
}
}

0 comments on commit 8e2bcc7

Please sign in to comment.