diff --git a/src/com/android/settings/tts/TtsEnginePreference.java b/src/com/android/settings/tts/TtsEnginePreference.java index 6ea414dd4b7..8f15db266e2 100644 --- a/src/com/android/settings/tts/TtsEnginePreference.java +++ b/src/com/android/settings/tts/TtsEnginePreference.java @@ -30,6 +30,8 @@ import androidx.preference.PreferenceViewHolder; import com.android.settings.R; +import androidx.annotation.VisibleForTesting; + public class TtsEnginePreference extends Preference { @@ -45,6 +47,7 @@ public class TtsEnginePreference extends Preference { * The shared radio button state, which button is checked etc. */ private final RadioButtonGroupState mSharedState; + private RadioButton mRadioButton; /** * When true, the change callbacks on the radio button will not @@ -95,6 +98,12 @@ public class TtsEnginePreference extends Preference { mPreventRadioButtonCallbacks = true; rb.setChecked(isChecked); mPreventRadioButtonCallbacks = false; + mRadioButton = rb; + } + + @Override + public void onClick() { + mRadioButton.setChecked(true); } private boolean shouldDisplayDataAlert() { diff --git a/tests/robotests/src/com/android/settings/tts/TtsEnginePreferenceTest.java b/tests/robotests/src/com/android/settings/tts/TtsEnginePreferenceTest.java new file mode 100644 index 00000000000..5cd948824a9 --- /dev/null +++ b/tests/robotests/src/com/android/settings/tts/TtsEnginePreferenceTest.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2019 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.tts; + +import static com.google.common.truth.Truth.assertThat; + +import android.content.Context; +import android.speech.tts.TextToSpeech; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Checkable; + +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 TtsEnginePreferenceTest { + + private static final String KEY = "test_key"; + + private TtsEnginePreference mPreference; + private Context mContext; + private PreferenceViewHolder mViewHolder; + private View mRootView; + private FakeRadioButtonGroupState mState; + + @Before + public void setUp() { + mContext = RuntimeEnvironment.application; + + final TextToSpeech.EngineInfo info = new TextToSpeech.EngineInfo(); + info.system = true; + mState = new FakeRadioButtonGroupState(); + mPreference = new TtsEnginePreference(mContext, info, mState); + mPreference.setKey(KEY); + + // Create preference view holder + final LayoutInflater inflater = LayoutInflater.from(mContext); + mRootView = View.inflate(mContext, mPreference.getLayoutResource(), null /* parent */); + final ViewGroup widgetFrame = mRootView.findViewById(android.R.id.widget_frame); + inflater.inflate(mPreference.getWidgetLayoutResource(), widgetFrame); + mViewHolder = PreferenceViewHolder.createInstanceForTests(mRootView); + } + + @Test + public void onClick_shouldInvokeOnCheckedChangeListener() { + mPreference.onBindViewHolder(mViewHolder); + + mPreference.onClick(); + + assertThat(mState.getCurrentKey()).isEqualTo(mPreference.getKey()); + } + + public static class FakeRadioButtonGroupState implements + TtsEnginePreference.RadioButtonGroupState { + + private String mKey; + + @Override + public Checkable getCurrentChecked() { + return null; + } + + @Override + public String getCurrentKey() { + return mKey; + } + + @Override + public void setCurrentChecked(Checkable current) { + } + + @Override + public void setCurrentKey(String key) { + mKey = key; + } + } +}