OSDN Git Service

Cannot switch between multiple TTS engines
authorRaff Tsai <rafftsai@google.com>
Wed, 19 Jun 2019 08:21:20 +0000 (16:21 +0800)
committerRaff Tsai <rafftsai@google.com>
Wed, 19 Jun 2019 08:35:35 +0000 (16:35 +0800)
That is caused by layout xml changes. The radio button was clickable
in old xml resource. But it is not clickable in new xml resource.
Therefore we can't receive click callback. Fixed by changing
Radio button state when preference is clicked.

Fixes: 135285101
Test: manual, make RunSettingsRoboTests ROBOTEST_FILTER="com.android.settings.tts"
Change-Id: Idd7bf37d9ccbc1b56d41978d19dc05c8a81cc49a

src/com/android/settings/tts/TtsEnginePreference.java
tests/robotests/src/com/android/settings/tts/TtsEnginePreferenceTest.java [new file with mode: 0644]

index 6ea414d..8f15db2 100644 (file)
@@ -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 (file)
index 0000000..5cd9488
--- /dev/null
@@ -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;
+        }
+    }
+}