File size: 8,753 Bytes
0827183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import unittest
from typing import List

from botbuilder.dialogs.choices import Choice, ChoiceFactory, ChoiceFactoryOptions
from botbuilder.schema import (
    ActionTypes,
    Activity,
    ActivityTypes,
    Attachment,
    AttachmentLayoutTypes,
    CardAction,
    HeroCard,
    InputHints,
    SuggestedActions,
)
from botframework.connector import Channels


class ChoiceFactoryTest(unittest.TestCase):
    color_choices: List[Choice] = [Choice("red"), Choice("green"), Choice("blue")]
    choices_with_actions: List[Choice] = [
        Choice(
            "ImBack",
            action=CardAction(
                type=ActionTypes.im_back, title="ImBack Action", value="ImBack Value"
            ),
        ),
        Choice(
            "MessageBack",
            action=CardAction(
                type=ActionTypes.message_back,
                title="MessageBack Action",
                value="MessageBack Value",
            ),
        ),
        Choice(
            "PostBack",
            action=CardAction(
                type=ActionTypes.post_back,
                title="PostBack Action",
                value="PostBack Value",
            ),
        ),
    ]

    def test_inline_should_render_choices_inline(self):
        activity = ChoiceFactory.inline(ChoiceFactoryTest.color_choices, "select from:")
        self.assertEqual("select from: (1) red, (2) green, or (3) blue", activity.text)

    def test_should_render_choices_as_a_list(self):
        activity = ChoiceFactory.list_style(
            ChoiceFactoryTest.color_choices, "select from:"
        )
        self.assertEqual(
            "select from:\n\n   1. red\n   2. green\n   3. blue", activity.text
        )

    def test_should_render_unincluded_numbers_choices_as_a_list(self):
        activity = ChoiceFactory.list_style(
            ChoiceFactoryTest.color_choices,
            "select from:",
            options=ChoiceFactoryOptions(include_numbers=False),
        )
        self.assertEqual(
            "select from:\n\n   - red\n   - green\n   - blue", activity.text
        )

    def test_should_render_choices_as_suggested_actions(self):
        expected = Activity(
            type=ActivityTypes.message,
            text="select from:",
            input_hint=InputHints.expecting_input,
            suggested_actions=SuggestedActions(
                actions=[
                    CardAction(type=ActionTypes.im_back, value="red", title="red"),
                    CardAction(type=ActionTypes.im_back, value="green", title="green"),
                    CardAction(type=ActionTypes.im_back, value="blue", title="blue"),
                ]
            ),
        )

        activity = ChoiceFactory.suggested_action(
            ChoiceFactoryTest.color_choices, "select from:"
        )

        self.assertEqual(expected, activity)

    def test_should_render_choices_as_hero_card(self):
        expected = Activity(
            type=ActivityTypes.message,
            input_hint=InputHints.expecting_input,
            attachment_layout=AttachmentLayoutTypes.list,
            attachments=[
                Attachment(
                    content=HeroCard(
                        text="select from:",
                        buttons=[
                            CardAction(
                                type=ActionTypes.im_back, value="red", title="red"
                            ),
                            CardAction(
                                type=ActionTypes.im_back, value="green", title="green"
                            ),
                            CardAction(
                                type=ActionTypes.im_back, value="blue", title="blue"
                            ),
                        ],
                    ),
                    content_type="application/vnd.microsoft.card.hero",
                )
            ],
        )

        activity = ChoiceFactory.hero_card(
            ChoiceFactoryTest.color_choices, "select from:"
        )

        self.assertEqual(expected, activity)

    def test_should_automatically_choose_render_style_based_on_channel_type(self):
        expected = Activity(
            type=ActivityTypes.message,
            text="select from:",
            input_hint=InputHints.expecting_input,
            suggested_actions=SuggestedActions(
                actions=[
                    CardAction(type=ActionTypes.im_back, value="red", title="red"),
                    CardAction(type=ActionTypes.im_back, value="green", title="green"),
                    CardAction(type=ActionTypes.im_back, value="blue", title="blue"),
                ]
            ),
        )
        activity = ChoiceFactory.for_channel(
            Channels.emulator, ChoiceFactoryTest.color_choices, "select from:"
        )

        self.assertEqual(expected, activity)

    def test_should_choose_correct_styles_for_teams(self):
        expected = Activity(
            type=ActivityTypes.message,
            input_hint=InputHints.expecting_input,
            attachment_layout=AttachmentLayoutTypes.list,
            attachments=[
                Attachment(
                    content=HeroCard(
                        text="select from:",
                        buttons=[
                            CardAction(
                                type=ActionTypes.im_back, value="red", title="red"
                            ),
                            CardAction(
                                type=ActionTypes.im_back, value="green", title="green"
                            ),
                            CardAction(
                                type=ActionTypes.im_back, value="blue", title="blue"
                            ),
                        ],
                    ),
                    content_type="application/vnd.microsoft.card.hero",
                )
            ],
        )
        activity = ChoiceFactory.for_channel(
            Channels.ms_teams, ChoiceFactoryTest.color_choices, "select from:"
        )
        self.assertEqual(expected, activity)

    def test_should_include_choice_actions_in_suggested_actions(self):
        expected = Activity(
            type=ActivityTypes.message,
            text="select from:",
            input_hint=InputHints.expecting_input,
            suggested_actions=SuggestedActions(
                actions=[
                    CardAction(
                        type=ActionTypes.im_back,
                        value="ImBack Value",
                        title="ImBack Action",
                    ),
                    CardAction(
                        type=ActionTypes.message_back,
                        value="MessageBack Value",
                        title="MessageBack Action",
                    ),
                    CardAction(
                        type=ActionTypes.post_back,
                        value="PostBack Value",
                        title="PostBack Action",
                    ),
                ]
            ),
        )
        activity = ChoiceFactory.suggested_action(
            ChoiceFactoryTest.choices_with_actions, "select from:"
        )
        self.assertEqual(expected, activity)

    def test_should_include_choice_actions_in_hero_cards(self):
        expected = Activity(
            type=ActivityTypes.message,
            input_hint=InputHints.expecting_input,
            attachment_layout=AttachmentLayoutTypes.list,
            attachments=[
                Attachment(
                    content=HeroCard(
                        text="select from:",
                        buttons=[
                            CardAction(
                                type=ActionTypes.im_back,
                                value="ImBack Value",
                                title="ImBack Action",
                            ),
                            CardAction(
                                type=ActionTypes.message_back,
                                value="MessageBack Value",
                                title="MessageBack Action",
                            ),
                            CardAction(
                                type=ActionTypes.post_back,
                                value="PostBack Value",
                                title="PostBack Action",
                            ),
                        ],
                    ),
                    content_type="application/vnd.microsoft.card.hero",
                )
            ],
        )
        activity = ChoiceFactory.hero_card(
            ChoiceFactoryTest.choices_with_actions, "select from:"
        )
        self.assertEqual(expected, activity)