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.spy; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.hardware.input.InputManager; 27 import android.support.v7.preference.Preference; 28 import android.view.InputDevice; 29 30 import com.android.settings.R; 31 import com.android.settings.testutils.SettingsRobolectricTestRunner; 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.RuntimeEnvironment; 41 import org.robolectric.annotation.Config; 42 43 @RunWith(SettingsRobolectricTestRunner.class) 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 56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 when(mContext.getSystemService(InputManager.class)).thenReturn(mIm); 59 mController = new PhysicalKeyboardPreferenceController(mContext, null /* lifecycle */); 60 } 61 62 @After 63 public void tearDown() { 64 ShadowInputDevice.reset(); 65 } 66 67 @Test 68 public void testPhysicalKeyboard_byDefault_shouldBeShown() { 69 final Context context = spy(RuntimeEnvironment.application.getApplicationContext()); 70 mController = new PhysicalKeyboardPreferenceController(context, null); 71 72 assertThat(mController.isAvailable()).isTrue(); 73 } 74 75 @Test 76 @Config(qualifiers = "mcc999") 77 public void testPhysicalKeyboard_ifDisabled_shouldNotBeShown() { 78 final Context context = spy(RuntimeEnvironment.application.getApplicationContext()); 79 mController = new PhysicalKeyboardPreferenceController(context, null); 80 81 assertThat(mController.isAvailable()).isFalse(); 82 } 83 84 @Test 85 @Config(shadows = ShadowInputDevice.class) 86 public void updateState_noKeyboard_setDisconnectedSummary() { 87 ShadowInputDevice.sDeviceIds = new int[0]; 88 mController.updateState(mPreference); 89 90 verify(mPreference).setSummary(R.string.disconnected); 91 } 92 93 @Test 94 @Config(shadows = ShadowInputDevice.class) 95 public void updateState_hasKeyboard_setSummaryToKeyboardName() { 96 final InputDevice device = mock(InputDevice.class); 97 when(device.isVirtual()).thenReturn(false); 98 when(device.isFullKeyboard()).thenReturn(true); 99 when(device.getName()).thenReturn("test_keyboard"); 100 ShadowInputDevice.sDeviceIds = new int[]{0}; 101 ShadowInputDevice.addDevice(0, device); 102 103 mController.updateState(mPreference); 104 105 verify(mPreference).setSummary(device.getName()); 106 } 107 } 108