|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MT License. |
| 3 | + |
| 4 | +package com.microsoft.bot.builder; |
| 5 | + |
| 6 | +import com.microsoft.bot.builder.adapters.TestAdapter; |
| 7 | +import com.microsoft.bot.builder.adapters.TestFlow; |
| 8 | +import com.microsoft.bot.connector.Channels; |
| 9 | +import com.microsoft.bot.schema.Activity; |
| 10 | +import com.microsoft.bot.schema.ChannelAccount; |
| 11 | +import com.microsoft.bot.schema.ConversationAccount; |
| 12 | +import com.microsoft.bot.schema.ConversationReference; |
| 13 | + |
| 14 | +import org.junit.Assert; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +public class SetSpeakMiddlewareTests { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void ConstructorValidation() { |
| 21 | + // no 'lang' |
| 22 | + Assert.assertThrows(IllegalArgumentException.class, () -> new SetSpeakMiddleware("voice", null, false)); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void NoFallback() { |
| 27 | + TestAdapter adapter = new TestAdapter(createConversation("NoFallback")) |
| 28 | + .use(new SetSpeakMiddleware("male", "en-us", false)); |
| 29 | + |
| 30 | + new TestFlow(adapter, turnContext -> { |
| 31 | + Activity activity = MessageFactory.text("OK"); |
| 32 | + |
| 33 | + return turnContext.sendActivity(activity).thenApply(result -> null); |
| 34 | + }).send("foo").assertReply(obj -> { |
| 35 | + Activity activity = (Activity) obj; |
| 36 | + Assert.assertNull(activity.getSpeak()); |
| 37 | + }).startTest().join(); |
| 38 | + } |
| 39 | + |
| 40 | + // fallback instanceof true, for any ChannelId other than emulator, |
| 41 | + // directlinespeech, or telephony should |
| 42 | + // just set Activity.Speak to Activity.Text if Speak instanceof empty. |
| 43 | + @Test |
| 44 | + public void FallbackNullSpeak() { |
| 45 | + TestAdapter adapter = new TestAdapter(createConversation("NoFallback")) |
| 46 | + .use(new SetSpeakMiddleware("male", "en-us", true)); |
| 47 | + |
| 48 | + new TestFlow(adapter, turnContext -> { |
| 49 | + Activity activity = MessageFactory.text("OK"); |
| 50 | + |
| 51 | + return turnContext.sendActivity(activity).thenApply(result -> null); |
| 52 | + }).send("foo").assertReply(obj -> { |
| 53 | + Activity activity = (Activity) obj; |
| 54 | + Assert.assertEquals(activity.getText(), activity.getSpeak()); |
| 55 | + }).startTest().join(); |
| 56 | + } |
| 57 | + |
| 58 | + // fallback instanceof true, for any ChannelId other than emulator, |
| 59 | + // directlinespeech, or telephony should |
| 60 | + // leave a non-empty Speak unchanged. |
| 61 | + @Test |
| 62 | + public void FallbackWithSpeak() { |
| 63 | + TestAdapter adapter = new TestAdapter(createConversation("Fallback")) |
| 64 | + .use(new SetSpeakMiddleware("male", "en-us", true)); |
| 65 | + |
| 66 | + new TestFlow(adapter, turnContext -> { |
| 67 | + Activity activity = MessageFactory.text("OK"); |
| 68 | + activity.setSpeak("speak value"); |
| 69 | + |
| 70 | + return turnContext.sendActivity(activity).thenApply(result -> null); |
| 71 | + }).send("foo").assertReply(obj -> { |
| 72 | + Activity activity = (Activity) obj; |
| 73 | + Assert.assertEquals("speak value", activity.getSpeak()); |
| 74 | + }).startTest().join(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void AddVoiceEmulator() { |
| 79 | + AddVoice(Channels.EMULATOR); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void AddVoiceDirectlineSpeech() { |
| 84 | + AddVoice(Channels.DIRECTLINESPEECH); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void AddVoiceTelephony() { |
| 89 | + AddVoice("telephony"); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + // Voice instanceof added to Speak property. |
| 94 | + public void AddVoice(String channelId) { |
| 95 | + TestAdapter adapter = new TestAdapter(createConversation("Fallback", channelId)) |
| 96 | + .use(new SetSpeakMiddleware("male", "en-us", true)); |
| 97 | + |
| 98 | + new TestFlow(adapter, turnContext -> { |
| 99 | + Activity activity = MessageFactory.text("OK"); |
| 100 | + |
| 101 | + return turnContext.sendActivity(activity).thenApply(result -> null); |
| 102 | + }).send("foo").assertReply(obj -> { |
| 103 | + Activity activity = (Activity) obj; |
| 104 | + Assert.assertEquals("<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' " |
| 105 | + + "xml:lang='en-us'><voice name='male'>OK</voice></speak>", |
| 106 | + activity.getSpeak()); |
| 107 | + }).startTest().join(); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void AddNoVoiceEmulator() { |
| 112 | + AddNoVoice(Channels.EMULATOR); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void AddNoVoiceDirectlineSpeech() { |
| 117 | + AddNoVoice(Channels.DIRECTLINESPEECH); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void AddNoVoiceTelephony() { |
| 122 | + AddNoVoice("telephony"); |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + // With no 'voice' specified, the Speak property instanceof unchanged. |
| 127 | + public void AddNoVoice(String channelId) { |
| 128 | + TestAdapter adapter = new TestAdapter(createConversation("Fallback", channelId)) |
| 129 | + .use(new SetSpeakMiddleware(null, "en-us", true)); |
| 130 | + |
| 131 | + new TestFlow(adapter, turnContext -> { |
| 132 | + Activity activity = MessageFactory.text("OK"); |
| 133 | + |
| 134 | + return turnContext.sendActivity(activity).thenApply(result -> null); |
| 135 | + }).send("foo").assertReply(obj -> { |
| 136 | + Activity activity = (Activity) obj; |
| 137 | + Assert.assertEquals("OK", |
| 138 | + activity.getSpeak()); |
| 139 | + }).startTest().join(); |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + private static ConversationReference createConversation(String name) { |
| 144 | + return createConversation(name, "User1", "Bot", "test"); |
| 145 | + } |
| 146 | + |
| 147 | + private static ConversationReference createConversation(String name, String channelId) { |
| 148 | + return createConversation(name, "User1", "Bot", channelId); |
| 149 | + } |
| 150 | + |
| 151 | + private static ConversationReference createConversation(String name, String user, String bot, String channelId) { |
| 152 | + ConversationReference conversationReference = new ConversationReference(); |
| 153 | + conversationReference.setChannelId(channelId); |
| 154 | + conversationReference.setServiceUrl("https://test.com"); |
| 155 | + conversationReference.setConversation(new ConversationAccount(false, name, name)); |
| 156 | + conversationReference.setUser(new ChannelAccount(user.toLowerCase(), user)); |
| 157 | + conversationReference.setBot(new ChannelAccount(bot.toLowerCase(), bot)); |
| 158 | + conversationReference.setLocale("en-us"); |
| 159 | + return conversationReference; |
| 160 | + } |
| 161 | +} |
0 commit comments