From c572be356f3c6eb8e4a540fad16cbe44aa03f54d Mon Sep 17 00:00:00 2001 From: Cecilia Avila <44245136+ceciliaavila@users.noreply.github.com> Date: Mon, 29 Aug 2022 17:01:16 -0300 Subject: [PATCH] [#6430] Adaptive ForEachElement does not exit when child action CancelAllDialogs is called (#6452) * Consider canceled status to end the dialog * Add unit test --- .../Actions/ForEachElement.cs | 5 +- .../ActionTests.cs | 6 ++ ...tion_Foreach_Nested_WithCancel.test.dialog | 67 +++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 tests/Microsoft.Bot.Builder.Dialogs.Adaptive.Tests/Tests/ActionTests/Action_Foreach_Nested_WithCancel.test.dialog diff --git a/libraries/Microsoft.Bot.Builder.Dialogs.Adaptive/Actions/ForEachElement.cs b/libraries/Microsoft.Bot.Builder.Dialogs.Adaptive/Actions/ForEachElement.cs index 9bcff70098..f43d22f973 100644 --- a/libraries/Microsoft.Bot.Builder.Dialogs.Adaptive/Actions/ForEachElement.cs +++ b/libraries/Microsoft.Bot.Builder.Dialogs.Adaptive/Actions/ForEachElement.cs @@ -263,6 +263,7 @@ private DialogContext CreateChildContext(DialogContext dc, DialogState childDial private bool ShouldEndDialog(DialogTurnResult turnResult, out DialogTurnResult finalTurnResult) { + DialogTurnStatus[] endedDialog = { DialogTurnStatus.Complete, DialogTurnStatus.Cancelled }; finalTurnResult = null; // Insure BreakLoop ends the dialog @@ -279,12 +280,12 @@ private bool ShouldEndDialog(DialogTurnResult turnResult, out DialogTurnResult f // the result will be nested. while (finalTurnResult.Result != null && finalTurnResult.Result is DialogTurnResult dtr - && dtr.ParentEnded && dtr.Status == DialogTurnStatus.Complete) + && dtr.ParentEnded && endedDialog.Contains(dtr.Status)) { finalTurnResult = dtr; } - return finalTurnResult.ParentEnded && finalTurnResult.Status == DialogTurnStatus.Complete; + return finalTurnResult.ParentEnded && endedDialog.Contains(finalTurnResult.Status); } private void UpdateActionScopeState(DialogContext dc, DialogState state) diff --git a/tests/Microsoft.Bot.Builder.Dialogs.Adaptive.Tests/ActionTests.cs b/tests/Microsoft.Bot.Builder.Dialogs.Adaptive.Tests/ActionTests.cs index 14d08fc017..895594711e 100644 --- a/tests/Microsoft.Bot.Builder.Dialogs.Adaptive.Tests/ActionTests.cs +++ b/tests/Microsoft.Bot.Builder.Dialogs.Adaptive.Tests/ActionTests.cs @@ -296,6 +296,12 @@ public async Task Action_Foreach_Nested() await TestUtils.RunTestScript(_resourceExplorerFixture.ResourceExplorer); } + [Fact] + public async Task Action_Foreach_Nested_WithCancel() + { + await TestUtils.RunTestScript(_resourceExplorerFixture.ResourceExplorer); + } + [Fact] public async Task Action_Foreach_Object() { diff --git a/tests/Microsoft.Bot.Builder.Dialogs.Adaptive.Tests/Tests/ActionTests/Action_Foreach_Nested_WithCancel.test.dialog b/tests/Microsoft.Bot.Builder.Dialogs.Adaptive.Tests/Tests/ActionTests/Action_Foreach_Nested_WithCancel.test.dialog new file mode 100644 index 0000000000..d46caf6b5e --- /dev/null +++ b/tests/Microsoft.Bot.Builder.Dialogs.Adaptive.Tests/Tests/ActionTests/Action_Foreach_Nested_WithCancel.test.dialog @@ -0,0 +1,67 @@ +{ + "$schema": "../../../tests.schema", + "$kind": "Microsoft.Test.Script", + "dialog": { + "$kind": "Microsoft.AdaptiveDialog", + "id": "root", + "triggers": [ + { + "$kind": "Microsoft.OnBeginDialog", + "actions": [ + { + "$kind": "Microsoft.SetProperty", + "property": "dialog.todo", + "value": "=['1', '2', '3']" + }, + { + "$kind": "Microsoft.Foreach", + "itemsProperty": "dialog.todo", + "actions": [ + { + "$kind": "Microsoft.SendActivity", + "activity": "I'm the Parent loop - index is: ${dialog.foreach.index}" + }, + { + "$kind": "Microsoft.Foreach", + "itemsProperty": "dialog.todo", + "actions": [ + { + "$kind": "Microsoft.SendActivity", + "activity": "I'm the child loop and I will cancel all dialogs" + }, + { + "$kind": "Microsoft.CancelAllDialogs" + }, + { + "$kind": "Microsoft.SendActivity", + "activity": "This shouldn't be sent" + } + ] + }, + { + "$kind": "Microsoft.SendActivity", + "activity": "This shouldn't be sent either" + } + ] + } + ] + } + ], + "autoEndDialog": true, + "defaultResultProperty": "dialog.result" + }, + "script": [ + { + "$kind": "Microsoft.Test.UserSays", + "text": "hi" + }, + { + "$kind": "Microsoft.Test.AssertReply", + "text": "I'm the Parent loop - index is: 0" + }, + { + "$kind": "Microsoft.Test.AssertReply", + "text": "I'm the child loop and I will cancel all dialogs" + } + ] +}