1 /* 2 * Copyright (C) 2020 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 18 19 import android.os.Handler 20 import android.testing.AndroidTestingRunner 21 import android.testing.TestableLooper 22 import androidx.test.filters.SmallTest 23 import com.android.systemui.SysuiTestCase 24 import com.android.systemui.util.settings.FakeSettings 25 import com.android.systemui.util.settings.SecureSettings 26 import com.google.common.truth.Truth.assertThat 27 import junit.framework.Assert.fail 28 import org.junit.After 29 import org.junit.Before 30 import org.junit.Test 31 import org.junit.runner.RunWith 32 33 private typealias Callback = (Int, Boolean) -> Unit 34 35 @SmallTest 36 @RunWith(AndroidTestingRunner::class) 37 @TestableLooper.RunWithLooper 38 class SettingObserverTest : SysuiTestCase() { 39 40 companion object { 41 private const val TEST_SETTING = "setting" 42 private const val USER = 0 43 private const val OTHER_USER = 1 44 private const val DEFAULT_VALUE = 1 45 private val FAIL_CALLBACK: Callback = { _, _ -> fail("Callback should not be called") } 46 } 47 48 private lateinit var testableLooper: TestableLooper 49 private lateinit var setting: SettingObserver 50 private lateinit var secureSettings: SecureSettings 51 52 private lateinit var callback: Callback 53 54 @Before 55 fun setUp() { 56 testableLooper = TestableLooper.get(this) 57 secureSettings = FakeSettings() 58 59 setting = object : SettingObserver( 60 secureSettings, 61 Handler(testableLooper.looper), 62 TEST_SETTING, 63 USER, 64 DEFAULT_VALUE 65 ) { 66 override fun handleValueChanged(value: Int, observedChange: Boolean) { 67 callback(value, observedChange) 68 } 69 } 70 71 // Default empty callback 72 callback = { _, _ -> Unit } 73 } 74 75 @After 76 fun tearDown() { 77 setting.isListening = false 78 } 79 80 @Test 81 fun testNotListeningByDefault() { 82 callback = FAIL_CALLBACK 83 84 assertThat(setting.isListening).isFalse() 85 secureSettings.putIntForUser(TEST_SETTING, 2, USER) 86 testableLooper.processAllMessages() 87 } 88 89 @Test 90 fun testChangedWhenListeningCallsCallback() { 91 var changed = false 92 callback = { _, _ -> changed = true } 93 94 setting.isListening = true 95 secureSettings.putIntForUser(TEST_SETTING, 2, USER) 96 testableLooper.processAllMessages() 97 98 assertThat(changed).isTrue() 99 } 100 101 @Test 102 fun testListensToCorrectSetting() { 103 callback = FAIL_CALLBACK 104 105 setting.isListening = true 106 secureSettings.putIntForUser("other", 2, USER) 107 testableLooper.processAllMessages() 108 } 109 110 @Test 111 fun testGetCorrectValue() { 112 secureSettings.putIntForUser(TEST_SETTING, 2, USER) 113 assertThat(setting.value).isEqualTo(2) 114 115 secureSettings.putIntForUser(TEST_SETTING, 4, USER) 116 assertThat(setting.value).isEqualTo(4) 117 } 118 119 @Test 120 fun testSetValue() { 121 setting.value = 5 122 assertThat(secureSettings.getIntForUser(TEST_SETTING, USER)).isEqualTo(5) 123 } 124 125 @Test 126 fun testChangeUser() { 127 setting.isListening = true 128 setting.setUserId(OTHER_USER) 129 130 setting.isListening = true 131 assertThat(setting.currentUser).isEqualTo(OTHER_USER) 132 } 133 134 @Test 135 fun testDoesntListenInOtherUsers() { 136 callback = FAIL_CALLBACK 137 setting.isListening = true 138 139 secureSettings.putIntForUser(TEST_SETTING, 3, OTHER_USER) 140 testableLooper.processAllMessages() 141 } 142 143 @Test 144 fun testListensToCorrectUserAfterChange() { 145 var changed = false 146 callback = { _, _ -> changed = true } 147 148 setting.isListening = true 149 setting.setUserId(OTHER_USER) 150 secureSettings.putIntForUser(TEST_SETTING, 2, OTHER_USER) 151 testableLooper.processAllMessages() 152 153 assertThat(changed).isTrue() 154 } 155 156 @Test 157 fun testDefaultValue() { 158 // Check default value before listening 159 assertThat(setting.value).isEqualTo(DEFAULT_VALUE) 160 161 // Check default value if setting is not set 162 setting.isListening = true 163 assertThat(setting.value).isEqualTo(DEFAULT_VALUE) 164 } 165 }