2016-07-29 47 views
0

我在註冊表單中使用了第三方庫從圖庫中選擇圖像。我的註冊表單工作正常。現在我想用espresso來測試它。我現在面臨的最大問題是如何在測試時設置個人資料照片的圖像視圖?如何在使用espresso進行測試時在imageview中設置圖像?

+0

你使用哪個庫。究竟是什麼問題?你到目前爲止嘗試了什麼?未進行測試時,配置文件圖像如何設置? – thaussma

回答

0

您應該使用espresso-intents來檢測來自相機膠捲的意圖並設置照片。

這裏有我使用的方法:

public static void simulatePictureFromCameraRoll(Uri pictureUri) throws Exception { 
    Exception returnException = null; 
    Intent resultData = new Intent(); 

    resultData.setData(pictureUri); 

    Intents.init(); 
    try { 
     Matcher<Intent> expectedIntent = hasAction(Intent.ACTION_GET_CONTENT); 
     intending(expectedIntent).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData)); 
     onView(withId(R.id.lytProfImageChooseFromLibrary)).perform(click()); 
     intended(expectedIntent); 
    } 
    catch (Exception e) { 
     returnException = e; 
    } 
    finally { 
     Intents.release(); 
    } 

    if (returnException != null) { 
     throw returnException; 
    } 
} 

希望這有助於。

+0

我捕獲異常是因爲確保Intents.release已完成很重要,或者如果您的測試多次調用Intents.init,則可能會出現問題 – jeprubio

相關問題