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.gestures; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Matchers.any; 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.app.Activity; 25 import android.content.ContentResolver; 26 import android.content.Context; 27 import android.provider.Settings; 28 import android.support.v7.preference.Preference; 29 30 import com.android.settings.R; 31 import com.android.settings.testutils.FakeFeatureFactory; 32 import com.android.settings.testutils.SettingsRobolectricTestRunner; 33 import com.android.settings.testutils.shadow.ShadowSecureSettings; 34 import com.android.settingslib.core.AbstractPreferenceController; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Answers; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.RuntimeEnvironment; 43 import org.robolectric.annotation.Config; 44 import org.robolectric.util.ReflectionHelpers; 45 46 import java.util.ArrayList; 47 import java.util.List; 48 49 @RunWith(SettingsRobolectricTestRunner.class) 50 public class GesturesSettingsPreferenceControllerTest { 51 52 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 53 private Activity mActivity; 54 @Mock 55 private Preference mPreference; 56 57 private GesturesSettingPreferenceController mController; 58 59 @Before setUp()60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 FakeFeatureFactory.setupForTest(); 63 mController = new GesturesSettingPreferenceController(mActivity); 64 } 65 66 @Test isAvailable_hasGesture_shouldReturnTrue()67 public void isAvailable_hasGesture_shouldReturnTrue() { 68 final List<AbstractPreferenceController> mControllers = new ArrayList<>(); 69 mControllers.add(new AbstractPreferenceController(RuntimeEnvironment.application) { 70 @Override 71 public boolean isAvailable() { 72 return true; 73 } 74 75 @Override 76 public String getPreferenceKey() { 77 return "test_key"; 78 } 79 }); 80 ReflectionHelpers.setField(mController, "mGestureControllers", mControllers); 81 82 assertThat(mController.isAvailable()).isTrue(); 83 } 84 85 @Test isAvailable_noGesture_shouldReturnFalse()86 public void isAvailable_noGesture_shouldReturnFalse() { 87 ReflectionHelpers.setField(mController, "mGestureControllers", 88 new ArrayList<AbstractPreferenceController>()); 89 90 assertThat(mController.isAvailable()).isFalse(); 91 } 92 93 @Test 94 @Config(shadows = ShadowSecureSettings.class) updateState_assistSupported_shouldSetToAssistGestureStatus()95 public void updateState_assistSupported_shouldSetToAssistGestureStatus() { 96 final FakeFeatureFactory featureFactory = 97 (FakeFeatureFactory) FakeFeatureFactory.getFactory(mActivity); 98 when(featureFactory.assistGestureFeatureProvider.isSupported(any(Context.class))) 99 .thenReturn(true); 100 when(featureFactory.assistGestureFeatureProvider.isSensorAvailable(any(Context.class))) 101 .thenReturn(true); 102 103 final ContentResolver cr = mActivity.getContentResolver(); 104 Settings.Secure.putInt(cr, Settings.Secure.ASSIST_GESTURE_ENABLED, 0); 105 Settings.Secure.putInt(cr, Settings.Secure.ASSIST_GESTURE_SILENCE_ALERTS_ENABLED, 0); 106 mController.updateState(mPreference); 107 verify(mActivity).getText(R.string.language_input_gesture_summary_off); 108 109 Settings.Secure.putInt(cr, Settings.Secure.ASSIST_GESTURE_ENABLED, 1); 110 Settings.Secure.putInt(cr, Settings.Secure.ASSIST_GESTURE_SILENCE_ALERTS_ENABLED, 0); 111 mController.updateState(mPreference); 112 verify(mActivity).getText(R.string.language_input_gesture_summary_on_with_assist); 113 114 Settings.Secure.putInt(cr, Settings.Secure.ASSIST_GESTURE_ENABLED, 0); 115 Settings.Secure.putInt(cr, Settings.Secure.ASSIST_GESTURE_SILENCE_ALERTS_ENABLED, 1); 116 mController.updateState(mPreference); 117 verify(mActivity).getText(R.string.language_input_gesture_summary_on_non_assist); 118 } 119 120 @Test 121 @Config(shadows = ShadowSecureSettings.class) updateState_sensorNotAvailable_shouldSetToEmptyStatus()122 public void updateState_sensorNotAvailable_shouldSetToEmptyStatus() { 123 final FakeFeatureFactory featureFactory = 124 (FakeFeatureFactory) FakeFeatureFactory.getFactory(mActivity); 125 when(featureFactory.assistGestureFeatureProvider.isSensorAvailable(any(Context.class))) 126 .thenReturn(false); 127 128 mController.updateState(mPreference); 129 verify(mPreference).setSummary(""); 130 } 131 } 132