1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.notification; 18 19 import static android.provider.Settings.Secure.NOTIFICATION_BUBBLES; 20 21 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 22 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; 23 import static com.android.settings.notification.BadgingNotificationPreferenceController.OFF; 24 import static com.android.settings.notification.BadgingNotificationPreferenceController.ON; 25 26 import static com.google.common.truth.Truth.assertThat; 27 28 import static org.junit.Assert.assertEquals; 29 import static org.mockito.Mockito.when; 30 31 import android.app.ActivityManager; 32 import android.content.Context; 33 import android.provider.Settings; 34 35 import androidx.preference.PreferenceScreen; 36 37 import com.android.settingslib.widget.MainSwitchPreference; 38 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Answers; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.RobolectricTestRunner; 46 import org.robolectric.RuntimeEnvironment; 47 import org.robolectric.shadow.api.Shadow; 48 import org.robolectric.shadows.ShadowActivityManager; 49 50 @RunWith(RobolectricTestRunner.class) 51 public class BubbleNotificationPreferenceControllerTest { 52 53 private Context mContext; 54 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 55 private PreferenceScreen mScreen; 56 57 private BubbleNotificationPreferenceController mController; 58 private MainSwitchPreference mPreference; 59 60 private static final String KEY_NOTIFICATION_BUBBLES = "notification_bubbles"; 61 62 @Before setUp()63 public void setUp() { 64 MockitoAnnotations.initMocks(this); 65 mContext = RuntimeEnvironment.application; 66 mController = new BubbleNotificationPreferenceController(mContext, 67 KEY_NOTIFICATION_BUBBLES); 68 mPreference = new MainSwitchPreference(RuntimeEnvironment.application); 69 mPreference.setKey(mController.getPreferenceKey()); 70 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 71 mController.displayPreference(mScreen); 72 } 73 74 @Test isAvailable_lowRam_returnsUnsupported()75 public void isAvailable_lowRam_returnsUnsupported() { 76 final ShadowActivityManager activityManager = 77 Shadow.extract(mContext.getSystemService(ActivityManager.class)); 78 activityManager.setIsLowRamDevice(true); 79 assertEquals(UNSUPPORTED_ON_DEVICE, mController.getAvailabilityStatus()); 80 } 81 82 @Test isAvailable_notLowRam_returnsAvailable()83 public void isAvailable_notLowRam_returnsAvailable() { 84 final ShadowActivityManager activityManager = 85 Shadow.extract(mContext.getSystemService(ActivityManager.class)); 86 activityManager.setIsLowRamDevice(false); 87 assertEquals(AVAILABLE, mController.getAvailabilityStatus()); 88 } 89 90 @Test updateState_settingIsOff_preferenceSetUnchecked()91 public void updateState_settingIsOff_preferenceSetUnchecked() { 92 Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF); 93 assertThat(Settings.Global.getInt(mContext.getContentResolver(), 94 NOTIFICATION_BUBBLES, ON)).isEqualTo(OFF); 95 96 mPreference.updateStatus(false); 97 98 assertThat(mPreference.isChecked()).isFalse(); 99 } 100 101 @Test onSwitchChanged_true_settingIsOff_flagShouldOn()102 public void onSwitchChanged_true_settingIsOff_flagShouldOn() { 103 Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF); 104 105 mController.onSwitchChanged(null, true); 106 107 assertThat(Settings.Global.getInt(mContext.getContentResolver(), 108 NOTIFICATION_BUBBLES, OFF)).isEqualTo(ON); 109 } 110 111 @Test onSwitchChanged_false_settingIsOn_flagShouldOff()112 public void onSwitchChanged_false_settingIsOn_flagShouldOff() { 113 Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON); 114 115 mController.onSwitchChanged(null, false); 116 117 assertThat(Settings.Global.getInt(mContext.getContentResolver(), 118 NOTIFICATION_BUBBLES, ON)).isEqualTo(OFF); 119 } 120 121 @Test setChecked_setFalse_disablesSetting()122 public void setChecked_setFalse_disablesSetting() { 123 Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON); 124 125 mController.setChecked(false); 126 int updatedValue = Settings.Global.getInt(mContext.getContentResolver(), 127 NOTIFICATION_BUBBLES, ON); 128 129 assertThat(updatedValue).isEqualTo(OFF); 130 } 131 132 @Test setChecked_setTrue_enablesSetting()133 public void setChecked_setTrue_enablesSetting() { 134 Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF); 135 136 mController.setChecked(true); 137 int updatedValue = Settings.Global.getInt(mContext.getContentResolver(), 138 NOTIFICATION_BUBBLES, OFF); 139 140 assertThat(updatedValue).isEqualTo(ON); 141 } 142 143 @Test isSliceable_returnsFalse()144 public void isSliceable_returnsFalse() { 145 assertThat(mController.isSliceable()).isFalse(); 146 } 147 } 148