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.SettingsRobolectricTestRunner; 26 import com.android.settings.TestConfig; 27 import com.android.settings.testutils.shadow.ShadowSecureSettings; 28 29 import org.junit.After; 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Answers; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.annotation.Config; 37 import org.robolectric.shadows.ShadowApplication; 38 39 import java.util.ArrayList; 40 import java.util.List; 41 42 import static android.provider.Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED; 43 import static com.google.common.truth.Truth.assertThat; 44 import static org.mockito.Matchers.anyInt; 45 import static org.mockito.Mockito.doReturn; 46 import static org.mockito.Mockito.mock; 47 import static org.mockito.Mockito.spy; 48 import static org.mockito.Mockito.when; 49 50 @RunWith(SettingsRobolectricTestRunner.class) 51 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 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, null, KEY_DOUBLE_TWIST); 66 } 67 68 @After tearDown()69 public void tearDown() { 70 ShadowSecureSettings.clear(); 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 106 @Config(shadows = {ShadowSecureSettings.class}) onPreferenceChange_hasWorkProfile_shouldUpdateSettingForWorkProfileUser()107 public void onPreferenceChange_hasWorkProfile_shouldUpdateSettingForWorkProfileUser() { 108 final int managedId = 2; 109 ShadowSecureSettings.putIntForUser( 110 null, CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 0, managedId); 111 DoubleTwistPreferenceController controller = 112 spy(new DoubleTwistPreferenceController(mContext, null, KEY_DOUBLE_TWIST)); 113 doReturn(managedId).when(controller).getManagedProfileUserId(); 114 115 // enable the gesture 116 controller.onPreferenceChange(null, true); 117 assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(), 118 CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 0, managedId)).isEqualTo(1); 119 120 // disable the gesture 121 controller.onPreferenceChange(null, false); 122 assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(), 123 CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 1, managedId)).isEqualTo(0); 124 } 125 126 @Test testSwitchEnabled_configIsSet_shouldReturnTrue()127 public void testSwitchEnabled_configIsSet_shouldReturnTrue() { 128 // Set the setting to be enabled. 129 final Context context = ShadowApplication.getInstance().getApplicationContext(); 130 Settings.System.putInt(context.getContentResolver(), 131 CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 1); 132 mController = new DoubleTwistPreferenceController(context, null, KEY_DOUBLE_TWIST); 133 134 assertThat(mController.isSwitchPrefEnabled()).isTrue(); 135 } 136 137 @Test testSwitchEnabled_configIsNotSet_shouldReturnFalse()138 public void testSwitchEnabled_configIsNotSet_shouldReturnFalse() { 139 // Set the setting to be disabled. 140 final Context context = ShadowApplication.getInstance().getApplicationContext(); 141 Settings.System.putInt(context.getContentResolver(), 142 CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 0); 143 mController = new DoubleTwistPreferenceController(context, null, KEY_DOUBLE_TWIST); 144 145 assertThat(mController.isSwitchPrefEnabled()).isFalse(); 146 } 147 } 148