Hides the settings entry in Magnification Settings if window magnification feature is not supported

If the device doesn't support window magnification featue, we
should hide the settings entry in mangification settings

Bug: 213414385
Test: atest ToggleScreenMagnificationPreferenceFragmentTest
Change-Id: I73f087da54cd2930c049dabb6b843c3b373e3c20
This commit is contained in:
mincheli
2022-03-21 00:32:48 +08:00
parent e7b38f27a3
commit 57b35f9f35
2 changed files with 30 additions and 4 deletions

View File

@@ -27,6 +27,7 @@ import android.content.ComponentName;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.icu.text.CaseMap; import android.icu.text.CaseMap;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
@@ -162,9 +163,14 @@ public class ToggleScreenMagnificationPreferenceFragment extends
@Override @Override
protected void initSettingsPreference() { protected void initSettingsPreference() {
// If the device doesn't support magnification area, it should hide the settings preference. // If the device doesn't support window magnification feature, it should hide the
if (!getContext().getResources().getBoolean( // settings preference.
com.android.internal.R.bool.config_magnification_area)) { final boolean supportWindowMagnification =
getContext().getResources().getBoolean(
com.android.internal.R.bool.config_magnification_area)
&& getContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_WINDOW_MAGNIFICATION);
if (!supportWindowMagnification) {
return; return;
} }
mSettingsPreference = new Preference(getPrefContext()); mSettingsPreference = new Preference(getPrefContext());

View File

@@ -36,6 +36,7 @@ import android.content.ComponentName;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Settings; import android.provider.Settings;
@@ -101,6 +102,8 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
private FragmentActivity mActivity; private FragmentActivity mActivity;
@Mock @Mock
private ContentResolver mContentResolver; private ContentResolver mContentResolver;
@Mock
private PackageManager mPackageManager;
@Before @Before
public void setUpTestFragment() { public void setUpTestFragment() {
@@ -110,6 +113,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
mFragment = spy(new TestToggleScreenMagnificationPreferenceFragment(mContext)); mFragment = spy(new TestToggleScreenMagnificationPreferenceFragment(mContext));
mResources = spy(mContext.getResources()); mResources = spy(mContext.getResources());
when(mContext.getResources()).thenReturn(mResources); when(mContext.getResources()).thenReturn(mResources);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
when(mFragment.getContext().getResources()).thenReturn(mResources); when(mFragment.getContext().getResources()).thenReturn(mResources);
when(mFragment.getActivity()).thenReturn(mActivity); when(mFragment.getActivity()).thenReturn(mActivity);
when(mActivity.getContentResolver()).thenReturn(mContentResolver); when(mActivity.getContentResolver()).thenReturn(mContentResolver);
@@ -334,10 +338,26 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
@Ignore("Ignore it since a NPE is happened in ShadowWindowManagerGlobal. (Ref. b/214161063)") @Ignore("Ignore it since a NPE is happened in ShadowWindowManagerGlobal. (Ref. b/214161063)")
@Test @Test
public void onCreateView_notSupportsMagnificationArea_settingsPreferenceIsNull() { public void onCreateView_magnificationAreaNotSupported_settingsPreferenceIsNull() {
when(mResources.getBoolean( when(mResources.getBoolean(
com.android.internal.R.bool.config_magnification_area)) com.android.internal.R.bool.config_magnification_area))
.thenReturn(false); .thenReturn(false);
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WINDOW_MAGNIFICATION))
.thenReturn(true);
mFragment.onCreateView(LayoutInflater.from(mContext), mock(ViewGroup.class), Bundle.EMPTY);
assertThat(mFragment.mSettingsPreference).isNull();
}
@Ignore("Ignore it since a NPE is happened in ShadowWindowManagerGlobal. (Ref. b/214161063)")
@Test
public void onCreateView_windowMagnificationNotSupported_settingsPreferenceIsNull() {
when(mResources.getBoolean(
com.android.internal.R.bool.config_magnification_area))
.thenReturn(true);
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WINDOW_MAGNIFICATION))
.thenReturn(false);
mFragment.onCreateView(LayoutInflater.from(mContext), mock(ViewGroup.class), Bundle.EMPTY); mFragment.onCreateView(LayoutInflater.from(mContext), mock(ViewGroup.class), Bundle.EMPTY);