7

Đây là lần đầu tiên tôi sử dụng ý định espresso, tôi đã theo dõi IntentsBasicSamle được cung cấp trong dự án thử nghiệm android nhưng không được giải quyết.Làm cách nào để chọn mục đích hình ảnh bằng cách sử dụng các mục đích Espresso?

Trong ứng dụng của mình, tôi có một hoạt động trong đó người dùng chọn nhiều ảnh và sau đó ảnh được trả về được hiển thị trong khung nhìn GridView, Bây giờ bằng cách sử dụng mục đích của espresso, tôi muốn thử tính năng này mà không cần đến bộ chọn ảnh thực tế và trả về một số hình ảnh cụ thể.

Không có lỗi nhưng vẫn mở cửa sổ trình chọn hình ảnh khi tôi chạy thử nghiệm. Tôi cảm thấy tôi đang làm điều đó sai, nhưng tôi không nhận được nó hoạt động như thế nào.

Tôi đang thử nghiệm sử dụng apk.

Đây là cách chọn ảnh được gọi là

App đang

Đây là cách tôi kêu gọi chọn hình ảnh Ý định và kết quả được xử lý trong onActivityResult.

if (ApiUtils.checkApiLevel(18)) { 
     //API18+ 
     intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 
    } 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent,  getActivity().getString(R.string.fragment_image_selection_select_picture)), 1); 

Testing Mã

@RunWith(AndroidJUnit4.class) 
@LargeTest 
public class ImagesTests { 

private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.company.recorder.RecorderActivity"; 
private static Class<? extends Activity> activityClass; 
private ServiceValidatorIdlingResource serviceValidatorIdlingResource; 

static { 
    try { 
     activityClass = (Class<? extends Activity>) Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME); 
    } catch (ClassNotFoundException e) { 
     throw new RuntimeException(e); 
    } 
} 

/** 
* Change espresso default view idling time 
* so that test can wait for long time 
*/ 
@Before 
public void registerWaitService() { 
    EspressoSolo.setIdlingResourceTimeout(30); 

    Instrumentation.ActivityResult result = createImageCaptureActivityResultStub(); 

    // Stub the Intent. 
    intending(hasAction(Intent.ACTION_GET_CONTENT)).respondWith(result); 

    //For API 18 and above 
    intending(hasAction(Intent.EXTRA_ALLOW_MULTIPLE)).respondWith(result); 

    //intending(not(isInternal())).respondWith(result); 
} 

private Instrumentation.ActivityResult createImageCaptureActivityResultStub() { 
    // Put the drawable in a bundle. 
    Bundle bundle = new Bundle(); 
    Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + 
      InstrumentationRegistry.getTargetContext().getPackageName()+ '/' + 
      InstrumentationRegistry.getTargetContext().getResources().getResourceTypeName(R.drawable.abc_ic_menu_copy_mtrl_am_alpha) + '/' + 
      InstrumentationRegistry.getTargetContext().getResources().getResourceEntryName(R.drawable.abc_ic_menu_copy_mtrl_am_alpha)); 

    Parcelable parcelable = (Parcelable)imageUri; 

    ArrayList<Parcelable> parcels = new ArrayList<>(); 
    parcels.add(parcelable); 

    bundle.putParcelableArrayList(Intent.EXTRA_STREAM, parcels); 

    // Create the Intent that will include the bundle. 
    Intent resultData = new Intent(); 
    resultData.putExtras(bundle); 

    // Create the ActivityResult with the Intent. 
    return new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData); 
} 

@Rule 
public IntentsTestRule<?> mIntentsRule = new IntentsTestRule<>(
     activityClass); 


/** 
* In this test images are taken during the recording 
* add the images taken during the recording through add photos card 
* displayed after recording is stopped. 
* 
* @throws Exception 
*/ 
@Test 
public void testAddImagesFromPhotosCard() throws Exception { 

    onView(withId(Recorder.getId("recorderpage_record"))).perform(click()); 

    Log.d("called", "other package"); 
    IdlingResource idlingResource0 = new RecordingWaitIdlingResource(5000); 
    Espresso.registerIdlingResources(idlingResource0); 

    Log.d("called", "other package"); 
    onView(withId(Recorder.getId("card_topText"))).perform(click()); 
    Espresso.unregisterIdlingResources(idlingResource0); 
    onView(withId(Recorder.getId("recorderpage_stop"))).perform(click()); 
    //Log.d("called", "other package"); 
    IdlingResource idlingResource2 = new RecordingWaitIdlingResource(2000); 
    Espresso.registerIdlingResources(idlingResource2); 
    onView(withId(Recorder.getId("recorderpage_statustext"))) 
      .perform(click()); 
    Espresso.unregisterIdlingResources(idlingResource2); 

    onView(withId(Recorder.getId("pager"))).perform(swipeLeft()); 

    onData(allOf()).onChildView(withId(Recorder.getId("recordsOverflow"))). 
      atPosition(0).perform(click()); 

    IdlingResource idlingResource3 = new RecordingWaitIdlingResource(2000); 
    Espresso.registerIdlingResources(idlingResource3); 
    onData(allOf()).onChildView(withId(Recorder.getId("recordsOverflow"))). 
      atPosition(0).perform(click()); 
    Espresso.unregisterIdlingResources(idlingResource3); 
    onView(withText("Add an image")).perform(click()); 

    Instrumentation.ActivityResult result = createImageCaptureActivityResultStub(); 
    intending(toPackage("com.android.documentsui")).respondWith(result); 

    /* I actually dont know which preinstalled app is opened by intent, I saw this com.android.documentsui, also tried with photos and gallery package names */ 

    intending(toPackage("com.android.documentsui.DocumentsActivity")).respondWith(result); 

    intending(hasAction(Intent.ACTION_GET_CONTENT)).respondWith(result); 

    //Now click on add images 
    onView(withId(Recorder.getId("menu_fragment_imagewizard_selection_add"))).perform(click()); 

    //Just waits for 2seconds 
    IdlingResource idlingResource4 = new RecordingWaitIdlingResource(2000); 
    Espresso.registerIdlingResources(idlingResource4); 
    onView(withText("Add images")).perform(click()); 
    Espresso.unregisterIdlingResources(idlingResource4); 

} 
} 

Trả lời

3

Cuối cùng, Đây là những gì làm việc cho tôi, đăng câu trả lời của tôi

Bundle bundle = new Bundle(); 
    ArrayList<Parcelable> parcels = new ArrayList<>(); 
    Intent resultData = new Intent(); 
    Uri uri1 = Uri.parse("file://mnt/sdcard/img01.jpg"); 
    Parcelable parcelable1 = (Parcelable) uri1; 
    parcels.add(parcelable1); 
    bundle.putParcelableArrayList(Intent.EXTRA_STREAM, parcels); 
    // Create the Intent that will include the bundle. 
    resultData.putExtras(bundle); 

    intending(not(isInternal())).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData)); 
0

Nó làm việc Try It .. Trong @ Trước Menthod

Intent resultData = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
intending(not(isInternal())).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData)); 

và Test Medthod là:

onView(withId(R.id.imgSelect)).perform(click()); 
onView(withId(R.id.img1)).check(matches(CustomMatcher.hasDrawable())); 
Các vấn đề liên quan