• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.development;
18 
19 import static android.provider.Settings.Secure.NOTIFICATION_BUBBLES;
20 
21 import static com.android.settings.development.BubbleGlobalPreferenceController.OFF;
22 import static com.android.settings.development.BubbleGlobalPreferenceController.ON;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.content.Context;
30 import android.provider.Settings;
31 
32 import androidx.preference.PreferenceScreen;
33 import androidx.preference.SwitchPreference;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.RuntimeEnvironment;
42 
43 @RunWith(RobolectricTestRunner.class)
44 public class BubbleGlobalPreferenceControllerTest {
45     private Context mContext;
46 
47     @Mock
48     private SwitchPreference mPreference;
49     @Mock
50     private PreferenceScreen mPreferenceScreen;
51 
52     private BubbleGlobalPreferenceController mController;
53 
54     @Before
setUp()55     public void setUp() {
56         MockitoAnnotations.initMocks(this);
57         mContext = RuntimeEnvironment.application;
58         mController = new BubbleGlobalPreferenceController(mContext);
59         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
60                 .thenReturn(mPreference);
61         mController.displayPreference(mPreferenceScreen);
62     }
63 
64     @Test
onPreferenceChange_settingEnabled_allowBubbles_shouldBeOn()65     public void onPreferenceChange_settingEnabled_allowBubbles_shouldBeOn() {
66         mController.onPreferenceChange(mPreference, true /* new value */);
67 
68         assertThat(isSettingEnabled()).isTrue();
69     }
70 
71     @Test
onPreferenceChange_settingDisabled_allowBubbles_shouldBeOff()72     public void onPreferenceChange_settingDisabled_allowBubbles_shouldBeOff() {
73         mController.onPreferenceChange(mPreference, false /* new value */);
74 
75         assertThat(isSettingEnabled()).isFalse();
76     }
77 
78     @Test
updateState_settingEnabled_preferenceShouldBeChecked()79     public void updateState_settingEnabled_preferenceShouldBeChecked() {
80         Settings.Secure.putInt(mContext.getContentResolver(),
81                 NOTIFICATION_BUBBLES, 1 /* enabled */);
82         mController.updateState(mPreference);
83 
84         verify(mPreference).setChecked(true);
85     }
86 
87     @Test
updateState_settingReset_defaultDisabled_preferenceShouldNotBeChecked()88     public void updateState_settingReset_defaultDisabled_preferenceShouldNotBeChecked() {
89         Settings.Secure.putInt(mContext.getContentResolver(),
90                 NOTIFICATION_BUBBLES, 0 /* enabled */);
91         mController.updateState(mPreference);
92 
93         verify(mPreference).setChecked(false);
94     }
95 
96     @Test
onDeveloperOptionsSwitchDisabled_shouldDisable()97     public void onDeveloperOptionsSwitchDisabled_shouldDisable() {
98         mController.onDeveloperOptionsSwitchDisabled();
99 
100         verify(mPreference).setChecked(false);
101         verify(mPreference).setEnabled(false);
102 
103         assertThat(isSettingEnabled()).isFalse();
104     }
105 
isSettingEnabled()106     private boolean isSettingEnabled() {
107         return Settings.Secure.getInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES,
108                 OFF /* default off */) == ON;
109     }
110 
111 }
112