-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
InstrumentationUtility.kt
580 lines (523 loc) · 19.3 KB
/
InstrumentationUtility.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
package org.commcare.utils
import android.app.Activity
import android.app.Instrumentation
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.net.wifi.WifiManager
import android.os.Build
import android.os.RemoteException
import android.provider.MediaStore
import android.view.View
import android.widget.ListView
import android.widget.TextView
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import androidx.test.espresso.DataInteraction
import androidx.test.espresso.Espresso
import androidx.test.espresso.Espresso.onData
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
import androidx.test.espresso.PerformException
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.ViewInteraction
import androidx.test.espresso.action.ViewActions.clearText
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.swipeUp
import androidx.test.espresso.action.ViewActions.typeText
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.Intents.intending
import androidx.test.espresso.intent.matcher.IntentMatchers
import androidx.test.espresso.intent.matcher.IntentMatchers.hasAction
import androidx.test.espresso.matcher.RootMatchers
import androidx.test.espresso.matcher.ViewMatchers.hasSibling
import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import androidx.test.espresso.matcher.ViewMatchers.withChild
import androidx.test.espresso.matcher.ViewMatchers.withClassName
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.espresso.util.HumanReadables
import androidx.test.espresso.util.TreeIterables
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.runner.intent.IntentCallback
import androidx.test.runner.intent.IntentMonitorRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import junit.framework.Assert
import org.commcare.CommCareInstrumentationTestApplication
import org.commcare.dalvik.R
import org.commcare.services.CommCareSessionService
import org.commcare.utils.CustomMatchers.withChildViewCount
import org.hamcrest.CoreMatchers.anything
import org.hamcrest.CoreMatchers.startsWith
import org.hamcrest.Matcher
import org.hamcrest.Matchers
import org.hamcrest.StringDescription
import org.javarosa.core.io.StreamsUtil
import org.junit.Assert.assertTrue
import java.io.File
import java.io.IOException
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
/**
* @author $|-|!˅@M
*/
object InstrumentationUtility {
const val IMAGE_CAPTURE_FILE_NAME = "ic_launcher.png"
@JvmStatic
fun stubCcz(cczName: String) {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val file = File(context.externalCacheDir, cczName)
if (!file.exists()) {
val inputStream = context.classLoader.getResourceAsStream(cczName)
try {
FileUtil.copyFile(inputStream, file)
} catch (e: IOException) {
e.printStackTrace()
}
}
stubFileSelection(file.absolutePath)
}
/**
* Installs the ccz by copying it into app-specific cache directory.
* @param cczName
*/
@JvmStatic
fun installApp(cczName: String) {
stubCcz(cczName)
openOptionsMenu()
onView(withText("Offline Install"))
.perform(click())
onView(withId(R.id.screen_multimedia_inflater_filefetch))
.perform(click())
onView(withId(R.id.screen_multimedia_inflater_install))
.perform(click())
}
@JvmStatic
fun uninstallCurrentApp() {
openOptionsMenu()
onView(withText("Go To App Manager"))
.perform(click())
clickListItem(R.id.apps_list_view, 0)
onView(withText("Uninstall"))
.perform(click())
onView(withText("OK"))
.inRoot(RootMatchers.isDialog())
.perform(click())
onView(withId(R.id.install_app_button))
.perform(click())
}
@JvmStatic
fun login(userName: String, password: String) {
enterText(R.id.edit_username, userName)
enterText(R.id.edit_password, password)
onView(isRoot()).perform(swipeUp())
onView(withId(R.id.login_button))
.perform(click())
}
@JvmStatic
fun openModule(text: String) {
onView(withText("Start"))
.perform(click())
onView(withText(text))
.perform(click())
}
@JvmStatic
fun openModule(module: Int) {
onView(withText("Start"))
.perform(click())
clickListItem(R.id.screen_suite_menu_list, module)
}
@JvmStatic
fun openForm(module: Int, form: Int) {
openModule(module)
clickListItem(R.id.screen_suite_menu_list, form)
}
@JvmStatic
fun openOptionsMenu() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
openActionBarOverflowOrOptionsMenu(context)
}
@JvmStatic
fun enableDeveloperMode() {
// Click on About CommCare 4 times to become developer.
for (i in 0..3) {
openOptionsMenu()
onView(withText("About CommCare"))
.perform(click())
onView(withText("OK"))
.perform(click())
}
}
/**
* Click the list item at a particular item position
* @param resId Resource reference to the list.
* @param position Position of the item to be clicked.
*/
@JvmStatic
fun clickListItem(@IdRes resId: Int, position: Int) {
onData(anything())
.inAdapterView(withId(resId))
.atPosition(position)
.perform(click())
}
/**
* Returns a subview inside a particular list item.
* @param listId Resource reference to the list.
* @param position Position of the list item whose subview is needed.
* @param subviewId Resource reference to the subview.
*/
@JvmStatic
fun getSubViewInListItem(@IdRes listId: Int, position: Int, @IdRes subviewId: Int): DataInteraction {
return onData(anything())
.inAdapterView(withId(listId))
.atPosition(position)
.onChildView(withId(subviewId))
}
/**
* Opens first incomplete form in the app.
* Need to have incomplete-form-enabled custom parameter set.
*/
@JvmStatic
fun openFirstIncompleteForm() {
onView(withText(startsWith("Incomplete")))
.perform(click())
clickListItem(R.id.screen_entity_select_list, 0)
}
@JvmStatic
fun logout() {
gotoHome()
onView(withId(R.id.home_gridview_buttons))
.perform(swipeUp())
onView(withText("Log out of CommCare"))
.perform(click())
}
/**
* Stubs the camera intent and uses R.mipmap.ic_launcher as the image to be returned by the camera.
*/
@JvmStatic
fun chooseImage() {
stubCamera()
var intentCallback = onImageCaptureIntentSent()
IntentMonitorRegistry.getInstance().addIntentCallback(intentCallback)
onView(withText(R.string.capture_image))
.perform(click())
IntentMonitorRegistry.getInstance().removeIntentCallback(intentCallback)
}
/**
* Click next button in the form
*/
@JvmStatic
fun nextPage() {
onView(withId(R.id.nav_btn_next))
.perform(click())
}
/**
* Click finish button in the form
*/
@JvmStatic
fun submitForm() {
onView(withId(R.id.nav_btn_finish))
.perform(click())
}
/**
* Sleep the testing thread for the specific number of seconds.
*/
@JvmStatic
fun sleep(seconds: Int) {
onView(isRoot())
.perform(sleep(
TimeUnit.SECONDS.toMillis(seconds.toLong())
))
}
/**
* Matches total occurrences of child inside parent with count.
*/
@JvmStatic
fun matchChildCount(parent: Class<*>, child: Class<*>, count: Int) {
onView(withClassName(Matchers.`is`(parent.canonicalName)))
.check(matches(
withChildViewCount(count,
withClassName(Matchers.`is`(child.canonicalName)))
))
}
/**
* Returns the count of total number of items present in the listView.
* @param resId Resource reference to the list.
*/
@JvmStatic
fun getListSize(@IdRes resId: Int): Int {
val application = InstrumentationRegistry
.getInstrumentation()
.targetContext
.applicationContext as CommCareInstrumentationTestApplication
val activity = application.currentActivity
val listView = activity.findViewById<ListView>(resId)
return listView.adapter.count
}
/**
* This method will toggle the wifi state in mobile.
* Starting with Android Q, applications are not allowed to enable/disable Wi-Fi.
*/
@JvmStatic
fun changeWifi(enable: Boolean) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
wifiManager.isWifiEnabled = enable
sleep(10) // Sleep 5 seconds so that wifi is set up.
} else {
throw IllegalAccessException("changeWifi should only be called in pre-android Q devices")
}
}
/**
* A wrapper around espresso's typeText api. This method will type text into the specified
* edittext and after that it will close the keyboard.
*/
@JvmStatic
fun enterText(@IdRes editTextId: Int, text: String) {
onView(withId(editTextId))
.perform(clearText())
onView(withId(editTextId))
.perform(typeText(text))
Espresso.closeSoftKeyboard()
}
fun enterText(text: String) {
onView(withClassName(Matchers.endsWith("EditText")))
.perform(typeText((text)))
Espresso.closeSoftKeyboard()
}
/**
* A utility to match the text present in a textfield
*/
fun matchTypedText(@IdRes editTextId: Int, text: String) {
onView(withId(editTextId)).check(matches(withText(text)))
}
@JvmStatic
@Throws(RemoteException::class)
fun rotatePortrait() {
val uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
uiDevice.setOrientationNatural()
sleep(2)
}
@JvmStatic
@Throws(RemoteException::class)
fun rotateLeft() {
val uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
uiDevice.setOrientationLeft()
sleep(2)
}
/**
* A workaround to Failed resolution of: Lkotlin/_Assertions;
* This will fail the test if the value is false.
*/
@JvmStatic
fun assert(value: Boolean, failMsg: String) {
if (!value) {
Assert.fail("Assertion Failed: $failMsg")
}
}
/**
* A utility to pressBack until Home screen is reached at most 6 times.
*/
@JvmStatic
fun gotoHome() {
for (i in 0..5) { // Try atmost 6 times.
if (onView(withId(R.id.home_gridview_buttons)).isPresent()) {
return
} else {
Espresso.pressBack()
}
}
}
fun hardPressBack() {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
mDevice.pressBack()
}
/**
* The method does following in order:
* 1. Closes keyboard.
* 2. Presses Back Button.
* 3. Selects the backOption: R.id.donotsave or R.id.saveincomplete.
*/
@JvmStatic
fun exitForm(@IdRes backOption: Int) {
Espresso.closeSoftKeyboard()
Espresso.pressBack()
onView(withText(backOption))
.perform(click())
}
/**
* A utility to select an item from the options menu.
* Caller need to pass the matcher for the item to be selected. It could be withText("") or
* withId(R.id.*)
*/
@JvmStatic
fun selectOptionItem(matcher: Matcher<View>) {
openOptionsMenu()
onView(matcher)
.perform(click())
}
@JvmStatic
fun stubIntentWithAction(action: String) {
Intents.intending(IntentMatchers.hasAction(action))
.respondWith(Instrumentation.ActivityResult(Activity.RESULT_OK, Intent()))
}
fun didLastLogSubmissionSucceed(): Boolean {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
return sharedPreferences.getBoolean(CommCareSessionService.LOG_SUBMISSION_RESULT_PREF, false)
}
@JvmStatic
fun <T> assertCurrentActivity(clazz: Class<T>) {
val application = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
as CommCareInstrumentationTestApplication
val activity = application.currentActivity
assert(clazz.isInstance(activity), "Current Activity is ${activity.localClassName}")
}
/**
* https://stackoverflow.com/a/61404621/6671572
*/
@JvmStatic
fun checkToast(msg: String) {
val uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
uiDevice.waitForIdle()
org.junit.Assert.assertTrue(uiDevice.hasObject(By.text(msg)))
}
//region private helpers.
/**
* Apparently Thread.sleep() doesn't work on
* https://youtu.be/isihPOY2vS4?t=674
*/
private fun sleep(millis: Long): ViewAction {
return object : ViewAction {
override fun getConstraints(): Matcher<View> {
return isRoot()
}
override fun getDescription(): String {
return "Going to sleep for " + millis + "milliseconds"
}
override fun perform(uiController: UiController, view: View) {
uiController.loopMainThreadForAtLeast(millis)
}
}
}
private fun stubCamera() {
// Build a result to return from the Camera app
val resultData = Intent()
val result = Instrumentation.ActivityResult(AppCompatActivity.RESULT_OK, resultData)
// Stub out the Camera. When an intent is sent to the Camera, this tells Espresso to respond
// with the ActivityResult we just created
intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result)
}
private fun stubFileSelection(filePath: String) {
val resultData = Intent()
val fileUri = Uri.parse(File(filePath).toString())
resultData.data = fileUri
val result = Instrumentation.ActivityResult(AppCompatActivity.RESULT_OK, resultData)
intending(hasAction(Intent.ACTION_GET_CONTENT)).respondWith(result)
}
fun onImageCaptureIntentSent() = IntentCallback { intent ->
val uri = intent.extras!!.getParcelable<Uri>(MediaStore.EXTRA_OUTPUT)
val context = InstrumentationRegistry.getInstrumentation().targetContext
val inputStream = context.classLoader.getResourceAsStream(IMAGE_CAPTURE_FILE_NAME)
val outputStream = context.contentResolver.openOutputStream(uri!!)
try {
StreamsUtil.writeFromInputToOutputUnmanaged(inputStream, outputStream)
} finally {
inputStream.close()
outputStream!!.close()
}
}
/**
* A utility to verify Form Fields with their values
*/
fun verifyFormCellAndValue(cellData: String, value: String) {
val result: Boolean = onView(
Matchers.allOf(
withId(R.id.detail_type_text),
withText(cellData),
hasSibling(
Matchers.allOf(
withId(R.id.detail_value_pane),
withChild(
Matchers.allOf(
withId(R.id.detail_value_text),
withText(value)
)
)
)
)
)
).isPresent()
assertTrue(result)
}
/**
* utility to search a case in Form list and select the same
*/
fun searchCaseAndSelect(text: String) {
onView(withId(R.id.search_action_bar)).perform(click())
enterText(androidx.appcompat.R.id.search_src_text, text)
onView(withId(R.id.screen_entity_detail_list)).isPresent()
clickListItem(R.id.screen_entity_select_list, 0)
}
/**
* A utility to wait until a certain view appears
* usage: onView(isRoot()).perform(waitForView(withText("<text>")))
*/
fun waitForView(viewMatcher: Matcher<View>, timeout: Long = 10000, waitForDisplayed: Boolean = true): ViewAction {
return object : ViewAction {
override fun getConstraints(): Matcher<View> {
return Matchers.any(View::class.java)
}
override fun getDescription(): String {
val matcherDescription = StringDescription()
viewMatcher.describeTo(matcherDescription)
return "wait for a specific view <$matcherDescription> to be ${if (waitForDisplayed) "displayed" else "not displayed during $timeout millis."}"
}
override fun perform(uiController: UiController, view: View) {
uiController.loopMainThreadUntilIdle()
val startTime = System.currentTimeMillis()
val endTime = startTime + timeout
val visibleMatcher = isDisplayed()
do {
val viewVisible = TreeIterables.breadthFirstViewTraversal(view)
.any { viewMatcher.matches(it) && visibleMatcher.matches(it) }
if (viewVisible == waitForDisplayed) return
uiController.loopMainThreadForAtLeast(50)
} while (System.currentTimeMillis() < endTime)
// Timeout happens.
throw PerformException.Builder()
.withActionDescription(this.description)
.withViewDescription(HumanReadables.describe(view))
.withCause(TimeoutException())
.build()
}
}
}
/**
* utility to get text of any TextView Object
*/
fun getText(matcher: ViewInteraction): String {
var text = String()
matcher.perform(object : ViewAction {
override fun getConstraints(): Matcher<View> {
return isAssignableFrom(TextView::class.java)
}
override fun getDescription(): String {
return "Text of the view"
}
override fun perform(uiController: UiController, view: View) {
val tv = view as TextView
text = tv.text.toString()
}
})
return text
}
//endregion
}