-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
SearchFragmentTest.kt
183 lines (167 loc) · 7.11 KB
/
SearchFragmentTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.example.github.ui.search
import android.view.KeyEvent
import androidx.databinding.DataBindingComponent
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.lifecycle.MutableLiveData
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.pressKey
import androidx.test.espresso.action.ViewActions.typeText
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.example.github.R
import com.android.example.github.binding.FragmentBindingAdapters
import com.android.example.github.util.CountingAppExecutorsRule
import com.android.example.github.util.DataBindingIdlingResourceRule
import com.android.example.github.util.RecyclerViewMatcher
import com.android.example.github.util.TaskExecutorWithIdlingResourceRule
import com.android.example.github.util.TestUtil
import com.android.example.github.util.ViewModelUtil
import com.android.example.github.util.disableProgressBarAnimations
import com.android.example.github.util.mock
import com.android.example.github.vo.Repo
import com.android.example.github.vo.Resource
import org.hamcrest.CoreMatchers.not
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.`when`
import org.mockito.Mockito.doNothing
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify
@RunWith(AndroidJUnit4::class)
class SearchFragmentTest {
@Rule
@JvmField
val executorRule = TaskExecutorWithIdlingResourceRule()
@Rule
@JvmField
val countingAppExecutors = CountingAppExecutorsRule()
@Rule
@JvmField
val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule<SearchFragment>()
private lateinit var mockBindingAdapter: FragmentBindingAdapters
private lateinit var viewModel: SearchViewModel
private val navController = mock<NavController>()
private val results = MutableLiveData<Resource<List<Repo>>>()
private val loadMoreStatus = MutableLiveData<SearchViewModel.LoadMoreState>()
@Before
fun init() {
viewModel = mock(SearchViewModel::class.java)
doReturn(loadMoreStatus).`when`(viewModel).loadMoreStatus
`when`(viewModel.results).thenReturn(results)
mockBindingAdapter = mock(FragmentBindingAdapters::class.java)
val scenario = launchFragmentInContainer(
themeResId = R.style.AppTheme) {
SearchFragment().apply {
appExecutors = countingAppExecutors.appExecutors
viewModelFactory = ViewModelUtil.createFor(viewModel)
dataBindingComponent = object : DataBindingComponent {
override fun getFragmentBindingAdapters(): FragmentBindingAdapters {
return mockBindingAdapter
}
}
}
}
dataBindingIdlingResourceRule.monitorFragment(scenario)
scenario.onFragment { fragment ->
Navigation.setViewNavController(fragment.requireView(), navController)
fragment.disableProgressBarAnimations()
}
}
@Test
fun search() {
onView(withId(R.id.progress_bar)).check(matches(not(isDisplayed())))
onView(withId(R.id.input)).perform(
typeText("foo"),
pressKey(KeyEvent.KEYCODE_ENTER)
)
verify(viewModel).setQuery("foo")
results.postValue(Resource.loading(null))
onView(withId(R.id.progress_bar)).check(matches(isDisplayed()))
}
@Test
fun loadResults() {
val repo = TestUtil.createRepo("foo", "bar", "desc")
results.postValue(Resource.success(arrayListOf(repo)))
onView(listMatcher().atPosition(0)).check(matches(hasDescendant(withText("foo/bar"))))
onView(withId(R.id.progress_bar)).check(matches(not(isDisplayed())))
}
@Test
fun dataWithLoading() {
val repo = TestUtil.createRepo("foo", "bar", "desc")
results.postValue(Resource.loading(arrayListOf(repo)))
onView(listMatcher().atPosition(0)).check(matches(hasDescendant(withText("foo/bar"))))
onView(withId(R.id.progress_bar)).check(matches(not(isDisplayed())))
}
@Test
fun error() {
results.postValue(Resource.error("failed to load", null))
onView(withId(R.id.error_msg)).check(matches(isDisplayed()))
}
@Test
fun loadMore() {
val repos = TestUtil.createRepos(50, "foo", "barr", "desc")
results.postValue(Resource.success(repos))
val action = RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(49)
onView(withId(R.id.repo_list)).perform(action)
onView(listMatcher().atPosition(49)).check(matches(isDisplayed()))
verify(viewModel).loadNextPage()
}
@Test
fun navigateToRepo() {
doNothing().`when`<SearchViewModel>(viewModel).loadNextPage()
val repo = TestUtil.createRepo("foo", "bar", "desc")
results.postValue(Resource.success(arrayListOf(repo)))
onView(withText("desc")).perform(click())
verify(navController).navigate(
SearchFragmentDirections.showRepo("foo", "bar")
)
}
@Test
fun loadMoreProgress() {
loadMoreStatus.postValue(SearchViewModel.LoadMoreState(true, null))
onView(withId(R.id.load_more_bar)).check(matches(isDisplayed()))
loadMoreStatus.postValue(SearchViewModel.LoadMoreState(false, null))
onView(withId(R.id.load_more_bar)).check(matches(not(isDisplayed())))
}
@Test
fun loadMoreProgressError() {
loadMoreStatus.postValue(SearchViewModel.LoadMoreState(true, "QQ"))
onView(withText("QQ")).check(
matches(
withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)
)
)
}
private fun listMatcher(): RecyclerViewMatcher {
return RecyclerViewMatcher(R.id.repo_list)
}
}