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