Merge "Add 'Archive' button to AppInfo screen" into main

This commit is contained in:
Mark Kim
2023-12-08 10:25:58 +00:00
committed by Android (Google) Code Review
9 changed files with 363 additions and 66 deletions

View File

@@ -0,0 +1,135 @@
/*
* 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.spa.app.appinfo
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInstaller
import android.content.pm.PackageManager
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.CloudUpload
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.R
import com.android.settingslib.spa.widget.button.ActionButton
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.spy
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
@RunWith(AndroidJUnit4::class)
class AppArchiveButtonTest {
@get:Rule
val composeTestRule = createComposeRule()
private val context: Context = spy(ApplicationProvider.getApplicationContext()) {}
private val packageInfoPresenter = mock<PackageInfoPresenter>()
private val userPackageManager = mock<PackageManager>()
private val packageInstaller = mock<PackageInstaller>()
private lateinit var appArchiveButton: AppArchiveButton
@Before
fun setUp() {
whenever(packageInfoPresenter.context).thenReturn(context)
whenever(packageInfoPresenter.userPackageManager).thenReturn(userPackageManager)
whenever(userPackageManager.packageInstaller).thenReturn(packageInstaller)
whenever(packageInfoPresenter.packageName).thenReturn(PACKAGE_NAME)
appArchiveButton = AppArchiveButton(packageInfoPresenter)
}
@Test
fun appArchiveButton_whenIsArchived_isDisabled() {
val app = ApplicationInfo().apply {
packageName = PACKAGE_NAME
isArchived = true
}
whenever(userPackageManager.isAppArchivable(app.packageName)).thenReturn(true)
val actionButton = setContent(app)
assertThat(actionButton.enabled).isFalse()
}
@Test
fun appArchiveButton_whenIsNotAppArchivable_isDisabled() {
val app = ApplicationInfo().apply {
packageName = PACKAGE_NAME
isArchived = false
}
whenever(userPackageManager.isAppArchivable(app.packageName)).thenReturn(false)
val actionButton = setContent(app)
assertThat(actionButton.enabled).isFalse()
}
@Test
fun appArchiveButton_displaysRightTextAndIcon() {
val app = ApplicationInfo().apply {
packageName = PACKAGE_NAME
isArchived = false
}
whenever(userPackageManager.isAppArchivable(app.packageName)).thenReturn(true)
val actionButton = setContent(app)
assertThat(actionButton.text).isEqualTo(context.getString(R.string.archive))
assertThat(actionButton.imageVector).isEqualTo(Icons.Outlined.CloudUpload)
}
@Test
fun appArchiveButton_clicked() {
val app = ApplicationInfo().apply {
packageName = PACKAGE_NAME
isArchived = false
}
whenever(userPackageManager.isAppArchivable(app.packageName)).thenReturn(true)
val actionButton = setContent(app)
actionButton.onClick()
verify(packageInstaller).requestArchive(
eq(PACKAGE_NAME),
any(),
eq(0)
)
}
private fun setContent(app: ApplicationInfo): ActionButton {
lateinit var actionButton: ActionButton
composeTestRule.setContent {
actionButton = appArchiveButton.getActionButton(app)
}
return actionButton
}
private companion object {
const val PACKAGE_NAME = "package.name"
}
}

View File

@@ -22,8 +22,10 @@ import android.content.pm.ApplicationInfo
import android.content.pm.FakeFeatureFlagsImpl
import android.content.pm.Flags
import android.content.pm.PackageInfo
import android.content.pm.PackageInstaller
import android.content.pm.PackageManager
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsEnabled
import androidx.compose.ui.test.assertIsNotDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
@@ -62,6 +64,9 @@ class AppButtonsTest {
@Mock
private lateinit var packageManager: PackageManager
@Mock
private lateinit var packageInstaller: PackageInstaller
private val featureFlags = FakeFeatureFlagsImpl()
@Before
@@ -74,6 +79,7 @@ class AppButtonsTest {
whenever(packageInfoPresenter.context).thenReturn(context)
whenever(packageInfoPresenter.packageName).thenReturn(PACKAGE_NAME)
whenever(packageInfoPresenter.userPackageManager).thenReturn(packageManager)
whenever(packageManager.packageInstaller).thenReturn(packageInstaller)
whenever(packageManager.getPackageInfo(PACKAGE_NAME, 0)).thenReturn(PACKAGE_INFO)
whenever(AppUtils.isMainlineModule(packageManager, PACKAGE_NAME)).thenReturn(false)
featureFlags.setFlag(Flags.FLAG_ARCHIVING, true)
@@ -118,8 +124,24 @@ class AppButtonsTest {
composeTestRule.onNodeWithText(context.getString(R.string.launch_instant_app)).assertIsNotDisplayed()
}
private fun setContent() {
whenever(packageInfoPresenter.flow).thenReturn(MutableStateFlow(PACKAGE_INFO))
@Test
fun uninstallButton_enabled_whenAppIsArchived() {
whenever(packageManager.getLaunchIntentForPackage(PACKAGE_NAME)).thenReturn(Intent())
featureFlags.setFlag(Flags.FLAG_ARCHIVING, true)
val packageInfo = PackageInfo().apply {
applicationInfo = ApplicationInfo().apply {
packageName = PACKAGE_NAME
isArchived = true
}
packageName = PACKAGE_NAME
}
setContent(packageInfo)
composeTestRule.onNodeWithText(context.getString(R.string.uninstall_text)).assertIsEnabled()
}
private fun setContent(packageInfo: PackageInfo = PACKAGE_INFO) {
whenever(packageInfoPresenter.flow).thenReturn(MutableStateFlow(packageInfo))
composeTestRule.setContent {
AppButtons(packageInfoPresenter, featureFlags)
}

View File

@@ -105,6 +105,7 @@ class PackageInfoPresenterTest {
fun isInterestedAppChange_archived_interested() {
val intent = Intent(Intent.ACTION_PACKAGE_REMOVED).apply {
data = Uri.parse("package:$PACKAGE_NAME")
putExtra(Intent.EXTRA_ARCHIVAL, true)
}
val isInterestedAppChange = packageInfoPresenter.isInterestedAppChange(intent)