1 /* 2 * Copyright (C) 2021 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.external 18 19 import android.content.ComponentName 20 import android.content.Context 21 import android.content.SharedPreferences 22 import android.service.quicksettings.Tile 23 import androidx.test.ext.junit.runners.AndroidJUnit4 24 import androidx.test.filters.SmallTest 25 import com.android.systemui.SysuiTestCase 26 import com.android.systemui.util.mockito.capture 27 import com.android.systemui.util.mockito.eq 28 import com.google.common.truth.Truth.assertThat 29 import org.junit.Before 30 import org.junit.Test 31 import org.junit.runner.RunWith 32 import org.mockito.Answers 33 import org.mockito.ArgumentCaptor 34 import org.mockito.ArgumentMatchers.any 35 import org.mockito.ArgumentMatchers.anyInt 36 import org.mockito.ArgumentMatchers.anyString 37 import org.mockito.Captor 38 import org.mockito.Mock 39 import org.mockito.Mockito.verify 40 import org.mockito.Mockito.`when` 41 import org.mockito.MockitoAnnotations 42 43 @SmallTest 44 @RunWith(AndroidJUnit4::class) 45 class CustomTileStatePersisterTest : SysuiTestCase() { 46 47 companion object { 48 private val TEST_COMPONENT = ComponentName("pkg", "cls") 49 private const val TEST_USER = 0 50 private val KEY = TileServiceKey(TEST_COMPONENT, TEST_USER) 51 52 private const val TEST_STATE = Tile.STATE_INACTIVE 53 private const val TEST_LABEL = "test_label" 54 private const val TEST_SUBTITLE = "test_subtitle" 55 private const val TEST_CONTENT_DESCRIPTION = "test_content_description" 56 private const val TEST_STATE_DESCRIPTION = "test_state_description" 57 private const val TEST_DEFAULT_LABEL = "default_label" 58 Tilenull59 private fun Tile.isEqualTo(other: Tile): Boolean { 60 return state == other.state && 61 label == other.label && 62 subtitle == other.subtitle && 63 contentDescription == other.contentDescription && 64 stateDescription == other.stateDescription 65 } 66 } 67 68 @Mock 69 private lateinit var mockContext: Context 70 @Mock 71 private lateinit var sharedPreferences: SharedPreferences 72 @Mock(answer = Answers.RETURNS_SELF) 73 private lateinit var editor: SharedPreferences.Editor 74 private lateinit var tile: Tile 75 private lateinit var customTileStatePersister: CustomTileStatePersister 76 77 @Captor 78 private lateinit var stringCaptor: ArgumentCaptor<String> 79 80 @Before setUpnull81 fun setUp() { 82 MockitoAnnotations.initMocks(this) 83 `when`(mockContext.getSharedPreferences(anyString(), anyInt())) 84 .thenReturn(sharedPreferences) 85 `when`(sharedPreferences.edit()).thenReturn(editor) 86 87 tile = Tile() 88 customTileStatePersister = CustomTileStatePersisterImpl(mockContext) 89 } 90 91 @Test testWriteStatenull92 fun testWriteState() { 93 tile.apply { 94 state = TEST_STATE 95 label = TEST_LABEL 96 subtitle = TEST_SUBTITLE 97 contentDescription = TEST_CONTENT_DESCRIPTION 98 stateDescription = TEST_STATE_DESCRIPTION 99 } 100 101 customTileStatePersister.persistState(KEY, tile) 102 103 verify(editor).putString(eq(KEY.toString()), capture(stringCaptor)) 104 105 assertThat(tile.isEqualTo(readTileFromString(stringCaptor.value))).isTrue() 106 } 107 108 @Test testReadStatenull109 fun testReadState() { 110 tile.apply { 111 state = TEST_STATE 112 label = TEST_LABEL 113 subtitle = TEST_SUBTITLE 114 contentDescription = TEST_CONTENT_DESCRIPTION 115 stateDescription = TEST_STATE_DESCRIPTION 116 } 117 118 `when`(sharedPreferences.getString(eq(KEY.toString()), any())) 119 .thenReturn(writeToString(tile)) 120 121 assertThat(tile.isEqualTo(customTileStatePersister.readState(KEY)!!)).isTrue() 122 } 123 124 @Test testReadStateDefaultnull125 fun testReadStateDefault() { 126 `when`(sharedPreferences.getString(any(), any())).thenAnswer { 127 it.getArgument(1) 128 } 129 130 assertThat(customTileStatePersister.readState(KEY)).isNull() 131 } 132 133 @Test testStoreNullsnull134 fun testStoreNulls() { 135 assertThat(tile.label).isNull() 136 137 customTileStatePersister.persistState(KEY, tile) 138 139 verify(editor).putString(eq(KEY.toString()), capture(stringCaptor)) 140 141 assertThat(readTileFromString(stringCaptor.value).label).isNull() 142 } 143 144 @Test testReadNullsnull145 fun testReadNulls() { 146 assertThat(tile.label).isNull() 147 148 `when`(sharedPreferences.getString(eq(KEY.toString()), any())) 149 .thenReturn(writeToString(tile)) 150 151 assertThat(customTileStatePersister.readState(KEY)!!.label).isNull() 152 } 153 154 @Test testRemoveStatenull155 fun testRemoveState() { 156 customTileStatePersister.removeState(KEY) 157 158 verify(editor).remove(KEY.toString()) 159 } 160 161 @Test testWithDefaultLabel_notStorednull162 fun testWithDefaultLabel_notStored() { 163 tile.setDefaultLabel(TEST_DEFAULT_LABEL) 164 165 `when`(sharedPreferences.getString(eq(KEY.toString()), any())) 166 .thenReturn(writeToString(tile)) 167 168 assertThat(customTileStatePersister.readState(KEY)!!.label).isNull() 169 } 170 } 171