gpt4 book ai didi

android - NoMatchingRootException 带有自定义 toast 的 Espresso

转载 作者:行者123 更新时间:2023-11-28 20:29:42 25 4
gpt4 key购买 nike

我从 Espresso 开始,我有一个自定义的 Toast,当用户输入错误时我会显示它(我知道我可以使用 EditText.setError(),但那只是怎么样)。

这是 Toast 的代码

private static void showToast(Context context, String message, boolean isError) {
if (context != null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView toastView = (TextView) inflater.inflate(R.layout.error_toast_layout, null);
if (!isError) {
toastView.setBackgroundColor(context.getResources().getColor(R.color.alert_positive));
}
toastView.setText(message);
Toast errorToast = new Toast(context);
errorToast.setGravity(Gravity.TOP, 0, 0);
errorToast.setDuration(Toast.LENGTH_SHORT);
errorToast.setView(toastView);
errorToast.show();
}
}

我正在编写一个小的 UI 测试,它将断言当用户输入错误的输入时会显示正确的 Toast

这是测试

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ForgotPasswordTest extends BaseEspressoTest<ForgotPasswordActivity> {

@Test
public void testEmailNotEntered() {
onView(withId(R.id.email_et)).perform(typeText("w"), closeSoftKeyboard());
onView(withId(R.id.btn_submit)).perform(click());
onView(withText(R.string.incorrect_email_dialog)).inRoot(withDecorView(not(mActivityRule.getActivity().getWindow().getDecorView()))).check(matches(isDisplayed()));
}
}

如果我用我的自定义 Toast 替换常规 Toast,这会很好用,但当我用我的 Toast 尝试时会失败。我可以看到在测试期间正在显示 Toast。

这是错误:

android.support.test.espresso.NoMatchingRootException: Matcher 'withdecor view not<com.android.internal.policy.impl.PhoneWindow$DecorView{3852d97V.ED.... R....... 0,0-1080,1920}>' did not match any of the followingroots:[Root{application-window-token=android.view.ViewRootImpl$W@2d629b84,window-token=android.view.ViewRootImpl$W@2d629b84,has-window-focus=true, layout-params-type=1,layout-params-string=WM.LayoutParams{(0,0)(fillxfill) sim=#22 ty=1fl=#8d810100 pfl=0x8 wanim=0x1030461 surfaceInsets=Rect(0, 0 - 0, 0)},decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=1080,height=1920, has-focus=true, 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, root-is-layout-requested=false,has-input-connection=false, x=0.0, y=0.0, child-count=1}}]

我还尝试根据 Toast 应该关注的事实来匹配它:

onView(withText(R.string.incorrect_email_dialog))
.inRoot(isFocusable()).check(matches(isDisplayed()));

但这只是说它无法在 View 层次结构中找到它:

No views in hierarchy found matching: with string from resource id:<2131165635>[incorrect_email_dialog] value: Please enter a valid email

最佳答案

您可以尝试使用下面给出的 CustomToastMatcher:

import android.os.IBinder;
import android.support.test.espresso.Root;
import android.view.WindowManager;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

public class ToastMatcher extends TypeSafeMatcher<Root> {

@Override 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) {
//means this window isn't contained by any other windows.
}
}
return false;
}
}

像这样在测试用例中使用它:

onView(withText(R.string.mssage)).inRoot(new ToastMatcher())
.check(matches(isDisplayed()));

其他引用资料: https://stackoverflow.com/a/33387980/2069407 http://baroqueworksdev.blogspot.de/2015/03/how-to-check-toast-window-on-android.html

关于android - NoMatchingRootException 带有自定义 toast 的 Espresso ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33961861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com