MraidNativeCommandHandler { void storePicture(@NonNull final Context context, @NonNull final String imageUrl, @NonNull MraidCommandFailureListener failureListener) throws MraidCommandException { if (!isStorePictureSupported(context)) { MoPubLog.d("Error downloading file - the device does not have an SD card mounted, or " + "the Android permission is not granted."); throw new MraidCommandException("Error downloading file " + " - the device does not have an SD card mounted, " + "or the Android permission is not granted."); } if (context instanceof Activity) { showUserDialog(context, imageUrl, failureListener); } else { Toast.makeText(context, "Downloading image to Picture gallery...", Toast.LENGTH_SHORT).show(); downloadImage(context, imageUrl, failureListener); } }  boolean isStorePictureSupported(Context context); static final String ANDROID_CALENDAR_CONTENT_TYPE; }
@Ignore("Mraid 2.0") @Test public void showUserDownloadImageAlert_withActivityContext_shouldDisplayAlertDialog() throws Exception { response = new TestHttpResponseWithHeaders(200, FAKE_IMAGE_DATA); subject.storePicture(context, IMAGE_URI_VALUE, mraidCommandFailureListener); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); ShadowAlertDialog shadowAlertDialog = shadowOf(alertDialog); assertThat(alertDialog.isShowing()); assertThat(shadowAlertDialog.getTitle()).isEqualTo("Save Image"); assertThat(shadowAlertDialog.getMessage()).isEqualTo("Download image to Picture gallery?"); assertThat(shadowAlertDialog.isCancelable()).isTrue(); assertThat(alertDialog.getButton(BUTTON_POSITIVE).hasOnClickListeners()); assertThat(alertDialog.getButton(BUTTON_NEGATIVE)).isNotNull(); }
@Ignore("Mraid 2.0") @Test public void showUserDownloadImageAlert_whenOkayClicked_shouldDownloadImage() throws Exception { response = new TestHttpResponseWithHeaders(200, FAKE_IMAGE_DATA); Robolectric.addPendingHttpResponse(response); subject.storePicture(context, IMAGE_URI_VALUE, mraidCommandFailureListener); ShadowAlertDialog.getLatestAlertDialog().getButton(BUTTON_POSITIVE).performClick(); ThreadUtils.pause(TIME_TO_PAUSE_FOR_NETWORK); assertThat(expectedFile.exists()).isTrue(); assertThat(expectedFile.length()).isEqualTo(FAKE_IMAGE_DATA.length()); }
@Ignore("Mraid 2.0") @Test public void showUserDownloadImageAlert_whenCancelClicked_shouldDismissDialog() throws Exception { response = new TestHttpResponseWithHeaders(200, FAKE_IMAGE_DATA); subject.storePicture(context, IMAGE_URI_VALUE, mraidCommandFailureListener); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); ShadowAlertDialog shadowAlertDialog = shadowOf(alertDialog); alertDialog.getButton(BUTTON_NEGATIVE).performClick(); assertThat(shadowAlertDialog.hasBeenDismissed()).isTrue(); assertThat(expectedFile.exists()).isFalse(); assertThat(expectedFile.length()).isEqualTo(0); }
@Ignore("MRAID 2.0") @Test public void showUserDownloadImageAlert_withAppContext_shouldToastAndDownloadImage() throws Exception { response = new TestHttpResponseWithHeaders(200, FAKE_IMAGE_DATA); Robolectric.addPendingHttpResponse(response); assertThat(ShadowToast.shownToastCount()).isEqualTo(0); subject.storePicture(context.getApplicationContext(), IMAGE_URI_VALUE, mraidCommandFailureListener); ThreadUtils.pause(TIME_TO_PAUSE_FOR_NETWORK); assertThat(ShadowToast.shownToastCount()).isEqualTo(1); assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo("Downloading image to Picture gallery..."); Robolectric.runUiThreadTasks(); assertThat(expectedFile.exists()).isTrue(); assertThat(expectedFile.length()).isEqualTo(FAKE_IMAGE_DATA.length()); }
@Ignore("MRAID 2.0") @Test public void showUserDownloadImageAlert_withAppContext_whenDownloadImageFails_shouldDisplayFailureToastAndNotDownloadImage() throws Exception { response = new TestHttpResponseWithHeaders(200, FAKE_IMAGE_DATA); Robolectric.addPendingHttpResponse(response); assertThat(ShadowToast.shownToastCount()).isEqualTo(0); subject.storePicture(context, "this is an invalid image url and cannot be downloaded", mraidCommandFailureListener); ThreadUtils.pause(TIME_TO_PAUSE_FOR_NETWORK); assertThat(ShadowToast.shownToastCount()).isEqualTo(1); assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo("Downloading image to Picture gallery..."); Robolectric.runUiThreadTasks(); assertThat(ShadowToast.shownToastCount()).isEqualTo(2); assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo("Image failed to download."); assertThat(expectedFile.exists()).isFalse(); assertThat(expectedFile.length()).isEqualTo(0); }
@Ignore("Mraid 2.0") @Test public void showUserDownloadImageAlert_whenStorePictureNotSupported_shouldFireErrorEvent_andNotToastNorAlertDialog() throws Exception { Robolectric.getShadowApplication().denyPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE); subject.storePicture(context, "http: assertThat(ShadowToast.shownToastCount()).isEqualTo(0); assertThat(ShadowAlertDialog.getLatestAlertDialog()).isNull(); verify(mraidCommandFailureListener).onFailure(any(MraidCommandException.class)); }