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 package com.android.avatarpicker.tests.unit.data
17 
18 
19 import android.content.res.Resources
20 import androidx.test.platform.app.InstrumentationRegistry
21 import com.android.avatarpicker.data.IllustrationsRepository
22 import com.android.avatarpicker.data.entity.ResourceEntity
23 import com.android.avatarpicker.tests.R
24 import com.google.common.truth.Truth
25 import kotlinx.coroutines.ExperimentalCoroutinesApi
26 import kotlinx.coroutines.launch
27 import kotlinx.coroutines.test.advanceUntilIdle
28 import kotlinx.coroutines.test.runTest
29 import org.junit.Assert.*
30 import org.junit.Before
31 import org.junit.Test
32 
33 class IllustrationsRepositoryTest {
34 
35     private lateinit var resources: Resources
36     private lateinit var illustrationsRepository: IllustrationsRepository
37 
38     @Before
setUpnull39     fun setUp() {
40         resources =
41             InstrumentationRegistry.getInstrumentation().getContext().getResources()
42         illustrationsRepository = IllustrationsRepository(resources)
43     }
44 
45     @OptIn(ExperimentalCoroutinesApi::class)
46     @Test
<lambda>null47     fun testEmptyArrays() = runTest {
48         illustrationsRepository.imagesDescriptionsResId = R.array.avatar_image_descriptions_empty
49         illustrationsRepository.imagesResId = R.array.avatar_images_empty
50         var items = listOf<ResourceEntity>()
51         launch { items = illustrationsRepository.getItems() }
52         advanceUntilIdle()
53         Truth.assertThat(items.size).isEqualTo(0)
54     }
55 
56 
57     @OptIn(ExperimentalCoroutinesApi::class)
58     @Test
<lambda>null59     fun testPngArrays() = runTest {
60         illustrationsRepository.imagesDescriptionsResId = R.array.avatar_image_descriptions_png
61         illustrationsRepository.imagesResId = R.array.avatar_images_png
62         var items = listOf<ResourceEntity>()
63         launch { items = illustrationsRepository.getItems() }
64         advanceUntilIdle()
65 
66         Truth.assertThat(items.size).isEqualTo(3)
67         Truth.assertThat(items[0].drawableId).isEqualTo(R.drawable.light_blue)
68         Truth.assertThat(items[0].descriptionId).isEqualTo(R.string.light_blue)
69         Truth.assertThat(items[0].colorInt).isNull()
70         Truth.assertThat(items[1].drawableId).isEqualTo(R.drawable.light_yellow)
71         Truth.assertThat(items[1].descriptionId).isEqualTo(R.string.light_yellow)
72         Truth.assertThat(items[1].colorInt).isNull()
73         Truth.assertThat(items[2].drawableId).isEqualTo(R.drawable.white)
74         Truth.assertThat(items[2].descriptionId).isEqualTo(R.string.white)
75         Truth.assertThat(items[2].colorInt).isNull()
76     }
77 
78 
79     @OptIn(ExperimentalCoroutinesApi::class)
80     @Test
<lambda>null81     fun testEmptyDescriptionArray() = runTest {
82         illustrationsRepository.imagesDescriptionsResId = R.array.avatar_image_descriptions_empty
83         illustrationsRepository.imagesResId = R.array.avatar_images_png
84         var ietms = listOf<ResourceEntity>()
85         launch { ietms = illustrationsRepository.getItems() }
86         advanceUntilIdle()
87 
88         Truth.assertThat(ietms.size).isEqualTo(3)
89         Truth.assertThat(ietms[0].drawableId).isEqualTo(R.drawable.light_blue)
90         Truth.assertThat(ietms[0].descriptionId).isEqualTo(R.string.default_user_icon_description)
91         Truth.assertThat(ietms[0].colorInt).isNull()
92         Truth.assertThat(ietms[1].drawableId).isEqualTo(R.drawable.light_yellow)
93         Truth.assertThat(ietms[1].descriptionId).isEqualTo(R.string.default_user_icon_description)
94         Truth.assertThat(ietms[1].colorInt).isNull()
95         Truth.assertThat(ietms[2].drawableId).isEqualTo(R.drawable.white)
96         Truth.assertThat(ietms[2].descriptionId).isEqualTo(R.string.default_user_icon_description)
97         Truth.assertThat(ietms[2].colorInt).isNull()
98     }
99 }