1 /* 2 * Copyright (C) 2025 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.launcher3 18 19 import android.content.Context.MODE_PRIVATE 20 import android.platform.uiautomatorhelpers.DeviceHelpers.context 21 import androidx.test.ext.junit.runners.AndroidJUnit4 22 import androidx.test.filters.SmallTest 23 import com.android.launcher3.LauncherPrefs.Companion.backedUpItem 24 import com.google.common.truth.Truth.assertThat 25 import java.util.UUID 26 import org.junit.After 27 import org.junit.Test 28 import org.junit.runner.RunWith 29 30 @SmallTest 31 @RunWith(AndroidJUnit4::class) 32 class ProxyPrefsTest { 33 34 private val prefName = "pref-test-" + UUID.randomUUID().toString() 35 <lambda>null36 private val proxyPrefs by lazy { 37 ProxyPrefs( 38 context, 39 context.getSharedPreferences(prefName, MODE_PRIVATE).apply { edit().clear().commit() }, 40 ) 41 } <lambda>null42 private val launcherPrefs by lazy { LauncherPrefs(context) } 43 44 @After tearDownnull45 fun tearDown() { 46 context.deleteSharedPreferences(prefName) 47 } 48 49 @Test returns fallback value if presentnull50 fun `returns fallback value if present`() { 51 launcherPrefs.putSync(TEST_ENTRY.to("new_value")) 52 assertThat(proxyPrefs.get(TEST_ENTRY)).isEqualTo("new_value") 53 } 54 55 @Test returns default value if not presentnull56 fun `returns default value if not present`() { 57 launcherPrefs.removeSync(TEST_ENTRY) 58 assertThat(proxyPrefs.get(TEST_ENTRY)).isEqualTo("default_value") 59 } 60 61 @Test returns overridden value if presentnull62 fun `returns overridden value if present`() { 63 launcherPrefs.putSync(TEST_ENTRY.to("new_value")) 64 proxyPrefs.putSync(TEST_ENTRY.to("overridden_value")) 65 assertThat(proxyPrefs.get(TEST_ENTRY)).isEqualTo("overridden_value") 66 } 67 68 @Test value not present when removednull69 fun `value not present when removed`() { 70 launcherPrefs.putSync(TEST_ENTRY.to("new_value")) 71 proxyPrefs.removeSync(TEST_ENTRY) 72 assertThat(proxyPrefs.has(TEST_ENTRY)).isFalse() 73 } 74 75 @Test returns default if removednull76 fun `returns default if removed`() { 77 launcherPrefs.putSync(TEST_ENTRY.to("new_value")) 78 proxyPrefs.removeSync(TEST_ENTRY) 79 assertThat(proxyPrefs.get(TEST_ENTRY)).isEqualTo("default_value") 80 } 81 82 @Test value present on initnull83 fun `value present on init`() { 84 launcherPrefs.putSync(TEST_ENTRY.to("new_value")) 85 assertThat(proxyPrefs.has(TEST_ENTRY)).isTrue() 86 } 87 88 @Test value absent on initnull89 fun `value absent on init`() { 90 launcherPrefs.removeSync(TEST_ENTRY) 91 assertThat(proxyPrefs.has(TEST_ENTRY)).isFalse() 92 } 93 94 companion object { 95 96 val TEST_ENTRY = 97 backedUpItem("test_prefs", "default_value", EncryptionType.DEVICE_PROTECTED) 98 } 99 } 100