Restricting testExtractDocumentId and testDocumentProviderContentQuery test cases on API level 33 since numerous security updates have been included, preventing us from modifying the default behavior of ContentResolver. So running the test case on this APi level leads to test failure.

This commit is contained in:
MohitMaliFtechiz 2024-03-16 20:06:30 +05:30
parent dbbd02b08b
commit e1c69aa259

View File

@ -354,97 +354,109 @@ class FileUtilsInstrumentationTest {
@Test @Test
fun testExtractDocumentId() { fun testExtractDocumentId() {
val dummyDownloadUriData = arrayOf( // We are not running this test case on Android 13 and above. In this version,
DummyUrlData( // numerous security updates have been included, preventing us from modifying the
null, // default behavior of ContentResolver.
"raw:$expectedFilePath", // Therefore, we are restricting this test to API level 33 and below.
Uri.parse("${downloadDocumentUriPrefix}raw%3A%2Fstorage%2Femulated%2F0%2F$commonUri") if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
), val dummyDownloadUriData = arrayOf(
DummyUrlData( DummyUrlData(
null, null,
expectedFilePath, "raw:$expectedFilePath",
Uri.parse("$downloadDocumentUriPrefix%2Fstorage%2Femulated%2F0%2F$commonUri") Uri.parse("${downloadDocumentUriPrefix}raw%3A%2Fstorage%2Femulated%2F0%2F$commonUri")
), ),
DummyUrlData( DummyUrlData(
null, null,
"", expectedFilePath,
Uri.parse(downloadUriPrefix) Uri.parse("$downloadDocumentUriPrefix%2Fstorage%2Femulated%2F0%2F$commonUri")
) ),
) DummyUrlData(
null,
dummyDownloadUriData.forEach { dummyUrlData -> "",
dummyUrlData.uri?.let { uri -> Uri.parse(downloadUriPrefix)
Assertions.assertEquals(
FileUtils.extractDocumentId(uri, DocumentResolverWrapper()),
dummyUrlData.expectedFileName
) )
} )
}
// Testing with a dynamically generated URI. This URI creates at runtime, dummyDownloadUriData.forEach { dummyUrlData ->
// and passing it statically would result in an `IllegalArgumentException` exception. dummyUrlData.uri?.let { uri ->
// Therefore, we simulate this scenario using the `DocumentsContractWrapper` Assertions.assertEquals(
// to conduct the test. FileUtils.extractDocumentId(uri, DocumentResolverWrapper()),
val mockDocumentsContractWrapper: DocumentResolverWrapper = mockk() dummyUrlData.expectedFileName
val expectedDocumentId = "1000020403" )
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { }
// Below Android 10, download URI schemes are of the content type, such as: }
// content://com.android.chrome.FileProvider/downloads/alpinelinux_en_all_nopic_2022-12.zim
// As a result, this method will not be called during that time. We are not testing it on // Testing with a dynamically generated URI. This URI creates at runtime,
// Android versions below 10, as it would result in an "IllegalArgumentException" exception. // and passing it statically would result in an `IllegalArgumentException` exception.
val mockedUri = Uri.parse("$downloadUriPrefix$expectedDocumentId") // Therefore, we simulate this scenario using the `DocumentsContractWrapper`
every { mockDocumentsContractWrapper.getDocumentId(mockedUri) } returns expectedDocumentId // to conduct the test.
val actualDocumentId = FileUtils.extractDocumentId(mockedUri, mockDocumentsContractWrapper) val mockDocumentsContractWrapper: DocumentResolverWrapper = mockk()
assertEquals(expectedDocumentId, actualDocumentId) val expectedDocumentId = "1000020403"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// Below Android 10, download URI schemes are of the content type, such as:
// content://com.android.chrome.FileProvider/downloads/alpinelinux_en_all_nopic_2022-12.zim
// As a result, this method will not be called during that time. We are not testing it on
// Android versions below 10, as it would result in an "IllegalArgumentException" exception.
val mockedUri = Uri.parse("$downloadUriPrefix$expectedDocumentId")
every { mockDocumentsContractWrapper.getDocumentId(mockedUri) } returns expectedDocumentId
val actualDocumentId = FileUtils.extractDocumentId(mockedUri, mockDocumentsContractWrapper)
assertEquals(expectedDocumentId, actualDocumentId)
}
} }
} }
@Test @Test
fun testDocumentProviderContentQuery() { fun testDocumentProviderContentQuery() {
// test to get the download uri on old device // We are not running this test case on Android 13 and above. In this version,
testWithDownloadUri( // numerous security updates have been included, preventing us from modifying the
Uri.parse("${downloadDocumentUriPrefix}raw%3A%2Fstorage%2Femulated%2F0%2F$commonUri"), // default behavior of ContentResolver.
expectedFilePath // Therefore, we are restricting this test to API level 33 and below.
) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
// test to get the download uri on old device
testWithDownloadUri(
Uri.parse("${downloadDocumentUriPrefix}raw%3A%2Fstorage%2Femulated%2F0%2F$commonUri"),
expectedFilePath
)
// test to get the download uri on new device // test to get the download uri on new device
testWithDownloadUri( testWithDownloadUri(
Uri.parse("$downloadDocumentUriPrefix%2Fstorage%2Femulated%2F0%2F$commonUri"), Uri.parse("$downloadDocumentUriPrefix%2Fstorage%2Femulated%2F0%2F$commonUri"),
expectedFilePath expectedFilePath
) )
// test with all possible download uris // test with all possible download uris
val contentUriPrefixes = arrayOf( val contentUriPrefixes = arrayOf(
"content://downloads/public_downloads", "content://downloads/public_downloads",
"content://downloads/my_downloads", "content://downloads/my_downloads",
"content://downloads/all_downloads" "content://downloads/all_downloads"
) )
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// Below Android 10, download URI schemes are of the content type, such as: // Below Android 10, download URI schemes are of the content type, such as:
// content://com.android.chrome.FileProvider/downloads/alpinelinux_en_all_nopic_2022-12.zim // content://com.android.chrome.FileProvider/downloads/alpinelinux_en_all_nopic_2022-12.zim
// As a result, this method will not be called during that time. We are not testing it on // As a result, this method will not be called during that time. We are not testing it on
// Android versions below 10, as it would result in an "IllegalArgumentException" exception. // Android versions below 10, as it would result in an "IllegalArgumentException" exception.
contentUriPrefixes.forEach { contentUriPrefixes.forEach {
val mockDocumentsContractWrapper: DocumentResolverWrapper = mockk() val mockDocumentsContractWrapper: DocumentResolverWrapper = mockk()
val expectedDocumentId = "1000020403" val expectedDocumentId = "1000020403"
val mockedUri = Uri.parse("$it/$expectedDocumentId") val mockedUri = Uri.parse("$it/$expectedDocumentId")
every { mockDocumentsContractWrapper.getDocumentId(mockedUri) } returns expectedDocumentId every { mockDocumentsContractWrapper.getDocumentId(mockedUri) } returns expectedDocumentId
every { every {
mockDocumentsContractWrapper.query( mockDocumentsContractWrapper.query(
context!!, context!!,
mockedUri,
"_data",
null,
null,
null
)
} returns expectedFilePath
testWithDownloadUri(
mockedUri, mockedUri,
"_data", expectedFilePath,
null, mockDocumentsContractWrapper
null,
null
) )
} returns expectedFilePath }
testWithDownloadUri(
mockedUri,
expectedFilePath,
mockDocumentsContractWrapper
)
} }
} }
} }