1 /* 2 * Copyright (C) 2022 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.ArgumentMatchers.any; 22 import static org.mockito.Mockito.doNothing; 23 import static org.mockito.Mockito.spy; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.content.Context; 28 import android.content.Intent; 29 import android.hardware.input.InputDeviceIdentifier; 30 import android.provider.Settings; 31 32 import androidx.preference.Preference; 33 import androidx.test.core.app.ApplicationProvider; 34 35 import com.android.settings.inputmethod.PhysicalKeyboardFragment.HardKeyboardDeviceInfo; 36 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 37 38 import org.junit.Before; 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.ArgumentCaptor; 43 import org.mockito.Mock; 44 import org.mockito.junit.MockitoJUnit; 45 import org.mockito.junit.MockitoRule; 46 import org.robolectric.RobolectricTestRunner; 47 import org.robolectric.annotation.Config; 48 49 import java.util.ArrayList; 50 import java.util.List; 51 52 /** Tests for {@link KeyboardSettingsPreferenceController} */ 53 @RunWith(RobolectricTestRunner.class) 54 @Config(shadows = { 55 com.android.settings.testutils.shadow.ShadowInputManager.class, 56 }) 57 public class KeyboardSettingsPreferenceControllerTest { 58 59 private static final int VENDOR_ID = 123; 60 private static final int PRODUCT_ID = 456; 61 62 @Rule 63 public MockitoRule mMockitoRule = MockitoJUnit.rule(); 64 65 private static final String PREFERENCE_KEY = "keyboard_settings"; 66 67 @Mock 68 private CachedBluetoothDevice mCachedBluetoothDevice; 69 @Mock 70 private InputDeviceIdentifier mInputDeviceIdentifier; 71 72 private Context mContext; 73 private KeyboardSettingsPreferenceController mController; 74 75 @Before setUp()76 public void setUp() { 77 mContext = spy(ApplicationProvider.getApplicationContext()); 78 doNothing().when(mContext).startActivity(any()); 79 mController = spy(new KeyboardSettingsPreferenceController(mContext, PREFERENCE_KEY)); 80 mController.init(mCachedBluetoothDevice); 81 } 82 83 @Test handlePreferenceTreeClick_expected()84 public void handlePreferenceTreeClick_expected() { 85 Preference mKeyboardPreference = new Preference(mContext); 86 mKeyboardPreference.setKey(PREFERENCE_KEY); 87 final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); 88 String address = "BT_ADDRESS"; 89 HardKeyboardDeviceInfo deviceInfo = 90 new HardKeyboardDeviceInfo( 91 "TEST_DEVICE", 92 mInputDeviceIdentifier, 93 "TEST_DEVICE_LABEL", 94 address, 95 VENDOR_ID, 96 PRODUCT_ID); 97 List<HardKeyboardDeviceInfo> keyboards = new ArrayList<>(); 98 keyboards.add(deviceInfo); 99 when(mController.getHardKeyboardList()).thenReturn(keyboards); 100 when(mCachedBluetoothDevice.getAddress()).thenReturn(address); 101 102 mController.handlePreferenceTreeClick(mKeyboardPreference); 103 104 verify(mContext).startActivity(captor.capture()); 105 assertThat(captor.getValue().getAction()).isEqualTo(Settings.ACTION_HARD_KEYBOARD_SETTINGS); 106 } 107 108 @Test handlePreferenceTreeClick_notExpected()109 public void handlePreferenceTreeClick_notExpected() { 110 Preference mOtherPreference = new Preference(mContext); 111 mOtherPreference.setKey("not_keyboard_settings"); 112 113 assertThat(mController.handlePreferenceTreeClick(mOtherPreference)).isFalse(); 114 } 115 } 116