• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.hardware.Sensor;
28 import android.hardware.SensorManager;
29 import android.os.UserManager;
30 import android.provider.Settings;
31 
32 import com.android.settings.R;
33 import com.android.settings.testutils.shadow.SettingsShadowResources;
34 import com.android.settings.testutils.shadow.ShadowDoubleTwistPreferenceController;
35 
36 import org.junit.After;
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.RobolectricTestRunner;
44 import org.robolectric.RuntimeEnvironment;
45 import org.robolectric.annotation.Config;
46 
47 import java.util.ArrayList;
48 import java.util.List;
49 
50 @RunWith(RobolectricTestRunner.class)
51 @Config(shadows = SettingsShadowResources.class)
52 public class DoubleTwistPreferenceControllerTest {
53 
54     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
55     private Context mContext;
56     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
57     private SensorManager mSensorManager;
58     private DoubleTwistPreferenceController mController;
59     private static final String KEY_DOUBLE_TWIST = "gesture_double_twist";
60 
61     @Before
setUp()62     public void setUp() {
63         MockitoAnnotations.initMocks(this);
64         when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mock(UserManager.class));
65         mController = new DoubleTwistPreferenceController(mContext, KEY_DOUBLE_TWIST);
66     }
67 
68     @After
tearDown()69     public void tearDown() {
70         SettingsShadowResources.reset();
71     }
72 
73     @Test
isAvailable_hasSensor_shouldReturnTrue()74     public void isAvailable_hasSensor_shouldReturnTrue() {
75         // Mock sensors
76         final List<Sensor> sensorList = new ArrayList<>();
77         sensorList.add(mock(Sensor.class));
78         when(mContext.getResources().getString(anyInt())).thenReturn("test");
79         when(mContext.getSystemService(Context.SENSOR_SERVICE)).thenReturn(mSensorManager);
80         when(mSensorManager.getSensorList(anyInt())).thenReturn(sensorList);
81         when(sensorList.get(0).getName()).thenReturn("test");
82         when(sensorList.get(0).getVendor()).thenReturn("test");
83 
84         assertThat(mController.isAvailable()).isTrue();
85     }
86 
87     @Test
isAvailable_noSensor_shouldReturnFalse()88     public void isAvailable_noSensor_shouldReturnFalse() {
89         assertThat(mController.isAvailable()).isFalse();
90     }
91 
92     @Test
isAvailable_differentSensor_shouldReturnFalse()93     public void isAvailable_differentSensor_shouldReturnFalse() {
94         // Mock sensors
95         final List<Sensor> sensorList = new ArrayList<>();
96         sensorList.add(mock(Sensor.class));
97         when(mContext.getResources().getString(anyInt())).thenReturn("test");
98         when(mContext.getSystemService(Context.SENSOR_SERVICE)).thenReturn(mSensorManager);
99         when(mSensorManager.getSensorList(anyInt())).thenReturn(sensorList);
100         when(sensorList.get(0).getName()).thenReturn("not_test");
101 
102         assertThat(mController.isAvailable()).isFalse();
103     }
104 
105     @Test
isSuggestionCompleted_doubleTwist_trueWhenNotAvailable()106     public void isSuggestionCompleted_doubleTwist_trueWhenNotAvailable() {
107         SettingsShadowResources.overrideResource(
108                 R.string.gesture_double_twist_sensor_name, "nonexistant name");
109         SettingsShadowResources.overrideResource(
110                 R.string.gesture_double_twist_sensor_vendor, "nonexistant vendor");
111 
112         assertThat(DoubleTwistPreferenceController.isSuggestionComplete(
113                 RuntimeEnvironment.application, null /* prefs */))
114                 .isTrue();
115     }
116 
117     @Test
118     @Config(shadows = ShadowDoubleTwistPreferenceController.class)
onPreferenceChange_hasWorkProfile_shouldUpdateSettingForWorkProfileUser()119     public void onPreferenceChange_hasWorkProfile_shouldUpdateSettingForWorkProfileUser() {
120         final int managedId = 2;
121         Settings.Secure.putIntForUser(
122                 null, Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 0, managedId);
123         DoubleTwistPreferenceController controller =
124                 spy(new DoubleTwistPreferenceController(mContext, KEY_DOUBLE_TWIST));
125         ShadowDoubleTwistPreferenceController.setManagedProfileId(managedId);
126 
127         // enable the gesture
128         controller.onPreferenceChange(null, true);
129         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
130                 Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 0, managedId)).isEqualTo(1);
131 
132         // disable the gesture
133         controller.onPreferenceChange(null, false);
134         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
135                 Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 1, managedId)).isEqualTo(0);
136     }
137 
138     @Test
testIsChecked_configIsSet_shouldReturnTrue()139     public void testIsChecked_configIsSet_shouldReturnTrue() {
140         // Set the setting to be enabled.
141         final Context context = RuntimeEnvironment.application;
142         Settings.Secure.putInt(context.getContentResolver(),
143                 Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 1);
144         mController = new DoubleTwistPreferenceController(context, KEY_DOUBLE_TWIST);
145 
146         assertThat(mController.isChecked()).isTrue();
147     }
148 
149     @Test
testIsChecked_configIsNotSet_shouldReturnFalse()150     public void testIsChecked_configIsNotSet_shouldReturnFalse() {
151         // Set the setting to be disabled.
152         final Context context = RuntimeEnvironment.application;
153         Settings.Secure.putInt(context.getContentResolver(),
154                 Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 0);
155         mController = new DoubleTwistPreferenceController(context, KEY_DOUBLE_TWIST);
156 
157         assertThat(mController.isChecked()).isFalse();
158     }
159 
160     @Test
isSliceableCorrectKey_returnsTrue()161     public void isSliceableCorrectKey_returnsTrue() {
162         final DoubleTwistPreferenceController controller =
163         new DoubleTwistPreferenceController(mContext,"gesture_double_twist");
164         assertThat(controller.isSliceable()).isTrue();
165     }
166 
167     @Test
isSliceableIncorrectKey_returnsFalse()168     public void isSliceableIncorrectKey_returnsFalse() {
169         final DoubleTwistPreferenceController controller =
170         new DoubleTwistPreferenceController(mContext, "bad_key");
171         assertThat(controller.isSliceable()).isFalse();
172     }
173 }
174