fix: 引入Settings的Module

This commit is contained in:
2024-12-10 14:57:24 +08:00
parent ad8fc8731d
commit df105485bd
6934 changed files with 896168 additions and 2 deletions

View File

@@ -0,0 +1,77 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.system
import android.content.Context
import android.content.Intent
import android.telephony.CarrierConfigManager
import androidx.core.os.persistableBundleOf
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.stub
import org.mockito.kotlin.verify
@RunWith(AndroidJUnit4::class)
class ClientInitiatedActionRepositoryTest {
private val mockCarrierConfigManager = mock<CarrierConfigManager>()
private val context = mock<Context> {
on { applicationContext } doReturn mock
on { getSystemService(CarrierConfigManager::class.java) } doReturn mockCarrierConfigManager
}
private val repository = ClientInitiatedActionRepository(context)
@Test
fun onSystemUpdate_notEnabled() {
mockCarrierConfigManager.stub {
on { getConfigForSubId(any(), any()) } doReturn persistableBundleOf()
}
repository.onSystemUpdate()
verify(context, never()).sendBroadcast(any())
}
@Test
fun onSystemUpdate_enabled() {
mockCarrierConfigManager.stub {
on { getConfigForSubId(any(), any()) } doReturn persistableBundleOf(
CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL to true,
CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_INTENT_STRING to ACTION,
)
}
repository.onSystemUpdate()
val intent = argumentCaptor<Intent> {
verify(context).sendBroadcast(capture())
}.firstValue
assertThat(intent.action).isEqualTo(ACTION)
}
private companion object {
const val ACTION = "ACTION"
}
}

View File

@@ -0,0 +1,113 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.system
import android.content.Context
import android.content.Intent
import android.os.UserManager
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.SettingsActivity
import com.android.settings.core.BasePreferenceController
import com.android.settings.development.DevelopmentSettingsDashboardFragment
import com.android.settingslib.spaprivileged.framework.common.userManager
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.doNothing
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.spy
import org.mockito.kotlin.stub
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
@RunWith(AndroidJUnit4::class)
class DeveloperOptionsControllerTest {
@get:Rule
val composeTestRule = createComposeRule()
private val mockUserManager = mock<UserManager>()
private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
on { userManager } doReturn mockUserManager
doNothing().whenever(mock).startActivity(any())
}
private val controller = DeveloperOptionsController(context, TEST_KEY)
@Test
fun getAvailabilityStatus_isAdminUser_returnAvailable() {
mockUserManager.stub {
on { isAdminUser } doReturn true
}
val availabilityStatus = controller.getAvailabilityStatus()
assertThat(availabilityStatus).isEqualTo(BasePreferenceController.AVAILABLE)
}
@Test
fun getAvailabilityStatus_notAdminUser_returnDisabledForUser() {
mockUserManager.stub {
on { isAdminUser } doReturn false
}
val availabilityStatus = controller.getAvailabilityStatus()
assertThat(availabilityStatus).isEqualTo(BasePreferenceController.DISABLED_FOR_USER)
}
@Test
fun title_isDisplayed() {
composeTestRule.setContent {
controller.DeveloperOptionsPreference()
}
composeTestRule.onNodeWithText(
context.getString(com.android.settingslib.R.string.development_settings_title)
).assertIsDisplayed()
}
@Test
fun onClick_launchDevelopmentSettingsDashboardFragment() {
composeTestRule.setContent {
controller.DeveloperOptionsPreference()
}
composeTestRule.onNodeWithText(
context.getString(com.android.settingslib.R.string.development_settings_title)
).performClick()
val intent = argumentCaptor<Intent> {
verify(context).startActivity(capture())
}.firstValue
assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
.isEqualTo(DevelopmentSettingsDashboardFragment::class.qualifiedName)
}
private companion object {
const val TEST_KEY = "test_key"
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.system
import android.content.Context
import android.os.Bundle
import android.os.SystemUpdateManager
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.spy
import org.mockito.kotlin.whenever
@RunWith(AndroidJUnit4::class)
class SystemUpdateManagerExtKtTest {
private val mockSystemUpdateManager = mock<SystemUpdateManager>()
private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
on { getSystemService(SystemUpdateManager::class.java) } doReturn mockSystemUpdateManager
}
@Test
fun getSystemUpdateInfo() = runTest {
val bundle = Bundle()
whenever(mockSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle)
val info = context.getSystemUpdateInfo()
assertThat(info).isSameInstanceAs(bundle)
}
}

View File

