Fix cannot switch between multiple TTS engines
- We didn't reset reset radio button state when user click back while dialog was popped. Change the UI, when user really accepts the dialog content, then changes radio button state. - Migrate TtsEnginePreferenceFragment to RadioButtonPickerFragment Fixes: 135588938 Test: manual Change-Id: Ia824e08d59c77a23e6590ff0f5b7d897a73a1ff8
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* 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 static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.pm.ServiceInfo;
|
||||
import android.os.Bundle;
|
||||
import android.speech.tts.TextToSpeech;
|
||||
import android.speech.tts.TtsEngines;
|
||||
|
||||
import com.android.settings.testutils.shadow.ShadowTtsEngines;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadow.api.Shadow;
|
||||
import org.robolectric.shadows.ShadowPackageManager;
|
||||
import org.robolectric.shadows.androidx.fragment.FragmentController;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class TtsEnginePreferenceFragmentTest {
|
||||
|
||||
private Context mContext;
|
||||
private TtsEnginePreferenceFragment mTtsEnginePreferenceFragment;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
|
||||
final ResolveInfo info = new ResolveInfo();
|
||||
final ServiceInfo serviceInfo = spy(new ServiceInfo());
|
||||
serviceInfo.packageName = mContext.getPackageName();
|
||||
serviceInfo.name = mContext.getClass().getName();
|
||||
info.serviceInfo = serviceInfo;
|
||||
doReturn("title").when(serviceInfo).loadLabel(any(PackageManager.class));
|
||||
doReturn(1).when(serviceInfo).getIconResource();
|
||||
|
||||
final ShadowPackageManager spm = Shadow.extract(mContext.getPackageManager());
|
||||
spm.addResolveInfoForIntent(
|
||||
new Intent(TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE), info);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
ShadowTtsEngines.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCandidates_AddEngines_returnCorrectEngines() {
|
||||
mTtsEnginePreferenceFragment = FragmentController.of(new TtsEnginePreferenceFragment(),
|
||||
new Bundle())
|
||||
.create()
|
||||
.get();
|
||||
|
||||
assertThat(mTtsEnginePreferenceFragment.getCandidates().size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = {ShadowTtsEngines.class})
|
||||
public void getDefaultKey_validKey_returnCorrectKey() {
|
||||
final String TEST_ENGINE = "test_engine";
|
||||
final TtsEngines engine = mock(TtsEngines.class);
|
||||
ShadowTtsEngines.setInstance(engine);
|
||||
mTtsEnginePreferenceFragment = FragmentController.of(new TtsEnginePreferenceFragment(),
|
||||
new Bundle())
|
||||
.create()
|
||||
.get();
|
||||
when(engine.getDefaultEngine()).thenReturn(TEST_ENGINE);
|
||||
|
||||
assertThat(mTtsEnginePreferenceFragment.getDefaultKey()).isEqualTo(TEST_ENGINE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = {ShadowTtsEngines.class})
|
||||
public void setDefaultKey_validKey_callingTtsEngineFunction() {
|
||||
final TtsEngines engine = mock(TtsEngines.class);
|
||||
ShadowTtsEngines.setInstance(engine);
|
||||
mTtsEnginePreferenceFragment = FragmentController.of(new TtsEnginePreferenceFragment(),
|
||||
new Bundle())
|
||||
.create()
|
||||
.get();
|
||||
|
||||
mTtsEnginePreferenceFragment.setDefaultKey(mContext.getPackageName());
|
||||
|
||||
verify(engine).isEngineInstalled(mContext.getPackageName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setDefaultKey_validKey_updateCheckedState() {
|
||||
mTtsEnginePreferenceFragment = spy(FragmentController.of(new TtsEnginePreferenceFragment(),
|
||||
new Bundle())
|
||||
.create()
|
||||
.get());
|
||||
|
||||
mTtsEnginePreferenceFragment.setDefaultKey(mContext.getPackageName());
|
||||
|
||||
verify(mTtsEnginePreferenceFragment).updateCheckedState(mContext.getPackageName());
|
||||
}
|
||||
}
|
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user