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.Answers.RETURNS_DEEP_STUBS;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25 
26 import android.app.admin.DevicePolicyManager;
27 import android.content.ContentResolver;
28 import android.content.Context;
29 import android.provider.Settings.Global;
30 
31 import androidx.fragment.app.FragmentActivity;
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 import org.robolectric.annotation.Config;
43 
44 @RunWith(RobolectricTestRunner.class)
45 @Config(shadows = {
46         com.android.settings.testutils.shadow.ShadowFragment.class,
47 })
48 public class DockingSoundPreferenceControllerTest {
49 
50     @Mock
51     private PreferenceScreen mScreen;
52     @Mock(answer = RETURNS_DEEP_STUBS)
53     private FragmentActivity mActivity;
54     @Mock
55     private ContentResolver mContentResolver;
56     @Mock
57     private SoundSettings mSetting;
58     @Mock(answer = RETURNS_DEEP_STUBS)
59     private Context mContext;
60 
61     private DockingSoundPreferenceController mController;
62     private SwitchPreference mPreference;
63 
64     @Before
setUp()65     public void setUp() {
66         MockitoAnnotations.initMocks(this);
67         doReturn(mock(DevicePolicyManager.class)).when(mContext)
68                 .getSystemService(Context.DEVICE_POLICY_SERVICE);
69         when(mSetting.getActivity()).thenReturn(mActivity);
70         when(mActivity.getContentResolver()).thenReturn(mContentResolver);
71         mPreference = new SwitchPreference(RuntimeEnvironment.application);
72         when(mActivity.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
73             .thenReturn(true);
74         mController = new DockingSoundPreferenceController(mContext, mSetting, null);
75         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
76         doReturn(mScreen).when(mSetting).getPreferenceScreen();
77     }
78 
79     @Test
isAvailable_hasDockSettings_shouldReturnTrue()80     public void isAvailable_hasDockSettings_shouldReturnTrue() {
81         when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
82             .thenReturn(true);
83 
84         assertThat(mController.isAvailable()).isTrue();
85     }
86 
87     @Test
isAvailable_noDockSettings_shouldReturnFalse()88     public void isAvailable_noDockSettings_shouldReturnFalse() {
89         when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
90             .thenReturn(false);
91 
92         assertThat(mController.isAvailable()).isFalse();
93     }
94 
95     @Test
displayPreference_dockingSoundEnabled_shouldCheckedPreference()96     public void displayPreference_dockingSoundEnabled_shouldCheckedPreference() {
97         Global.putInt(mContentResolver, Global.DOCK_SOUNDS_ENABLED, 1);
98 
99         mController.displayPreference(mScreen);
100 
101         assertThat(mPreference.isChecked()).isTrue();
102     }
103 
104     @Test
displayPreference_dockingSoundDisabled_shouldUncheckedPreference()105     public void displayPreference_dockingSoundDisabled_shouldUncheckedPreference() {
106         Global.putInt(mContentResolver, Global.DOCK_SOUNDS_ENABLED, 0);
107 
108         mController.displayPreference(mScreen);
109 
110         assertThat(mPreference.isChecked()).isFalse();
111     }
112 
113     @Test
onPreferenceChanged_preferenceChecked_shouldEnabledDockingSound()114     public void onPreferenceChanged_preferenceChecked_shouldEnabledDockingSound() {
115         mController.displayPreference(mScreen);
116 
117         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, true);
118 
119         assertThat(Global.getInt(mContentResolver, Global.DOCK_SOUNDS_ENABLED, 1)).isEqualTo(1);
120     }
121 
122     @Test
onPreferenceChanged_preferenceUnchecked_shouldDisabledDockingSound()123     public void onPreferenceChanged_preferenceUnchecked_shouldDisabledDockingSound() {
124         mController.displayPreference(mScreen);
125 
126         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, false);
127 
128         assertThat(Global.getInt(mContentResolver, Global.DOCK_SOUNDS_ENABLED, 1)).isEqualTo(0);
129     }
130 }
131