Show icons for allowed contacts

Test: atest com.android.settings.notification.modes
Bug: 346551087
Flag: android.app.modes_ui
Change-Id: If2b6b06b4a9c16bdefb03850ad1615e96c601fbd
This commit is contained in:
Matías Hernández
2024-07-21 00:47:31 +02:00
parent 5c466c5ba3
commit d8b9fe8f01
13 changed files with 559 additions and 97 deletions

View File

@@ -16,17 +16,47 @@
package com.android.settings.notification.modes;
import static android.service.notification.ZenPolicy.CONVERSATION_SENDERS_IMPORTANT;
import static android.service.notification.ZenPolicy.PEOPLE_TYPE_ANYONE;
import static android.service.notification.ZenPolicy.PEOPLE_TYPE_CONTACTS;
import static android.service.notification.ZenPolicy.PEOPLE_TYPE_NONE;
import static android.service.notification.ZenPolicy.PEOPLE_TYPE_STARRED;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Flags;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.pm.ShortcutInfo;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.service.notification.ConversationChannelWrapper;
import android.service.notification.ZenPolicy;
import android.view.LayoutInflater;
import android.view.View;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.notification.modes.ZenHelperBackend.Contact;
import com.android.settingslib.notification.ConversationIconFactory;
import com.android.settingslib.notification.modes.TestModeBuilder;
import com.android.settingslib.notification.modes.ZenMode;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.MoreExecutors;
import org.junit.Before;
import org.junit.Rule;
@@ -34,39 +64,160 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.stubbing.Answer;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.Collection;
@EnableFlags(Flags.FLAG_MODES_UI)
@RunWith(RobolectricTestRunner.class)
public final class ZenModePeopleLinkPreferenceControllerTest {
private ZenModePeopleLinkPreferenceController mController;
private CircularIconsPreference mPreference;
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private Context mContext;
@Mock
private ZenHelperBackend mHelperBackend;
@Mock private ZenHelperBackend mHelperBackend;
@Mock private ConversationIconFactory mConversationIconFactory;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
CircularIconSet.sExecutorService = MoreExecutors.newDirectExecutorService();
mPreference = new CircularIconsPreference(mContext, MoreExecutors.directExecutor());
// Ensure the preference view is bound & measured (needed to add icons).
View preferenceView = LayoutInflater.from(mContext).inflate(mPreference.getLayoutResource(),
null);
preferenceView.measure(View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY));
PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(preferenceView);
mPreference.onBindViewHolder(holder);
mController = new ZenModePeopleLinkPreferenceController(
mContext, "something", mHelperBackend);
mContext, "something", mHelperBackend, mConversationIconFactory);
setUpContacts(ImmutableList.of(), ImmutableList.of());
setUpImportantConversations(ImmutableList.of());
when(mHelperBackend.getContactPhoto(any())).then(
(Answer<Drawable>) invocationOnMock -> photoOf(invocationOnMock.getArgument(0)));
when(mConversationIconFactory.getConversationDrawable((ShortcutInfo) any(), any(), anyInt(),
anyBoolean())).thenReturn(new ColorDrawable(Color.BLACK));
}
@Test
@EnableFlags(Flags.FLAG_MODES_UI)
public void testHasSummary() {
CircularIconsPreference pref = mock(CircularIconsPreference.class);
public void updateState_setsSummary() {
mController.updateState(mPreference, TestModeBuilder.EXAMPLE);
mController.updateZenMode(pref, TestModeBuilder.EXAMPLE);
assertThat(mPreference.getSummary()).isNotNull();
assertThat(mPreference.getSummary().toString()).isNotEmpty();
}
verify(pref).setSummary(any());
verify(pref).displayIcons(eq(CircularIconSet.EMPTY));
@Test
public void updateState_starredCallsNoMessages_displaysStarredContacts() {
setUpContacts(ImmutableList.of(1, 2, 3, 4), ImmutableList.of(2, 3));
ZenMode mode = new TestModeBuilder()
.setZenPolicy(new ZenPolicy.Builder()
.allowCalls(PEOPLE_TYPE_STARRED)
.allowMessages(PEOPLE_TYPE_NONE)
.build())
.build();
mController.updateState(mPreference, mode);
assertThat(mPreference.getIcons()).hasSize(2);
assertThat(mPreference.getIcons().stream()
.map(ColorDrawable.class::cast)
.map(d -> d.getColor()).toList())
.containsExactly(2, 3).inOrder();
}
@Test
public void updateState_starredCallsContactMessages_displaysAllContacts() {
setUpContacts(ImmutableList.of(1, 2, 3, 4), ImmutableList.of(2, 3));
ZenMode mode = new TestModeBuilder()
.setZenPolicy(new ZenPolicy.Builder()
.allowCalls(PEOPLE_TYPE_STARRED)
.allowMessages(PEOPLE_TYPE_CONTACTS)
.build())
.build();
mController.updateState(mPreference, mode);
assertThat(mPreference.getIcons()).hasSize(4);
assertThat(mPreference.getIcons().stream()
.map(ColorDrawable.class::cast)
.map(d -> d.getColor()).toList())
.containsExactly(1, 2, 3, 4).inOrder();
}
@Test
public void updateState_anyoneCallsContactMessages_displaysAnyonePlaceholder() {
setUpContacts(ImmutableList.of(1, 2, 3, 4), ImmutableList.of(2, 3));
ZenMode mode = new TestModeBuilder()
.setZenPolicy(new ZenPolicy.Builder()
.allowCalls(PEOPLE_TYPE_ANYONE)
.allowMessages(PEOPLE_TYPE_CONTACTS)
.build())
.build();
mController.updateState(mPreference, mode);
assertThat(mPreference.getIcons()).hasSize(1);
verify(mHelperBackend, never()).getContactPhoto(any());
}
@Test
public void updateState_noContactsButImportantConversations_displaysConversations() {
setUpContacts(ImmutableList.of(), ImmutableList.of());
setUpImportantConversations(ImmutableList.of(1, 2, 3));
ZenMode mode = new TestModeBuilder()
.setZenPolicy(new ZenPolicy.Builder()
.allowCalls(PEOPLE_TYPE_CONTACTS)
.allowMessages(PEOPLE_TYPE_CONTACTS)
.allowConversations(CONVERSATION_SENDERS_IMPORTANT)
.build())
.build();
mController.updateState(mPreference, mode);
assertThat(mPreference.getIcons()).hasSize(3);
verify(mConversationIconFactory, times(3)).getConversationDrawable((ShortcutInfo) any(),
any(), anyInt(), anyBoolean());
}
private void setUpContacts(Collection<Integer> allIds, Collection<Integer> starredIds) {
when(mHelperBackend.getAllContacts()).thenReturn(ImmutableList.copyOf(
allIds.stream()
.map(id -> new Contact(id, "#" + id, Uri.parse("photo://" + id)))
.toList()));
when(mHelperBackend.getStarredContacts()).thenReturn(ImmutableList.copyOf(
starredIds.stream()
.map(id -> new Contact(id, "#" + id, Uri.parse("photo://" + id)))
.toList()));
}
private void setUpImportantConversations(Collection<Integer> ids) {
when(mHelperBackend.getImportantConversations()).thenReturn(ImmutableList.copyOf(
ids.stream()
.map(id -> {
ConversationChannelWrapper channel = new ConversationChannelWrapper();
channel.setNotificationChannel(
new NotificationChannel(id.toString(), id.toString(),
NotificationManager.IMPORTANCE_DEFAULT));
return channel;
})
.toList()));
}
private static ColorDrawable photoOf(Contact contact) {
return new ColorDrawable((int) contact.id());
}
}

