First bits of "add a mode"

* Preference below the modes list.
* Temporarily triggers addition of a mode with default name and type=SCHEDULE_TIME (type will be "manual only" later).
* Fixed sorting of modes in the list when refreshing (new modes were added at the bottom instead of where they should, the same would've happened for renamed modes).
* Minor polishes (extracted fragment launch to helper class, renamed item controller class for clarity).

Test: atest com.android.settings.notification.modes
Bug: 326442408
Fixes: 347198709
Flag: android.app.modes_ui
Change-Id: Ie276c92181c5374faf74592433595e7e15a5efc0
This commit is contained in:
Matías Hernández
2024-06-14 15:46:51 +02:00
parent 16f9205fb6
commit 8409c39d94
9 changed files with 263 additions and 40 deletions

View File

@@ -31,8 +31,16 @@ import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.service.notification.ZenPolicy;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import com.android.settingslib.search.SearchIndexableRaw;
import com.google.common.collect.ImmutableList;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -43,6 +51,7 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@@ -75,15 +84,71 @@ public class ZenModesListPreferenceControllerTest {
private ZenModesBackend mBackend;
private ZenModesListPreferenceController mPrefController;
private PreferenceCategory mPreference;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mPreference = new PreferenceCategory(mContext);
PreferenceManager preferenceManager = new PreferenceManager(mContext);
PreferenceScreen preferenceScreen = preferenceManager.createPreferenceScreen(mContext);
preferenceScreen.addPreference(mPreference);
mPrefController = new ZenModesListPreferenceController(mContext, null, mBackend);
}
@Test
@EnableFlags(Flags.FLAG_MODES_UI)
public void updateState_addsPreferences() {
ImmutableList<ZenMode> modes = ImmutableList.of(newMode("One"), newMode("Two"),
newMode("Three"), newMode("Four"), newMode("Five"));
when(mBackend.getModes()).thenReturn(modes);
mPrefController.updateState(mPreference);
assertThat(mPreference.getPreferenceCount()).isEqualTo(5);
List<ZenModesListItemPreference> itemPreferences = getModeListItems(mPreference);
assertThat(itemPreferences.stream().map(pref -> pref.mZenMode).toList())
.containsExactlyElementsIn(modes)
.inOrder();
for (int i = 0; i < modes.size(); i++) {
assertThat(((ZenModesListItemPreference) (mPreference.getPreference(i))).mZenMode)
.isEqualTo(modes.get(i));
}
}
@Test
@EnableFlags(Flags.FLAG_MODES_UI)
public void updateState_secondTime_updatesPreferences() {
ImmutableList<ZenMode> modes = ImmutableList.of(newMode("One"), newMode("Two"),
newMode("Three"), newMode("Four"), newMode("Five"));
when(mBackend.getModes()).thenReturn(modes);
mPrefController.updateState(mPreference);
assertThat(mPreference.getPreferenceCount()).isEqualTo(5);
List<ZenModesListItemPreference> oldPreferences = getModeListItems(mPreference);
ImmutableList<ZenMode> updatedModes = ImmutableList.of(modes.get(0), modes.get(1),
newMode("Two.1"), newMode("Two.2"), modes.get(2), /* deleted "Four" */
modes.get(4));
when(mBackend.getModes()).thenReturn(updatedModes);
mPrefController.updateState(mPreference);
List<ZenModesListItemPreference> newPreferences = getModeListItems(mPreference);
assertThat(newPreferences.stream().map(pref -> pref.mZenMode).toList())
.containsExactlyElementsIn(updatedModes)
.inOrder();
// Verify that the old preference controllers were reused instead of creating new ones.
assertThat(newPreferences.get(0)).isSameInstanceAs(oldPreferences.get(0));
assertThat(newPreferences.get(1)).isSameInstanceAs(oldPreferences.get(1));
assertThat(newPreferences.get(4)).isSameInstanceAs(oldPreferences.get(2));
assertThat(newPreferences.get(5)).isSameInstanceAs(oldPreferences.get(4));
}
@Test
@DisableFlags(Flags.FLAG_MODES_UI)
public void testModesUiOff_notAvailableAndNoSearchData() {
@@ -151,4 +216,28 @@ public class ZenModesListPreferenceControllerTest {
assertThat(item1.key).isEqualTo(TEST_MODE_ID);
assertThat(item1.title).isEqualTo(TEST_MODE_NAME);
}
private static ZenMode newMode(String id) {
return new ZenMode(
id,
new AutomaticZenRule.Builder("Mode " + id, Uri.parse("test_uri"))
.setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY)
.setZenPolicy(new ZenPolicy.Builder().allowAllSounds().build())
.build(),
false);
}
/**
* Returns the child preferences of the {@code group}, sorted by their
* {@link Preference#getOrder} value (which is the order they will be sorted by and displayed
* in the UI).
*/
private List<ZenModesListItemPreference> getModeListItems(PreferenceGroup group) {
ArrayList<ZenModesListItemPreference> items = new ArrayList<>();
for (int i = 0; i < group.getPreferenceCount(); i++) {
items.add((ZenModesListItemPreference) group.getPreference(i));
}
items.sort(Comparator.comparing(Preference::getOrder));
return items;
}
}