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.screenrecord.ui 18 19 import android.graphics.drawable.TestStubDrawable 20 import android.text.TextUtils 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.screenrecord.domain.ui.ScreenRecordTileMapper 29 import com.android.systemui.qs.tiles.impl.screenrecord.qsScreenRecordTileConfig 30 import com.android.systemui.qs.tiles.viewmodel.QSTileState 31 import com.android.systemui.res.R 32 import com.android.systemui.screenrecord.data.model.ScreenRecordModel 33 import org.junit.Before 34 import org.junit.Test 35 import org.junit.runner.RunWith 36 37 @SmallTest 38 @RunWith(AndroidJUnit4::class) 39 class ScreenRecordTileMapperTest : SysuiTestCase() { 40 private val kosmos = Kosmos() 41 private val config = kosmos.qsScreenRecordTileConfig 42 43 private lateinit var mapper: ScreenRecordTileMapper 44 45 @Before setupnull46 fun setup() { 47 mapper = 48 ScreenRecordTileMapper( 49 context.orCreateTestableResources 50 .apply { 51 addOverride(R.drawable.qs_screen_record_icon_on, TestStubDrawable()) 52 addOverride(R.drawable.qs_screen_record_icon_off, TestStubDrawable()) 53 } 54 .resources, 55 context.theme, 56 ) 57 } 58 59 @Test activeStateMatchesRecordingDataModelnull60 fun activeStateMatchesRecordingDataModel() { 61 val inputModel = ScreenRecordModel.Recording 62 63 val outputState = mapper.map(config, inputModel) 64 65 val expectedState = 66 createScreenRecordTileState( 67 QSTileState.ActivationState.ACTIVE, 68 R.drawable.qs_screen_record_icon_on, 69 context.getString(R.string.quick_settings_screen_record_stop), 70 ) 71 QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState) 72 } 73 74 @Test activeStateMatchesStartingDataModelnull75 fun activeStateMatchesStartingDataModel() { 76 val timeLeft = 0L 77 val inputModel = ScreenRecordModel.Starting(timeLeft) 78 79 val outputState = mapper.map(config, inputModel) 80 81 val expectedState = 82 createScreenRecordTileState( 83 QSTileState.ActivationState.ACTIVE, 84 R.drawable.qs_screen_record_icon_on, 85 String.format("%d...", timeLeft), 86 ) 87 QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState) 88 } 89 90 @Test inactiveStateMatchesDisabledDataModelnull91 fun inactiveStateMatchesDisabledDataModel() { 92 val inputModel = ScreenRecordModel.DoingNothing 93 94 val outputState = mapper.map(config, inputModel) 95 96 val expectedState = 97 createScreenRecordTileState( 98 QSTileState.ActivationState.INACTIVE, 99 R.drawable.qs_screen_record_icon_off, 100 context.getString(R.string.quick_settings_screen_record_start), 101 ) 102 QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState) 103 } 104 createScreenRecordTileStatenull105 private fun createScreenRecordTileState( 106 activationState: QSTileState.ActivationState, 107 iconRes: Int, 108 secondaryLabel: String, 109 ): QSTileState { 110 val label = context.getString(R.string.quick_settings_screen_record_label) 111 112 return QSTileState( 113 Icon.Loaded(context.getDrawable(iconRes)!!, null), 114 iconRes, 115 label, 116 activationState, 117 secondaryLabel, 118 setOf(QSTileState.UserAction.CLICK), 119 if (TextUtils.isEmpty(secondaryLabel)) label 120 else TextUtils.concat(label, ", ", secondaryLabel), 121 null, 122 if (activationState == QSTileState.ActivationState.INACTIVE) 123 QSTileState.SideViewIcon.Chevron 124 else QSTileState.SideViewIcon.None, 125 QSTileState.EnabledState.ENABLED, 126 Switch::class.qualifiedName, 127 ) 128 } 129 } 130