From 067fd9e95f5a3fdbc91ac72c83e739cb4469d9d0 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 26 Jul 2023 08:50:55 -0400 Subject: [PATCH 1/2] test(mvux): Ensure that feed parameters shares the same context as the VM --- .../Generator/Given_VMWithCommands.cs | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/Uno.Extensions.Reactive.UI.Tests/Generator/Given_VMWithCommands.cs diff --git a/src/Uno.Extensions.Reactive.UI.Tests/Generator/Given_VMWithCommands.cs b/src/Uno.Extensions.Reactive.UI.Tests/Generator/Given_VMWithCommands.cs new file mode 100644 index 0000000000..6afbdb45f0 --- /dev/null +++ b/src/Uno.Extensions.Reactive.UI.Tests/Generator/Given_VMWithCommands.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Data; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Uno.Extensions.Reactive.UI; +using Uno.UI.RuntimeTests; + +namespace Uno.Extensions.Reactive.WinUI.Tests.Generator; + +[TestClass] +[RunsOnUIThread] +public partial class Given_VMWithCommands +{ + public partial class When_ParameterFeed_Then_SubscribedWithSameContext_ViewModel + { + public int FeedInvokeCount { get; private set; } + + public int CommandLastParameter { get; private set; } = -1; + + public IFeed MyFeed => Feed.Async(async ct => ++FeedInvokeCount); + + public void DoSomething(int myFeed, CancellationToken ct) + => CommandLastParameter = myFeed; + } + + [TestMethod] + public async Task When_ParameterFeed_Then_SubscribedWithSameContext() + { + var vm = new BindableWhen_ParameterFeed_Then_SubscribedWithSameContext_ViewModel(); + + await UIHelper.WaitFor(() => vm.MyFeed != 0, default); + + var current = vm.MyFeed; + vm.DoSomething.Execute(null); + + await UIHelper.WaitFor(() => vm.Model.CommandLastParameter != -1, default); + + Assert.AreEqual(1, vm.FeedInvokeCount); + Assert.AreEqual(current, vm.Model.CommandLastParameter); + } + + [TestMethod] + public async Task When_ParameterFeed_Then_SubscribedWithSameContext_UsingUI() + { + FeedView view; + Button doSomething; + var vm = new BindableWhen_ParameterFeed_Then_SubscribedWithSameContext_ViewModel(); + var ui = new StackPanel + { + DataContext = vm, + Children = + { + (view = new FeedView()), + (doSomething = new Button()) + } + }; + + view.SetBinding(FeedView.SourceProperty, new Binding { Path = new PropertyPath("MyFeed") }); + doSomething.SetBinding(Button.CommandProperty, new Binding { Path = new PropertyPath("DoSomething") }); + + await UIHelper.Load(ui, default); + + await UIHelper.WaitFor(() => vm.MyFeed is not 0, default); + + var current = vm.MyFeed; + doSomething.Command.Execute(null); + + await UIHelper.WaitFor(() => vm.Model.CommandLastParameter != -1, default); + + Assert.AreEqual(1, vm.FeedInvokeCount); + Assert.AreEqual(current, vm.Model.CommandLastParameter); + } +} From 9bed5239b2e58ab4c7606b30289d2bee997d6422 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 10 Aug 2023 17:29:07 -0400 Subject: [PATCH 2/2] chore: Fix CI --- .../Generator/Given_VMWithCommands.cs | 1 + src/Uno.Extensions.Reactive/Utils/FeedUIHelper.cs | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Uno.Extensions.Reactive.UI.Tests/Generator/Given_VMWithCommands.cs b/src/Uno.Extensions.Reactive.UI.Tests/Generator/Given_VMWithCommands.cs index 6afbdb45f0..182dc9ae4f 100644 --- a/src/Uno.Extensions.Reactive.UI.Tests/Generator/Given_VMWithCommands.cs +++ b/src/Uno.Extensions.Reactive.UI.Tests/Generator/Given_VMWithCommands.cs @@ -7,6 +7,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Uno.Extensions.Reactive.UI; using Uno.UI.RuntimeTests; +using Button = Microsoft.UI.Xaml.Controls.Button; namespace Uno.Extensions.Reactive.WinUI.Tests.Generator; diff --git a/src/Uno.Extensions.Reactive/Utils/FeedUIHelper.cs b/src/Uno.Extensions.Reactive/Utils/FeedUIHelper.cs index 216814d4a0..9e1a0423e3 100644 --- a/src/Uno.Extensions.Reactive/Utils/FeedUIHelper.cs +++ b/src/Uno.Extensions.Reactive/Utils/FeedUIHelper.cs @@ -48,7 +48,17 @@ public static async IAsyncEnumerable> GetSource(IFeed feed, Sou } } - private static async ValueTask> GetSourceEnumerator(ISignal signal, SourceContext context) + private static async ValueTask>> GetSourceEnumerator(IFeed signal, SourceContext context) + { + var ct = context.Token; + var src = DispatcherHelper.HasThreadAccess + ? await Task.Run(() => context.GetOrCreateSource(signal), ct).ConfigureAwait(false) + : context.GetOrCreateSource(signal); + + return src.GetAsyncEnumerator(ct); + } + + private static async ValueTask> GetSourceEnumerator(ISignal signal, SourceContext context) { var ct = context.Token; var src = DispatcherHelper.HasThreadAccess