From a7c14d474a9113b4fb88138b551be0e81a4c001b Mon Sep 17 00:00:00 2001 From: Chuck Liao Date: Fri, 30 Mar 2018 18:41:18 +0800 Subject: [PATCH] Refactor badging notification preference controller. - Convert inheritance from AbstractPreferenceController to TogglePreferenceController. - Register BadgingNotificationPreferenceController in XML. - Add RoboTests test cases for BadgingNotificationPreferenceController. Fixes: 67997784 Test: RunSettingsRoboTests Change-Id: If10744c067f065e7c2465ca1fff66895d7dc1c56 Merged-In: If10744c067f065e7c2465ca1fff66895d7dc1c56 --- res/xml/configure_notification_settings.xml | 3 +- .../BadgingNotificationPreferenceController.java | 37 ++++++------- .../ConfigureNotificationSettings.java | 3 -- ...adgingNotificationPreferenceControllerTest.java | 61 +++++++++++++++++++--- 4 files changed, 73 insertions(+), 31 deletions(-) diff --git a/res/xml/configure_notification_settings.xml b/res/xml/configure_notification_settings.xml index 612f5e7623..291d9a8da6 100644 --- a/res/xml/configure_notification_settings.xml +++ b/res/xml/configure_notification_settings.xml @@ -28,7 +28,8 @@ + android:title="@string/notification_badging_title" + settings:controller="com.android.settings.notification.BadgingNotificationPreferenceController"/> buildPreferenceControllers(Context context, Lifecycle lifecycle, Application app, Fragment host) { final List controllers = new ArrayList<>(); - final BadgingNotificationPreferenceController badgeController = - new BadgingNotificationPreferenceController(context); final PulseNotificationPreferenceController pulseController = new PulseNotificationPreferenceController(context); final LockScreenNotificationPreferenceController lockScreenNotificationController = @@ -106,7 +104,6 @@ public class ConfigureNotificationSettings extends DashboardFragment { } controllers.add(new RecentNotifyingAppsPreferenceController( context, new NotificationBackend(), app, host)); - controllers.add(badgeController); controllers.add(pulseController); controllers.add(lockScreenNotificationController); controllers.add(new NotificationRingtonePreferenceController(context) { diff --git a/tests/robotests/src/com/android/settings/notification/BadgingNotificationPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/BadgingNotificationPreferenceControllerTest.java index d852aaa5b3..f9991949e4 100644 --- a/tests/robotests/src/com/android/settings/notification/BadgingNotificationPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/BadgingNotificationPreferenceControllerTest.java @@ -17,6 +17,12 @@ package com.android.settings.notification; import static android.provider.Settings.Secure.NOTIFICATION_BADGING; + +import static com.android.settings.core.BasePreferenceController.AVAILABLE; +import static com.android.settings.core.BasePreferenceController.DISABLED_UNSUPPORTED; +import static com.android.settings.notification.BadgingNotificationPreferenceController.OFF; +import static com.android.settings.notification.BadgingNotificationPreferenceController.ON; + import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -54,10 +60,13 @@ public class BadgingNotificationPreferenceControllerTest { private BadgingNotificationPreferenceController mController; private Preference mPreference; + private static final String KEY_NOTIFICATION_BADGING = "notification_badging"; + @Before public void setUp() { MockitoAnnotations.initMocks(this); - mController = new BadgingNotificationPreferenceController(mContext); + mController = new BadgingNotificationPreferenceController(mContext, + KEY_NOTIFICATION_BADGING); mPreference = new Preference(RuntimeEnvironment.application); mPreference.setKey(mController.getPreferenceKey()); when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); @@ -88,9 +97,10 @@ public class BadgingNotificationPreferenceControllerTest { public void updateState_preferenceSetCheckedWhenSettingIsOn() { final TwoStatePreference preference = mock(TwoStatePreference.class); final Context context = RuntimeEnvironment.application; - Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, 1); + Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, ON); - mController = new BadgingNotificationPreferenceController(context); + mController = new BadgingNotificationPreferenceController(context, + KEY_NOTIFICATION_BADGING); mController.updateState(preference); verify(preference).setChecked(true); @@ -100,9 +110,10 @@ public class BadgingNotificationPreferenceControllerTest { public void updateState_preferenceSetUncheckedWhenSettingIsOff() { final TwoStatePreference preference = mock(TwoStatePreference.class); final Context context = RuntimeEnvironment.application; - Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, 0); + Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, OFF); - mController = new BadgingNotificationPreferenceController(context); + mController = new BadgingNotificationPreferenceController(context, + KEY_NOTIFICATION_BADGING); mController.updateState(preference); verify(preference).setChecked(false); @@ -118,11 +129,11 @@ public class BadgingNotificationPreferenceControllerTest { public void testSetValue_updatesCorrectly() { final int newValue = 0; ContentResolver resolver = mContext.getContentResolver(); - Settings.Secure.putInt(resolver, Settings.Secure.NOTIFICATION_BADGING, 1); + Settings.Secure.putInt(resolver, Settings.Secure.NOTIFICATION_BADGING, ON); ((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue); final int updatedValue = - Settings.Secure.getInt(resolver, Settings.Secure.NOTIFICATION_BADGING, 1); + Settings.Secure.getInt(resolver, Settings.Secure.NOTIFICATION_BADGING, ON); assertThat(updatedValue).isEqualTo(newValue); } @@ -138,4 +149,40 @@ public class BadgingNotificationPreferenceControllerTest { assertThat(newValue).isEqualTo(currentValue); } + + @Test + public void isChecked_settingIsOff_shouldReturnFalse() { + Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, OFF); + + assertThat(mController.isChecked()).isFalse(); + } + + @Test + public void isChecked_settingIsOn_shouldReturnTrue() { + Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, ON); + + assertThat(mController.isChecked()).isTrue(); + } + + @Test + public void setChecked_setFalse_disablesSetting() { + Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, ON); + + mController.setChecked(false); + int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(), + NOTIFICATION_BADGING, -1); + + assertThat(updatedValue).isEqualTo(OFF); + } + + @Test + public void setChecked_setTrue_enablesSetting() { + Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, OFF); + + mController.setChecked(true); + int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(), + NOTIFICATION_BADGING, -1); + + assertThat(updatedValue).isEqualTo(ON); + } } -- 2.11.0