View File

@@ -35,13 +35,11 @@ import static com.android.settings.notification.modes.ZenModePrioritySendersPref
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Flags;
import android.content.Context;
import android.database.Cursor;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.service.notification.ZenPolicy;
@@ -56,6 +54,8 @@ import com.android.settingslib.notification.modes.ZenMode;
import com.android.settingslib.notification.modes.ZenModesBackend;
import com.android.settingslib.widget.SelectorWithWidgetPreference;
import com.google.common.collect.ImmutableList;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -105,22 +105,11 @@ public final class ZenModePrioritySendersPreferenceControllerTest {
mPreferenceScreen.addPreference(mCallsPrefCategory);
mPreferenceScreen.addPreference(mMessagesPrefCategory);
Cursor cursor = mock(Cursor.class);
when(cursor.getCount()).thenReturn(1);
when(mHelperBackend.queryAllContactsData()).thenReturn(cursor);
}
// Makes a preference with the provided key and whether it's a checkbox with
// mSelectorClickListener as the onClickListener set.
private SelectorWithWidgetPreference makePreference(
String key, boolean isCheckbox, boolean isMessages) {
final SelectorWithWidgetPreference pref =
new SelectorWithWidgetPreference(mContext, isCheckbox);
pref.setKey(key);
pref.setOnClickListener(
isMessages ? mMessagesController.mSelectorClickListener
: mCallsController.mSelectorClickListener);
return pref;
when(mHelperBackend.getStarredContacts()).thenReturn(ImmutableList.of());
when(mHelperBackend.getAllContacts()).thenReturn(
ImmutableList.of(new ZenHelperBackend.Contact(1, "The only contact", null)));
when(mHelperBackend.getAllContactsCount()).thenReturn(1);
when(mHelperBackend.getImportantConversations()).thenReturn(ImmutableList.of());
}
@Test

