From 4fc75124fe90daff88f213ba204a157e40ba2b96 Mon Sep 17 00:00:00 2001 From: Andrey Epin Date: Tue, 21 Mar 2023 10:56:58 -0700 Subject: Do not crash on reading URI metadata Do not crash if content resolver throws an exception while trying to read an URI metadata. Bug: 273890881 Test: check that the Chooser is not failing Change-Id: I161e29246bfcb1081a500b90c88c8c06c1d0df72 --- .../contentpreview/ChooserContentPreviewUiTest.kt | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'java/tests') diff --git a/java/tests/src/com/android/intentresolver/contentpreview/ChooserContentPreviewUiTest.kt b/java/tests/src/com/android/intentresolver/contentpreview/ChooserContentPreviewUiTest.kt index 04a136b4..58b8a21d 100644 --- a/java/tests/src/com/android/intentresolver/contentpreview/ChooserContentPreviewUiTest.kt +++ b/java/tests/src/com/android/intentresolver/contentpreview/ChooserContentPreviewUiTest.kt @@ -23,6 +23,8 @@ import android.graphics.Bitmap import android.net.Uri import com.android.intentresolver.ImageLoader import com.android.intentresolver.TestFeatureFlagRepository +import com.android.intentresolver.any +import com.android.intentresolver.anyOrNull import com.android.intentresolver.contentpreview.ChooserContentPreviewUi.ActionFactory import com.android.intentresolver.flags.Flags import com.android.intentresolver.mock @@ -143,6 +145,53 @@ class ChooserContentPreviewUiTest { verify(transitionCallback, times(1)).onAllTransitionElementsReady() } + @Test + fun test_ChooserContentPreview_single_uri_crashing_getType_to_file_preview() { + val uri = Uri.parse("content://$PROVIDER_NAME/test.pdf") + val targetIntent = Intent(Intent.ACTION_SEND).apply { + putExtra(Intent.EXTRA_STREAM, uri) + } + whenever(contentResolver.getType(any())) + .thenThrow(SecurityException("Test getType() exception")) + val testSubject = ChooserContentPreviewUi( + targetIntent, + contentResolver, + imageClassifier, + imageLoader, + actionFactory, + transitionCallback, + featureFlagRepository + ) + assertThat(testSubject.preferredContentPreview) + .isEqualTo(ContentPreviewType.CONTENT_PREVIEW_FILE) + verify(transitionCallback, times(1)).onAllTransitionElementsReady() + } + + @Test + fun test_ChooserContentPreview_single_uri_crashing_metadata_to_file_preview() { + val uri = Uri.parse("content://$PROVIDER_NAME/test.pdf") + val targetIntent = Intent(Intent.ACTION_SEND).apply { + putExtra(Intent.EXTRA_STREAM, uri) + } + whenever(contentResolver.getType(any())).thenReturn("application/pdf") + whenever(contentResolver.query(any(), anyOrNull(), anyOrNull(), anyOrNull())) + .thenThrow(SecurityException("Test query() exception")) + whenever(contentResolver.getStreamTypes(any(), any())) + .thenThrow(SecurityException("Test getStreamType() exception")) + val testSubject = ChooserContentPreviewUi( + targetIntent, + contentResolver, + imageClassifier, + imageLoader, + actionFactory, + transitionCallback, + featureFlagRepository + ) + assertThat(testSubject.preferredContentPreview) + .isEqualTo(ContentPreviewType.CONTENT_PREVIEW_FILE) + verify(transitionCallback, times(1)).onAllTransitionElementsReady() + } + @Test fun test_ChooserContentPreview_single_uri_with_preview_to_image_preview() { val uri = Uri.parse("content://$PROVIDER_NAME/test.pdf") @@ -283,4 +332,5 @@ class ChooserContentPreviewUiTest { .isEqualTo(ContentPreviewType.CONTENT_PREVIEW_FILE) verify(transitionCallback, times(1)).onAllTransitionElementsReady() } + } -- cgit v1.2.3-59-g8ed1b