Upgrade to Pro — share decks privately, control downloads, hide ads and more …

FragmentScenarioとFragmentFactoryで楽しくUIテスト / Fr...

tkmnzm
June 28, 2019

FragmentScenarioとFragmentFactoryで楽しくUIテスト / FragmentScenario x FragmentFactory UI Test

tkmnzm

June 28, 2019
Tweet

More Decks by tkmnzm

Other Decks in Programming

Transcript

  1. FragmentScenario @Test fun test() { val scenario = launchFragmentInContainer<MyFragment>() //

    Fragmentͷ࠶ੜ੒ scenario.recreate() onView(withId(R.id.stored_text)).check(matches(withText("Stored"))) }
  2. FragmentScenario @Test fun test() { val scenario = launchFragmentInContainer<MyFragment>() //

    FragmentʹΞΫηε͍ͨ͠ͱ͖ scenario.onFragment { fragment -> fragment.clearText() } // EspressoͰͷૢ࡞͸onFragmentϒϩοΫ಺ʹ͸ॻ͔ͳ͍ onView(withId(R.id.text)).check(matches(withText(""))) }
  3. launchFragmentInContainer(fragmentArgs: Bundle? = null, @StyleRes themeResId: Int = .., factory:

    FragmentFactory? = null) FragmentFactory!!! ґଘΛ੾Γ཭͍ͨ͠
  4. FragmentFactory class MyFragmentFactory() : FragmentFactory() { override fun instanNate(classLoader: ClassLoader,

    className: String): Fragment { // classʹରͯ͠ͲͷFragmentΛੜ੒͢Δ͔Λ͜͜ʹॻ͍͍ͯ͘ if (className == MyFragment::class.java.name) { return MyFragment() } return super.instanNate(classLoader, className) } }
  5. Custom Constructor class MyFragment(private val viewModelFactory: .. ) : Fragment()

    { private lateinit var viewModel: MyViewModel override fun onAcNvityCreated(savedInstanceState: Bundle?) { super.onAcNvityCreated(savedInstanceState) viewModel = ViewModelProviders.of(this,
 viewModelFactory).get(MyViewModel::class.java) } }
  6. Custom Constructor class MyFragment(private val viewModelFactory: .. ) : Fragment()

    { private lateinit var viewModel: MyViewModel override fun onAcNvityCreated(savedInstanceState: Bundle?) { super.onAcNvityCreated(savedInstanceState) viewModel = ViewModelProviders.of(this,
 viewModelFactory).get(MyViewModel::class.java) } } ViewModelFactoryΛ ίϯετϥΫλͰड͚औΔFragment
  7. Custom Constructor class MyFragmentFactory() : FragmentFactory() { override fun instanNate(classLoader:

    ClassLoader, className: String): Fragment { if (className == MyFragment::class.java.name) { // ґଘΛίϯετϥΫλͰ౉ͯ͠Fragment Λੜ੒ val viewModelFactory = MyViewModelFactory(MyDataSource()) return MyFragment(viewModelFactory) } return super.instanNate(classLoader, className) } }
  8. FragmentFactoryΛ࢖͏ class MyAcNvity : AppCompatAcNvity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) // setContentViewͷલʹFragmentFactoryΛઃఆ͢Δ // AcNvity͔ΒFragmentFactoryܦ༝ͰґଘΛ౉͢ͷ΋Մ supportFragmentManager.fragmentFactory = MyFragmentFactory(applicaNon) setContentView(R.layout.acNvity_my) } }
  9. FragmentFactory x UIςετ @Test fun test() { launchFragmentInContainer<MyFragment>(factory = object

    : FragmentFactory() {
 override fun instanNate(classLoader: ClassLoader, className: String): Fragment { val viewModelFactory = 
 ViewModelFactory(ApplicaNonProvider.getApplicaNonContext(), 
 TestDataSource()) return MyFragment(viewModelFactory) } }) } FragmentFactory x UIςετ
  10. FragmentFactory x UIςετ @Test fun test() { launchFragmentInContainer<MyFragment>(factory = object

    : FragmentFactory() {
 override fun instanNate(classLoader: ClassLoader, className: String): Fragment { val viewModelFactory = 
 ViewModelFactory(ApplicaNonProvider.getApplicaNonContext(), 
 TestDataSource()) return MyFragment(viewModelFactory) } }) } FragmentFactory x UIςετ Ұ෦Λελϒʹஔ͖׵͑
  11. FragmentFactory x UIςετ @Test fun test() { launchFragmentInContainer<MyFragment>(factory = object

    : FragmentFactory() {
 override fun instanNate(classLoader: ClassLoader, className: String): Fragment { val viewModelFactory = 
 ViewModelFactory(ApplicaNonProvider.getApplicaNonContext(), 
 TestDataSource()) return MyFragment(viewModelFactory) } }) } FragmentFactory x UIςετ ͬ͞͞ͱґଘΛίϯτϩʔϧͰ͖ͯ
 ͨͷ͍͠