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 static com.android.settings.core.BasePreferenceController.AVAILABLE; 20 import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE; 21 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; 22 23 import static com.google.common.truth.Truth.assertThat; 24 25 import static org.mockito.Mockito.never; 26 import static org.mockito.Mockito.spy; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.when; 29 30 import android.content.Context; 31 import android.hardware.input.InputManager; 32 import android.view.InputDevice; 33 34 import org.junit.Before; 35 import org.junit.Ignore; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Answers; 39 import org.mockito.Mock; 40 import org.mockito.MockitoAnnotations; 41 import org.robolectric.RobolectricTestRunner; 42 import org.robolectric.RuntimeEnvironment; 43 import org.robolectric.annotation.Config; 44 45 @RunWith(RobolectricTestRunner.class) 46 public class GameControllerPreferenceControllerTest { 47 48 @Mock 49 private InputManager mInputManager; 50 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 51 private InputDevice mInputDevice; 52 53 private Context mContext; 54 private GameControllerPreferenceController mController; 55 56 @Before setUp()57 public void setUp() { 58 MockitoAnnotations.initMocks(this); 59 mContext = spy(RuntimeEnvironment.application); 60 when(mContext.getSystemService(Context.INPUT_SERVICE)).thenReturn(mInputManager); 61 mController = new GameControllerPreferenceController(mContext, "test_key"); 62 } 63 64 @Test testLifecycle_shouldRegisterInputManager()65 public void testLifecycle_shouldRegisterInputManager() { 66 mController.onResume(); 67 68 // register is called, but unregister should not be called. 69 verify(mInputManager).registerInputDeviceListener(mController, null); 70 verify(mInputManager, never()).unregisterInputDeviceListener(mController); 71 72 mController.onPause(); 73 // register is not called any more times, but unregister should be called once. 74 verify(mInputManager).registerInputDeviceListener(mController, null); 75 verify(mInputManager).unregisterInputDeviceListener(mController); 76 } 77 78 @Test getAvailabilityStatus_hasDeviceWithVibrator_shouldReturnAvailable()79 public void getAvailabilityStatus_hasDeviceWithVibrator_shouldReturnAvailable() { 80 when(mInputManager.getInputDeviceIds()).thenReturn(new int[] {1}); 81 when(mInputManager.getInputDevice(1)).thenReturn(mInputDevice); 82 when(mInputDevice.isVirtual()).thenReturn(false); 83 when(mInputDevice.getVibrator().hasVibrator()).thenReturn(true); 84 85 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 86 } 87 88 @Test getAvailabilityStatus_hasNoVibratingDevice_shouldReturnDisabled()89 public void getAvailabilityStatus_hasNoVibratingDevice_shouldReturnDisabled() { 90 when(mInputManager.getInputDeviceIds()).thenReturn(new int[] {1}); 91 when(mInputManager.getInputDevice(1)).thenReturn(mInputDevice); 92 when(mInputDevice.isVirtual()).thenReturn(false); 93 when(mInputDevice.getVibrator().hasVibrator()).thenReturn(false); 94 95 assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); 96 } 97 98 @Test getAvailabilityStatus_hasNoPhysicalDevice_shouldReturnDisabled()99 public void getAvailabilityStatus_hasNoPhysicalDevice_shouldReturnDisabled() { 100 when(mInputManager.getInputDeviceIds()).thenReturn(new int[] {1}); 101 when(mInputManager.getInputDevice(1)).thenReturn(mInputDevice); 102 when(mInputDevice.isVirtual()).thenReturn(true); 103 104 assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); 105 } 106 107 @Test getAvailabilityStatus_hasNoDevice_shouldReturnDisabled()108 public void getAvailabilityStatus_hasNoDevice_shouldReturnDisabled() { 109 when(mInputManager.getInputDeviceIds()).thenReturn(new int[] {}); 110 111 assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); 112 } 113 114 @Ignore 115 @Test 116 @Config(qualifiers = "mcc999") getAvailabilityStatus_ifDisabled_shouldReturnDisabled()117 public void getAvailabilityStatus_ifDisabled_shouldReturnDisabled() { 118 mController = new GameControllerPreferenceController(mContext, "testkey"); 119 120 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 121 } 122 123 @Test setChecked_toEnabled_shouldSetToSettingsProvider()124 public void setChecked_toEnabled_shouldSetToSettingsProvider() { 125 mController.setChecked(true); 126 assertThat(mController.isChecked()).isTrue(); 127 } 128 129 @Test setChecked_toDisabled_shouldSetToSettingsProvider()130 public void setChecked_toDisabled_shouldSetToSettingsProvider() { 131 mController.setChecked(true); 132 mController.setChecked(false); 133 assertThat(mController.isChecked()).isFalse(); 134 } 135 } 136