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 android.app.Activity; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.os.Vibrator; 23 import android.support.v14.preference.SwitchPreference; 24 import android.support.v7.preference.PreferenceScreen; 25 import android.provider.Settings.System; 26 27 import com.android.settings.testutils.SettingsRobolectricTestRunner; 28 import com.android.settings.TestConfig; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.annotation.Config; 36 import org.robolectric.shadows.ShadowApplication; 37 38 import static com.google.common.truth.Truth.assertThat; 39 40 import static org.mockito.Mockito.doReturn; 41 import static org.mockito.Mockito.when; 42 43 @RunWith(SettingsRobolectricTestRunner.class) 44 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 45 public class VibrateOnTouchPreferenceControllerTest { 46 47 @Mock 48 private PreferenceScreen mScreen; 49 @Mock 50 private Activity mActivity; 51 @Mock 52 private ContentResolver mContentResolver; 53 @Mock 54 private SoundSettings mSetting; 55 @Mock 56 private Context mContext; 57 @Mock 58 private Vibrator mVibrator; 59 60 private VibrateOnTouchPreferenceController mController; 61 private SwitchPreference mPreference; 62 63 @Before 64 public void setUp() { 65 MockitoAnnotations.initMocks(this); 66 when(mActivity.getSystemService(Context.VIBRATOR_SERVICE)).thenReturn(mVibrator); 67 when(mContext.getSystemService(Context.VIBRATOR_SERVICE)).thenReturn(mVibrator); 68 when(mVibrator.hasVibrator()).thenReturn(true); 69 when(mSetting.getActivity()).thenReturn(mActivity); 70 when(mActivity.getContentResolver()).thenReturn(mContentResolver); 71 mPreference = new SwitchPreference(ShadowApplication.getInstance().getApplicationContext()); 72 mController = new VibrateOnTouchPreferenceController(mContext, mSetting, null); 73 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 74 doReturn(mScreen).when(mSetting).getPreferenceScreen(); 75 } 76 77 @Test 78 public void isAvailable_hasHaptic_shouldReturnTrue() { 79 80 assertThat(mController.isAvailable()).isTrue(); 81 } 82 83 @Test 84 public void isAvailable_noHaptic_shouldReturnFalse() { 85 when(mVibrator.hasVibrator()).thenReturn(false); 86 87 assertThat(mController.isAvailable()).isFalse(); 88 } 89 90 @Test 91 public void displayPreference_hapticEnabled_shouldCheckedPreference() { 92 System.putInt(mContentResolver, System.HAPTIC_FEEDBACK_ENABLED, 1); 93 94 mController.displayPreference(mScreen); 95 96 assertThat(mPreference.isChecked()).isTrue(); 97 } 98 99 @Test 100 public void displayPreference_hapticDisabled_shouldUncheckedPreference() { 101 System.putInt(mContentResolver, System.HAPTIC_FEEDBACK_ENABLED, 0); 102 103 mController.displayPreference(mScreen); 104 105 assertThat(mPreference.isChecked()).isFalse(); 106 } 107 108 @Test 109 public void onPreferenceChanged_preferenceChecked_shouldEnabledHaptic() { 110 mController.displayPreference(mScreen); 111 112 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, true); 113 114 assertThat(System.getInt(mContentResolver, System.HAPTIC_FEEDBACK_ENABLED, 1)).isEqualTo(1); 115 } 116 117 @Test 118 public void onPreferenceChanged_preferenceUnchecked_shouldDisabledHaptic() { 119 mController.displayPreference(mScreen); 120 121 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, false); 122 123 assertThat(System.getInt(mContentResolver, System.HAPTIC_FEEDBACK_ENABLED, 1)).isEqualTo(0); 124 } 125 } 126