1 /*
2  * Copyright (C) 2023 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 package com.android.systemui.clipboardoverlay
17 
18 import android.content.ContentResolver
19 import android.content.Context
20 import android.net.Uri
21 import androidx.test.filters.SmallTest
22 import com.android.systemui.SysuiTestCase
23 import com.android.systemui.util.mockito.whenever
24 import java.io.IOException
25 import kotlinx.coroutines.CoroutineScope
26 import kotlinx.coroutines.ExperimentalCoroutinesApi
27 import kotlinx.coroutines.test.StandardTestDispatcher
28 import kotlinx.coroutines.test.runTest
29 import org.junit.Assert.assertNull
30 import org.junit.Before
31 import org.junit.Test
32 import org.mockito.ArgumentMatchers.any
33 import org.mockito.ArgumentMatchers.eq
34 import org.mockito.Mock
35 import org.mockito.Mockito.verify
36 import org.mockito.MockitoAnnotations
37 
38 @SmallTest
39 @OptIn(ExperimentalCoroutinesApi::class)
40 class ClipboardImageLoaderTest : SysuiTestCase() {
41     @Mock private lateinit var mockContext: Context
42 
43     @Mock private lateinit var mockContentResolver: ContentResolver
44 
45     private lateinit var clipboardImageLoader: ClipboardImageLoader
46 
47     @Before
48     fun setup() {
49         MockitoAnnotations.initMocks(this)
50     }
51 
52     @Test
53     @Throws(IOException::class)
54     fun test_imageLoadSuccess() = runTest {
55         val testDispatcher = StandardTestDispatcher(this.testScheduler)
56         clipboardImageLoader =
57             ClipboardImageLoader(mockContext, testDispatcher, CoroutineScope(testDispatcher))
58         val testUri = Uri.parse("testUri")
59         whenever(mockContext.contentResolver).thenReturn(mockContentResolver)
60         whenever(mockContext.resources).thenReturn(context.resources)
61 
62         clipboardImageLoader.load(testUri)
63 
64         verify(mockContentResolver).loadThumbnail(eq(testUri), any(), any())
65     }
66 
67     @OptIn(ExperimentalCoroutinesApi::class)
68     @Test
69     @Throws(IOException::class)
70     fun test_imageLoadFailure() = runTest {
71         val testDispatcher = StandardTestDispatcher(this.testScheduler)
72         clipboardImageLoader =
73             ClipboardImageLoader(mockContext, testDispatcher, CoroutineScope(testDispatcher))
74         val testUri = Uri.parse("testUri")
75         whenever(mockContext.contentResolver).thenReturn(mockContentResolver)
76         whenever(mockContext.resources).thenReturn(context.resources)
77 
78         val res = clipboardImageLoader.load(testUri)
79 
80         verify(mockContentResolver).loadThumbnail(eq(testUri), any(), any())
81         assertNull(res)
82     }
83 }
84