Skip to content
Permalink
Browse files

add test for NumberPickersFragment

  • Loading branch information...
bouzuya committed Aug 12, 2019
1 parent cfc2127 commit 7a21076be1ea605242a09bfa043498bce0354edd
@@ -59,5 +59,5 @@ dependencies {
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'com.google.truth:truth:0.44'
androidTestImplementation 'org.mockito:mockito-android:3.0.0'
androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.25.0'
}
@@ -2,13 +2,18 @@ package net.bouzuya.numberpickers

import android.widget.NumberPicker
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.hamcrest.Matchers.`is`
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify

@RunWith(AndroidJUnit4::class)
class NumberPickersFragmentTest {
@@ -36,4 +41,23 @@ class NumberPickersFragmentTest {
.check(matches(isDisplayed()))
.check(matches(withText("10")))
}

@Test
fun clickOkButton() {
// before
val viewModel = mock(NumberPickersViewModel::class.java)
val factory = ServiceLocator.numberPickersViewModelFactory
ServiceLocator.numberPickersViewModelFactory = object : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T = viewModel as T
}
launchFragmentInContainer<NumberPickersFragment>()

onView(withId(R.id.okButton)).perform(click())

verify(viewModel).ok()

// after
ServiceLocator.numberPickersViewModelFactory = factory
}
}
@@ -10,7 +10,9 @@ import net.bouzuya.numberpickers.databinding.NumberPickersFragmentBinding


class NumberPickersFragment : Fragment() {
private val viewModel: NumberPickersViewModel by viewModels()
private val viewModel: NumberPickersViewModel by viewModels {
ServiceLocator.providesNumberPickersViewModelFactory()
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
@@ -0,0 +1,21 @@
package net.bouzuya.numberpickers

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

object ServiceLocator {
// public for testing
var numberPickersViewModelFactory: ViewModelProvider.Factory? = null

fun providesNumberPickersViewModelFactory(): ViewModelProvider.Factory {
if (numberPickersViewModelFactory == null) {
numberPickersViewModelFactory = object : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return NumberPickersViewModel() as T
}
}
}
return numberPickersViewModelFactory ?: throw IllegalStateException()
}
}

0 comments on commit 7a21076

Please sign in to comment.
You can’t perform that action at this time.