• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  import android.widget.Switch;
35  
36  import androidx.preference.PreferenceScreen;
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.Test;
43  import org.junit.runner.RunWith;
44  import org.mockito.Answers;
45  import org.mockito.Mock;
46  import org.mockito.MockitoAnnotations;
47  import org.robolectric.RobolectricTestRunner;
48  import org.robolectric.RuntimeEnvironment;
49  import org.robolectric.annotation.Config;
50  import org.robolectric.shadow.api.Shadow;
51  import org.robolectric.shadows.ShadowActivityManager;
52  
53  @RunWith(RobolectricTestRunner.class)
54  @Config(shadows = {ShadowInteractionJankMonitor.class})
55  public class BubbleNotificationPreferenceControllerTest {
56  
57      private Context mContext;
58      @Mock(answer = Answers.RETURNS_DEEP_STUBS)
59      private PreferenceScreen mScreen;
60  
61      @Mock
62      private Switch mSwitch;
63  
64      private BubbleNotificationPreferenceController mController;
65      private MainSwitchPreference mPreference;
66  
67      private static final String KEY_NOTIFICATION_BUBBLES = "notification_bubbles";
68  
69      @Before
setUp()70      public void setUp() {
71          MockitoAnnotations.initMocks(this);
72          mContext = RuntimeEnvironment.application;
73          mController = new BubbleNotificationPreferenceController(mContext,
74                  KEY_NOTIFICATION_BUBBLES);
75          mPreference = new MainSwitchPreference(RuntimeEnvironment.application);
76          mPreference.setKey(mController.getPreferenceKey());
77          when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
78          mController.displayPreference(mScreen);
79      }
80  
81      @Test
isAvailable_lowRam_returnsUnsupported()82      public void isAvailable_lowRam_returnsUnsupported() {
83          final ShadowActivityManager activityManager =
84                  Shadow.extract(mContext.getSystemService(ActivityManager.class));
85          activityManager.setIsLowRamDevice(true);
86          assertEquals(UNSUPPORTED_ON_DEVICE, mController.getAvailabilityStatus());
87      }
88  
89      @Test
isAvailable_notLowRam_returnsAvailable()90      public void isAvailable_notLowRam_returnsAvailable() {
91          final ShadowActivityManager activityManager =
92                  Shadow.extract(mContext.getSystemService(ActivityManager.class));
93          activityManager.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.updateStatus(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.onSwitchChanged(mSwitch, 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.onSwitchChanged(mSwitch, 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