View File

@@ -89,31 +89,38 @@ public class ZenModesSummaryHelperTest {
@Test
public void getPeopleSummary_noOne() {
ZenMode zenMode = new TestModeBuilder()
.setZenPolicy(new ZenPolicy.Builder().disallowAllSounds().build())
.build();
ZenPolicy policy = new ZenPolicy.Builder().disallowAllSounds().build();
assertThat(mSummaryHelper.getPeopleSummary(zenMode)).isEqualTo("No one can interrupt");
assertThat(mSummaryHelper.getPeopleSummary(policy)).isEqualTo("No one can interrupt");
}
@Test
public void getPeopleSummary_some() {
ZenMode zenMode = new TestModeBuilder()
.setZenPolicy(new ZenPolicy.Builder().allowCalls(PEOPLE_TYPE_CONTACTS).build())
ZenPolicy policy = new ZenPolicy.Builder().allowCalls(PEOPLE_TYPE_CONTACTS).build();
assertThat(mSummaryHelper.getPeopleSummary(policy)).isEqualTo("Some people can interrupt");
}
@Test
public void getPeopleSummary_onlyRepeatCallers() {
ZenPolicy policy = new ZenPolicy.Builder()
.disallowAllSounds()
.allowRepeatCallers(true)
.build();
assertThat(mSummaryHelper.getPeopleSummary(zenMode)).isEqualTo("Some people can interrupt");
assertThat(mSummaryHelper.getPeopleSummary(policy)).isEqualTo(
"Repeat callers can interrupt");
}
@Test
public void getPeopleSummary_all() {
ZenMode zenMode = new TestModeBuilder()
.setZenPolicy(new ZenPolicy.Builder().allowCalls(PEOPLE_TYPE_ANYONE).
allowConversations(CONVERSATION_SENDERS_ANYONE)
.allowMessages(PEOPLE_TYPE_ANYONE).build())
ZenPolicy policy = new ZenPolicy.Builder()
.allowCalls(PEOPLE_TYPE_ANYONE)
.allowConversations(CONVERSATION_SENDERS_ANYONE)
.allowMessages(PEOPLE_TYPE_ANYONE)
.build();
assertThat(mSummaryHelper.getPeopleSummary(zenMode)).isEqualTo("All people can interrupt");
assertThat(mSummaryHelper.getPeopleSummary(policy)).isEqualTo("All people can interrupt");
}
@Test