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