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 android.content.Context; 20 import android.hardware.Sensor; 21 import android.hardware.SensorManager; 22 import android.os.UserManager; 23 import android.provider.Settings; 24 25 import com.android.settings.testutils.SettingsRobolectricTestRunner; 26 import com.android.settings.TestConfig; 27 import com.android.settings.testutils.shadow.ShadowSecureSettings; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Answers; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.annotation.Config; 36 import org.robolectric.shadows.ShadowApplication; 37 38 import java.util.ArrayList; 39 import java.util.List; 40 41 import static com.google.common.truth.Truth.assertThat; 42 import static org.mockito.Matchers.anyInt; 43 import static org.mockito.Mockito.mock; 44 import static org.mockito.Mockito.spy; 45 import static org.mockito.Mockito.when; 46 47 import com.android.settings.testutils.shadow.ShadowDoubleTwistPreferenceController; 48 49 @RunWith(SettingsRobolectricTestRunner.class) 50 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 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, null, KEY_DOUBLE_TWIST); 65 } 66 67 @Test isAvailable_hasSensor_shouldReturnTrue()68 public void isAvailable_hasSensor_shouldReturnTrue() { 69 // Mock sensors 70 final List<Sensor> sensorList = new ArrayList<>(); 71 sensorList.add(mock(Sensor.class)); 72 when(mContext.getResources().getString(anyInt())).thenReturn("test"); 73 when(mContext.getSystemService(Context.SENSOR_SERVICE)).thenReturn(mSensorManager); 74 when(mSensorManager.getSensorList(anyInt())).thenReturn(sensorList); 75 when(sensorList.get(0).getName()).thenReturn("test"); 76 when(sensorList.get(0).getVendor()).thenReturn("test"); 77 78 assertThat(mController.isAvailable()).isTrue(); 79 } 80 81 @Test isAvailable_noSensor_shouldReturnFalse()82 public void isAvailable_noSensor_shouldReturnFalse() { 83 assertThat(mController.isAvailable()).isFalse(); 84 } 85 86 @Test isAvailable_differentSensor_shouldReturnFalse()87 public void isAvailable_differentSensor_shouldReturnFalse() { 88 // Mock sensors 89 final List<Sensor> sensorList = new ArrayList<>(); 90 sensorList.add(mock(Sensor.class)); 91 when(mContext.getResources().getString(anyInt())).thenReturn("test"); 92 when(mContext.getSystemService(Context.SENSOR_SERVICE)).thenReturn(mSensorManager); 93 when(mSensorManager.getSensorList(anyInt())).thenReturn(sensorList); 94 when(sensorList.get(0).getName()).thenReturn("not_test"); 95 96 assertThat(mController.isAvailable()).isFalse(); 97 } 98 99 @Test 100 @Config(shadows = { 101 ShadowDoubleTwistPreferenceController.class, 102 ShadowSecureSettings.class}) onPreferenceChange_hasWorkProfile_shouldUpdateSettingForWorkProfileUser()103 public void onPreferenceChange_hasWorkProfile_shouldUpdateSettingForWorkProfileUser() { 104 final int managedId = 2; 105 ShadowSecureSettings.putIntForUser( 106 null, Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 0, managedId); 107 DoubleTwistPreferenceController controller = 108 spy(new DoubleTwistPreferenceController(mContext, null, KEY_DOUBLE_TWIST)); 109 ShadowDoubleTwistPreferenceController.setManagedProfileId(managedId); 110 111 // enable the gesture 112 controller.onPreferenceChange(null, true); 113 assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(), 114 Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 0, managedId)).isEqualTo(1); 115 116 // disable the gesture 117 controller.onPreferenceChange(null, false); 118 assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(), 119 Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 1, managedId)).isEqualTo(0); 120 } 121 122 @Test testSwitchEnabled_configIsSet_shouldReturnTrue()123 public void testSwitchEnabled_configIsSet_shouldReturnTrue() { 124 // Set the setting to be enabled. 125 final Context context = ShadowApplication.getInstance().getApplicationContext(); 126 Settings.System.putInt(context.getContentResolver(), 127 Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 1); 128 mController = new DoubleTwistPreferenceController(context, null, KEY_DOUBLE_TWIST); 129 130 assertThat(mController.isSwitchPrefEnabled()).isTrue(); 131 } 132 133 @Test testSwitchEnabled_configIsNotSet_shouldReturnFalse()134 public void testSwitchEnabled_configIsNotSet_shouldReturnFalse() { 135 // Set the setting to be disabled. 136 final Context context = ShadowApplication.getInstance().getApplicationContext(); 137 Settings.System.putInt(context.getContentResolver(), 138 Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 0); 139 mController = new DoubleTwistPreferenceController(context, null, KEY_DOUBLE_TWIST); 140 141 assertThat(mController.isSwitchPrefEnabled()).isFalse(); 142 } 143 } 144