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.qs.tiles.impl.qr.ui
18 
19 import android.content.Intent
20 import android.graphics.drawable.TestStubDrawable
21 import android.widget.Switch
22 import androidx.test.ext.junit.runners.AndroidJUnit4
23 import androidx.test.filters.SmallTest
24 import com.android.systemui.SysuiTestCase
25 import com.android.systemui.common.shared.model.Icon
26 import com.android.systemui.kosmos.Kosmos
27 import com.android.systemui.qs.tiles.impl.custom.QSTileStateSubject
28 import com.android.systemui.qs.tiles.impl.qr.domain.model.QRCodeScannerTileModel
29 import com.android.systemui.qs.tiles.impl.qr.qsQRCodeScannerTileConfig
30 import com.android.systemui.qs.tiles.viewmodel.QSTileState
31 import com.android.systemui.util.mockito.mock
32 import org.junit.Before
33 import org.junit.Test
34 import org.junit.runner.RunWith
35 
36 @SmallTest
37 @RunWith(AndroidJUnit4::class)
38 class QRCodeScannerTileMapperTest : SysuiTestCase() {
39     private val kosmos = Kosmos()
40     private val config = kosmos.qsQRCodeScannerTileConfig
41 
42     private lateinit var mapper: QRCodeScannerTileMapper
43 
44     @Before
setupnull45     fun setup() {
46         mapper =
47             QRCodeScannerTileMapper(
48                 context.orCreateTestableResources
49                     .apply {
50                         addOverride(
51                             com.android.systemui.res.R.drawable.ic_qr_code_scanner,
52                             TestStubDrawable(),
53                         )
54                     }
55                     .resources,
56                 context.theme,
57             )
58     }
59 
60     @Test
availableModelnull61     fun availableModel() {
62         val mockIntent = mock<Intent>()
63         val inputModel = QRCodeScannerTileModel.Available(mockIntent)
64 
65         val outputState = mapper.map(config, inputModel)
66 
67         val expectedState = createQRCodeScannerTileState(QSTileState.ActivationState.INACTIVE, null)
68         QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState)
69     }
70 
71     @Test
temporarilyUnavailableModelnull72     fun temporarilyUnavailableModel() {
73         val inputModel = QRCodeScannerTileModel.TemporarilyUnavailable
74 
75         val outputState = mapper.map(config, inputModel)
76 
77         val expectedState =
78             createQRCodeScannerTileState(
79                 QSTileState.ActivationState.UNAVAILABLE,
80                 context.getString(
81                     com.android.systemui.res.R.string.qr_code_scanner_updating_secondary_label
82                 ),
83             )
84         QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState)
85     }
86 
createQRCodeScannerTileStatenull87     private fun createQRCodeScannerTileState(
88         activationState: QSTileState.ActivationState,
89         secondaryLabel: String?,
90     ): QSTileState {
91         val label = context.getString(com.android.systemui.res.R.string.qr_code_scanner_title)
92         return QSTileState(
93             Icon.Loaded(
94                 context.getDrawable(com.android.systemui.res.R.drawable.ic_qr_code_scanner)!!,
95                 null,
96             ),
97             com.android.systemui.res.R.drawable.ic_qr_code_scanner,
98             label,
99             activationState,
100             secondaryLabel,
101             setOf(QSTileState.UserAction.CLICK),
102             label,
103             null,
104             QSTileState.SideViewIcon.Chevron,
105             QSTileState.EnabledState.ENABLED,
106             Switch::class.qualifiedName,
107         )
108     }
109 }
110