Distinguish the profile of DND-bypassing apps
Bug: 353273192 Test: atest ZenModeAppsLinkPreferenceControllerTest ZenModesSummaryHelperTest Flag: android.app.modes_ui Change-Id: I31d8836dbba38d04c9ab3b3180025cb8ddcfbe6d
This commit is contained in:
@@ -22,17 +22,22 @@ import static android.provider.Settings.EXTRA_AUTOMATIC_ZEN_RULE_ID;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.app.Flags;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.platform.test.annotations.EnableFlags;
|
||||
import android.platform.test.flag.junit.SetFlagsRule;
|
||||
import android.service.notification.ZenPolicy;
|
||||
@@ -41,6 +46,7 @@ import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settingslib.applications.ApplicationsState;
|
||||
import com.android.settingslib.applications.ApplicationsState.AppEntry;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
import com.android.settingslib.notification.modes.TestModeBuilder;
|
||||
import com.android.settingslib.notification.modes.ZenMode;
|
||||
@@ -58,6 +64,7 @@ import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@EnableFlags(Flags.FLAG_MODES_UI)
|
||||
@@ -90,13 +97,13 @@ public final class ZenModeAppsLinkPreferenceControllerTest {
|
||||
mZenModesBackend, mHelperBackend);
|
||||
}
|
||||
|
||||
private ApplicationsState.AppEntry createAppEntry(String packageName, String label) {
|
||||
ApplicationsState.AppEntry entry = mock(ApplicationsState.AppEntry.class);
|
||||
entry.info = new ApplicationInfo();
|
||||
entry.info.packageName = packageName;
|
||||
entry.label = label;
|
||||
entry.info.uid = 0;
|
||||
return entry;
|
||||
private AppEntry createAppEntry(String packageName, int userId) {
|
||||
ApplicationInfo applicationInfo = new ApplicationInfo();
|
||||
applicationInfo.packageName = packageName;
|
||||
applicationInfo.uid = UserHandle.getUid(userId, new Random().nextInt(100));
|
||||
AppEntry appEntry = new AppEntry(mContext, applicationInfo, 1);
|
||||
appEntry.label = packageName;
|
||||
return appEntry;
|
||||
}
|
||||
|
||||
private ZenMode createPriorityChannelsZenMode() {
|
||||
@@ -137,14 +144,52 @@ public final class ZenModeAppsLinkPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void testGetAppsBypassingDnd() {
|
||||
ApplicationsState.AppEntry entry = createAppEntry("test", "testLabel");
|
||||
ApplicationsState.AppEntry entryConv = createAppEntry("test_conv", "test_convLabel");
|
||||
List<ApplicationsState.AppEntry> appEntries = List.of(entry, entryConv);
|
||||
ApplicationsState.AppEntry app1 = createAppEntry("app1", mContext.getUserId());
|
||||
ApplicationsState.AppEntry app2 = createAppEntry("app2", mContext.getUserId());
|
||||
List<ApplicationsState.AppEntry> allApps = List.of(app1, app2);
|
||||
|
||||
when(mHelperBackend.getPackagesBypassingDnd(mContext.getUserId(),
|
||||
false)).thenReturn(List.of("test"));
|
||||
false)).thenReturn(List.of("app1"));
|
||||
|
||||
assertThat(mController.getAppsBypassingDnd(appEntries)).containsExactly("testLabel");
|
||||
assertThat(mController.getAppsBypassingDndSortedByName(allApps)).containsExactly(app1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAppsBypassingDnd_sortsByName() {
|
||||
ApplicationsState.AppEntry appC = createAppEntry("C", mContext.getUserId());
|
||||
ApplicationsState.AppEntry appA = createAppEntry("A", mContext.getUserId());
|
||||
ApplicationsState.AppEntry appB = createAppEntry("B", mContext.getUserId());
|
||||
List<ApplicationsState.AppEntry> allApps = List.of(appC, appA, appB);
|
||||
|
||||
when(mHelperBackend.getPackagesBypassingDnd(eq(mContext.getUserId()), anyBoolean()))
|
||||
.thenReturn(List.of("B", "C", "A"));
|
||||
|
||||
assertThat(mController.getAppsBypassingDndSortedByName(allApps))
|
||||
.containsExactly(appA, appB, appC).inOrder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAppsBypassingDnd_withWorkProfile_includesProfileAndSorts() {
|
||||
UserInfo workProfile = new UserInfo(10, "Work Profile", 0);
|
||||
workProfile.userType = UserManager.USER_TYPE_PROFILE_MANAGED;
|
||||
UserManager userManager = mContext.getSystemService(UserManager.class);
|
||||
shadowOf(userManager).addProfile(mContext.getUserId(), 10, workProfile);
|
||||
|
||||
ApplicationsState.AppEntry personalCopy = createAppEntry("app", mContext.getUserId());
|
||||
ApplicationsState.AppEntry workCopy = createAppEntry("app", 10);
|
||||
ApplicationsState.AppEntry otherPersonal = createAppEntry("p2", mContext.getUserId());
|
||||
ApplicationsState.AppEntry otherWork = createAppEntry("w2", 10);
|
||||
List<ApplicationsState.AppEntry> allApps = List.of(workCopy, personalCopy, otherPersonal,
|
||||
otherWork);
|
||||
|
||||
when(mHelperBackend.getPackagesBypassingDnd(eq(mContext.getUserId()), anyBoolean()))
|
||||
.thenReturn(List.of("app", "p2"));
|
||||
when(mHelperBackend.getPackagesBypassingDnd(eq(10), anyBoolean()))
|
||||
.thenReturn(List.of("app"));
|
||||
|
||||
// Personal copy before work copy (names match).
|
||||
assertThat(mController.getAppsBypassingDndSortedByName(allApps))
|
||||
.containsExactly(personalCopy, workCopy, otherPersonal).inOrder();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -156,7 +201,7 @@ public final class ZenModeAppsLinkPreferenceControllerTest {
|
||||
|
||||
// Create some applications.
|
||||
ArrayList<ApplicationsState.AppEntry> appEntries = new ArrayList<>();
|
||||
appEntries.add(createAppEntry("test", "pkgLabel"));
|
||||
appEntries.add(createAppEntry("test", mContext.getUserId()));
|
||||
|
||||
when(mHelperBackend.getPackagesBypassingDnd(
|
||||
mContext.getUserId(), false))
|
||||
@@ -170,7 +215,7 @@ public final class ZenModeAppsLinkPreferenceControllerTest {
|
||||
|
||||
// Manually triggers the callback that will happen on rebuild.
|
||||
mController.mAppSessionCallbacks.onRebuildComplete(appEntries);
|
||||
assertThat(String.valueOf(preference.getSummary())).isEqualTo("pkgLabel can interrupt");
|
||||
assertThat(String.valueOf(preference.getSummary())).isEqualTo("test can interrupt");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -28,10 +28,16 @@ import static android.service.notification.ZenPolicy.VISUAL_EFFECT_LIGHTS;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.app.Flags;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.net.Uri;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.platform.test.annotations.EnableFlags;
|
||||
import android.platform.test.flag.junit.SetFlagsRule;
|
||||
import android.service.notification.Condition;
|
||||
@@ -39,9 +45,12 @@ import android.service.notification.ZenDeviceEffects;
|
||||
import android.service.notification.ZenModeConfig;
|
||||
import android.service.notification.ZenPolicy;
|
||||
|
||||
import com.android.settingslib.applications.ApplicationsState.AppEntry;
|
||||
import com.android.settingslib.notification.modes.TestModeBuilder;
|
||||
import com.android.settingslib.notification.modes.ZenMode;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -50,10 +59,12 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.Random;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ZenModesSummaryHelperTest {
|
||||
private static final int WORK_PROFILE_ID = 3;
|
||||
|
||||
private Context mContext;
|
||||
private ZenHelperBackend mBackend;
|
||||
|
||||
@@ -69,6 +80,11 @@ public class ZenModesSummaryHelperTest {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mBackend = new ZenHelperBackend(mContext);
|
||||
mSummaryHelper = new ZenModeSummaryHelper(mContext, mBackend);
|
||||
|
||||
UserInfo workProfile = new UserInfo(WORK_PROFILE_ID, "Work Profile", 0);
|
||||
workProfile.userType = UserManager.USER_TYPE_PROFILE_MANAGED;
|
||||
UserManager userManager = mContext.getSystemService(UserManager.class);
|
||||
shadowOf(userManager).addProfile(mContext.getUserId(), WORK_PROFILE_ID, workProfile);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -306,7 +322,7 @@ public class ZenModesSummaryHelperTest {
|
||||
.build())
|
||||
.build();
|
||||
|
||||
assertThat(mSummaryHelper.getAppsSummary(zenMode, new LinkedHashSet<>())).isEqualTo("None");
|
||||
assertThat(mSummaryHelper.getAppsSummary(zenMode, ImmutableList.of())).isEqualTo("None");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -321,40 +337,58 @@ public class ZenModesSummaryHelperTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAppsSummary_formatAppsListEmpty() {
|
||||
Set<String> apps = new LinkedHashSet<>();
|
||||
public void formatAppsList_listEmpty() {
|
||||
ImmutableList<AppEntry> apps = ImmutableList.of();
|
||||
assertThat(mSummaryHelper.formatAppsList(apps)).isEqualTo("No apps can interrupt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAppsSummary_formatAppsListSingle() {
|
||||
Set<String> apps = Set.of("My App");
|
||||
public void formatAppsList_single() {
|
||||
ImmutableList<AppEntry> apps = ImmutableList.of(newAppEntry("My App"));
|
||||
assertThat(mSummaryHelper.formatAppsList(apps)).isEqualTo("My App can interrupt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAppsSummary_formatAppsListTwo() {
|
||||
Set<String> apps = Set.of("My App", "SecondApp");
|
||||
public void formatAppsList_two() {
|
||||
ImmutableList<AppEntry> apps = ImmutableList.of(newAppEntry("My App"),
|
||||
newAppEntry("SecondApp"));
|
||||
assertThat(mSummaryHelper.formatAppsList(apps)).isEqualTo("My App and SecondApp "
|
||||
+ "can interrupt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAppsSummary_formatAppsListThree() {
|
||||
Set<String> apps = Set.of("My App", "SecondApp", "ThirdApp");
|
||||
public void formatAppsList_three() {
|
||||
ImmutableList<AppEntry> apps = ImmutableList.of(newAppEntry("My App"),
|
||||
newAppEntry("SecondApp"), newAppEntry("ThirdApp"));
|
||||
assertThat(mSummaryHelper.formatAppsList(apps)).isEqualTo("My App, SecondApp, "
|
||||
+ "and ThirdApp can interrupt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAppsSummary_formatAppsListMany() {
|
||||
Set<String> apps = Set.of("My App", "SecondApp", "ThirdApp", "FourthApp",
|
||||
"FifthApp", "SixthApp");
|
||||
// Note that apps are selected alphabetically.
|
||||
assertThat(mSummaryHelper.formatAppsList(apps)).isEqualTo("FifthApp, FourthApp, "
|
||||
public void formatAppsList_many() {
|
||||
ImmutableList<AppEntry> apps = ImmutableList.of(newAppEntry("My App"),
|
||||
newAppEntry("SecondApp"), newAppEntry("ThirdApp"), newAppEntry("FourthApp"),
|
||||
newAppEntry("FifthApp"), newAppEntry("SixthApp"));
|
||||
assertThat(mSummaryHelper.formatAppsList(apps)).isEqualTo("My App, SecondApp, "
|
||||
+ "and 4 more can interrupt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatAppsList_singleWorkProfile() {
|
||||
ImmutableList<AppEntry> apps = ImmutableList.of(newAppEntry("My App", WORK_PROFILE_ID));
|
||||
assertThat(mSummaryHelper.formatAppsList(apps)).isEqualTo("My App (Work) can interrupt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatAppsList_mixOfProfiles() {
|
||||
ImmutableList<AppEntry> apps = ImmutableList.of(
|
||||
newAppEntry("My App", mContext.getUserId()),
|
||||
newAppEntry("My App", WORK_PROFILE_ID),
|
||||
newAppEntry("SecondApp", mContext.getUserId()));
|
||||
assertThat(mSummaryHelper.formatAppsList(apps)).isEqualTo("My App, My App (Work), "
|
||||
+ "and SecondApp can interrupt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAppsSummary_priorityApps() {
|
||||
ZenMode zenMode = new TestModeBuilder()
|
||||
@@ -362,13 +396,26 @@ public class ZenModesSummaryHelperTest {
|
||||
.allowChannels(ZenPolicy.CHANNEL_POLICY_PRIORITY)
|
||||
.build())
|
||||
.build();
|
||||
Set<String> apps = Set.of("My App", "SecondApp", "ThirdApp", "FourthApp",
|
||||
"FifthApp", "SixthApp");
|
||||
ImmutableList<AppEntry> apps = ImmutableList.of(newAppEntry("My App"),
|
||||
newAppEntry("SecondApp"), newAppEntry("ThirdApp"), newAppEntry("FourthApp"),
|
||||
newAppEntry("FifthApp"), newAppEntry("SixthApp"));
|
||||
|
||||
assertThat(mSummaryHelper.getAppsSummary(zenMode, apps)).isEqualTo("FifthApp, FourthApp, "
|
||||
assertThat(mSummaryHelper.getAppsSummary(zenMode, apps)).isEqualTo("My App, SecondApp, "
|
||||
+ "and 4 more can interrupt");
|
||||
}
|
||||
|
||||
private AppEntry newAppEntry(String name) {
|
||||
return newAppEntry(name, mContext.getUserId());
|
||||
}
|
||||
|
||||
private AppEntry newAppEntry(String name, int userId) {
|
||||
ApplicationInfo applicationInfo = new ApplicationInfo();
|
||||
applicationInfo.uid = UserHandle.getUid(userId, new Random().nextInt(100));
|
||||
AppEntry appEntry = new AppEntry(mContext, applicationInfo, 1);
|
||||
appEntry.label = name;
|
||||
return appEntry;
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnableFlags(Flags.FLAG_MODES_UI)
|
||||
public void getSoundSummary_off_noRules() {
|
||||
|
||||
Reference in New Issue
Block a user