Enable all aspect ratio options in UserAspectRatioManagerTest

testGetUserMinAspectRatioEntry fails for foldables and tablets due to
some aspect ratio options being disabled. Mock Resources to return all
default aspect ratio options and strings to make test device agnostic.

Clean up unused variables and remove getLauncherApps in
UserAspectRatioManager.

Add more tests in UserAspectRatioManagerTest.

Fix: 304685321
Test: atest SettingsUnitTests:UserAspectRatioManagerTest
Change-Id: I6f4bbd60863be0c66139955507157619a5fc6662
This commit is contained in:
Graciela Wissen Putri
2023-10-24 13:48:49 +00:00
parent 2260edba20
commit 3ced0bd60d
2 changed files with 94 additions and 56 deletions

View File

@@ -17,6 +17,9 @@
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_DISPLAY_SIZE;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_FULLSCREEN;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_SPLIT_SCREEN;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_UNSET;
@@ -29,8 +32,10 @@ import static com.android.settings.applications.appcompat.UserAspectRatioManager
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
@@ -43,10 +48,6 @@ import android.content.pm.LauncherActivityInfo;
import android.content.pm.LauncherApps;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.platform.test.rule.DeviceTypeRule;
import android.platform.test.rule.FoldableOnly;
import android.platform.test.rule.LargeScreenOnly;
import android.platform.test.rule.TabletOnly;
import android.provider.DeviceConfig;
import androidx.test.core.app.ApplicationProvider;
@@ -57,9 +58,7 @@ import com.android.settings.testutils.ResourcesUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;
import java.util.List;
@@ -68,7 +67,6 @@ import java.util.List;
* To run this test: atest SettingsUnitTests:UserAspectRatioManagerTest
*/
@RunWith(AndroidJUnit4.class)
@LargeScreenOnly
public class UserAspectRatioManagerTest {
private Context mContext;
@@ -80,23 +78,19 @@ public class UserAspectRatioManagerTest {
private LauncherApps mLauncherApps;
private List<LauncherActivityInfo> mLauncherActivities;
@Rule
public TestRule mDeviceTypeRule = new DeviceTypeRule();
@Before
public void setUp() {
mContext = spy(ApplicationProvider.getApplicationContext());
mResources = spy(mContext.getResources());
mResources = mock(Resources.class);
mLauncherApps = mock(LauncherApps.class);
mLauncherActivities = mock(List.class);
mUtils = new UserAspectRatioManager(mContext) {
@Override
LauncherApps getLauncherApps() {
return mLauncherApps;
}
};
when(mContext.getResources()).thenReturn(mResources);
when(mContext.getSystemService(LauncherApps.class)).thenReturn(mLauncherApps);
enableAllDefaultAspectRatioOptions();
mUtils = new UserAspectRatioManager(mContext);
doReturn(mLauncherActivities).when(mLauncherApps).getActivityList(anyString(), any());
mOriginalSettingsFlag = DeviceConfig.getProperty(
@@ -136,7 +130,7 @@ public class UserAspectRatioManagerTest {
public void testCanDisplayAspectRatioUi_hasLauncher_propertyFalse_returnFalse()
throws PackageManager.NameNotFoundException {
mockProperty(PROPERTY_COMPAT_ALLOW_USER_ASPECT_RATIO_OVERRIDE, false);
doReturn(true).when(mLauncherActivities).isEmpty();
doReturn(false).when(mLauncherActivities).isEmpty();
final ApplicationInfo canDisplay = new ApplicationInfo();
canDisplay.packageName = mPackageName;
@@ -228,10 +222,10 @@ public class UserAspectRatioManagerTest {
}
@Test
@FoldableOnly
public void testGetUserMinAspectRatioEntry_Foldable() {
public void testGetUserMinAspectRatioEntry() {
final Context context = ApplicationProvider.getApplicationContext();
// R.string.user_aspect_ratio_app_default
final String appDefault = ResourcesUtils.getResourcesString(mContext,
final String appDefault = ResourcesUtils.getResourcesString(context,
"user_aspect_ratio_app_default");
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_UNSET, mPackageName))
.isEqualTo(appDefault);
@@ -240,33 +234,24 @@ public class UserAspectRatioManagerTest {
.isEqualTo(appDefault);
// R.string.user_aspect_ratio_half_screen
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_SPLIT_SCREEN,
mPackageName)).isEqualTo(ResourcesUtils.getResourcesString(mContext,
mPackageName)).isEqualTo(ResourcesUtils.getResourcesString(context,
"user_aspect_ratio_half_screen"));
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_FULLSCREEN,
mPackageName)).isEqualTo(ResourcesUtils.getResourcesString(mContext,
"user_aspect_ratio_fullscreen"));
}
@Test
@TabletOnly
public void testGetUserMinAspectRatioEntry_Tablet() {
// 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, mPackageName))
.isEqualTo(appDefault);
// should always return default if value does not correspond to anything
assertThat(mUtils.getUserMinAspectRatioEntry(-1, mPackageName))
.isEqualTo(appDefault);
// R.string.user_aspect_ratio_half_screen
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_SPLIT_SCREEN,
mPackageName)).isEqualTo(ResourcesUtils.getResourcesString(mContext,
"user_aspect_ratio_half_screen"));
// R.string.user_aspect_ratio_display_size
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_DISPLAY_SIZE,
mPackageName)).isEqualTo(ResourcesUtils.getResourcesString(context,
"user_aspect_ratio_device_size"));
// R.string.user_aspect_ratio_16_9
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_16_9, mPackageName))
.isEqualTo(ResourcesUtils.getResourcesString(mContext, "user_aspect_ratio_16_9"));
.isEqualTo(ResourcesUtils.getResourcesString(context, "user_aspect_ratio_16_9"));
// R.string.user_aspect_ratio_4_3
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_4_3, mPackageName))
.isEqualTo(ResourcesUtils.getResourcesString(context, "user_aspect_ratio_4_3"));
// R.string.user_aspect_ratio_3_2
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_3_2, mPackageName))
.isEqualTo(ResourcesUtils.getResourcesString(context, "user_aspect_ratio_3_2"));
// R.string.user_aspect_ratio_fullscreen
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_FULLSCREEN,
mPackageName)).isEqualTo(ResourcesUtils.getResourcesString(mContext,
mPackageName)).isEqualTo(ResourcesUtils.getResourcesString(context,
"user_aspect_ratio_fullscreen"));
}
@@ -274,10 +259,71 @@ public class UserAspectRatioManagerTest {
public void testGetUserMinAspectRatioEntry_fullscreenDisabled_shouldReturnDefault() {
setAspectRatioFullscreenBuildTimeFlagEnabled(false);
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_FULLSCREEN,
mPackageName)).isEqualTo(ResourcesUtils.getResourcesString(mContext,
mPackageName)).isEqualTo(ResourcesUtils.getResourcesString(
ApplicationProvider.getApplicationContext(),
"user_aspect_ratio_app_default"));
}
@Test
public void testGetUserMinAspectRatioEntry_nonDefaultString_shouldReturnNewString() {
final String newOptionName = "new_option_name";
when(mResources.getIntArray(anyInt())).thenReturn(new int[] {USER_MIN_ASPECT_RATIO_UNSET});
when(mResources.getStringArray(anyInt())).thenReturn(new String[] {newOptionName});
mUtils = new UserAspectRatioManager(mContext);
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_UNSET, mPackageName))
.isEqualTo(newOptionName);
}
@Test
public void testGetUserMinAspectRatioMapping_noAppDefault_shouldThrowException() {
when(mResources.getIntArray(anyInt())).thenReturn(new int[] {USER_MIN_ASPECT_RATIO_4_3});
when(mResources.getStringArray(anyInt())).thenReturn(new String[] {"4:3"});
assertThrows(RuntimeException.class, () -> new UserAspectRatioManager(mContext));
}
@Test
public void testGetUserMinAspectRatioMapping_configLengthMismatch_shouldThrowException() {
when(mResources.getIntArray(anyInt())).thenReturn(new int[] {
USER_MIN_ASPECT_RATIO_UNSET,
USER_MIN_ASPECT_RATIO_4_3});
when(mResources.getStringArray(anyInt())).thenReturn(new String[] {"4:3"});
assertThrows(RuntimeException.class, () -> new UserAspectRatioManager(mContext));
}
private void enableAllDefaultAspectRatioOptions() {
final int[] aspectRatioOptions = new int[] {
USER_MIN_ASPECT_RATIO_UNSET,
USER_MIN_ASPECT_RATIO_SPLIT_SCREEN,
USER_MIN_ASPECT_RATIO_DISPLAY_SIZE,
USER_MIN_ASPECT_RATIO_4_3,
USER_MIN_ASPECT_RATIO_16_9,
USER_MIN_ASPECT_RATIO_3_2,
USER_MIN_ASPECT_RATIO_FULLSCREEN};
when(mResources.getIntArray(anyInt())).thenReturn(aspectRatioOptions);
// String array config overlay with @null values so default strings should be used
when(mResources.getStringArray(anyInt())).thenReturn(new String[aspectRatioOptions.length]);
final Context context = ApplicationProvider.getApplicationContext();
mockString(context, "user_aspect_ratio_app_default");
mockString(context, "user_aspect_ratio_half_screen");
mockString(context, "user_aspect_ratio_device_size");
mockString(context, "user_aspect_ratio_4_3");
mockString(context, "user_aspect_ratio_16_9");
mockString(context, "user_aspect_ratio_3_2");
mockString(context, "user_aspect_ratio_fullscreen");
}
private void mockString(Context context, String stringResName) {
final int resId = ResourcesUtils.getResourcesId(context, "string", stringResName);
final String string = ResourcesUtils.getResourcesString(context, stringResName);
when(mContext.getString(resId)).thenReturn(string);
}
private void mockProperty(String propertyName, boolean value)
throws PackageManager.NameNotFoundException {
PackageManager.Property prop = new PackageManager.Property(