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.inputmethod; 18 19 import android.content.Context; 20 import android.hardware.input.InputManager; 21 import android.view.InputDevice; 22 23 import com.android.settings.SettingsRobolectricTestRunner; 24 import com.android.settings.TestConfig; 25 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.mockito.Answers; 30 import org.mockito.Mock; 31 import org.mockito.MockitoAnnotations; 32 import org.robolectric.annotation.Config; 33 34 import static com.google.common.truth.Truth.assertThat; 35 import static org.mockito.Mockito.never; 36 import static org.mockito.Mockito.verify; 37 import static org.mockito.Mockito.when; 38 39 @RunWith(SettingsRobolectricTestRunner.class) 40 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 41 public class GameControllerPreferenceControllerTest { 42 43 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 44 private Context mContext; 45 @Mock 46 private InputManager mInputManager; 47 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 48 private InputDevice mInputDevice; 49 50 private GameControllerPreferenceController mController; 51 52 @Before setUp()53 public void setUp() { 54 MockitoAnnotations.initMocks(this); 55 when(mContext.getSystemService(Context.INPUT_SERVICE)).thenReturn(mInputManager); 56 mController = new GameControllerPreferenceController(mContext); 57 } 58 59 @Test testLifecycle_shouldRegisterInputManager()60 public void testLifecycle_shouldRegisterInputManager() { 61 mController.onResume(); 62 63 // register is called, but unregister should not be called. 64 verify(mInputManager).registerInputDeviceListener(mController, null); 65 verify(mInputManager, never()).unregisterInputDeviceListener(mController); 66 67 mController.onPause(); 68 // register is not called any more times, but unregister should be called once. 69 verify(mInputManager).registerInputDeviceListener(mController, null); 70 verify(mInputManager).unregisterInputDeviceListener(mController); 71 } 72 73 @Test testIsAvailable_hasDeviceWithVibrator_shouldReturnTrue()74 public void testIsAvailable_hasDeviceWithVibrator_shouldReturnTrue() { 75 when(mInputManager.getInputDeviceIds()).thenReturn(new int[]{1}); 76 when(mInputManager.getInputDevice(1)).thenReturn(mInputDevice); 77 when(mInputDevice.isVirtual()).thenReturn(false); 78 when(mInputDevice.getVibrator().hasVibrator()).thenReturn(true); 79 80 assertThat(mController.isAvailable()).isTrue(); 81 } 82 83 @Test testIsAvailable_hasNoVibratingDevice_shouldReturnFalse()84 public void testIsAvailable_hasNoVibratingDevice_shouldReturnFalse() { 85 when(mInputManager.getInputDeviceIds()).thenReturn(new int[]{1}); 86 when(mInputManager.getInputDevice(1)).thenReturn(mInputDevice); 87 when(mInputDevice.isVirtual()).thenReturn(false); 88 when(mInputDevice.getVibrator().hasVibrator()).thenReturn(false); 89 90 assertThat(mController.isAvailable()).isFalse(); 91 } 92 93 @Test testIsAvailable_hasNoPhysicalDevice_shouldReturnFalse()94 public void testIsAvailable_hasNoPhysicalDevice_shouldReturnFalse() { 95 when(mInputManager.getInputDeviceIds()).thenReturn(new int[]{1}); 96 when(mInputManager.getInputDevice(1)).thenReturn(mInputDevice); 97 when(mInputDevice.isVirtual()).thenReturn(true); 98 99 assertThat(mController.isAvailable()).isFalse(); 100 } 101 102 @Test testIsAvailable_hasNoDevice_shouldReturnFalse()103 public void testIsAvailable_hasNoDevice_shouldReturnFalse() { 104 when(mInputManager.getInputDeviceIds()).thenReturn(new int[]{}); 105 106 assertThat(mController.isAvailable()).isFalse(); 107 } 108 } 109