Fix preferences under Related category can not launch its own page in bluetooth device details page

Root Cause: KeyboardSettingsPreferenceController override
handlePreferenceTreeClick() without checking the preference key, it will lead to handle all preferences' click action.

Solution: Check the preference is the expected preference key

Bug: 264017256
Test: make RunSettingsRoboTests ROBOTEST_FILTER=KeyboardSettingsPreferenceControllerTest
Change-Id: Idcdadc323df5b758b4b21329227e2bb721b1c394
This commit is contained in:
jasonwshsu
2022-12-30 19:35:15 +08:00
committed by Jason Hsu
parent 181119f0b5
commit 829a671265
2 changed files with 94 additions and 0 deletions

View File

@@ -50,6 +50,10 @@ public class KeyboardSettingsPreferenceController extends BasePreferenceControll
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (!getPreferenceKey().equals(preference.getKey())) {
return false;
}
final Intent intent = new Intent(Settings.ACTION_HARD_KEYBOARD_SETTINGS);
intent.setClass(mContext, PhysicalKeyboardActivity.class);
intent.putExtra(PhysicalKeyboardFragment.EXTRA_BT_ADDRESS, mCachedDevice.getAddress());

View File

@@ -0,0 +1,90 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.inputmethod;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import androidx.preference.Preference;
import androidx.test.core.app.ApplicationProvider;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
/** Tests for {@link KeyboardSettingsPreferenceController} */
@RunWith(RobolectricTestRunner.class)
public class KeyboardSettingsPreferenceControllerTest {
@Rule
public MockitoRule mMockitoRule = MockitoJUnit.rule();
private static final String PREFERENCE_KEY = "keyboard_settings";
@Mock
private Activity mActivity;
@Mock
private CachedBluetoothDevice mCachedBluetoothDevice;
@Captor
private ArgumentCaptor<Intent> mIntentArgumentCaptor;
private Context mContext;
private KeyboardSettingsPreferenceController mController;
@Before
public void setUp() {
mContext = spy(ApplicationProvider.getApplicationContext());
mController = new KeyboardSettingsPreferenceController(mContext, PREFERENCE_KEY);
mController.init(mCachedBluetoothDevice, mActivity);
}
@Test
public void handlePreferenceTreeClick_expected() {
Preference mKeyboardPreference = new Preference(mContext);
mKeyboardPreference.setKey(PREFERENCE_KEY);
mController.handlePreferenceTreeClick(mKeyboardPreference);
verify(mActivity).startActivityForResult(mIntentArgumentCaptor.capture(), eq(0));
Intent expectedIntent = mIntentArgumentCaptor.getValue();
assertThat(expectedIntent.getAction()).isEqualTo(Settings.ACTION_HARD_KEYBOARD_SETTINGS);
}
@Test
public void handlePreferenceTreeClick_notExpected() {
Preference mOtherPreference = new Preference(mContext);
mOtherPreference.setKey("not_keyboard_settings");
assertThat(mController.handlePreferenceTreeClick(mOtherPreference)).isFalse();
}
}