1 /* 2 * Copyright (C) 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.systemui.graphics 18 19 import android.content.ContentProvider 20 import android.content.ContentValues 21 import android.content.Context 22 import android.database.Cursor 23 import android.net.Uri 24 import androidx.test.ext.junit.runners.AndroidJUnit4 25 import androidx.test.filters.SmallTest 26 import androidx.test.rule.provider.ProviderTestRule 27 import com.android.systemui.SysuiTestCase 28 import com.android.systemui.kosmos.testDispatcher 29 import com.android.systemui.kosmos.testScope 30 import com.android.systemui.testKosmos 31 import com.google.common.truth.Truth.assertThat 32 import kotlinx.coroutines.test.runTest 33 import org.junit.Before 34 import org.junit.Rule 35 import org.junit.Test 36 import org.junit.runner.RunWith 37 import org.mockito.kotlin.mock 38 import org.mockito.kotlin.whenever 39 40 const val AUTHORITY = "exception.provider.authority" 41 val TEST_URI = Uri.Builder().scheme("content").authority(AUTHORITY).path("path").build() 42 43 @SmallTest 44 @kotlinx.coroutines.ExperimentalCoroutinesApi 45 @RunWith(AndroidJUnit4::class) 46 class ImageLoaderContentProviderTest : SysuiTestCase() { 47 48 private val kosmos = testKosmos() 49 private val testScope = kosmos.testScope 50 private val mockContext = mock<Context>() 51 private lateinit var imageLoader: ImageLoader 52 53 @Rule 54 @JvmField 55 @Suppress("DEPRECATION") 56 public val providerTestRule = 57 ProviderTestRule.Builder(ExceptionThrowingContentProvider::class.java, AUTHORITY).build() 58 59 @Before setUpnull60 fun setUp() { 61 whenever(mockContext.contentResolver).thenReturn(providerTestRule.resolver) 62 imageLoader = ImageLoader(mockContext, kosmos.testDispatcher) 63 } 64 65 @Test(expected = IllegalArgumentException::class) loadFromTestContentProvider_throwsExceptionnull66 fun loadFromTestContentProvider_throwsException() { 67 // This checks if the resolution actually throws the exception from test provider. 68 mockContext.contentResolver.query(TEST_URI, null, null, null) 69 } 70 71 @Test loadFromRuntimeExceptionThrowingProvider_returnsNullnull72 fun loadFromRuntimeExceptionThrowingProvider_returnsNull() = 73 testScope.runTest { assertThat(imageLoader.loadBitmap(ImageLoader.Uri(TEST_URI))).isNull() } 74 } 75 76 class ExceptionThrowingContentProvider : ContentProvider() { querynull77 override fun query( 78 uri: Uri, 79 projection: Array<out String>?, 80 selection: String?, 81 selectionArgs: Array<out String>?, 82 sortOrder: String?, 83 ): Cursor? { 84 throw IllegalArgumentException("Test exception") 85 } 86 getTypenull87 override fun getType(uri: Uri): String? { 88 throw IllegalArgumentException("Test exception") 89 } 90 insertnull91 override fun insert(uri: Uri, values: ContentValues?): Uri? { 92 throw IllegalArgumentException("Test exception") 93 } 94 deletenull95 override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int { 96 throw IllegalArgumentException("Test exception") 97 } 98 updatenull99 override fun update( 100 uri: Uri, 101 values: ContentValues?, 102 selection: String?, 103 selectionArgs: Array<out String>?, 104 ): Int { 105 throw IllegalArgumentException("Test exception") 106 } 107 onCreatenull108 override fun onCreate(): Boolean = true 109 } 110