• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
56     @Mock
57     private Context mContext;
58     @Mock
59     private InputManager mIm;
60     @Mock
61     private Preference mPreference;
62     @Mock
63     private InputDeviceIdentifier mIdentifier;
64 
65     private PhysicalKeyboardPreferenceController mController;
66 
67     @Before
setUp()68     public void setUp() {
69         MockitoAnnotations.initMocks(this);
70         when(mContext.getSystemService(InputManager.class)).thenReturn(mIm);
71         mController = new PhysicalKeyboardPreferenceController(mContext, null /* lifecycle */);
72     }
73 
74     @After
tearDown()75     public void tearDown() {
76         ShadowInputDevice.reset();
77     }
78 
79     @Test
testPhysicalKeyboard_byDefault_shouldBeShown()80     public void testPhysicalKeyboard_byDefault_shouldBeShown() {
81         final Context context = spy(RuntimeEnvironment.application.getApplicationContext());
82         List<HardKeyboardDeviceInfo> keyboards = new ArrayList<>();
83         keyboards.add(new HardKeyboardDeviceInfo(
84                 DEVICE_NAME,
85                 mIdentifier,
86                 LAYOUT_LABEL,
87                 BLUETOOTHADDRESS));
88         mController = spy(new PhysicalKeyboardPreferenceController(context, null));
89         when(mController.getKeyboards()).thenReturn(keyboards);
90 
91         boolean result = mController.isAvailable();
92 
93         assertThat(result).isTrue();
94     }
95 
96     @Test
97     @Config(qualifiers = "mcc999")
testPhysicalKeyboard_ifDisabled_shouldNotBeShown()98     public void testPhysicalKeyboard_ifDisabled_shouldNotBeShown() {
99         final Context context = spy(RuntimeEnvironment.application.getApplicationContext());
100         mController = new PhysicalKeyboardPreferenceController(context, null);
101 
102         assertThat(mController.isAvailable()).isFalse();
103     }
104 
105     @Test
106     @Config(shadows = ShadowInputDevice.class)
updateState_noKeyboard_setPreferenceVisibleFalse()107     public void updateState_noKeyboard_setPreferenceVisibleFalse() {
108         ShadowInputDevice.sDeviceIds = new int[0];
109         mController.updateState(mPreference);
110 
111         verify(mPreference).setVisible(false);
112     }
113 
114     @Test
115     @Config(shadows = ShadowInputDevice.class)
updateState_hasKeyboard_setSummaryToKeyboardName()116     public void updateState_hasKeyboard_setSummaryToKeyboardName() {
117         final InputDevice device = mock(InputDevice.class);
118         when(device.isVirtual()).thenReturn(false);
119         when(device.isFullKeyboard()).thenReturn(true);
120         when(device.getName()).thenReturn("test_keyboard");
121         ShadowInputDevice.sDeviceIds = new int[]{0};
122         ShadowInputDevice.addDevice(0, device);
123 
124         mController.updateState(mPreference);
125 
126         verify(mPreference).setSummary(device.getName());
127     }
128 }
129