Disable setting items in App details page

Prior to this cl, we allow user to tap on those
setting items which belong to another user profile app.
However, we already observed some functional broken cases now.
Such as, device can't get the storage data/screen time/mobile data
battery, notification for those apps from another user profile.
Therefore, we decide to grey out those setting items,
and we don't allow user to tap on unsupported setting items.

Test: Download apk in different user profile, and see setting items
is disabled/enabled correctly. and run robo test.
Fix: 230303570

Change-Id: I1bb6b1d8b52f6a00088b2f0e4279b896d568f8a6
This commit is contained in:
Tsung-Mao Fang
2022-05-05 18:10:45 +08:00
parent 9d0da1b613
commit 05de9819e9
11 changed files with 210 additions and 19 deletions

View File

@@ -28,6 +28,7 @@ import static org.mockito.Mockito.when;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.BatteryUsageStats;
@@ -41,6 +42,7 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.SettingsActivity;
import com.android.settings.fuelgauge.BatteryDiffEntry;
import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settingslib.applications.ApplicationsState;
import org.junit.Before;
import org.junit.Test;
@@ -176,6 +178,26 @@ public class AppBatteryPreferenceControllerTest {
assertThat(mBatteryPreference.getSummary()).isEqualTo("60% use for past 24 hours");
}
@Test
public void displayPreference_noEntry_preferenceShouldSetEmptySummary() {
mController.mParent.setAppEntry(null);
mController.displayPreference(mScreen);
assertThat(mBatteryPreference.getSummary()).isEqualTo("");
}
@Test
public void displayPreference_appIsNotInstalled_preferenceShouldSetEmptySummary() {
final ApplicationsState.AppEntry appEntry = mock(ApplicationsState.AppEntry.class);
appEntry.info = new ApplicationInfo();
mController.mParent.setAppEntry(appEntry);
mController.displayPreference(mScreen);
assertThat(mBatteryPreference.getSummary()).isEqualTo("");
}
@Test
public void isBatteryStatsAvailable_hasBatteryStatsHelperAndSipper_returnTrue() {
mController.mBatteryUsageStats = mBatteryUsageStats;

View File

@@ -34,6 +34,7 @@ import android.os.Bundle;
import androidx.loader.app.LoaderManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.datausage.AppDataUsage;
@@ -54,6 +55,8 @@ public class AppDataUsagePreferenceControllerTest {
private LoaderManager mLoaderManager;
@Mock
private AppInfoDashboardFragment mFragment;
@Mock
private PreferenceScreen mScreen;
private Context mContext;
private AppDataUsagePreferenceController mController;
@@ -71,7 +74,7 @@ public class AppDataUsagePreferenceControllerTest {
doReturn(true).when(mController).isBandwidthControlEnabled();
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
@@ -79,13 +82,14 @@ public class AppDataUsagePreferenceControllerTest {
doReturn(false).when(mController).isBandwidthControlEnabled();
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
.isEqualTo(BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
}
@Test
public void onResume_notAvailable_shouldNotRestartDataLoader() {
doReturn(mLoaderManager).when(mFragment).getLoaderManager();
doReturn(BasePreferenceController.CONDITIONALLY_UNAVAILABLE).when(mController).getAvailabilityStatus();
doReturn(BasePreferenceController.CONDITIONALLY_UNAVAILABLE).when(
mController).getAvailabilityStatus();
mController.onResume();
@@ -130,4 +134,45 @@ public class AppDataUsagePreferenceControllerTest {
verify(preference).setSummary(any());
}
@Test
public void displayPreference_noEntry_preferenceShouldNotEnable() {
mController.mAppEntry = null;
Preference preference = new Preference(mContext);
when(mScreen.findPreference(any())).thenReturn(preference);
doReturn(true).when(mController).isBandwidthControlEnabled();
mController.displayPreference(mScreen);
assertThat(preference.isEnabled()).isFalse();
}
@Test
public void displayPreference_appIsInstalled_preferenceShouldEnable() {
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = new ApplicationInfo();
appEntry.info.flags = ApplicationInfo.FLAG_INSTALLED;
mController.mAppEntry = appEntry;
Preference preference = new Preference(mContext);
when(mScreen.findPreference(any())).thenReturn(preference);
doReturn(true).when(mController).isBandwidthControlEnabled();
mController.displayPreference(mScreen);
assertThat(preference.isEnabled()).isTrue();
}
@Test
public void displayPreference_appIsNotInstalled_preferenceShouldDisable() {
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = new ApplicationInfo();
mController.mAppEntry = appEntry;
Preference preference = new Preference(mContext);
when(mScreen.findPreference(any())).thenReturn(preference);
doReturn(true).when(mController).isBandwidthControlEnabled();
mController.displayPreference(mScreen);
assertThat(preference.isEnabled()).isFalse();
}
}

View File

@@ -21,7 +21,6 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -32,6 +31,7 @@ import android.os.Bundle;
import androidx.loader.app.LoaderManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.applications.AppStorageSettings;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
@@ -52,6 +52,8 @@ public class AppStoragePreferenceControllerTest {
private LoaderManager mLoaderManager;
@Mock
private AppInfoDashboardFragment mFragment;
@Mock
private PreferenceScreen mScreen;
private Context mContext;
private AppStoragePreferenceController mController;
@@ -89,10 +91,48 @@ public class AppStoragePreferenceControllerTest {
}
@Test
public void updateState_shouldUpdatePreferenceSummary() {
public void displayPreference_noEntry_preferenceShouldNotEnable() {
mController.mAppEntry = null;
Preference preference = new Preference(mContext);
when(mScreen.findPreference(any())).thenReturn(preference);
mController.displayPreference(mScreen);
assertThat(preference.isEnabled()).isFalse();
}
@Test
public void displayPreference_appIsInstalled_preferenceShouldEnable() {
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = new ApplicationInfo();
when(mFragment.getAppEntry()).thenReturn(appEntry);
appEntry.info.flags = ApplicationInfo.FLAG_INSTALLED;
mController.mAppEntry = appEntry;
Preference preference = new Preference(mContext);
when(mScreen.findPreference(any())).thenReturn(preference);
mController.displayPreference(mScreen);
assertThat(preference.isEnabled()).isTrue();
}
@Test
public void displayPreference_appIsNotInstalled_preferenceShouldDisable() {
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = new ApplicationInfo();
mController.mAppEntry = appEntry;
Preference preference = new Preference(mContext);
when(mScreen.findPreference(any())).thenReturn(preference);
mController.displayPreference(mScreen);
assertThat(preference.isEnabled()).isFalse();
}
@Test
public void updateState_hasAppEntry_shouldUpdatePreferenceSummary() {
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = new ApplicationInfo();
mController.mAppEntry = appEntry;
Preference preference = mock(Preference.class);
mController.updateState(preference);
@@ -102,12 +142,12 @@ public class AppStoragePreferenceControllerTest {
@Test
public void updateState_entryIsNull_shouldNotUpdatePreferenceSummary() {
when(mFragment.getAppEntry()).thenReturn(null);
Preference preference = mock(Preference.class);
mController.mAppEntry = null;
Preference preference = new Preference(mContext);
mController.updateState(preference);
verify(preference, never()).setSummary(any());
assertThat(preference.getSummary()).isEqualTo("");
}
@Test

View File

@@ -20,7 +20,9 @@ import static android.content.Intent.EXTRA_PACKAGE_NAME;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -35,6 +37,7 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.applications.ApplicationsState;
import org.junit.Before;
import org.junit.Test;
@@ -125,4 +128,42 @@ public class TimeSpentInAppPreferenceControllerTest {
verify(mFeatureFactory.applicationFeatureProvider).getTimeSpentInApp(
nullable(String.class));
}
@Test
public void displayPreference_noEntry_preferenceShouldNotEnable() {
mController.mAppEntry = null;
Preference preference = new Preference(mContext);
when(mScreen.findPreference(any())).thenReturn(preference);
mController.displayPreference(mScreen);
assertThat(preference.isEnabled()).isFalse();
}
@Test
public void displayPreference_appIsInstalled_preferenceShouldEnable() {
final ApplicationsState.AppEntry appEntry = mock(ApplicationsState.AppEntry.class);
appEntry.info = new ApplicationInfo();
appEntry.info.flags = ApplicationInfo.FLAG_INSTALLED;
mController.mAppEntry = appEntry;
Preference preference = new Preference(mContext);
when(mScreen.findPreference(any())).thenReturn(preference);
mController.displayPreference(mScreen);
assertThat(preference.isEnabled()).isTrue();
}
@Test
public void displayPreference_appIsNotInstalled_preferenceShouldDisable() {
final ApplicationsState.AppEntry appEntry = mock(ApplicationsState.AppEntry.class);
appEntry.info = new ApplicationInfo();
mController.mAppEntry = appEntry;
Preference preference = new Preference(mContext);
when(mScreen.findPreference(any())).thenReturn(preference);
mController.displayPreference(mScreen);
assertThat(preference.isEnabled()).isFalse();
}
}