我想写一个Espresso matcher来验证`ImageView'是否有特定的位图设置。由于应用程序通过Glide进行图像加载,我认为在实际比较预期位图和实际位图之前,我必须在测试方面做同样的事情来考虑裁剪/居中。
这是我到目前为止所提出的:
BitmapRequestBuilder<Uri, Bitmap> bitmapRequest = Glide.with(imageView.getContext())
.load(Uri.parse("file:///android_asset/" + mPath))
.asBitmap();
switch (imageView.getScaleType()) {
case CENTER_CROP:
bitmapRequest.centerCrop();
break;
case FIT_CENTER:
case FIT_START:
case FIT_END:
bitmapRequest.fitCenter();
break;
default:
// no scaling applied to the ImageView under test
}
AtomicReference<Bitmap> bmRef = new AtomicReference<>();
bitmapRequest.into(new SimpleTarget<Bitmap>(
imageView.getMeasuredWidth(),
imageView.getMeasuredHeight()
) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
bmRef.set(resource);
}
});
// ???
try {
Bitmap expected = bmRef.get();
return expected.sameAs(bitmap);
} catch (Exception e) {
throw new IllegalStateException("could not load asset " + mPath, e);
}
现在这里的问题当然是我遇到了僵局。我在主线程上(匹配器在主线程IIRC上执行)并且Glide想要一个后端线程来加载位图,然后在主线程(在'onResourceReady')本身返回。所以我需要从外面等待内部发布的结果,同时保持主线程运行。
我(未成功)尝试通过Looper.loop()
中的// ???
推进当前循环器,并尝试了常规锁定/等待方法,但没有任何效果。我没有想法......
答案 0 :(得分:-2)
对于这样的情况,我使用简单的select conversionid
from conversationMembers
group by conversionid
having sum(userId = 2) > 0 and
sum(userId = 7) > 0 and
count(distinct userId) = 2;
,这不是最好的选择,但它对我有用。
sleep
答案 1 :(得分:-2)
这样做的一种方法是将比较逻辑保存在onResourceReady中, 然后,如果您正在使用任何事件总线,则使用结果触发事件,并将UI逻辑放入订阅此事件的方法中。如果没有使用任何事件总线,则LocalBroadcastManager广播结果。
例如:
private BroadcastReceiver receiver;
private void doWhateverYouWantWithResult(boolean result) {
}
bitmapRequest.into(new SimpleTarget<Bitmap>(
imageView.getMeasuredWidth(),
imageView.getMeasuredHeight()
) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
bmRef.set(resource);
Bitmap expected = resource;
boolean result = expected.sameAs(bitmap);
Intent localIntent =
new Intent(BROADCAST_ACTION)
// Puts the status into the Intent
.putExtra(MATCH_RESULT, result);
// Broadcasts the Intent to receivers in this app.
LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
}
});
@Override
public void onResume(){
super.onResume();
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean result = intent.getBooleanExtra(MATCH_RESULT);
doWhateverYouWantWithResult(result);
}
};
LocalBroadcastManager.getInstance(getContext())
.registerReceiver(receiver, new IntentFilter(BROADCAST_ACTION));
}
@Override
public void onPause(){
super.onPause();
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(receiver);
}