Merge "Update SpellCheckerPreference layout to support divider" into main

This commit is contained in:
Edgar Wang
2023-11-23 07:50:41 +00:00
committed by Android (Google) Code Review
2 changed files with 100 additions and 8 deletions

View File

@@ -25,6 +25,7 @@ import android.view.View;
import android.view.View.OnClickListener;
import android.view.textservice.SpellCheckerInfo;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog.Builder;
import androidx.preference.PreferenceViewHolder;
@@ -41,11 +42,15 @@ import com.android.settings.R;
class SpellCheckerPreference extends CustomListPreference {
private final SpellCheckerInfo[] mScis;
private Intent mIntent;
@VisibleForTesting
Intent mIntent;
public SpellCheckerPreference(final Context context, final SpellCheckerInfo[] scis) {
super(context, null);
mScis = scis;
setLayoutResource(
com.android.settingslib.widget.preference.twotarget.R.layout.preference_two_target);
setWidgetLayoutResource(R.layout.preference_widget_gear);
if (scis == null) {
return;
@@ -108,14 +113,26 @@ class SpellCheckerPreference extends CustomListPreference {
@Override
public void onBindViewHolder(PreferenceViewHolder view) {
super.onBindViewHolder(view);
final View divider = view.findViewById(
com.android.settingslib.widget.preference.twotarget.R.id.two_target_divider);
final View widgetFrame = view.findViewById(android.R.id.widget_frame);
if (divider != null) {
divider.setVisibility(mIntent != null ? View.VISIBLE : View.GONE);
}
if (widgetFrame != null) {
widgetFrame.setVisibility(mIntent != null ? View.VISIBLE : View.GONE);
}
View settingsButton = view.findViewById(R.id.settings_button);
settingsButton.setVisibility(mIntent != null ? View.VISIBLE : View.INVISIBLE);
settingsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onSettingsButtonClicked();
}
});
if (settingsButton != null) {
settingsButton.setVisibility(mIntent != null ? View.VISIBLE : View.INVISIBLE);
settingsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onSettingsButtonClicked();
}
});
}
}
private void onSettingsButtonClicked() {

View File

@@ -0,0 +1,75 @@
/*
* Copyright (C) 2023 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.Mockito.spy;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.textservice.SpellCheckerInfo;
import androidx.preference.PreferenceViewHolder;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class SpellCheckerPreferenceTest {
private Context mContext;
private PreferenceViewHolder mViewHolder;
private View mDivider;
private SpellCheckerPreference mPreference;
private final SpellCheckerInfo[] mScis = new SpellCheckerInfo[]{};
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mPreference = new SpellCheckerPreference(mContext, mScis);
}
@Test
public void onBindViewHolder_withIntent_DividerIsVisible() {
final View view = spy(View.inflate(mContext, mPreference.getLayoutResource(), null));
mViewHolder = PreferenceViewHolder.createInstanceForTests(view);
mDivider = view.findViewById(
com.android.settingslib.widget.preference.twotarget.R.id.two_target_divider);
mPreference.mIntent = new Intent(Intent.ACTION_MAIN);
mPreference.onBindViewHolder(mViewHolder);
assertThat(mDivider.getVisibility()).isEqualTo(View.VISIBLE);
}
@Test
public void onBindViewHolder_withoutIntent_DividerIsNotExist() {
final View view = spy(View.inflate(mContext, mPreference.getLayoutResource(), null));
mViewHolder = PreferenceViewHolder.createInstanceForTests(view);
mDivider = view.findViewById(
com.android.settingslib.widget.preference.twotarget.R.id.two_target_divider);
mPreference.onBindViewHolder(mViewHolder);
assertThat(mDivider.getVisibility()).isEqualTo(View.GONE);
}
}