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 import androidx.test.core.app.ApplicationProvider; 37 38 import com.android.settingslib.testutils.shadow.ShadowInteractionJankMonitor; 39 import com.android.settingslib.widget.MainSwitchPreference; 40 41 import org.junit.Before; 42 import org.junit.Rule; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Answers; 46 import org.mockito.Mock; 47 import org.mockito.junit.MockitoJUnit; 48 import org.mockito.junit.MockitoRule; 49 import org.robolectric.RobolectricTestRunner; 50 import org.robolectric.annotation.Config; 51 import org.robolectric.shadow.api.Shadow; 52 import org.robolectric.shadows.ShadowActivityManager; 53 54 @RunWith(RobolectricTestRunner.class) 55 @Config(shadows = { 56 ShadowInteractionJankMonitor.class, 57 ShadowActivityManager.class, 58 }) 59 public class BubbleNotificationPreferenceControllerTest { 60 @Rule 61 public final MockitoRule mMockitoRule = MockitoJUnit.rule(); 62 63 private static final String KEY_NOTIFICATION_BUBBLES = "notification_bubbles"; 64 private Context mContext; 65 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 66 private PreferenceScreen mScreen; 67 68 private BubbleNotificationPreferenceController mController; 69 private MainSwitchPreference mPreference; 70 71 private ShadowActivityManager mActivityManager; 72 73 @Before setUp()74 public void setUp() { 75 mContext = ApplicationProvider.getApplicationContext(); 76 mController = new BubbleNotificationPreferenceController(mContext, 77 KEY_NOTIFICATION_BUBBLES); 78 mPreference = new MainSwitchPreference(mContext); 79 mPreference.setKey(mController.getPreferenceKey()); 80 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 81 mController.displayPreference(mScreen); 82 mActivityManager = Shadow.extract(mContext.getSystemService(ActivityManager.class)); 83 } 84 85 @Test isAvailable_lowRam_returnsUnsupported()86 public void isAvailable_lowRam_returnsUnsupported() { 87 mActivityManager.setIsLowRamDevice(true); 88 assertEquals(UNSUPPORTED_ON_DEVICE, mController.getAvailabilityStatus()); 89 } 90 91 @Test isAvailable_notLowRam_returnsAvailable()92 public void isAvailable_notLowRam_returnsAvailable() { 93 mActivityManager.setIsLowRamDevice(false); 94 assertEquals(AVAILABLE, mController.getAvailabilityStatus()); 95 } 96 97 @Test updateState_settingIsOff_preferenceSetUnchecked()98 public void updateState_settingIsOff_preferenceSetUnchecked() { 99 Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF); 100 assertThat(Settings.Global.getInt(mContext.getContentResolver(), 101 NOTIFICATION_BUBBLES, ON)).isEqualTo(OFF); 102 103 mPreference.setChecked(false); 104 105 assertThat(mPreference.isChecked()).isFalse(); 106 } 107 108 @Test onSwitchChanged_true_settingIsOff_flagShouldOn()109 public void onSwitchChanged_true_settingIsOff_flagShouldOn() { 110 Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF); 111 112 mController.setChecked(true); 113 114 assertThat(Settings.Global.getInt(mContext.getContentResolver(), 115 NOTIFICATION_BUBBLES, OFF)).isEqualTo(ON); 116 } 117 118 @Test onSwitchChanged_false_settingIsOn_flagShouldOff()119 public void onSwitchChanged_false_settingIsOn_flagShouldOff() { 120 Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON); 121 122 mController.setChecked(false); 123 124 assertThat(Settings.Global.getInt(mContext.getContentResolver(), 125 NOTIFICATION_BUBBLES, ON)).isEqualTo(OFF); 126 } 127 128 @Test setChecked_setFalse_disablesSetting()129 public void setChecked_setFalse_disablesSetting() { 130 Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON); 131 132 mController.setChecked(false); 133 int updatedValue = Settings.Global.getInt(mContext.getContentResolver(), 134 NOTIFICATION_BUBBLES, ON); 135 136 assertThat(updatedValue).isEqualTo(OFF); 137 } 138 139 @Test setChecked_setTrue_enablesSetting()140 public void setChecked_setTrue_enablesSetting() { 141 Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF); 142 143 mController.setChecked(true); 144 int updatedValue = Settings.Global.getInt(mContext.getContentResolver(), 145 NOTIFICATION_BUBBLES, OFF); 146 147 assertThat(updatedValue).isEqualTo(ON); 148 } 149 150 @Test isSliceable_returnsFalse()151 public void isSliceable_returnsFalse() { 152 assertThat(mController.isSliceable()).isFalse(); 153 } 154 } 155