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.when;
23 
24 import android.content.ContentResolver;
25 import android.content.Context;
26 import android.os.Vibrator;
27 import android.provider.Settings.System;
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 
41 @RunWith(RobolectricTestRunner.class)
42 public class VibrateOnTouchPreferenceControllerTest {
43 
44     @Mock
45     private PreferenceScreen mScreen;
46     @Mock
47     private FragmentActivity mActivity;
48     @Mock
49     private ContentResolver mContentResolver;
50     @Mock
51     private SoundSettings mSetting;
52     @Mock
53     private Context mContext;
54     @Mock
55     private Vibrator mVibrator;
56 
57     private VibrateOnTouchPreferenceController mController;
58     private SwitchPreference mPreference;
59 
60     @Before
setUp()61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63         when(mActivity.getSystemService(Context.VIBRATOR_SERVICE)).thenReturn(mVibrator);
64         when(mContext.getSystemService(Context.VIBRATOR_SERVICE)).thenReturn(mVibrator);
65         when(mVibrator.hasVibrator()).thenReturn(true);
66         when(mSetting.getActivity()).thenReturn(mActivity);
67         when(mActivity.getContentResolver()).thenReturn(mContentResolver);
68         mPreference = new SwitchPreference(RuntimeEnvironment.application);
69         mController = new VibrateOnTouchPreferenceController(mContext, mSetting, null);
70         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
71         doReturn(mScreen).when(mSetting).getPreferenceScreen();
72     }
73 
74     @Test
isAvailable_hasHaptic_shouldReturnTrue()75     public void isAvailable_hasHaptic_shouldReturnTrue() {
76         assertThat(mController.isAvailable()).isTrue();
77     }
78 
79     @Test
isAvailable_noHaptic_shouldReturnFalse()80     public void isAvailable_noHaptic_shouldReturnFalse() {
81         when(mVibrator.hasVibrator()).thenReturn(false);
82 
83         assertThat(mController.isAvailable()).isFalse();
84     }
85 
86     @Test
displayPreference_hapticEnabled_shouldCheckedPreference()87     public void displayPreference_hapticEnabled_shouldCheckedPreference() {
88         System.putInt(mContentResolver, System.HAPTIC_FEEDBACK_ENABLED, 1);
89 
90         mController.displayPreference(mScreen);
91 
92         assertThat(mPreference.isChecked()).isTrue();
93     }
94 
95     @Test
displayPreference_hapticDisabled_shouldUncheckedPreference()96     public void displayPreference_hapticDisabled_shouldUncheckedPreference() {
97         System.putInt(mContentResolver, System.HAPTIC_FEEDBACK_ENABLED, 0);
98 
99         mController.displayPreference(mScreen);
100 
101         assertThat(mPreference.isChecked()).isFalse();
102     }
103 
104     @Test
onPreferenceChanged_preferenceChecked_shouldEnabledHaptic()105     public void onPreferenceChanged_preferenceChecked_shouldEnabledHaptic() {
106         mController.displayPreference(mScreen);
107 
108         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, true);
109 
110         assertThat(System.getInt(mContentResolver, System.HAPTIC_FEEDBACK_ENABLED, 1)).isEqualTo(1);
111     }
112 
113     @Test
onPreferenceChanged_preferenceUnchecked_shouldDisabledHaptic()114     public void onPreferenceChanged_preferenceUnchecked_shouldDisabledHaptic() {
115         mController.displayPreference(mScreen);
116 
117         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, false);
118 
119         assertThat(System.getInt(mContentResolver, System.HAPTIC_FEEDBACK_ENABLED, 1)).isEqualTo(0);
120     }
121 }
122