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 com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.times;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceScreen;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.RuntimeEnvironment;
38 
39 @RunWith(RobolectricTestRunner.class)
40 public class SilentStatusBarPreferenceControllerTest {
41 
42     @Mock
43     private NotificationBackend mBackend;
44     @Mock
45     private PreferenceScreen mScreen;
46 
47     private Context mContext;
48     private SilentStatusBarPreferenceController mController;
49     private Preference mPreference;
50 
51     @Before
setUp()52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54         mContext = RuntimeEnvironment.application;
55         mController = new SilentStatusBarPreferenceController(mContext);
56         mController.setBackend(mBackend);
57         mPreference = new Preference(mContext);
58         mPreference.setKey(mController.getPreferenceKey());
59         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
60     }
61 
62     @Test
isChecked_settingIsOff()63     public void isChecked_settingIsOff() {
64         when(mBackend.shouldHideSilentStatusBarIcons(any())).thenReturn(false);
65         assertThat(mController.isChecked()).isFalse();
66     }
67 
68     @Test
isChecked_settingIsOn()69     public void isChecked_settingIsOn() {
70         when(mBackend.shouldHideSilentStatusBarIcons(any())).thenReturn(true);
71         assertThat(mController.isChecked()).isTrue();
72     }
73 
74     @Test
setChecked_Off()75     public void setChecked_Off() {
76         mController.setChecked(false);
77         verify(mBackend, times(1)).setHideSilentStatusIcons(false);
78     }
79 
80     @Test
setChecked_On()81     public void setChecked_On() {
82         mController.setChecked(true);
83         verify(mBackend, times(1)).setHideSilentStatusIcons(true);
84     }
85 }