Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Updates to SetSpeakMiddleware #1123

Merged
merged 4 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,17 @@ public class SetSpeakMiddleware implements Middleware {

private final String voiceName;
private final boolean fallbackToTextForSpeak;
private final String lang;

/**
* Initializes a new instance of the {@link SetSpeakMiddleware} class.
*
* @param voiceName The SSML voice name attribute value.
* @param lang The xml:lang value.
* @param fallbackToTextForSpeak true if an empt Activity.Speak is populated
* with Activity.getText().
*/
public SetSpeakMiddleware(String voiceName, String lang, boolean fallbackToTextForSpeak) {
public SetSpeakMiddleware(String voiceName, boolean fallbackToTextForSpeak) {
this.voiceName = voiceName;
this.fallbackToTextForSpeak = fallbackToTextForSpeak;
if (lang == null) {
throw new IllegalArgumentException("lang cannot be null.");
} else {
this.lang = lang;
}
}

/**
Expand Down Expand Up @@ -73,10 +66,11 @@ public CompletableFuture<Void> onTurn(TurnContext turnContext, NextDelegate next
activity.setSpeak(
String.format("<voice name='%s'>%s</voice>", voiceName, activity.getSpeak()));
}

activity.setSpeak(String
.format("<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' "
+ "xml:lang='%s'>%s</speak>", lang, activity.getSpeak()));
+ "xml:lang='%s'>%s</speak>",
activity.getLocale() != null ? activity.getLocale() : "en-US",
activity.getSpeak()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@

public class SetSpeakMiddlewareTests {

@Test
public void ConstructorValidation() {
// no 'lang'
Assert.assertThrows(IllegalArgumentException.class, () -> new SetSpeakMiddleware("voice", null, false));
}

@Test
public void NoFallback() {
TestAdapter adapter = new TestAdapter(createConversation("NoFallback"))
.use(new SetSpeakMiddleware("male", "en-us", false));
.use(new SetSpeakMiddleware("male", false));

new TestFlow(adapter, turnContext -> {
Activity activity = MessageFactory.text("OK");
Expand All @@ -43,7 +37,7 @@ public void NoFallback() {
@Test
public void FallbackNullSpeak() {
TestAdapter adapter = new TestAdapter(createConversation("NoFallback"))
.use(new SetSpeakMiddleware("male", "en-us", true));
.use(new SetSpeakMiddleware("male", true));

new TestFlow(adapter, turnContext -> {
Activity activity = MessageFactory.text("OK");
Expand All @@ -61,7 +55,7 @@ public void FallbackNullSpeak() {
@Test
public void FallbackWithSpeak() {
TestAdapter adapter = new TestAdapter(createConversation("Fallback"))
.use(new SetSpeakMiddleware("male", "en-us", true));
.use(new SetSpeakMiddleware("male", true));

new TestFlow(adapter, turnContext -> {
Activity activity = MessageFactory.text("OK");
Expand Down Expand Up @@ -93,7 +87,7 @@ public void AddVoiceTelephony() {
// Voice instanceof added to Speak property.
public void AddVoice(String channelId) {
TestAdapter adapter = new TestAdapter(createConversation("Fallback", channelId))
.use(new SetSpeakMiddleware("male", "en-us", true));
.use(new SetSpeakMiddleware("male", true));

new TestFlow(adapter, turnContext -> {
Activity activity = MessageFactory.text("OK");
Expand All @@ -119,14 +113,14 @@ public void AddNoVoiceDirectlineSpeech() {

@Test
public void AddNoVoiceTelephony() {
AddNoVoice("telephony");
AddNoVoice(Channels.TELEPHONY);
}


// With no 'voice' specified, the Speak property instanceof unchanged.
public void AddNoVoice(String channelId) {
TestAdapter adapter = new TestAdapter(createConversation("Fallback", channelId))
.use(new SetSpeakMiddleware(null, "en-us", true));
.use(new SetSpeakMiddleware(null, true));

new TestFlow(adapter, turnContext -> {
Activity activity = MessageFactory.text("OK");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,9 @@ private Channels() {
* Test channel.
*/
public static final String TEST = "test";

/**
* Telephony channel.
*/
public static final String TELEPHONY = "telephony";
}