1 /* <lambda>null2 * Copyright 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.intentresolver.contentpreview 18 19 import android.content.ContentInterface 20 import android.database.MatrixCursor 21 import android.media.MediaMetadata 22 import android.net.Uri 23 import android.provider.DocumentsContract 24 import com.google.common.truth.Truth.assertWithMessage 25 import org.junit.Test 26 import org.mockito.kotlin.any 27 import org.mockito.kotlin.anyOrNull 28 import org.mockito.kotlin.eq 29 import org.mockito.kotlin.mock 30 import org.mockito.kotlin.whenever 31 32 class UriMetadataReaderTest { 33 private val uri = Uri.parse("content://org.pkg.app/item") 34 private val contentResolver = mock<ContentInterface>() 35 36 @Test 37 fun testImageUri() { 38 val mimeType = "image/png" 39 whenever(contentResolver.getType(uri)).thenReturn(mimeType) 40 val testSubject = UriMetadataReaderImpl(contentResolver, DefaultMimeTypeClassifier) 41 42 testSubject.getMetadata(uri).let { fileInfo -> 43 assertWithMessage("Wrong uri").that(fileInfo.uri).isEqualTo(uri) 44 assertWithMessage("Wrong mime type").that(fileInfo.mimeType).isEqualTo(mimeType) 45 assertWithMessage("Wrong preview URI").that(fileInfo.previewUri).isEqualTo(uri) 46 } 47 } 48 49 @Test 50 fun testFileUriWithImageTypeSupport() { 51 val mimeType = "application/pdf" 52 val imageType = "image/png" 53 whenever(contentResolver.getType(uri)).thenReturn(mimeType) 54 whenever(contentResolver.getStreamTypes(eq(uri), any())).thenReturn(arrayOf(imageType)) 55 val testSubject = UriMetadataReaderImpl(contentResolver, DefaultMimeTypeClassifier) 56 57 testSubject.getMetadata(uri).let { fileInfo -> 58 assertWithMessage("Wrong uri").that(fileInfo.uri).isEqualTo(uri) 59 assertWithMessage("Wrong mime type").that(fileInfo.mimeType).isEqualTo(mimeType) 60 assertWithMessage("Wrong preview URI").that(fileInfo.previewUri).isEqualTo(uri) 61 } 62 } 63 64 @Test 65 fun testFileUriWithThumbnailSupport() { 66 val mimeType = "application/pdf" 67 whenever(contentResolver.getType(uri)).thenReturn(mimeType) 68 val columns = arrayOf(DocumentsContract.Document.COLUMN_FLAGS) 69 whenever(contentResolver.query(eq(uri), eq(columns), anyOrNull(), anyOrNull())) 70 .thenReturn( 71 MatrixCursor(columns).apply { 72 addRow(arrayOf(DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL)) 73 } 74 ) 75 val testSubject = UriMetadataReaderImpl(contentResolver, DefaultMimeTypeClassifier) 76 77 testSubject.getMetadata(uri).let { fileInfo -> 78 assertWithMessage("Wrong uri").that(fileInfo.uri).isEqualTo(uri) 79 assertWithMessage("Wrong mime type").that(fileInfo.mimeType).isEqualTo(mimeType) 80 assertWithMessage("Wrong preview URI").that(fileInfo.previewUri).isEqualTo(uri) 81 } 82 } 83 84 @Test 85 fun testFileUriWithPreviewUri() { 86 val mimeType = "application/pdf" 87 val previewUri = uri.buildUpon().appendQueryParameter("preview", null).build() 88 whenever(contentResolver.getType(uri)).thenReturn(mimeType) 89 val columns = arrayOf(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI) 90 whenever(contentResolver.query(eq(uri), eq(columns), anyOrNull(), anyOrNull())) 91 .thenReturn(MatrixCursor(columns).apply { addRow(arrayOf(previewUri.toString())) }) 92 val testSubject = UriMetadataReaderImpl(contentResolver, DefaultMimeTypeClassifier) 93 94 testSubject.getMetadata(uri).let { fileInfo -> 95 assertWithMessage("Wrong uri").that(fileInfo.uri).isEqualTo(uri) 96 assertWithMessage("Wrong mime type").that(fileInfo.mimeType).isEqualTo(mimeType) 97 assertWithMessage("Wrong preview URI").that(fileInfo.previewUri).isEqualTo(previewUri) 98 } 99 } 100 } 101