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