Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 523d3926 authored by Edgar Wang's avatar Edgar Wang
Browse files

Update SpellCheckerPreference layout to support divider

Bug: 246499664
Test: robotest and visual
Change-Id: I96908f4b566820b20a57e0161d74dfed2c566ea5
parent 6bb2c735
Loading
Loading
Loading
Loading
+25 −8
Original line number Diff line number Diff line
@@ -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,7 +113,18 @@ 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);
        if (settingsButton != null) {
            settingsButton.setVisibility(mIntent != null ? View.VISIBLE : View.INVISIBLE);
            settingsButton.setOnClickListener(new OnClickListener() {
                @Override
@@ -117,6 +133,7 @@ class SpellCheckerPreference extends CustomListPreference {
                }
            });
        }
    }

    private void onSettingsButtonClicked() {
        final Context context = getContext();
+75 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}