2016-02-25 91 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); 
    }