1 /*
2  * Copyright (C) 2018 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.gestures;
18 
19 import static android.provider.Settings.Secure.VOLUME_HUSH_GESTURE;
20 import static android.provider.Settings.Secure.VOLUME_HUSH_MUTE;
21 import static android.provider.Settings.Secure.VOLUME_HUSH_OFF;
22 import static android.provider.Settings.Secure.VOLUME_HUSH_VIBRATE;
23 
24 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
25 
26 import static com.google.common.truth.Truth.assertThat;
27 
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.when;
30 
31 import android.content.Context;
32 import android.content.res.Resources;
33 import android.provider.Settings;
34 
35 import com.android.settings.R;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.RuntimeEnvironment;
44 
45 @RunWith(RobolectricTestRunner.class)
46 public class PreventRingingParentPreferenceControllerTest {
47 
48     @Mock
49     private Resources mResources;
50 
51     private Context mContext;
52     private PreventRingingParentPreferenceController mController;
53 
54     @Before
setUp()55     public void setUp() {
56         MockitoAnnotations.initMocks(this);
57         mContext = spy(RuntimeEnvironment.application.getApplicationContext());
58         mController = new PreventRingingParentPreferenceController(mContext, "test_key");
59     }
60 
61     @Test
testIsAvailable_configIsTrue_shouldAvailableUnSearchable()62     public void testIsAvailable_configIsTrue_shouldAvailableUnSearchable() {
63         when(mContext.getResources()).thenReturn(mResources);
64         when(mResources.getBoolean(
65                 com.android.internal.R.bool.config_volumeHushGestureEnabled)).thenReturn(true);
66 
67         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
68     }
69 
70     @Test
testIsAvailable_configIsFalse_shouldReturnFalse()71     public void testIsAvailable_configIsFalse_shouldReturnFalse() {
72         when(mContext.getResources()).thenReturn(mResources);
73         when(mResources.getBoolean(
74                 com.android.internal.R.bool.config_volumeHushGestureEnabled)).thenReturn(false);
75 
76         assertThat(mController.isAvailable()).isFalse();
77     }
78 
79     @Test
updateState_summaryUpdated()80     public void updateState_summaryUpdated() {
81         Settings.Secure.putInt(mContext.getContentResolver(), VOLUME_HUSH_GESTURE,
82                 VOLUME_HUSH_MUTE);
83         assertThat(mController.getSummary()).isEqualTo(mContext.getResources().getText(
84                 R.string.prevent_ringing_option_mute_summary));
85 
86         Settings.Secure.putInt(mContext.getContentResolver(), VOLUME_HUSH_GESTURE,
87                 VOLUME_HUSH_VIBRATE);
88         assertThat(mController.getSummary()).isEqualTo(mContext.getResources().getText(
89                 R.string.prevent_ringing_option_vibrate_summary));
90 
91         Settings.Secure.putInt(mContext.getContentResolver(), VOLUME_HUSH_GESTURE,
92                 VOLUME_HUSH_OFF);
93         assertThat(mController.getSummary()).isEqualTo(mContext.getResources().getText(
94                 R.string.prevent_ringing_option_none_summary));
95     }
96 }
97