android / android/android-test

Toast message assertions not working with android 11 and target sdk 30

Aperta
#803 32 commenti 63 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Java
Stelle
1.2k
Fork
342
Merge medio
11h 29m
PR unite (30g)
2

Descrizione

### Description
On the app I am testing there are a number of toast messages that I check to make sure toasts are displayed with correct text. My espresso tests started to fail when using Android 11 and SDK30.

### Steps to Reproduce
I have a simple check where I try to check the text appears on screen for a toast.
`onView(withText(R.string.toast_text)).inRoot(
withDecorView(not(activityRule.activity.window.decorView)))
.check(matches(isDisplayed()))`

I have also tried with a Custom toast matcher without any luck

fun onToast(
text: Int,
@IntRange(from = 1) maximumRetries: Int = MAX_RETRIES
): ViewInteraction = onView(withText(text)).inRoot(ToastMatcher(maximumRetries))

class ToastMatcher(private val maximumRetries: Int) : TypeSafeMatcher() {

private var currentFailures: Int = 0

override fun describeTo(description: Description?) {
description?.appendText("no toast found after")
}

override fun matchesSafely(item: Root?): Boolean {
val type: Int? = item?.windowLayoutParams?.get()?.type

if(TYPE_TOAST == type || TYPE_APPLICATION_OVERLAY == type) {
val windowToken = item.decorView.windowToken
val appToken = item.decorView.applicationWindowToken

if(windowToken == appToken) {
return true
}
}

return ++currentFailures >= maximumRetries
}
}

`onToast(R.string.toast_text)
.check(matches(isDisplayed()))`

### Expected Results
Tests pass.
This works as expected when using OS lower than android 11. It also works as expected when using android 11 and sdk 29.
I also tried updating the version of androidx and espresso to the latest version 3.3.0 and 1.3.0 but this did not make any difference.

### Actual Results
Result when using withDecorView
`androidx.test.espresso.NoMatchingRootException: Matcher 'with decor view not ' did not match any of the following roots: [Root{application-window-token=android.view.ViewRootImpl$W@1337585, window-token=android.view.ViewRootImpl$W@1337585, has-window-focus=true, layout-params-type=1, layout-params-string={(0,0)(fillxfill) sim={state=hidden} ty=BASE_APPLICATION wanim=0x10302fe
fl=LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDS
pfl=FORCE_DRAW_STATUS_BAR_BACKGROUND FIT_INSETS_CONTROLLED
fitSides=}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=1080, height=2160, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params={(0,0)(fillxfill) sim={state=hidden} ty=BASE_APPLICATION wanim=0x10302fe
fl=LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDS
pfl=FORCE_DRAW_STATUS_BAR_BACKGROUND FIT_INSETS_CONTROLLED
fitSides=}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}}]
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1736)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:96)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:59)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:322)
at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:306)`

Result when using ToastMatcher
`androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131755255>[toast_text] value: Toast Text.
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:com.squareup.timessquare.CalendarPickerView{1af12d7 VFED.VC.. ........ 0,0-1080,1657 #7f09010e app:id/calendarPv}
View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=1080, height=2160, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params={(0,0)(fillxfill) ty=BASE_APPLICATION wanim=0x10302fe
fl=LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDS
pfl=FORCE_DRAW_STATUS_BAR_BACKGROUND FIT_INSETS_CONTROLLED
fitSides=}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|`

### AndroidX Test and Android OS Versions
AndroidX Test: 1.2.0
Espresso: 3.2.0
Android OS: Android 11 (API 30)

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Riproduci l’assertion del Toast con Espresso su Android 11/API 30 e target SDK 30, utilizzando gli esempi mostrati di withDecorView e ToastMatcher. Inizia esaminando la gestione delle root di Espresso e dei Toast in relazione a questi matcher; il lavoro è completo quando un test di regressione trova in modo affidabile il Toast, preservando al contempo il comportamento sulle versioni precedenti di Android.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
android, kotlin
Ambito
mobile-dev, testing
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.