1 /*
2  * Copyright (C) 2017 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.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.hardware.input.InputDeviceIdentifier;
28 import android.hardware.input.InputManager;
29 import android.view.InputDevice;
30 
31 import androidx.preference.Preference;
32 
33 import com.android.settings.inputmethod.PhysicalKeyboardFragment.HardKeyboardDeviceInfo;
34 import com.android.settings.testutils.shadow.ShadowInputDevice;
35 
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.RuntimeEnvironment;
44 import org.robolectric.annotation.Config;
45 
46 import java.util.ArrayList;
47 import java.util.List;
48 
49 @RunWith(RobolectricTestRunner.class)
50 public class PhysicalKeyboardPreferenceControllerTest {
51 
52     private static final String DEVICE_NAME = "deviceName";
53     private static final String LAYOUT_LABEL = "deviceLayutLabel";
54     private static final String BLUETOOTHADDRESS = "deviceBluetoothAddress";
55     private static final int VENDOR_ID = 123;
56     private static final int PRODUCT_ID = 456;
57 
58     @Mock
59     private Context mContext;
60     @Mock
61     private InputManager mIm;
62     @Mock
63     private Preference mPreference;
64     @Mock
65     private InputDeviceIdentifier mIdentifier;
66 
67     private PhysicalKeyboardPreferenceController mController;
68 
69     @Before
setUp()70     public void setUp() {
71         MockitoAnnotations.initMocks(this);
72         when(mContext.getSystemService(InputManager.class)).thenReturn(mIm);
73         mController = new PhysicalKeyboardPreferenceController(mContext, null /* lifecycle */);
74     }
75 
76     @After
tearDown()77     public void tearDown() {
78         ShadowInputDevice.reset();
79     }
80 
81     @Test
testPhysicalKeyboard_byDefault_shouldBeShown()82     public void testPhysicalKeyboard_byDefault_shouldBeShown() {
83         final Context context = spy(RuntimeEnvironment.application.getApplicationContext());
84         List<HardKeyboardDeviceInfo> keyboards = new ArrayList<>();
85         keyboards.add(new HardKeyboardDeviceInfo(
86                 DEVICE_NAME,
87                 mIdentifier,
88                 LAYOUT_LABEL,
89                 BLUETOOTHADDRESS,
90                 VENDOR_ID,
91                 PRODUCT_ID));
92         mController = spy(new PhysicalKeyboardPreferenceController(context, null));
93         when(mController.getKeyboards()).thenReturn(keyboards);
94 
95         boolean result = mController.isAvailable();
96 
97         assertThat(result).isTrue();
98     }
99 
100     @Test
101     @Config(qualifiers = "mcc999")
testPhysicalKeyboard_ifDisabled_shouldNotBeShown()102     public void testPhysicalKeyboard_ifDisabled_shouldNotBeShown() {
103         final Context context = spy(RuntimeEnvironment.application.getApplicationContext());
104         mController = new PhysicalKeyboardPreferenceController(context, null);
105 
106         assertThat(mController.isAvailable()).isFalse();
107     }
108 
109     @Test
110     @Config(shadows = ShadowInputDevice.class)
updateState_noKeyboard_setPreferenceVisibleFalse()111     public void updateState_noKeyboard_setPreferenceVisibleFalse() {
112         ShadowInputDevice.sDeviceIds = new int[0];
113         mController.updateState(mPreference);
114 
115         verify(mPreference).setVisible(false);
116     }
117 
118     @Test
119     @Config(shadows = ShadowInputDevice.class)
updateState_hasKeyboard_setSummaryToKeyboardName()120     public void updateState_hasKeyboard_setSummaryToKeyboardName() {
121         final InputDevice device = mock(InputDevice.class);
122         when(device.isVirtual()).thenReturn(false);
123         when(device.isFullKeyboard()).thenReturn(true);
124         when(device.getName()).thenReturn("test_keyboard");
125         ShadowInputDevice.sDeviceIds = new int[]{0};
126         ShadowInputDevice.addDevice(0, device);
127 
128         mController.updateState(mPreference);
129 
130         verify(mPreference).setSummary(device.getName());
131     }
132 }
133