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 
17 package com.android.settings.deviceinfo.regulatory
18 
19 import android.content.Context
20 import android.graphics.drawable.Drawable
21 import android.os.SystemProperties
22 import androidx.annotation.DrawableRes
23 import androidx.core.graphics.drawable.toBitmap
24 import androidx.test.core.app.ApplicationProvider
25 import androidx.test.ext.junit.runners.AndroidJUnit4
26 import com.android.dx.mockito.inline.extended.ExtendedMockito
27 import com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn
28 import com.android.settings.deviceinfo.regulatory.RegulatoryInfo.KEY_COO
29 import com.android.settings.deviceinfo.regulatory.RegulatoryInfo.KEY_SKU
30 import com.android.settings.deviceinfo.regulatory.RegulatoryInfo.getRegulatoryInfo
31 import com.android.settings.tests.spa_unit.R
32 import com.google.common.truth.Truth.assertThat
33 import org.junit.After
34 import org.junit.Before
35 import org.junit.Test
36 import org.junit.runner.RunWith
37 import org.mockito.MockitoSession
38 import org.mockito.quality.Strictness
39 
40 @RunWith(AndroidJUnit4::class)
41 class RegulatoryInfoTest {
42     private lateinit var mockSession: MockitoSession
43 
44     private val context: Context = ApplicationProvider.getApplicationContext()
45 
46     @Before
setUpnull47     fun setUp() {
48         mockSession = ExtendedMockito.mockitoSession()
49             .initMocks(this)
50             .mockStatic(SystemProperties::class.java)
51             .strictness(Strictness.LENIENT)
52             .startMocking()
53     }
54 
55     @After
tearDownnull56     fun tearDown() {
57         mockSession.finishMocking()
58     }
59 
60     @Test
getRegulatoryInfo_noSkuProperty_shouldReturnDefaultLabelnull61     fun getRegulatoryInfo_noSkuProperty_shouldReturnDefaultLabel() {
62         doReturn("").`when` { SystemProperties.get(KEY_SKU) }
63 
64         val regulatoryInfo = context.getRegulatoryInfo()
65 
66         assertDrawableSameAs(regulatoryInfo, R.drawable.regulatory_info)
67     }
68 
69     @Test
getResourceId_noCooProperty_shouldReturnSkuLabelnull70     fun getResourceId_noCooProperty_shouldReturnSkuLabel() {
71         doReturn("sku").`when` { SystemProperties.get(KEY_SKU) }
72         doReturn("").`when` { SystemProperties.get(KEY_COO) }
73 
74         val regulatoryInfo = context.getRegulatoryInfo()
75 
76         assertDrawableSameAs(regulatoryInfo, R.drawable.regulatory_info_sku)
77     }
78 
79     @Test
getResourceId_hasSkuAndCooProperties_shouldReturnCooLabelnull80     fun getResourceId_hasSkuAndCooProperties_shouldReturnCooLabel() {
81         doReturn("sku1").`when` { SystemProperties.get(KEY_SKU) }
82         doReturn("coo").`when` { SystemProperties.get(KEY_COO) }
83 
84         val regulatoryInfo = context.getRegulatoryInfo()
85 
86         assertDrawableSameAs(regulatoryInfo, R.drawable.regulatory_info_sku1_coo)
87     }
88 
89     @Test
getResourceId_noCorrespondingCooLabel_shouldReturnSkuLabelnull90     fun getResourceId_noCorrespondingCooLabel_shouldReturnSkuLabel() {
91         doReturn("sku").`when` { SystemProperties.get(KEY_SKU) }
92         doReturn("unknown").`when` { SystemProperties.get(KEY_COO) }
93 
94         val regulatoryInfo = context.getRegulatoryInfo()
95 
96         assertDrawableSameAs(regulatoryInfo, R.drawable.regulatory_info_sku)
97     }
98 
99     @Test
getCoonull100     fun getCoo() {
101         doReturn(COO).`when` { SystemProperties.get(KEY_COO) }
102 
103         val coo = RegulatoryInfo.getCoo()
104 
105         assertThat(coo).isEqualTo(COO)
106     }
107 
108     @Test
getSkunull109     fun getSku() {
110         doReturn(SKU).`when` { SystemProperties.get(KEY_SKU) }
111 
112         val coo = RegulatoryInfo.getSku()
113 
114         assertThat(coo).isEqualTo(SKU)
115     }
116 
assertDrawableSameAsnull117     private fun assertDrawableSameAs(drawable: Drawable?, @DrawableRes resId: Int) {
118         val expected = context.getDrawable(resId)!!.toBitmap()
119         assertThat(drawable!!.toBitmap().sameAs(expected)).isTrue()
120     }
121 
122     private companion object {
123         const val SKU = "ABC"
124         const val COO = "CN"
125     }
126 }
127