Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit 5480d2f

Browse files
committed
Removes ugly cast from GithubBrowserSample
1 parent c3ac1a3 commit 5480d2f

File tree

6 files changed

+15
-15
lines changed

6 files changed

+15
-15
lines changed

GithubBrowserSample/app/src/androidTest/java/com/android/example/github/ui/repo/RepoFragmentTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class RepoFragmentTest {
7070
val countingAppExecutors = CountingAppExecutorsRule()
7171
@Rule
7272
@JvmField
73-
val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule()
73+
val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule<RepoFragment>()
7474

7575
private val navController = mock<NavController>()
7676
private val repoLiveData = MutableLiveData<Resource<Repo>>()

GithubBrowserSample/app/src/androidTest/java/com/android/example/github/ui/search/SearchFragmentTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class SearchFragmentTest {
6969
val countingAppExecutors = CountingAppExecutorsRule()
7070
@Rule
7171
@JvmField
72-
val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule()
72+
val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule<SearchFragment>()
7373

7474
private lateinit var mockBindingAdapter: FragmentBindingAdapters
7575
private lateinit var viewModel: SearchViewModel
@@ -180,4 +180,4 @@ class SearchFragmentTest {
180180
private fun listMatcher(): RecyclerViewMatcher {
181181
return RecyclerViewMatcher(R.id.repo_list)
182182
}
183-
}
183+
}

GithubBrowserSample/app/src/androidTest/java/com/android/example/github/ui/user/UserFragmentTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class UserFragmentTest {
6464
val countingAppExecutors = CountingAppExecutorsRule()
6565
@Rule
6666
@JvmField
67-
val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule()
67+
val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule<UserFragment>()
6868
private lateinit var viewModel: UserViewModel
6969
private lateinit var mockBindingAdapter: FragmentBindingAdapters
7070
private val navController = mock<NavController>()
@@ -198,4 +198,4 @@ class UserFragmentTest {
198198
repoListData.postValue(Resource.success(repos))
199199
return repos
200200
}
201-
}
201+
}

GithubBrowserSample/app/src/androidTest/java/com/android/example/github/util/DataBindingIdlingResource.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import java.util.UUID
3434
* calling [monitorFragment] with a [FragmentScenario], thereby monitoring all bindings in that
3535
* fragment and any child views.
3636
*/
37-
class DataBindingIdlingResource : IdlingResource {
37+
class DataBindingIdlingResource<F : Fragment> : IdlingResource {
3838
// list of registered callbacks
3939
private val idlingCallbacks = mutableListOf<IdlingResource.ResourceCallback>()
4040
// give it a unique id to workaround an espresso bug where you cannot register/unregister
@@ -44,14 +44,14 @@ class DataBindingIdlingResource : IdlingResource {
4444
// onTransitionToIdle callbacks if Espresso never thought we were idle in the first place.
4545
private var wasNotIdle = false
4646

47-
private lateinit var scenario: FragmentScenario<out Fragment>
47+
private lateinit var scenario: FragmentScenario<F>
4848

4949
override fun getName() = "DataBinding $id"
5050

5151
/**
5252
* Sets the fragment from a [FragmentScenario] to be used from [DataBindingIdlingResource].
5353
*/
54-
fun monitorFragment(fragmentScenario: FragmentScenario<out Fragment>) {
54+
fun monitorFragment(fragmentScenario: FragmentScenario<F>) {
5555
scenario = fragmentScenario
5656
}
5757

@@ -67,7 +67,7 @@ class DataBindingIdlingResource : IdlingResource {
6767
} else {
6868
wasNotIdle = true
6969
// check next frame
70-
(scenario as FragmentScenario<Fragment>).onFragment { fragment ->
70+
scenario.onFragment { fragment ->
7171
fragment.view?.postDelayed({
7272
if (fragment.view != null) {
7373
isIdleNow
@@ -87,7 +87,7 @@ class DataBindingIdlingResource : IdlingResource {
8787
*/
8888
private fun getBindings(): List<ViewDataBinding> {
8989
lateinit var bindings: List<ViewDataBinding>
90-
(scenario as FragmentScenario<Fragment>).onFragment { fragment ->
90+
scenario.onFragment { fragment ->
9191
bindings = fragment.requireView().flattenHierarchy().mapNotNull { view ->
9292
DataBindingUtil.getBinding<ViewDataBinding>(view)
9393
}

GithubBrowserSample/app/src/androidTest/java/com/android/example/github/util/DataBindingIdlingResourceRule.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import org.junit.runner.Description
2525
/**
2626
* A JUnit rule that registers an idling resource for all fragment views that use data binding.
2727
*/
28-
class DataBindingIdlingResourceRule() : TestWatcher() {
29-
private val idlingResource = DataBindingIdlingResource()
28+
class DataBindingIdlingResourceRule<F : Fragment>() : TestWatcher() {
29+
private val idlingResource = DataBindingIdlingResource<F>()
3030

31-
fun monitorFragment(fragmentScenario: FragmentScenario<out Fragment>) {
31+
fun monitorFragment(fragmentScenario: FragmentScenario<F>) {
3232
idlingResource.monitorFragment(fragmentScenario)
3333
}
3434

GithubBrowserSample/app/src/androidTest/java/com/android/example/github/util/DataBindingIdlingResourceTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ import java.util.concurrent.atomic.AtomicBoolean
4848

4949
@RunWith(AndroidJUnit4::class)
5050
class DataBindingIdlingResourceTest {
51-
private val idlingResource = DataBindingIdlingResource()
51+
private val idlingResource = DataBindingIdlingResource<TestFragment>()
5252
private lateinit var scenario: FragmentScenario<TestFragment>
5353

5454
@Before
5555
fun init() {
56-
scenario = launchFragmentInContainer<TestFragment>()
56+
scenario = launchFragmentInContainer()
5757
idlingResource.monitorFragment(scenario)
5858
IdlingRegistry.getInstance().register(idlingResource)
5959
Espresso.onIdle()

0 commit comments

Comments
 (0)