|
6 | 6 | import aiounittest |
7 | 7 | from recognizers_text import Culture |
8 | 8 |
|
9 | | -from botbuilder.core import ConversationState, MemoryStorage, TurnContext |
| 9 | +from botbuilder.core import CardFactory, ConversationState, MemoryStorage, TurnContext |
10 | 10 | from botbuilder.core.adapters import TestAdapter |
11 | 11 | from botbuilder.dialogs import DialogSet, DialogTurnResult, DialogTurnStatus |
12 | 12 | from botbuilder.dialogs.choices import Choice, ListStyle |
@@ -588,3 +588,108 @@ async def exec_test(turn_context: TurnContext): |
588 | 588 | ) |
589 | 589 | step3 = await step2.send("1") |
590 | 590 | await step3.assert_reply("red") |
| 591 | + |
| 592 | + async def test_should_display_choices_on_hero_card(self): |
| 593 | + size_choices = ["large", "medium", "small"] |
| 594 | + |
| 595 | + async def exec_test(turn_context: TurnContext): |
| 596 | + dialog_context = await dialogs.create_context(turn_context) |
| 597 | + |
| 598 | + results: DialogTurnResult = await dialog_context.continue_dialog() |
| 599 | + |
| 600 | + if results.status == DialogTurnStatus.Empty: |
| 601 | + options = PromptOptions( |
| 602 | + prompt=Activity( |
| 603 | + type=ActivityTypes.message, text="Please choose a size." |
| 604 | + ), |
| 605 | + choices=size_choices, |
| 606 | + ) |
| 607 | + await dialog_context.prompt("prompt", options) |
| 608 | + elif results.status == DialogTurnStatus.Complete: |
| 609 | + selected_choice = results.result |
| 610 | + await turn_context.send_activity(selected_choice.value) |
| 611 | + |
| 612 | + await convo_state.save_changes(turn_context) |
| 613 | + |
| 614 | + def assert_expected_activity( |
| 615 | + activity: Activity, description |
| 616 | + ): # pylint: disable=unused-argument |
| 617 | + assert len(activity.attachments) == 1 |
| 618 | + assert ( |
| 619 | + activity.attachments[0].content_type |
| 620 | + == CardFactory.content_types.hero_card |
| 621 | + ) |
| 622 | + assert activity.attachments[0].content.text == "Please choose a size." |
| 623 | + |
| 624 | + adapter = TestAdapter(exec_test) |
| 625 | + |
| 626 | + convo_state = ConversationState(MemoryStorage()) |
| 627 | + dialog_state = convo_state.create_property("dialogState") |
| 628 | + dialogs = DialogSet(dialog_state) |
| 629 | + |
| 630 | + choice_prompt = ChoicePrompt("prompt") |
| 631 | + |
| 632 | + # Change the ListStyle of the prompt to ListStyle.none. |
| 633 | + choice_prompt.style = ListStyle.hero_card |
| 634 | + |
| 635 | + dialogs.add(choice_prompt) |
| 636 | + |
| 637 | + step1 = await adapter.send("Hello") |
| 638 | + step2 = await step1.assert_reply(assert_expected_activity) |
| 639 | + step3 = await step2.send("1") |
| 640 | + await step3.assert_reply(size_choices[0]) |
| 641 | + |
| 642 | + async def test_should_display_choices_on_hero_card_with_additional_attachment(self): |
| 643 | + size_choices = ["large", "medium", "small"] |
| 644 | + card = CardFactory.adaptive_card( |
| 645 | + { |
| 646 | + "type": "AdaptiveCard", |
| 647 | + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", |
| 648 | + "version": "1.2", |
| 649 | + "body": [], |
| 650 | + } |
| 651 | + ) |
| 652 | + card_activity = Activity(attachments=[card]) |
| 653 | + |
| 654 | + async def exec_test(turn_context: TurnContext): |
| 655 | + dialog_context = await dialogs.create_context(turn_context) |
| 656 | + |
| 657 | + results: DialogTurnResult = await dialog_context.continue_dialog() |
| 658 | + |
| 659 | + if results.status == DialogTurnStatus.Empty: |
| 660 | + options = PromptOptions(prompt=card_activity, choices=size_choices) |
| 661 | + await dialog_context.prompt("prompt", options) |
| 662 | + elif results.status == DialogTurnStatus.Complete: |
| 663 | + selected_choice = results.result |
| 664 | + await turn_context.send_activity(selected_choice.value) |
| 665 | + |
| 666 | + await convo_state.save_changes(turn_context) |
| 667 | + |
| 668 | + def assert_expected_activity( |
| 669 | + activity: Activity, description |
| 670 | + ): # pylint: disable=unused-argument |
| 671 | + assert len(activity.attachments) == 2 |
| 672 | + assert ( |
| 673 | + activity.attachments[0].content_type |
| 674 | + == CardFactory.content_types.adaptive_card |
| 675 | + ) |
| 676 | + assert ( |
| 677 | + activity.attachments[1].content_type |
| 678 | + == CardFactory.content_types.hero_card |
| 679 | + ) |
| 680 | + |
| 681 | + adapter = TestAdapter(exec_test) |
| 682 | + |
| 683 | + convo_state = ConversationState(MemoryStorage()) |
| 684 | + dialog_state = convo_state.create_property("dialogState") |
| 685 | + dialogs = DialogSet(dialog_state) |
| 686 | + |
| 687 | + choice_prompt = ChoicePrompt("prompt") |
| 688 | + |
| 689 | + # Change the ListStyle of the prompt to ListStyle.none. |
| 690 | + choice_prompt.style = ListStyle.hero_card |
| 691 | + |
| 692 | + dialogs.add(choice_prompt) |
| 693 | + |
| 694 | + step1 = await adapter.send("Hello") |
| 695 | + await step1.assert_reply(assert_expected_activity) |
0 commit comments