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