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 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.hardware.input.InputManager; 26 import android.support.v7.preference.Preference; 27 import android.view.InputDevice; 28 29 import com.android.settings.R; 30 import com.android.settings.testutils.SettingsRobolectricTestRunner; 31 import com.android.settings.TestConfig; 32 import com.android.settings.testutils.shadow.ShadowInputDevice; 33 34 import org.junit.After; 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.annotation.Config; 41 42 @RunWith(SettingsRobolectricTestRunner.class) 43 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 44 public class PhysicalKeyboardPreferenceControllerTest { 45 46 @Mock 47 private Context mContext; 48 @Mock 49 private InputManager mIm; 50 @Mock 51 private Preference mPreference; 52 53 private PhysicalKeyboardPreferenceController mController; 54 55 @Before setUp()56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 when(mContext.getSystemService(Context.INPUT_SERVICE)).thenReturn(mIm); 59 mController = new PhysicalKeyboardPreferenceController(mContext, null /* lifecycle */); 60 } 61 62 @After tearDown()63 public void tearDown() { 64 ShadowInputDevice.reset(); 65 } 66 67 @Test shouldAlwaysBeAvailable()68 public void shouldAlwaysBeAvailable() { 69 assertThat(mController.isAvailable()).isTrue(); 70 } 71 72 @Test 73 @Config(shadows = { 74 ShadowInputDevice.class, 75 }) updateState_noKeyboard_setDisconnectedSummary()76 public void updateState_noKeyboard_setDisconnectedSummary() { 77 ShadowInputDevice.sDeviceIds = new int[]{}; 78 mController.updateState(mPreference); 79 80 verify(mPreference).setSummary(R.string.disconnected); 81 } 82 83 @Test 84 @Config(shadows = ShadowInputDevice.class) updateState_hasKeyboard_setSummaryToKeyboardName()85 public void updateState_hasKeyboard_setSummaryToKeyboardName() { 86 final InputDevice device = mock(InputDevice.class); 87 when(device.isVirtual()).thenReturn(false); 88 when(device.isFullKeyboard()).thenReturn(true); 89 when(device.getName()).thenReturn("test_keyboard"); 90 ShadowInputDevice.sDeviceIds = new int[]{0}; 91 ShadowInputDevice.addDevice(0, device); 92 93 mController.updateState(mPreference); 94 95 verify(mPreference).setSummary(device.getName()); 96 } 97 98 } 99