Java 一点一点地喝浓缩咖啡
我正在用浓缩咖啡在Android上测试屏幕。在我的屏幕上,我有一个我正在测试的可绘制图像。我发现很难测试它。以下是我正在使用的代码:Java 一点一点地喝浓缩咖啡,java,android,unit-testing,android-espresso,Java,Android,Unit Testing,Android Espresso,我正在用浓缩咖啡在Android上测试屏幕。在我的屏幕上,我有一个我正在测试的可绘制图像。我发现很难测试它。以下是我正在使用的代码: @Test public void clickOnRefreshMenuButton_showsRecipesSuccesfullySyncedMessage() { // Checks clicking on the refresh menu item. onView(withId(R.id.action_refresh))
@Test
public void clickOnRefreshMenuButton_showsRecipesSuccesfullySyncedMessage()
{
// Checks clicking on the refresh menu item.
onView(withId(R.id.action_refresh))
.check(matches(withDrawable(R.drawable.ic_refresh_white_24dp)));
// Checks if the "Recipe sync completed." message is displayed.
onView(withText(R.string.recipe_list_sync_completed)).
inRoot(withDecorView(not(is(activityTestRule
.getActivity()
.getWindow()
.getDecorView()))))
.check(matches(isDisplayed()));
}
static class DrawableMatcher extends TypeSafeMatcher<View> {
private final int expectedId;
private String resourceName;
static final int EMPTY = -1;
static final int ANY = -2;
public DrawableMatcher(int resourceId) {
super(View.class);
this.expectedId = resourceId;
}
@Override
protected boolean matchesSafely(View target) {
if (!(target instanceof ImageView)) {
return false;
}
ImageView imageView = (ImageView) target;
if (expectedId == EMPTY) {
return imageView.getDrawable() == null;
}
if (expectedId == ANY) {
return imageView.getDrawable() != null;
}
Resources resources = target.getContext().getResources();
Drawable expectedDrawable = resources.getDrawable(expectedId, null);
resourceName = resources.getResourceEntryName(expectedId);
if (expectedDrawable == null) {
return false;
}
Bitmap bitmap = getBitmap(imageView.getDrawable());
Bitmap otherBitmap = getBitmap(expectedDrawable);
return bitmap.sameAs(otherBitmap);
}
@Override
public void describeTo(Description description) {
description.appendText("with drawable from resource id: ");
description.appendValue(expectedId);
if (resourceName != null) {
description.appendText("[");
description.appendText(resourceName);
description.appendText("]");
}
}
private Bitmap getBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}
@测试
public void单击刷新菜单按钮显示密码成功完整同步消息()
{
//选中单击“刷新”菜单项。
onView(带id(R.id.action\u刷新))
.检查(匹配项(可撤销(R.drawable.ic_刷新_白色_24dp));
//检查是否显示“配方同步完成”消息。
onView(带文本(R.string.recipe\u list\u sync\u completed))。
inRoot(带DecorView(非)is(活动测试规则
.getActivity()
.getWindow()
.getDecorView())
.检查(匹配项(isDisplayed());
}
静态类DrawableMatcher扩展了TypeSafeMatcher{
私人终审法院;
私有字符串resourceName;
静态final int EMPTY=-1;
静态最终int ANY=-2;
公共DrawableMatcher(int resourceId){
super(View.class);
this.expectedId=resourceId;
}
@凌驾
受保护的布尔匹配安全(查看目标){
如果(!(ImageView的目标实例)){
返回false;
}
ImageView ImageView=(ImageView)目标;
if(expectedId==空){
返回imageView.getDrawable()==null;
}
if(expectedId==ANY){
返回imageView.getDrawable()!=null;
}
Resources=target.getContext().getResources();
Drawable expectedDrawable=resources.getDrawable(expectedId,null);
resourceName=resources.getResourceEntryName(expectedId);
如果(expectedDrawable==null){
返回false;
}
位图Bitmap=getBitmap(imageView.getDrawable());
位图otherBitmap=getBitmap(预期可绘制);
返回位图.sameAs(其他位图);
}
@凌驾
公共无效说明(说明){
description.appendText(“可从资源id提取:”);
说明.附录值(expectedId);
if(resourceName!=null){
说明.附录文本(“[”);
description.appendText(resourceName);
说明.附录文本(“]”);
}
}
私有位图getBitmap(可绘制可绘制){
位图Bitmap=Bitmap.createBitmap(drawable.getIntrinsicWidth(),
getIntrinsicHeight(),Bitmap.Config.ARGB_8888);
画布=新画布(位图);
drawable.setBounds(0,0,canvas.getWidth(),canvas.getHeight());
可绘制。绘制(画布);
返回位图;
}
}
“我的XML文件”是一个菜单,它将刷新列表中的项目:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_refresh"
android:icon="@drawable/ic_refresh_white_24dp"
android:title="@string/action_refresh"
app:showAsAction="ifRoom"/>
</menu>
StackTrace Caused by: junit.framework.AssertionFailedError: 'with drawable
from resource id: <2130837605>' doesn't match the selected view. Expected:
with drawable from resource id: <2130837605> Got:
"ActionMenuItemView{id=2131558605, res-name=action_refresh, desc=Refresh,
visibility=VISIBLE, width=123, height=122, has-focus=false, has-
focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true,
is-focused=false"
StackTrace由以下原因引起:junit.framework.AssertionFailedError:'可绘制
来自资源id:'与所选视图不匹配。预期:
使用可从资源id提取的:获取:
ActionMenuItemView{id=2131558605,res name=action\u refresh,desc=refresh,
可见性=可见,宽度=123,高度=122,has focus=false,has-
focusable=false,has window focus=true,is clickable=true,is enabled=true,
聚焦=假“
StackTrace由以下原因引起:junit.framework.AssertionFailedError:'with drawable from resource id:'与所选视图不匹配。应为:with drawable from resource id:get:'ActionMenuItemView{id=2131558605,res name=action_refresh,desc=refresh,visibility=VISIBLE,width=123,height=122,has focus=false,has focusable=false,has window focus=true,is clickable=true,is enabled=true,is focused=false“请在您的问题中添加(编辑它)