2016-02-25 69 views
9

我想写一个Espresso匹配器来验证'ImageView'是否有特定的位图集。由于该应用通过Glide进行图像加载,因此在考虑剪切/居中之前,我认为我必须在测试方面做同样的事情,然后才能实际比较预期位置和实际位图。与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()// ???推进目前的活套,并尝试了常规的锁定/等待方法,但没有任何工作。我没有想法...

回答

-1

对于这样的情况,我使用简单的sleep,这不是最好的选择,但它适用于我。

public static void waitForActivity(long time) { 
    try { 
     Thread.sleep(time); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
} 
+0

这并不在这种情况下工作,遗憾的是,刚刚又试了一次。 –

-1

的一种方式做到这一点,以保持内部onResourceReady, 你比较逻辑,然后如果你使用任何eventbus然后触发与结果事件,并把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); 
    }