@@ -0,0 +1,168 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.system
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.os.SystemClock
import android.os.SystemUpdateManager
import android.os.UserManager
import androidx.lifecycle.testing.TestLifecycleOwner
import androidx.preference.Preference
import androidx.preference.PreferenceScreen
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.R
import com.android.settingslib.spaprivileged.framework.common.userManager
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.spy
import org.mockito.kotlin.whenever
@RunWith(AndroidJUnit4::class)
class SystemUpdatePreferenceControllerTest {
private val mockUserManager = mock<UserManager>()
private val mockSystemUpdateManager = mock<SystemUpdateManager>()
private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
on { userManager } doReturn mockUserManager
on { getSystemService(SystemUpdateManager::class.java) } doReturn mockSystemUpdateManager
}
private val resources = spy(context.resources) {
on { getBoolean(R.bool.config_show_system_update_settings) } doReturn true
}
private val preference = Preference(context).apply { key = KEY }
private val preferenceScreen = mock<PreferenceScreen> {
onGeneric { findPreference(KEY) } doReturn preference
}
private val controller = SystemUpdatePreferenceController(context, KEY)
@Before
fun setUp() {
whenever(context.resources).thenReturn(resources)
}
@Test
fun updateNonIndexable_ifAvailable_shouldNotUpdate() {
whenever(mockUserManager.isAdminUser).thenReturn(true)
val keys = mutableListOf<String>()
controller.updateNonIndexableKeys(keys)
assertThat(keys).isEmpty()
}
@Test
fun updateNonIndexable_ifNotAvailable_shouldUpdate() {
whenever(mockUserManager.isAdminUser).thenReturn(false)
val keys = mutableListOf<String>()
controller.updateNonIndexableKeys(keys)
assertThat(keys).containsExactly(KEY)
}
@Test
fun displayPrefs_ifVisible_butNotAdminUser_shouldNotDisplay() {
whenever(mockUserManager.isAdminUser).thenReturn(false)
controller.displayPreference(preferenceScreen)
assertThat(preference.isVisible).isFalse()
}
@Test
fun displayPrefs_ifAdminUser_butNotVisible_shouldNotDisplay() {
whenever(mockUserManager.isAdminUser).thenReturn(true)
whenever(resources.getBoolean(R.bool.config_show_system_update_settings)).thenReturn(false)
controller.displayPreference(preferenceScreen)
assertThat(preference.isVisible).isFalse()
}
@Test
fun displayPrefs_ifAvailable_shouldDisplay() {
whenever(mockUserManager.isAdminUser).thenReturn(true)
controller.displayPreference(preferenceScreen)
assertThat(preference.isVisible).isTrue()
}
@Test
fun updateState_systemUpdateStatusUnknown_shouldSetToAndroidVersion() {
val bundle = Bundle().apply {
putInt(SystemUpdateManager.KEY_STATUS, SystemUpdateManager.STATUS_UNKNOWN)
}
whenever(mockSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle)
controller.displayPreference(preferenceScreen)
controller.onViewCreated(TestLifecycleOwner())
SystemClock.sleep(100)
assertThat(preference.summary).isEqualTo(
context.getString(
R.string.android_version_summary,
Build.VERSION.RELEASE_OR_PREVIEW_DISPLAY,
)
)
}
@Test
fun updateState_systemUpdateStatusIdle_shouldSetToAndroidVersion() {
val testReleaseName = "ANDROID TEST VERSION"
val bundle = Bundle().apply {
putInt(SystemUpdateManager.KEY_STATUS, SystemUpdateManager.STATUS_IDLE)
putString(SystemUpdateManager.KEY_TITLE, testReleaseName)
}
whenever(mockSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle)
controller.displayPreference(preferenceScreen)
controller.onViewCreated(TestLifecycleOwner())
SystemClock.sleep(100)
assertThat(preference.summary)
.isEqualTo(context.getString(R.string.android_version_summary, testReleaseName))
}
@Test
fun updateState_systemUpdateInProgress_shouldSetToUpdatePending() {
val bundle = Bundle().apply {
putInt(SystemUpdateManager.KEY_STATUS, SystemUpdateManager.STATUS_WAITING_DOWNLOAD)
}
whenever(mockSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle)
controller.displayPreference(preferenceScreen)
controller.onViewCreated(TestLifecycleOwner())
SystemClock.sleep(100)
assertThat(preference.summary)
.isEqualTo(context.getString(R.string.android_version_pending_update_summary))
}
private companion object {
const val KEY = "test_key"
}
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.system
import android.content.Context
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.provider.Settings
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.eq
import org.mockito.kotlin.argThat
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.spy
import org.mockito.kotlin.stub
@RunWith(AndroidJUnit4::class)
class SystemUpdateRepositoryTest {
private val mockPackageManager = mock<PackageManager>()
private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
on { packageManager } doReturn mockPackageManager
}
private val repository = SystemUpdateRepository(context)
@Test
fun getSystemUpdateIntent_noResolveActivity_returnNull() {
val intent = repository.getSystemUpdateIntent()
assertThat(intent).isNull()
}
@Test
fun getSystemUpdateIntent_hasResolveActivity_returnIntent() {
mockPackageManager.stub {
on {
resolveActivity(
argThat { action == Settings.ACTION_SYSTEM_UPDATE_SETTINGS },
eq(PackageManager.MATCH_SYSTEM_ONLY),
)
} doReturn RESOLVE_INFO
}
val intent = repository.getSystemUpdateIntent()
assertThat(intent?.component?.packageName).isEqualTo(PACKAGE_NAME)
assertThat(intent?.component?.className).isEqualTo(ACTIVITY_NAME)
}
private companion object {
const val PACKAGE_NAME = "package.name"
const val ACTIVITY_NAME = "ActivityName"
val RESOLVE_INFO = ResolveInfo().apply {
activityInfo = ActivityInfo().apply {
packageName = PACKAGE_NAME
name = ACTIVITY_NAME
}
}
}
}