android - Detecting toasts in Marshmallow -
i'm writing ui tests espresso , i'm trying assert toast messages. problem i'm having code works on lollipop fails on marshmallow , can't understand why.
my code opening dialog, filling email , clicking button. should bring toast , (i confirmed visual inspection on device) test fails detect toast.
for purpose created custom matcher class toasts:
import android.os.ibinder; import android.support.test.espresso.root; import android.view.windowmanager; import org.hamcrest.description; import org.hamcrest.matcher; import org.hamcrest.typesafematcher; import static com.google.android.exoplayer.util.assertions.checkargument; import static com.google.android.exoplayer.util.assertions.checknotnull; import static org.hamcrest.corematchers.equalto; public class toastmatcher{ public static matcher<root> withtoasttext(string toasttext) { // use preconditions fail fast when test creating invalid matcher. checkargument(!(toasttext.equals(null))); return withtoasttext(equalto(toasttext)); } public static matcher<root> withtoasttext(final matcher<string> matchertext) { // use preconditions fail fast when test creating invalid matcher. checknotnull(matchertext); return new typesafematcher<root>() { public void describeto(description description) { description.appendtext("is toast"); } @override public boolean matchessafely(root root) { int type = root.getwindowlayoutparams().get().type; if ((type == windowmanager.layoutparams.type_toast)) { ibinder windowtoken = root.getdecorview().getwindowtoken(); ibinder apptoken = root.getdecorview().getapplicationwindowtoken(); if (windowtoken == apptoken) { // windowtoken == apptoken means window isn't contained other windows. // if window activity, have type_base_application. return true; } } return false; } }; } }
as test case
@test public void success() { string successmessage = mactivityrule.getactivity().getresources().getstring(r.string.info_recover_instructions); string ok = mactivityrule.getactivity().getresources().getstring(android.r.string.ok); // click forgot button onview(withid(r.id.lg_forgot)).perform(click()); // fill email onview(withclassname(endswith("edittext"))).perform(typetext(username)); // click ok button onview(withtext(ok)) .check(matches(isenabled())) .perform(click()); // assert toast onview(withtext(successmessage)).inroot(toastmatcher.withtoasttext(successmessage)).check(matches(isdisplayed())); }
as said, works fine on lollipop, on marshmallow gives me following error:
android.support.test.espresso.nomatchingrootexception: matcher 'is toast' did not match of following roots: [root{application-window-token=android.view.viewrootimpl$w@a66b932, window-token=android.view.viewrootimpl$w@a66b932, has-window-focus=true, layout-params-type=2, layout-params-string=wm.layoutparams{(0,0)(wrapxwrap) gr=#11 sim=#20 ty=2 fl=#1820002 fmt=-3 wanim=0x103045c needsmenukey=2}, decor-view-string=decorview{id=-1, visibility=visible, width=1026, height=483, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}}, root{application-window-token=android.view.viewrootimpl$w@8d64683, window-token=android.view.viewrootimpl$w@8d64683, has-window-focus=false, layout-params-type=2, layout-params-string=wm.layoutparams{(0,0)(wrapxwrap) gr=#11 sim=#20 ty=2 fl=#1800002 fmt=-3 wanim=0x103045c needsmenukey=2}, decor-view-string=decorview{id=-1, visibility=visible, width=1026, height=598, has-focus=true, has-focusable=true, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}}, root{application-window-token=android.view.viewrootimpl$w@90a2000, window-token=android.view.viewrootimpl$w@90a2000, has-window-focus=false, layout-params-type=1, layout-params-string=wm.layoutparams{(0,0)(fillxfill) ty=1 fl=#81810100 wanim=0x103045b needsmenukey=2}, decor-view-string=decorview{id=-1, visibility=visible, width=1080, height=1920, has-focus=true, has-focusable=true, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}}]
am doing wrong or should done differently on android marshmallow?
try out. not sure if work on marshmallow:
onview(withtext(r.string.toasttext)) .inroot(withdecorview(not(is(mactivityrule.getactivity().getwindow().getdecorview())))) .check(matches(isdisplayed()));
Comments
Post a Comment