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.InputManager; 28 import android.view.InputDevice; 29 30 import androidx.preference.Preference; 31 32 import com.android.settings.R; 33 import com.android.settings.testutils.shadow.ShadowInputDevice; 34 35 import org.junit.After; 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 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 PhysicalKeyboardPreferenceControllerTest { 47 48 @Mock 49 private Context mContext; 50 @Mock 51 private InputManager mIm; 52 @Mock 53 private Preference mPreference; 54 55 private PhysicalKeyboardPreferenceController mController; 56 57 @Before setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 when(mContext.getSystemService(InputManager.class)).thenReturn(mIm); 61 mController = new PhysicalKeyboardPreferenceController(mContext, null /* lifecycle */); 62 } 63 64 @After tearDown()65 public void tearDown() { 66 ShadowInputDevice.reset(); 67 } 68 69 @Test testPhysicalKeyboard_byDefault_shouldBeShown()70 public void testPhysicalKeyboard_byDefault_shouldBeShown() { 71 final Context context = spy(RuntimeEnvironment.application.getApplicationContext()); 72 mController = new PhysicalKeyboardPreferenceController(context, null); 73 74 assertThat(mController.isAvailable()).isTrue(); 75 } 76 77 @Test 78 @Config(qualifiers = "mcc999") testPhysicalKeyboard_ifDisabled_shouldNotBeShown()79 public void testPhysicalKeyboard_ifDisabled_shouldNotBeShown() { 80 final Context context = spy(RuntimeEnvironment.application.getApplicationContext()); 81 mController = new PhysicalKeyboardPreferenceController(context, null); 82 83 assertThat(mController.isAvailable()).isFalse(); 84 } 85 86 @Test 87 @Config(shadows = ShadowInputDevice.class) updateState_noKeyboard_setDisconnectedSummary()88 public void updateState_noKeyboard_setDisconnectedSummary() { 89 ShadowInputDevice.sDeviceIds = new int[0]; 90 mController.updateState(mPreference); 91 92 verify(mPreference).setSummary(R.string.keyboard_disconnected); 93 } 94 95 @Test 96 @Config(shadows = ShadowInputDevice.class) updateState_hasKeyboard_setSummaryToKeyboardName()97 public void updateState_hasKeyboard_setSummaryToKeyboardName() { 98 final InputDevice device = mock(InputDevice.class); 99 when(device.isVirtual()).thenReturn(false); 100 when(device.isFullKeyboard()).thenReturn(true); 101 when(device.getName()).thenReturn("test_keyboard"); 102 ShadowInputDevice.sDeviceIds = new int[]{0}; 103 ShadowInputDevice.addDevice(0, device); 104 105 mController.updateState(mPreference); 106 107 verify(mPreference).setSummary(device.getName()); 108 } 109 } 110