-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemindersActivityTest.kt
345 lines (256 loc) · 10.7 KB
/
RemindersActivityTest.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package com.udacity.project4
import android.app.Activity
import android.app.Application
import android.os.SystemClock
import android.widget.Toast
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ActivityScenario.launch
import androidx.test.core.app.ApplicationProvider.getApplicationContext
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.IdlingRegistry
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.RootMatchers.withDecorView
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.filters.SdkSuppress
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import androidx.test.uiautomator.*
import com.google.android.material.snackbar.Snackbar
import com.udacity.project4.locationreminders.ReminderDescriptionActivity
import com.udacity.project4.locationreminders.RemindersActivity
import com.udacity.project4.locationreminders.data.ReminderDataSource
import com.udacity.project4.locationreminders.data.local.LocalDB
import com.udacity.project4.locationreminders.data.local.RemindersLocalRepository
import com.udacity.project4.locationreminders.reminderslist.RemindersListViewModel
import com.udacity.project4.locationreminders.savereminder.SaveReminderViewModel
import com.udacity.project4.util.DataBindingIdlingResource
import com.udacity.project4.util.monitorActivity
import com.udacity.project4.util.monitorFragment
import com.udacity.project4.utils.EspressoIdlingResource
import kotlinx.coroutines.runBlocking
import org.hamcrest.Matchers.`is`
import org.hamcrest.Matchers.not
import org.hamcrest.core.StringContains.containsString
import org.junit.*
import org.junit.runner.RunWith
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.dsl.module
import org.koin.test.KoinTest
import org.koin.test.get
@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 18)
@LargeTest
//END TO END test to black box test the app
class RemindersActivityTest :
KoinTest {// Extended Koin Test - embed autoclose @after method to close Koin after every test
private lateinit var repository: ReminderDataSource
private lateinit var appContext: Application
companion object {
private lateinit var uiDevice: UiDevice
private const val windowOff = "settings put global window_animation_scale 0.0"
private const val animatorOff = "settings put global animator_duration_scale 0.0"
private const val transitionOff = "settings put global transition_animation_scale 0.0"
@BeforeClass
@JvmStatic
fun setup() {
uiDevice = UiDevice.getInstance(getInstrumentation())
uiDevice.executeShellCommand(windowOff)
uiDevice.executeShellCommand(transitionOff)
uiDevice.executeShellCommand(animatorOff)
}
}
/**
* As we use Koin as a Service Locator Library to develop our code, we'll also use Koin to test our code.
* at this step we will initialize Koin related code to be able to use it in out testing.
*/
@Before
fun init() {
//stop the original app koin
stopKoin()
appContext = getApplicationContext()
val myModule = module {
viewModel {
RemindersListViewModel(
appContext,
get() as ReminderDataSource
)
}
single {
SaveReminderViewModel(
appContext,
get() as ReminderDataSource
)
}
single { RemindersLocalRepository(get()) as ReminderDataSource }
single { LocalDB.createRemindersDao(appContext) }
}
//declare a new koin module
startKoin {
modules(listOf(myModule))
}
//Get our real repository
repository = get()
//clear the data to start fresh
runBlocking {
repository.deleteAllReminders()
}
}
private val dataBindingIdlingResource = DataBindingIdlingResource()
@Before
fun registerIdlingResources() {
IdlingRegistry.getInstance().register(EspressoIdlingResource.countingIdlingResource)
IdlingRegistry.getInstance().register(dataBindingIdlingResource)
}
@After
fun unregisterIdlingResources() {
IdlingRegistry.getInstance().unregister(EspressoIdlingResource.countingIdlingResource)
IdlingRegistry.getInstance().unregister(dataBindingIdlingResource)
}
// get activity context
private fun getActivity(activityScenario: ActivityScenario<RemindersActivity>): Activity? {
var activity: Activity? = null
activityScenario.onActivity {
activity = it
}
return activity
}
@Test
fun check_enable_location() {
val activity = launch(RemindersActivity::class.java)
dataBindingIdlingResource.monitorActivity(activity)
viewWithId(R.id.addReminderFAB).perform(click())
viewWithId(R.id.selectLocation).perform(click())
SystemClock.sleep(5000)
uiDevice.findObject(UiSelector().descriptionContains("My Location")).click()
SystemClock.sleep(3000)
viewWithId(R.id.map).perform(longClick())
viewWithId(R.id.map).perform(click())
viewWithId(R.id.saveBtn).click()
}
/*This test will fail on API Level 30+
* This is a known issue:
* https://github.com/android/android-test/issues/803
* */
//Ensure location is set manually,
// going to Google maps - updating the location
@Test
fun e2e_saveAReminder_completeJourney() {
val activity = launch(RemindersActivity::class.java)
dataBindingIdlingResource.monitorActivity(activity)
viewWithId(R.id.addReminderFAB).perform(click())
viewWithId(R.id.selectLocation).perform(click())
SystemClock.sleep(5000)
uiDevice.findObject(UiSelector().descriptionContains("My Location")).click()
SystemClock.sleep(3000)
viewWithId(R.id.map).perform(longClick())
viewWithId(R.id.map).perform(click())
viewWithId(R.id.saveBtn).click()
viewWithId(R.id.selectedLocation).check(matches(withText(containsString("Dropped Pin"))))
viewWithId(R.id.reminderTitle).type(remindertitle)
viewWithId(R.id.reminderDescription).type(reminderDesc)
viewWithId(R.id.saveReminder).click()
viewWithText(R.string.reminder_saved).inRoot(
withDecorView(
not(
`is`
(
getActivity(activity)?.window?.decorView
)
)
)
).check(
matches(
isDisplayed()
)
)
viewWithId(R.id.reminderssRecyclerView).check(matches(hasDescendant(withText(remindertitle))))
SystemClock.sleep(2000)
uiDevice.openNotification()
activity.close()
uiDevice.wait(Until.hasObject(By.text(remindertitle)), 2000)
uiDevice.findObject(UiSelector().textContains(remindertitle)).clickAndWaitForNewWindow()
viewWithText(remindertitle).check(matches(isDisplayed()))
}
/**
* Test: title error is displayed with correct error text
* */
@Test
fun e2e_correctTitleErrorDisplayed() {
val activityScenarioRule = launch(RemindersActivity::class.java)
dataBindingIdlingResource.monitorActivity(activityScenarioRule)
viewWithId(R.id.addReminderFAB).perform(click())
viewWithId(R.id.selectLocation).perform(click())
SystemClock.sleep(5000)
uiDevice.findObject(UiSelector().descriptionContains("My Location")).click()
SystemClock.sleep(3000)
viewWithId(R.id.map).perform(longClick())
SystemClock.sleep(2000)
viewWithId(R.id.map).perform(click())
viewWithId(R.id.saveBtn).click()
viewWithId(R.id.reminderDescription).type(reminderDesc)
viewWithId(R.id.saveReminder).click()
//check error snackbar with text R.string.err_enter_title
onView(withId(com.google.android.material.R.id.snackbar_text))
.check(matches(withText(R.string.err_enter_title)))
activityScenarioRule.close()
}
/**
* Test: location error is displayed with correct error text
* */
@Test
fun e2e_correctLocationErrorDisplayed() {
//GIVEN: correct a title exists
val activityScenarioRule = launch(RemindersActivity::class.java)
dataBindingIdlingResource.monitorActivity(activityScenarioRule)
viewWithId(R.id.addReminderFAB).perform(click())
viewWithId(R.id.reminderTitle).type(remindertitle)
//WHEN: save reminder button is clicked
viewWithId(R.id.saveReminder).click()
//THEN: snackbar appears with the appropriate error text
onView(withId(com.google.android.material.R.id.snackbar_text))
.check(matches(withText(R.string.err_select_location)))
activityScenarioRule.close()
}
/**
* Test: snackbar appears when SaveLocationFragment launches with correct text
* */
@Test
fun e2e_snackBarWithCorrectTextAppears_launch_SaveLocationFragment() {
val activityScenarioRule = launch(RemindersActivity::class.java)
dataBindingIdlingResource.monitorActivity(activityScenarioRule)
viewWithId(R.id.addReminderFAB).perform(click())
viewWithId(R.id.selectLocation).perform(click())
onView(withId(com.google.android.material.R.id.snackbar_text))
.check(matches(withText(R.string.selection_location_message)))
activityScenarioRule.close()
}
@Test
fun e2e_selectLocation_clickSaveWithoutChoosingPoi_resultToastAppears() {
val activity = launch(RemindersActivity::class.java)
dataBindingIdlingResource.monitorActivity(activity)
onView(withId(R.id.addReminderFAB)).perform(click())
onView(withId(R.id.selectLocation)).perform(click())
SystemClock.sleep(3000)
onView(withId(R.id.saveBtn)).click()
viewWithText(R.string.selection_location_message).inRoot(
withDecorView(
not(
`is`
(
getActivity(activity)?.window?.decorView
)
)
)
).check(
matches(
isDisplayed()
)
)
activity.close()
}
}