[speech] fix settings robo tests
Bug: 300161951 Test: atest SettingsRoboTests:com.android.settings.tts Change-Id: I31a97eb9dae4fb65c52af3075ad4aa82a7165d49
This commit is contained in:
@@ -165,6 +165,9 @@ public class TtsEnginePreferenceFragment extends RadioButtonPickerFragment {
|
|||||||
private void updateDefaultEngine(String engine) {
|
private void updateDefaultEngine(String engine) {
|
||||||
Log.d(TAG, "Updating default synth to : " + engine);
|
Log.d(TAG, "Updating default synth to : " + engine);
|
||||||
|
|
||||||
|
// Step 1: Shut down the existing TTS engine.
|
||||||
|
Log.i(TAG, "Shutting down current tts engine");
|
||||||
|
if (mTts != null) {
|
||||||
// Keep track of the previous engine that was being used. So that
|
// Keep track of the previous engine that was being used. So that
|
||||||
// we can reuse the previous engine.
|
// we can reuse the previous engine.
|
||||||
//
|
//
|
||||||
@@ -172,9 +175,6 @@ public class TtsEnginePreferenceFragment extends RadioButtonPickerFragment {
|
|||||||
// the very least that we successfully bound to the engine service.
|
// the very least that we successfully bound to the engine service.
|
||||||
mPreviousEngine = mTts.getCurrentEngine();
|
mPreviousEngine = mTts.getCurrentEngine();
|
||||||
|
|
||||||
// Step 1: Shut down the existing TTS engine.
|
|
||||||
Log.i(TAG, "Shutting down current tts engine");
|
|
||||||
if (mTts != null) {
|
|
||||||
try {
|
try {
|
||||||
mTts.shutdown();
|
mTts.shutdown();
|
||||||
mTts = null;
|
mTts = null;
|
||||||
|
@@ -24,6 +24,7 @@ import static org.mockito.Mockito.mock;
|
|||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.robolectric.Shadows.shadowOf;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@@ -31,6 +32,7 @@ import android.content.pm.PackageManager;
|
|||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
import android.content.pm.ServiceInfo;
|
import android.content.pm.ServiceInfo;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Looper;
|
||||||
import android.speech.tts.TextToSpeech;
|
import android.speech.tts.TextToSpeech;
|
||||||
import android.speech.tts.TtsEngines;
|
import android.speech.tts.TtsEngines;
|
||||||
|
|
||||||
@@ -43,6 +45,7 @@ import org.junit.runner.RunWith;
|
|||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.annotation.Implements;
|
||||||
import org.robolectric.shadow.api.Shadow;
|
import org.robolectric.shadow.api.Shadow;
|
||||||
import org.robolectric.shadows.ShadowPackageManager;
|
import org.robolectric.shadows.ShadowPackageManager;
|
||||||
import org.robolectric.shadows.androidx.fragment.FragmentController;
|
import org.robolectric.shadows.androidx.fragment.FragmentController;
|
||||||
@@ -50,12 +53,15 @@ import org.robolectric.shadows.androidx.fragment.FragmentController;
|
|||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class TtsEnginePreferenceFragmentTest {
|
public class TtsEnginePreferenceFragmentTest {
|
||||||
|
|
||||||
|
@Implements(TextToSpeech.class)
|
||||||
|
public static class NoOpShadowTextToSpeech {}
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private TtsEnginePreferenceFragment mTtsEnginePreferenceFragment;
|
private TtsEnginePreferenceFragment mTtsEnginePreferenceFragment;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
mContext = RuntimeEnvironment.application;
|
mContext = RuntimeEnvironment.systemContext;
|
||||||
|
|
||||||
final ResolveInfo info = new ResolveInfo();
|
final ResolveInfo info = new ResolveInfo();
|
||||||
final ServiceInfo serviceInfo = spy(new ServiceInfo());
|
final ServiceInfo serviceInfo = spy(new ServiceInfo());
|
||||||
@@ -76,17 +82,19 @@ public class TtsEnginePreferenceFragmentTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Config(shadows = {NoOpShadowTextToSpeech.class})
|
||||||
public void getCandidates_AddEngines_returnCorrectEngines() {
|
public void getCandidates_AddEngines_returnCorrectEngines() {
|
||||||
mTtsEnginePreferenceFragment = FragmentController.of(new TtsEnginePreferenceFragment(),
|
mTtsEnginePreferenceFragment = FragmentController.of(new TtsEnginePreferenceFragment(),
|
||||||
new Bundle())
|
new Bundle())
|
||||||
.create()
|
.create()
|
||||||
.get();
|
.get();
|
||||||
|
shadowOf(Looper.getMainLooper()).idle();
|
||||||
|
|
||||||
assertThat(mTtsEnginePreferenceFragment.getCandidates().size()).isEqualTo(1);
|
assertThat(mTtsEnginePreferenceFragment.getCandidates().size()).isEqualTo(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Config(shadows = {ShadowTtsEngines.class})
|
@Config(shadows = {ShadowTtsEngines.class, NoOpShadowTextToSpeech.class})
|
||||||
public void getDefaultKey_validKey_returnCorrectKey() {
|
public void getDefaultKey_validKey_returnCorrectKey() {
|
||||||
final String TEST_ENGINE = "test_engine";
|
final String TEST_ENGINE = "test_engine";
|
||||||
final TtsEngines engine = mock(TtsEngines.class);
|
final TtsEngines engine = mock(TtsEngines.class);
|
||||||
@@ -95,13 +103,14 @@ public class TtsEnginePreferenceFragmentTest {
|
|||||||
new Bundle())
|
new Bundle())
|
||||||
.create()
|
.create()
|
||||||
.get();
|
.get();
|
||||||
|
shadowOf(Looper.getMainLooper()).idle();
|
||||||
when(engine.getDefaultEngine()).thenReturn(TEST_ENGINE);
|
when(engine.getDefaultEngine()).thenReturn(TEST_ENGINE);
|
||||||
|
|
||||||
assertThat(mTtsEnginePreferenceFragment.getDefaultKey()).isEqualTo(TEST_ENGINE);
|
assertThat(mTtsEnginePreferenceFragment.getDefaultKey()).isEqualTo(TEST_ENGINE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Config(shadows = {ShadowTtsEngines.class})
|
@Config(shadows = {ShadowTtsEngines.class, NoOpShadowTextToSpeech.class})
|
||||||
public void setDefaultKey_validKey_callingTtsEngineFunction() {
|
public void setDefaultKey_validKey_callingTtsEngineFunction() {
|
||||||
final TtsEngines engine = mock(TtsEngines.class);
|
final TtsEngines engine = mock(TtsEngines.class);
|
||||||
ShadowTtsEngines.setInstance(engine);
|
ShadowTtsEngines.setInstance(engine);
|
||||||
@@ -109,6 +118,7 @@ public class TtsEnginePreferenceFragmentTest {
|
|||||||
new Bundle())
|
new Bundle())
|
||||||
.create()
|
.create()
|
||||||
.get();
|
.get();
|
||||||
|
shadowOf(Looper.getMainLooper()).idle();
|
||||||
|
|
||||||
mTtsEnginePreferenceFragment.setDefaultKey(mContext.getPackageName());
|
mTtsEnginePreferenceFragment.setDefaultKey(mContext.getPackageName());
|
||||||
|
|
||||||
@@ -116,14 +126,17 @@ public class TtsEnginePreferenceFragmentTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Config(shadows = {NoOpShadowTextToSpeech.class})
|
||||||
public void setDefaultKey_validKey_updateCheckedState() {
|
public void setDefaultKey_validKey_updateCheckedState() {
|
||||||
mTtsEnginePreferenceFragment = spy(FragmentController.of(new TtsEnginePreferenceFragment(),
|
mTtsEnginePreferenceFragment = FragmentController.of(new TtsEnginePreferenceFragment(),
|
||||||
new Bundle())
|
new Bundle())
|
||||||
.create()
|
.create()
|
||||||
.get());
|
.get();
|
||||||
|
shadowOf(Looper.getMainLooper()).idle();
|
||||||
|
TtsEnginePreferenceFragment fragmentSpy = spy(mTtsEnginePreferenceFragment);
|
||||||
|
|
||||||
mTtsEnginePreferenceFragment.setDefaultKey(mContext.getPackageName());
|
fragmentSpy.setDefaultKey(mContext.getPackageName());
|
||||||
|
|
||||||
verify(mTtsEnginePreferenceFragment).updateCheckedState(mContext.getPackageName());
|
verify(fragmentSpy).updateCheckedState(mContext.getPackageName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user