Skip to content

Commit c60aad2

Browse files
committed
Write unit tests for android.speech.tts.
Change-Id: Ie5b3b4cdd13be2babee9a44bae00da179b372d12
1 parent 9a03482 commit c60aad2

File tree

7 files changed

+460
-7
lines changed

7 files changed

+460
-7
lines changed

core/java/android/speech/tts/TextToSpeech.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,11 @@ public class Engine {
486486
private final Object mStartLock = new Object();
487487

488488
private String mRequestedEngine;
489+
// Whether to initialize this TTS object with the default engine,
490+
// if the requested engine is not available. Valid only if mRequestedEngine
491+
// is not null. Used only for testing, though potentially useful API wise
492+
// too.
493+
private final boolean mUseFallback;
489494
private final Map<String, Uri> mEarcons;
490495
private final Map<String, Uri> mUtterances;
491496
private final Bundle mParams = new Bundle();
@@ -519,7 +524,7 @@ public TextToSpeech(Context context, OnInitListener listener) {
519524
* @param engine Package name of the TTS engine to use.
520525
*/
521526
public TextToSpeech(Context context, OnInitListener listener, String engine) {
522-
this(context, listener, engine, null);
527+
this(context, listener, engine, null, true);
523528
}
524529

525530
/**
@@ -529,10 +534,11 @@ public TextToSpeech(Context context, OnInitListener listener, String engine) {
529534
* @hide
530535
*/
531536
public TextToSpeech(Context context, OnInitListener listener, String engine,
532-
String packageName) {
537+
String packageName, boolean useFallback) {
533538
mContext = context;
534539
mInitListener = listener;
535540
mRequestedEngine = engine;
541+
mUseFallback = useFallback;
536542

537543
mEarcons = new HashMap<String, Uri>();
538544
mUtterances = new HashMap<String, Uri>();
@@ -567,10 +573,21 @@ private <R> R runAction(Action<R> action, R errorResult, String method, boolean
567573

568574
private int initTts() {
569575
// Step 1: Try connecting to the engine that was requested.
570-
if (mRequestedEngine != null && mEnginesHelper.isEngineInstalled(mRequestedEngine)) {
571-
if (connectToEngine(mRequestedEngine)) {
572-
mCurrentEngine = mRequestedEngine;
573-
return SUCCESS;
576+
if (mRequestedEngine != null) {
577+
if (mEnginesHelper.isEngineInstalled(mRequestedEngine)) {
578+
if (connectToEngine(mRequestedEngine)) {
579+
mCurrentEngine = mRequestedEngine;
580+
return SUCCESS;
581+
} else if (!mUseFallback) {
582+
mCurrentEngine = null;
583+
dispatchOnInit(ERROR);
584+
return ERROR;
585+
}
586+
} else if (!mUseFallback) {
587+
Log.i(TAG, "Requested engine not installed: " + mRequestedEngine);
588+
mCurrentEngine = null;
589+
dispatchOnInit(ERROR);
590+
return ERROR;
574591
}
575592
}
576593

core/java/android/webkit/WebView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ && getSettings().getJavaScriptEnabled()) {
13221322
final String packageName = ctx.getPackageName();
13231323
if (packageName != null) {
13241324
mTextToSpeech = new TextToSpeech(getContext(), null, null,
1325-
packageName + ".**webview**");
1325+
packageName + ".**webview**", true);
13261326
addJavascriptInterface(mTextToSpeech, ALIAS_ACCESSIBILITY_JS_INTERFACE);
13271327
}
13281328
}

tests/TtsTests/Android.mk

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# Copyright (C) 2011 The Android Open Source Project
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
LOCAL_PATH:= $(call my-dir)
17+
include $(CLEAR_VARS)
18+
19+
LOCAL_MODULE_TAGS := tests
20+
21+
LOCAL_SRC_FILES := $(call all-subdir-java-files)
22+
23+
LOCAL_STATIC_JAVA_LIBRARIES := littlemock
24+
LOCAL_JAVA_LIBRARIES := android.test.runner
25+
26+
LOCAL_PACKAGE_NAME := TtsTests
27+
28+
include $(BUILD_PACKAGE)

tests/TtsTests/AndroidManifest.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2011 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.speech.tts">
19+
<application>
20+
<uses-library android:name="android.test.runner" />
21+
22+
23+
<service android:name=".MockableTextToSpeechService"
24+
android:label="Mockable Text-to-speech Service">
25+
<intent-filter>
26+
<action android:name="android.intent.action.TTS_SERVICE" />
27+
<category android:name="android.intent.category.DEFAULT" />
28+
</intent-filter>
29+
</service>
30+
31+
<activity android:name=".MockableCheckVoiceData"
32+
android:theme="@android:style/Theme.NoDisplay">
33+
<intent-filter>
34+
<action android:name="android.speech.tts.engine.CHECK_TTS_DATA" />
35+
<category android:name="android.intent.category.DEFAULT" />
36+
</intent-filter>
37+
</activity>
38+
</application>
39+
40+
<instrumentation android:name="android.test.InstrumentationTestRunner"
41+
android:targetPackage="com.android.speech.tts"
42+
android:label="Tests for android.speech.tts" />
43+
</manifest>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (C) 2011 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.android.speech.tts;
18+
19+
import android.app.Activity;
20+
import android.content.Intent;
21+
import android.os.Bundle;
22+
import android.speech.tts.TextToSpeech;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
public class MockableCheckVoiceData extends Activity {
28+
29+
@Override
30+
protected void onCreate(Bundle savedInstanceState) {
31+
MockableTextToSpeechService.IDelegate delegate =
32+
MockableTextToSpeechService.getMocker();
33+
34+
ArrayList<String> availableLangs = delegate.getAvailableVoices();
35+
ArrayList<String> unavailableLangs = delegate.getUnavailableVoices();
36+
37+
final Intent returnVal = new Intent();
38+
39+
// Returns early.
40+
if (availableLangs == null) {
41+
setResult(TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL, returnVal);
42+
finish();
43+
return;
44+
}
45+
46+
returnVal.putStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES,
47+
availableLangs);
48+
49+
if (unavailableLangs != null && unavailableLangs.size() > 0) {
50+
returnVal.putStringArrayListExtra(TextToSpeech.Engine.EXTRA_UNAVAILABLE_VOICES,
51+
unavailableLangs);
52+
}
53+
54+
setResult(TextToSpeech.Engine.CHECK_VOICE_DATA_PASS, returnVal);
55+
finish();
56+
}
57+
58+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (C) 2011 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.android.speech.tts;
18+
19+
import android.speech.tts.SynthesisCallback;
20+
import android.speech.tts.SynthesisRequest;
21+
import android.speech.tts.TextToSpeechService;
22+
23+
import java.util.ArrayList;
24+
25+
public class MockableTextToSpeechService extends TextToSpeechService {
26+
27+
private static IDelegate sDelegate;
28+
29+
public static void setMocker(IDelegate delegate) {
30+
sDelegate = delegate;
31+
}
32+
33+
static IDelegate getMocker() {
34+
return sDelegate;
35+
}
36+
37+
@Override
38+
protected int onIsLanguageAvailable(String lang, String country, String variant) {
39+
return sDelegate.onIsLanguageAvailable(lang, country, variant);
40+
}
41+
42+
@Override
43+
protected String[] onGetLanguage() {
44+
return sDelegate.onGetLanguage();
45+
}
46+
47+
@Override
48+
protected int onLoadLanguage(String lang, String country, String variant) {
49+
return sDelegate.onLoadLanguage(lang, country, variant);
50+
}
51+
52+
@Override
53+
protected void onStop() {
54+
sDelegate.onStop();
55+
}
56+
57+
@Override
58+
protected void onSynthesizeText(SynthesisRequest request, SynthesisCallback callback) {
59+
sDelegate.onSynthesizeText(request, callback);
60+
}
61+
62+
public static interface IDelegate {
63+
int onIsLanguageAvailable(String lang, String country, String variant);
64+
65+
String[] onGetLanguage();
66+
67+
int onLoadLanguage(String lang, String country, String variant);
68+
69+
void onStop();
70+
71+
void onSynthesizeText(SynthesisRequest request, SynthesisCallback callback);
72+
73+
ArrayList<String> getAvailableVoices();
74+
75+
ArrayList<String> getUnavailableVoices();
76+
}
77+
78+
}

0 commit comments

Comments
 (0)