Use the prebuilt manual DND mode from TestModeBuilder in tests

Instead of unnecessarily creating them by hand. This also allows restricting the visibility of the ZenMode constructors (which should only be instantiated by ZenModesBackend in production code).

Bug: 360817586
Test: atest com.android.settings.notification.modes
Flag: android.app.modes_ui
Change-Id: Id5a232a5a0ffc7bc09652fc4b6e6dd2a6383a2ae
This commit is contained in:
Matías Hernández
2024-08-19 15:13:15 +02:00
parent 6b25f15fe6
commit c5c158ab38
6 changed files with 14 additions and 51 deletions

View File

@@ -18,11 +18,9 @@ package com.android.settings.notification.modes;
import static com.google.common.truth.Truth.assertThat;
import android.app.AutomaticZenRule;
import android.app.Flags;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
@@ -31,7 +29,6 @@ import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import com.android.settingslib.notification.modes.TestModeBuilder;
import com.android.settingslib.notification.modes.ZenMode;
import com.android.settingslib.notification.modes.ZenModesBackend;
import org.junit.Before;
@@ -73,10 +70,8 @@ public class ManualDurationPreferenceControllerTest {
@Test
public void testIsAvailable_onlyForManualDnd() {
assertThat(mPrefController.isAvailable(TestModeBuilder.EXAMPLE)).isFalse();
ZenMode manualDnd = ZenMode.manualDndMode(
new AutomaticZenRule.Builder("id", Uri.EMPTY).build(), false);
assertThat(mPrefController.isAvailable(manualDnd)).isTrue();
assertThat(mPrefController.isAvailable(TestModeBuilder.MANUAL_DND_ACTIVE)).isTrue();
assertThat(mPrefController.isAvailable(TestModeBuilder.MANUAL_DND_INACTIVE)).isTrue();
}
@Test

View File

@@ -23,11 +23,9 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.AutomaticZenRule;
import android.app.Flags;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
@@ -192,8 +190,7 @@ public final class ZenModeButtonPreferenceControllerTest {
Button button = new Button(mContext);
LayoutPreference pref = mock(LayoutPreference.class);
when(pref.findViewById(anyInt())).thenReturn(button);
ZenMode zenMode = ZenMode.manualDndMode(
new AutomaticZenRule.Builder("manual", Uri.EMPTY).build(), false);
ZenMode zenMode = TestModeBuilder.MANUAL_DND_INACTIVE;
mController.updateZenMode(pref, zenMode);
button.callOnClick();
@@ -207,8 +204,7 @@ public final class ZenModeButtonPreferenceControllerTest {
Button button = new Button(mContext);
LayoutPreference pref = mock(LayoutPreference.class);
when(pref.findViewById(anyInt())).thenReturn(button);
ZenMode zenMode = ZenMode.manualDndMode(
new AutomaticZenRule.Builder("manual", Uri.EMPTY).build(), false);
ZenMode zenMode = TestModeBuilder.MANUAL_DND_INACTIVE;
mController.updateZenMode(pref, zenMode);
button.callOnClick();

View File

@@ -19,7 +19,6 @@ package com.android.settings.notification.modes;
import static android.app.AutomaticZenRule.TYPE_OTHER;
import static android.app.AutomaticZenRule.TYPE_SCHEDULE_CALENDAR;
import static android.app.AutomaticZenRule.TYPE_SCHEDULE_TIME;
import static android.app.NotificationManager.INTERRUPTION_FILTER_PRIORITY;
import static android.platform.test.flag.junit.SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT;
import static com.android.settings.notification.modes.CharSequenceTruth.assertThat;
@@ -28,7 +27,6 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import android.app.AutomaticZenRule;
import android.app.Flags;
import android.content.Context;
import android.net.Uri;
@@ -125,12 +123,7 @@ public class ZenModeTriggerAddPreferenceControllerTest {
@Test
public void isAvailable_manualDND_false() {
ZenMode manualMode = ZenMode.manualDndMode(new AutomaticZenRule.Builder("Do Not Disturb",
Uri.parse("manual"))
.setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY)
.build(), /* isActive= */ false);
mController.setZenMode(manualMode);
mController.setZenMode(TestModeBuilder.MANUAL_DND_INACTIVE);
assertThat(mController.isAvailable()).isFalse();
}

View File

@@ -18,15 +18,12 @@ package com.android.settings.notification.modes;
import static android.app.AutomaticZenRule.TYPE_OTHER;
import static android.app.AutomaticZenRule.TYPE_SCHEDULE_CALENDAR;
import static android.app.NotificationManager.INTERRUPTION_FILTER_PRIORITY;
import static android.platform.test.flag.junit.SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT;
import static com.google.common.truth.Truth.assertThat;
import android.app.AutomaticZenRule;
import android.app.Flags;
import android.content.Context;
import android.net.Uri;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.service.notification.SystemZenRules;
@@ -116,12 +113,7 @@ public class ZenModeTriggerCategoryPreferenceControllerTest {
@Test
public void isAvailable_manualDND_false() {
ZenMode manualMode = ZenMode.manualDndMode(new AutomaticZenRule.Builder("Do Not Disturb",
Uri.parse("manual"))
.setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY)
.build(), /* isActive= */ false);
mController.setZenMode(manualMode);
mController.setZenMode(TestModeBuilder.MANUAL_DND_INACTIVE);
assertThat(mController.isAvailable()).isFalse();
}
}

View File

@@ -19,7 +19,6 @@ package com.android.settings.notification.modes;
import static android.app.AutomaticZenRule.TYPE_OTHER;
import static android.app.AutomaticZenRule.TYPE_SCHEDULE_CALENDAR;
import static android.app.AutomaticZenRule.TYPE_SCHEDULE_TIME;
import static android.app.NotificationManager.INTERRUPTION_FILTER_PRIORITY;
import static android.platform.test.flag.junit.SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT;
import static com.android.settings.notification.modes.CharSequenceTruth.assertThat;
@@ -35,13 +34,11 @@ import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
import android.app.AlertDialog;
import android.app.AutomaticZenRule;
import android.app.Flags;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Looper;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
@@ -149,12 +146,7 @@ public class ZenModeTriggerUpdatePreferenceControllerTest {
@Test
public void isAvailable_manualDND_false() {
ZenMode manualMode = ZenMode.manualDndMode(new AutomaticZenRule.Builder("Do Not Disturb",
Uri.parse("manual"))
.setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY)
.build(), /* isActive= */ false);
mController.setZenMode(manualMode);
mController.setZenMode(TestModeBuilder.MANUAL_DND_INACTIVE);
assertThat(mController.isAvailable()).isFalse();
}

View File

@@ -18,6 +18,8 @@ package com.android.settings.notification.modes;
import static android.app.NotificationManager.INTERRUPTION_FILTER_PRIORITY;
import static com.android.settingslib.notification.modes.TestModeBuilder.MANUAL_DND_INACTIVE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
@@ -71,13 +73,6 @@ public class ZenModesListPreferenceControllerTest {
.build())
.build();
private static final ZenMode TEST_MANUAL_MODE = ZenMode.manualDndMode(
new AutomaticZenRule.Builder("Do Not Disturb", Uri.EMPTY)
.setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY)
.setZenPolicy(new ZenPolicy.Builder().allowAllSounds().build())
.build(),
false);
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(
SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT);
@@ -152,7 +147,7 @@ public class ZenModesListPreferenceControllerTest {
@DisableFlags(Flags.FLAG_MODES_UI)
public void testModesUiOff_notAvailableAndNoSearchData() {
// There exist modes
when(mBackend.getModes()).thenReturn(List.of(TEST_MANUAL_MODE, TEST_MODE));
when(mBackend.getModes()).thenReturn(List.of(MANUAL_DND_INACTIVE, TEST_MODE));
assertThat(mPrefController.isAvailable()).isFalse();
List<SearchIndexableRaw> data = new ArrayList<>();
@@ -187,20 +182,20 @@ public class ZenModesListPreferenceControllerTest {
// Changing mode data so there's a different one mode doesn't keep any previous data
// (and setting that state up in the caller)
when(mBackend.getModes()).thenReturn(List.of(TEST_MANUAL_MODE));
when(mBackend.getModes()).thenReturn(List.of(MANUAL_DND_INACTIVE));
List<SearchIndexableRaw> newData = new ArrayList<>();
mPrefController.updateDynamicRawDataToIndex(newData);
assertThat(newData).hasSize(1);
SearchIndexableRaw newItem = newData.get(0);
assertThat(newItem.key).isEqualTo(TEST_MANUAL_MODE.getId());
assertThat(newItem.key).isEqualTo(MANUAL_DND_INACTIVE.getId());
assertThat(newItem.title).isEqualTo("Do Not Disturb"); // set above
}
@Test
@EnableFlags(Flags.FLAG_MODES_UI)
public void testUpdateDynamicRawDataToIndex_multipleModes() {
when(mBackend.getModes()).thenReturn(List.of(TEST_MANUAL_MODE, TEST_MODE));
when(mBackend.getModes()).thenReturn(List.of(MANUAL_DND_INACTIVE, TEST_MODE));
List<SearchIndexableRaw> data = new ArrayList<>();
mPrefController.updateDynamicRawDataToIndex(data);
@@ -208,7 +203,7 @@ public class ZenModesListPreferenceControllerTest {
// Should keep the order presented by getModes()
SearchIndexableRaw item0 = data.get(0);
assertThat(item0.key).isEqualTo(TEST_MANUAL_MODE.getId());
assertThat(item0.key).isEqualTo(MANUAL_DND_INACTIVE.getId());
assertThat(item0.title).isEqualTo("Do Not Disturb"); // set above
SearchIndexableRaw item1 = data.get(1);