1 /*
2  * Copyright (C) 2017 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.Mockito.doReturn;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.ContentResolver;
26 import android.content.Context;
27 import android.provider.Settings.Secure;
28 
29 import androidx.fragment.app.FragmentActivity;
30 import androidx.preference.PreferenceScreen;
31 import androidx.preference.SwitchPreference;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.robolectric.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 import org.robolectric.annotation.Config;
41 
42 @RunWith(RobolectricTestRunner.class)
43 @Config(shadows = {
44         com.android.settings.testutils.shadow.ShadowFragment.class,
45 })
46 public class ChargingSoundPreferenceControllerTest {
47 
48     @Mock
49     private PreferenceScreen mScreen;
50     @Mock
51     private FragmentActivity mActivity;
52     @Mock
53     private ContentResolver mContentResolver;
54     @Mock
55     private SoundSettings mSetting;
56 
57     private Context mContext;
58     private ChargingSoundPreferenceController mController;
59     private SwitchPreference mPreference;
60 
61     @Before
setUp()62     public void setUp() {
63         MockitoAnnotations.initMocks(this);
64         mContext = spy(RuntimeEnvironment.application);
65         when(mSetting.getActivity()).thenReturn(mActivity);
66         when(mActivity.getContentResolver()).thenReturn(mContentResolver);
67         mPreference = new SwitchPreference(RuntimeEnvironment.application);
68         mController = new ChargingSoundPreferenceController(mContext, mSetting, null);
69         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
70         doReturn(mScreen).when(mSetting).getPreferenceScreen();
71     }
72 
73     @Test
isAvailable_byDefault_isTrue()74     public void isAvailable_byDefault_isTrue() {
75         assertThat(mController.isAvailable()).isTrue();
76     }
77 
78     @Test
79     @Config(qualifiers = "mcc999")
isAvailable_whenNotVisible_isFalse()80     public void isAvailable_whenNotVisible_isFalse() {
81         assertThat(mController.isAvailable()).isFalse();
82     }
83 
84     @Test
displayPreference_chargingSoundEnabled_shouldCheckedPreference()85     public void displayPreference_chargingSoundEnabled_shouldCheckedPreference() {
86         Secure.putInt(mContentResolver, Secure.CHARGING_SOUNDS_ENABLED, 1);
87 
88         mController.displayPreference(mScreen);
89 
90         assertThat(mPreference.isChecked()).isTrue();
91     }
92 
93     @Test
displayPreference_chargingSoundDisabled_shouldUncheckedPreference()94     public void displayPreference_chargingSoundDisabled_shouldUncheckedPreference() {
95         Secure.putInt(mContentResolver, Secure.CHARGING_SOUNDS_ENABLED, 0);
96 
97         mController.displayPreference(mScreen);
98 
99         assertThat(mPreference.isChecked()).isFalse();
100     }
101 
102     @Test
onPreferenceChanged_preferenceChecked_shouldEnabledChargingSound()103     public void onPreferenceChanged_preferenceChecked_shouldEnabledChargingSound() {
104         mController.displayPreference(mScreen);
105 
106         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, true);
107 
108         assertThat(Secure.getInt(mContentResolver, Secure.CHARGING_SOUNDS_ENABLED, 1)).isEqualTo(1);
109     }
110 
111     @Test
onPreferenceChanged_preferenceUnchecked_shouldDisabledChargingSound()112     public void onPreferenceChanged_preferenceUnchecked_shouldDisabledChargingSound() {
113         mController.displayPreference(mScreen);
114 
115         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, false);
116 
117         assertThat(Secure.getInt(mContentResolver, Secure.CHARGING_SOUNDS_ENABLED, 1)).isEqualTo(0);
118     }
119 }
120