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.role 18 19 import android.os.UserHandle 20 import androidx.test.ext.junit.runners.AndroidJUnit4 21 import com.android.server.role.RoleServicePlatformHelper 22 import com.google.common.truth.Truth.assertWithMessage 23 import org.junit.Before 24 import org.junit.Test 25 import org.junit.runner.RunWith 26 import org.mockito.Mockito.mock 27 28 @RunWith(AndroidJUnit4::class) 29 class RoleUserStateTest { 30 private var roleServicePlatformHelper = mock(RoleServicePlatformHelper::class.java) 31 private var roleUserStateCallback = mock(RoleUserState.Callback::class.java) 32 33 private val userId = UserHandle.myUserId() 34 private val roleUserState = 35 RoleUserState(userId, roleServicePlatformHelper, roleUserStateCallback, false) 36 37 @Before setUpnull38 fun setUp() { 39 setUpRole(ROLE_NAME_1, true) 40 setUpRole(ROLE_NAME_2, false) 41 } 42 setUpRolenull43 private fun setUpRole(roleName: String, fallbackEnabled: Boolean) { 44 roleUserState.addRoleName(roleName) 45 roleUserState.setFallbackEnabled(roleName, fallbackEnabled) 46 } 47 48 @Test testUpgradeVersion_upgradeNotNeeded_remainsUnchangednull49 fun testUpgradeVersion_upgradeNotNeeded_remainsUnchanged() { 50 roleUserState.version = RoleUserState.VERSION_FALLBACK_STATE_MIGRATED 51 val legacyFallbackDisabledRoles = listOf(ROLE_NAME_1, ROLE_NAME_2) 52 53 roleUserState.upgradeVersion(legacyFallbackDisabledRoles) 54 55 assertRoleFallbackState(ROLE_NAME_1, roleUserState.isFallbackEnabled(ROLE_NAME_1), true) 56 assertRoleFallbackState(ROLE_NAME_2, roleUserState.isFallbackEnabled(ROLE_NAME_2), false) 57 } 58 59 @Test testUpgradeVersion_upgradeNeeded_disabledFallbackStateMigratednull60 fun testUpgradeVersion_upgradeNeeded_disabledFallbackStateMigrated() { 61 roleUserState.version = RoleUserState.VERSION_UNDEFINED 62 val legacyFallbackDisabledRoles = listOf(ROLE_NAME_1, ROLE_NAME_2) 63 64 roleUserState.upgradeVersion(legacyFallbackDisabledRoles) 65 66 assertRoleFallbackState(ROLE_NAME_1, roleUserState.isFallbackEnabled(ROLE_NAME_1), false) 67 assertRoleFallbackState(ROLE_NAME_2, roleUserState.isFallbackEnabled(ROLE_NAME_2), false) 68 } 69 70 @Test testUpgradeVersion_upgradeNeeded_enabledFallbackStateMigratednull71 fun testUpgradeVersion_upgradeNeeded_enabledFallbackStateMigrated() { 72 roleUserState.version = RoleUserState.VERSION_UNDEFINED 73 val legacyFallbackDisabledRoles = emptyList<String>() 74 75 roleUserState.upgradeVersion(legacyFallbackDisabledRoles) 76 77 assertRoleFallbackState(ROLE_NAME_1, roleUserState.isFallbackEnabled(ROLE_NAME_1), true) 78 assertRoleFallbackState(ROLE_NAME_2, roleUserState.isFallbackEnabled(ROLE_NAME_2), true) 79 } 80 assertRoleFallbackStatenull81 private fun assertRoleFallbackState(roleName: String, actual: Boolean, expected: Boolean) { 82 assertWithMessage( 83 "Fallback enabled state for role: $roleName is $actual while" + 84 " is expected to be $expected" 85 ) 86 .that(actual) 87 .isEqualTo(expected) 88 } 89 90 companion object { 91 private const val ROLE_NAME_1 = "ROLE_NAME_1" 92 private const val ROLE_NAME_2 = "ROLE_NAME_2" 93 } 94 } 95