2017-01-05 69 views
1

我正在爲正在編寫的應用程序編寫一些UI測試,並且需要根據推送通知切換圖像。爲了在沒有推送通知的情況下編寫測試,我將一個png文件放在androidTest res目錄中,然後使用來自測試資源的資源ID加載ContextCompat.getDrawable()。我還使用應用程序資源中的ID從應用程序資源中加載相同的圖像。應用程序資源與測試資源不同的可繪製加載

下面是加載資源代碼:

Drawable drawable1 = ContextCompat.getDrawable(setupRule.getActivity().getApplicationContext(), R.drawable.my_contact_pic); 
Drawable drawable2 = ContextCompat.getDrawable(setupRule.getActivity().getApplicationContext(), com.mycompany.myapp.devenv.test.R.drawable.my_contact_pic); 

drawable1是BitmapDrawable的一個實例,drawable2是NinePatchDrawable的一個實例。由於drawable2的基礎PNG是drawable1的PNG的副本,我希望它們都是BitmapDrawables,我可以在此編碼,但想知道爲什麼會發生這種情況以備將來參考。

+0

所以我終於想通了,從androidTest資源加載png,你必須指定InstrumentationRegister.getContext(),否則它會從應用程序資源中加載。我指定的資源ID指向NinePatch文件是巧合。 –

回答

0

所以我終於想通了,從androidTest資源加載png,你必須指定InstrumentationRegister.getContext(),否則它會從應用程序資源中加載。我指定的資源ID指向NinePatch文件是巧合。