Merge "[2/n] Add aspect ratio app list page under apps" into udc-qpr-dev am: 4e86d0d6b3

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/23819684

Change-Id: Iac8fc9b4d80e287a84e4a7d2917e76954ae64a9c
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Graciela Putri
2023-07-21 22:13:34 +00:00
committed by Automerger Merge Worker
12 changed files with 796 additions and 0 deletions

View File

@@ -0,0 +1,196 @@
/*
* 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.appcompat
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_SPLIT_SCREEN
import android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_UNSET
import android.os.Build
import androidx.compose.runtime.State
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.R
import com.android.settingslib.spa.framework.compose.stateOf
import com.android.settingslib.spa.testutils.FakeNavControllerWrapper
import com.android.settingslib.spa.testutils.firstWithTimeoutOrNull
import com.android.settingslib.spaprivileged.template.app.AppListItemModel
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
/**
* To run this test: atest SettingsSpaUnitTests:UserAspectRatioAppsPageProviderTest
*/
@RunWith(AndroidJUnit4::class)
class UserAspectRatioAppsPageProviderTest {
@get:Rule
val composeTestRule = createComposeRule()
private val context: Context = ApplicationProvider.getApplicationContext()
private val fakeNavControllerWrapper = FakeNavControllerWrapper()
@Test
fun aspectRatioAppsPageProvider_name() {
assertThat(UserAspectRatioAppsPageProvider.name).isEqualTo(EXPECTED_PROVIDER_NAME)
}
@Test
fun injectEntry_title() {
setInjectEntry()
composeTestRule.onNodeWithText(context.getString(R.string.screen_size_title))
.assertIsDisplayed()
}
@Test
fun injectEntry_summary() {
setInjectEntry()
composeTestRule.onNodeWithText(context.getString(R.string.screen_size_summary, Build.MODEL))
.assertIsDisplayed()
}
@Test
fun injectEntry_onClick_navigate() {
setInjectEntry()
composeTestRule.onNodeWithText(context.getString(R.string.screen_size_title)).performClick()
assertThat(fakeNavControllerWrapper.navigateCalledWith).isEqualTo("UserAspectRatioAppsPage")
}
private fun setInjectEntry() {
composeTestRule.setContent {
fakeNavControllerWrapper.Wrapper {
UserAspectRatioAppsPageProvider.buildInjectEntry().build().UiLayout()
}
}
}
@Test
fun title_displayed() {
composeTestRule.setContent {
UserAspectRatioAppList {}
}
composeTestRule.onNodeWithText(context.getString(R.string.screen_size_title))
.assertIsDisplayed()
}
@Test
fun item_labelDisplayed() {
setItemContent()
composeTestRule.onNodeWithText(LABEL).assertIsDisplayed()
}
@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun aspectRatioAppListModel_transform() = runTest {
val listModel = UserAspectRatioAppListModel(context)
val recordListFlow = listModel.transform(flowOf(USER_ID), flowOf(listOf(APP)))
val recordList = recordListFlow.firstWithTimeoutOrNull()!!
assertThat(recordList).hasSize(1)
assertThat(recordList[0].app).isSameInstanceAs(APP)
}
@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun aspectRatioAppListModel_filter() = runTest {
val listModel = UserAspectRatioAppListModel(context)
val recordListFlow = listModel.filter(flowOf(USER_ID), 0,
flowOf(listOf(APP_RECORD_NOT_DISPLAYED, APP_RECORD_SUGGESTED)))
val recordList = checkNotNull(recordListFlow.firstWithTimeoutOrNull())
assertThat(recordList).containsExactly(APP_RECORD_SUGGESTED)
}
private fun setItemContent() {
composeTestRule.setContent {
fakeNavControllerWrapper.Wrapper {
with(UserAspectRatioAppListModel(context)) {
AppListItemModel(
record = APP_RECORD_SUGGESTED,
label = LABEL,
summary = stateOf(SUMMARY)
).AppItem()
}
}
}
}
@Test
fun aspectRatioAppListModel_getSummaryDefault() {
val summaryState = setSummaryState(USER_MIN_ASPECT_RATIO_UNSET)
assertThat(summaryState.value)
.isEqualTo(context.getString(R.string.user_aspect_ratio_app_default))
}
@Test
fun aspectRatioAppListModel_getSummaryWhenSplitScreen() {
val summaryState = setSummaryState(USER_MIN_ASPECT_RATIO_SPLIT_SCREEN)
assertThat(summaryState.value)
.isEqualTo(context.getString(R.string.user_aspect_ratio_half_screen))
}
private fun setSummaryState(override: Int): State<String> {
val listModel = UserAspectRatioAppListModel(context)
lateinit var summaryState: State<String>
composeTestRule.setContent {
summaryState = listModel.getSummary(option = 0,
record = UserAspectRatioAppListItemModel(
app = APP,
override = override,
suggested = false,
canDisplay = true,
))
}
return summaryState
}
private companion object {
private const val EXPECTED_PROVIDER_NAME = "UserAspectRatioAppsPage"
private const val PACKAGE_NAME = "package.name"
private const val USER_ID = 0
private const val LABEL = "Label"
private const val SUMMARY = "Summary"
private val APP = ApplicationInfo().apply {
packageName = PACKAGE_NAME
}
private val APP_RECORD_SUGGESTED = UserAspectRatioAppListItemModel(
APP,
override = USER_MIN_ASPECT_RATIO_UNSET,
suggested = true,
canDisplay = true
)
private val APP_RECORD_NOT_DISPLAYED = UserAspectRatioAppListItemModel(
APP,
override = USER_MIN_ASPECT_RATIO_UNSET,
suggested = true,
canDisplay = false
)
}
}

