diff --git a/src/com/android/settings/applications/appcompat/UserAspectRatioManager.java b/src/com/android/settings/applications/appcompat/UserAspectRatioManager.java index b940dc844a5..3cca5f6771e 100644 --- a/src/com/android/settings/applications/appcompat/UserAspectRatioManager.java +++ b/src/com/android/settings/applications/appcompat/UserAspectRatioManager.java @@ -24,7 +24,6 @@ import static java.lang.Boolean.FALSE; import android.app.AppGlobals; import android.content.Context; -import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.IPackageManager; import android.content.pm.LauncherApps; @@ -48,10 +47,6 @@ import java.util.Map; * {@link PackageManager.UserMinAspectRatio} set by user */ public class UserAspectRatioManager { - private static final Intent LAUNCHER_ENTRY_INTENT = - new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER); - - // TODO(b/288142656): Enable user aspect ratio settings by default private static final boolean DEFAULT_VALUE_ENABLE_USER_ASPECT_RATIO_SETTINGS = true; @VisibleForTesting static final String KEY_ENABLE_USER_ASPECT_RATIO_SETTINGS = @@ -173,12 +168,9 @@ public class UserAspectRatioManager { DEFAULT_VALUE_ENABLE_USER_ASPECT_RATIO_FULLSCREEN); } - LauncherApps getLauncherApps() { - return mContext.getSystemService(LauncherApps.class); - } - private boolean hasLauncherEntry(@NonNull ApplicationInfo app) { - return !getLauncherApps().getActivityList(app.packageName, getUserHandleForUid(app.uid)) + return !mContext.getSystemService(LauncherApps.class) + .getActivityList(app.packageName, getUserHandleForUid(app.uid)) .isEmpty(); } @@ -232,7 +224,7 @@ public class UserAspectRatioManager { @NonNull private String getAccessibleOption(String numerator, String denominator) { - return mContext.getResources().getString(R.string.user_aspect_ratio_option_a11y, + return mContext.getString(R.string.user_aspect_ratio_option_a11y, numerator, denominator); } diff --git a/tests/unit/src/com/android/settings/applications/appcompat/UserAspectRatioManagerTest.java b/tests/unit/src/com/android/settings/applications/appcompat/UserAspectRatioManagerTest.java index cf2a1f06529..3783b8a945a 100644 --- a/tests/unit/src/com/android/settings/applications/appcompat/UserAspectRatioManagerTest.java +++ b/tests/unit/src/com/android/settings/applications/appcompat/UserAspectRatioManagerTest.java @@ -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 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(