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 48 import java.util.ArrayList; 49 import java.util.List; 50 51 /** Tests for {@link KeyboardSettingsPreferenceController} */ 52 @RunWith(RobolectricTestRunner.class) 53 public class KeyboardSettingsPreferenceControllerTest { 54 55 @Rule 56 public MockitoRule mMockitoRule = MockitoJUnit.rule(); 57 58 private static final String PREFERENCE_KEY = "keyboard_settings"; 59 60 @Mock 61 private CachedBluetoothDevice mCachedBluetoothDevice; 62 @Mock 63 private InputDeviceIdentifier mInputDeviceIdentifier; 64 65 private Context mContext; 66 private KeyboardSettingsPreferenceController mController; 67 68 @Before setUp()69 public void setUp() { 70 mContext = spy(ApplicationProvider.getApplicationContext()); 71 doNothing().when(mContext).startActivity(any()); 72 mController = spy(new KeyboardSettingsPreferenceController(mContext, PREFERENCE_KEY)); 73 mController.init(mCachedBluetoothDevice); 74 } 75 76 @Test handlePreferenceTreeClick_expected()77 public void handlePreferenceTreeClick_expected() { 78 Preference mKeyboardPreference = new Preference(mContext); 79 mKeyboardPreference.setKey(PREFERENCE_KEY); 80 final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); 81 String address = "BT_ADDRESS"; 82 HardKeyboardDeviceInfo deviceInfo = 83 new HardKeyboardDeviceInfo( 84 "TEST_DEVICE", 85 mInputDeviceIdentifier, 86 "TEST_DEVICE_LABEL", 87 address); 88 List<HardKeyboardDeviceInfo> keyboards = new ArrayList<>(); 89 keyboards.add(deviceInfo); 90 when(mController.getHardKeyboardList()).thenReturn(keyboards); 91 when(mCachedBluetoothDevice.getAddress()).thenReturn(address); 92 93 mController.handlePreferenceTreeClick(mKeyboardPreference); 94 95 verify(mContext).startActivity(captor.capture()); 96 assertThat(captor.getValue().getAction()).isEqualTo(Settings.ACTION_HARD_KEYBOARD_SETTINGS); 97 } 98 99 @Test handlePreferenceTreeClick_notExpected()100 public void handlePreferenceTreeClick_notExpected() { 101 Preference mOtherPreference = new Preference(mContext); 102 mOtherPreference.setKey("not_keyboard_settings"); 103 104 assertThat(mController.handlePreferenceTreeClick(mOtherPreference)).isFalse(); 105 } 106 } 107