View File

@@ -0,0 +1,129 @@
/*
* 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.applications.appcompat;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_16_9;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_3_2;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_4_3;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_SPLIT_SCREEN;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_UNSET;
import static com.android.settings.applications.appcompat.UserAspectRatioManager.KEY_ENABLE_USER_ASPECT_RATIO_SETTINGS;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.ResolveInfo;
import android.provider.DeviceConfig;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.testutils.ResourcesUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* To run this test: atest SettingsUnitTests:UserAspectRatioManagerTest
*/
@RunWith(AndroidJUnit4.class)
public class UserAspectRatioManagerTest {
private Context mContext;
private UserAspectRatioManager mUtils;
private String mOriginalFlag;
@Before
public void setUp() {
mContext = spy(ApplicationProvider.getApplicationContext());
mUtils = spy(new UserAspectRatioManager(mContext));
mOriginalFlag = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_WINDOW_MANAGER,
KEY_ENABLE_USER_ASPECT_RATIO_SETTINGS);
}
@After
public void tearDown() {
DeviceConfig.setProperty(DeviceConfig.NAMESPACE_WINDOW_MANAGER,
KEY_ENABLE_USER_ASPECT_RATIO_SETTINGS, mOriginalFlag, true /* makeDefault */);
}
@Test
public void testCanDisplayAspectRatioUi() {
final ApplicationInfo canDisplay = new ApplicationInfo();
canDisplay.packageName = "com.app.candisplay";
addResolveInfoLauncherEntry(canDisplay.packageName);
assertTrue(mUtils.canDisplayAspectRatioUi(canDisplay));
final ApplicationInfo noLauncherEntry = new ApplicationInfo();
noLauncherEntry.packageName = "com.app.nolauncherentry";
assertFalse(mUtils.canDisplayAspectRatioUi(noLauncherEntry));
}
@Test
public void testIsFeatureEnabled() {
assertFalse(UserAspectRatioManager.isFeatureEnabled(mContext));
DeviceConfig.setProperty(DeviceConfig.NAMESPACE_WINDOW_MANAGER,
KEY_ENABLE_USER_ASPECT_RATIO_SETTINGS, "true", false /* makeDefault */);
assertTrue(UserAspectRatioManager.isFeatureEnabled(mContext));
}
@Test
public void testGetUserMinAspectRatioEntry() {
// R.string.user_aspect_ratio_app_default
final String appDefault = ResourcesUtils.getResourcesString(mContext,
"user_aspect_ratio_app_default");
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_UNSET))
.isEqualTo(appDefault);
// should always return default if value does not correspond to anything
assertThat(mUtils.getUserMinAspectRatioEntry(-1))
.isEqualTo(appDefault);
// R.string.user_aspect_ratio_half_screen
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_SPLIT_SCREEN))
.isEqualTo(ResourcesUtils.getResourcesString(mContext,
"user_aspect_ratio_half_screen"));
// R.string.user_aspect_ratio_3_2
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_3_2))
.isEqualTo(ResourcesUtils.getResourcesString(mContext, "user_aspect_ratio_3_2"));
// R,string.user_aspect_ratio_4_3
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_4_3))
.isEqualTo(ResourcesUtils.getResourcesString(mContext, "user_aspect_ratio_4_3"));
// R.string.user_aspect_ratio_16_9
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_16_9))
.isEqualTo(ResourcesUtils.getResourcesString(mContext, "user_aspect_ratio_16_9"));
}
private void addResolveInfoLauncherEntry(String packageName) {
final ResolveInfo resolveInfo = mock(ResolveInfo.class);
final ActivityInfo activityInfo = mock(ActivityInfo.class);
activityInfo.packageName = packageName;
resolveInfo.activityInfo = activityInfo;
mUtils.addInfoHasLauncherEntry(resolveInfo);
}
}