【发布时间】:2016-07-12 12:37:11
【问题描述】:
我在检查是否使用 espresso 显示 toast 时遇到问题。我正在使用类:
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) {
// windowToken == appToken means this window isn't contained by any other windows.
// if it was a window for an activity, it would have TYPE_BASE_APPLICATION.
return true;
}
}
return false;
}
}
并通过以下方式检查 Toast:
onView(withText(R.string.unauthorized)).inRoot(new ToastMatcher())
.check(matches(isDisplayed()));
一切正常,直到我尝试检查同一类中的另一个吐司,例如:
@Test
public void messageOnBack() throws Exception{
pressBack();
onView(withText(R.string.exit_on_back)).inRoot(new ToastMatcher())
.check(matches(isDisplayed()));
然后第一个通过但第二个出错:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131165323>[unauthorized] value: Wrong login or password.
View Hierarchy:
+>LinearLayout{id=-1, visibility=VISIBLE, width=660, height=116, has-focus=false, has-focusable=false, 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}
|
+->AppCompatTextView{id=16908299, res-name=message, visibility=VISIBLE, width=528, height=58, has-focus=false, has-focusable=false, 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=66.0, y=29.0, text=Please click BACK again to exit., input-type=0, ime-target=false, has-links=false}
|
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
奇怪的是,当我注释掉其中一个测试时,第二个测试工作正常,没有任何更改。当一个吐司放在另一个吐司上时,浓缩咖啡似乎变得愚蠢。任何想法如何解决这个问题?
【问题讨论】:
-
编辑:我已经设法通过在正在测试的活动中公开敬酒并在测试外观后取消()每个敬酒来做到这一点,但它似乎不是最佳答案。
标签: android testing toast android-espresso