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