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