我希望能夠將Espresso
監視器Picasso
作爲IdlingResource
,以便我可以在圖像加載成功後運行ViewMatcher
。監測濃縮咖啡中IdlingResource的畢加索
從瀏覽Picasso
源代碼,我不明白爲什麼這不起作用。下面是我的嘗試:
Picasso picasso = new Picasso.Builder(context).build();
Field dispatcherField = Picasso.class.getDeclaredField("dispatcher");
dispatcherField.setAccessible(true);
try {
Dispatcher dispatcher = (Dispatcher) dispatcherField.get(picasso);
Espresso.registerLooperAsIdlingResource(dispatcher.dispatcherThread.getLooper());
} catch (NoSuchFieldException e) {
throw new PicassoHasBeenRefactoredException();
} catch (Exception e) {
e.printStackTrace();
}
onView(withId(R.id.image_view)).check(matches(withImage(R.drawable.drawable)));
(是的,我知道的,反映是噁心,但我找不到才能上Looper
一個手柄的另一種方式)
但是,試圖當它導致這個錯誤從ImageView
得到Bitmap
:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
要檢查預期一旦圖像已加載測試運行,我想代替IdlingResource
檢查的的引入Thread.sleep(1000)
它通過了。
可以肯定IdlingResource設置不正確,更重要的是,在檢查Espresso視圖之前等待Picasso完成加載的正確方法是什麼?
不能使用[onSucess回調(https://square.github.io/picasso/javadoc/com/squareup/picasso/Callback.html #onSuccess--)從畢加索設置您的IdelingResource? –
@RahulTiwari我不想修改任何生產代碼來適應測試,除非你的意思是別的嗎? –
我在談論使用回調畢加索提供'onSuccess'和'onError'並儘可能地去除反射。所以是的,我在談論修改代碼,但它不會影響任何功能。 –