using Bunit; using Microsoft.AspNetCore.Components; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Raw; using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages; /// Confirmation names and cancellation preserve the existing destructive callback contract. [Trait("Category", "Unit")] public sealed class ModalAccessibilityTests : BunitContext { /// Only the defined local focus-adapter calls are configured. public ModalAccessibilityTests() { JSInterop.SetupVoid("otDialog.update", _ => true).SetVoidResult(); JSInterop.SetupVoid("otDialog.deactivate", _ => true).SetVoidResult(); } /// Cancel closes a named confirmation without invoking the destructive action. [Fact] public void ConfirmDialog_IsNamedAndCancelDoesNotInvokeDelete() { JSInterop.Setup("otDialog.activate", _ => true).SetResult(true); var deletes = 0; var cut = Render(p => p.Add(x => x.Visible, true) .Add(x => x.Title, "Delete driver press-01").Add(x => x.Message, "Delete press-01? This cannot be undone.") .Add(x => x.OnConfirm, () => deletes++)); var dialog = cut.Find("[role=dialog]"); dialog.GetAttribute("aria-modal").ShouldBe("true"); var id = dialog.GetAttribute("aria-labelledby"); id.ShouldNotBeNullOrWhiteSpace(); cut.Find("#" + id).TextContent.ShouldContain("press-01"); var description = dialog.GetAttribute("aria-describedby"); description.ShouldNotBeNullOrWhiteSpace(); cut.Find("#" + description).TextContent.ShouldContain("cannot be undone"); cut.Find(".modal-footer .btn-outline-secondary").Click(); deletes.ShouldBe(0); cut.FindAll("[role=dialog]").ShouldBeEmpty(); } /// Closing while confirmation is in flight cannot imply the operation was cancelled. [Fact] public async Task ConfirmDialog_BusyClosePreservesInFlightOperation() { JSInterop.Setup("otDialog.activate", _ => true).SetResult(true); var entered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var pending = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var calls = 0; var cut = Render(p => p.Add(x => x.Visible, true).Add(x => x.OnConfirm, async () => { calls++; entered.TrySetResult(true); await pending.Task; })); var confirming = cut.Find(".modal-footer .btn-danger").TriggerEventAsync("onclick", new Microsoft.AspNetCore.Components.Web.MouseEventArgs()); await entered.Task; try { cut.Find(".btn-close").HasAttribute("disabled").ShouldBeTrue(); cut.Find(".modal-footer .btn-outline-secondary").HasAttribute("disabled").ShouldBeTrue(); cut.FindAll("[role=dialog]").Count.ShouldBe(1); calls.ShouldBe(1); await cut.Find(".modal-footer .btn-danger").TriggerEventAsync("onclick", new Microsoft.AspNetCore.Components.Web.MouseEventArgs()); calls.ShouldBe(1); // A queued second activation cannot start another deletion. } finally { await cut.InvokeAsync(() => pending.TrySetResult(true)); await confirming; } cut.FindAll("[role=dialog]").ShouldBeEmpty(); } /// A delayed old activation is cleaned before a later presentation is activated. [Fact] public void Frame_HideDuringDeferredActivation_ReopenActivatesFreshPresentation() { var activation = JSInterop.Setup("otDialog.activate", _ => true); var cut = Render(p => p.Add(x => x.Visible, true)); cut.Render(p => p.Add(x => x.Visible, false)); activation.SetResult(true); cut.WaitForAssertion(() => JSInterop.Invocations["otDialog.deactivate"].Count.ShouldBeGreaterThan(0)); cut.Render(p => p.Add(x => x.Visible, true)); cut.WaitForAssertion(() => JSInterop.Invocations["otDialog.activate"].Count.ShouldBe(2)); } /// Disposal waits for a pending activation and cleans any late native state. [Fact] public async Task Frame_DisposeDuringDeferredActivation_CleansLateActivation() { var activation = JSInterop.Setup("otDialog.activate", _ => true); var cut = Render(p => p.Add(x => x.Visible, true)); var disposing = cut.Instance.DisposeAsync().AsTask(); activation.SetResult(true); await disposing; cut.WaitForAssertion(() => JSInterop.Invocations["otDialog.deactivate"].Count.ShouldBeGreaterThan(0)); } /// A new target queued during activation opens only after the old native generation is cleaned. [Fact] public async Task Frame_TargetSwitchDuringDeferredActivation_IsSerialized() { var openedReplacement = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); long firstGeneration = 0; var activation = JSInterop.Setup("otDialog.activate", call => { if (firstGeneration > 0 && Convert.ToInt64(call.Arguments[2]) > firstGeneration) openedReplacement.TrySetResult(true); return true; }); var cut = Render(p => p.Add(x => x.Visible, true).Add(x => x.Target, "A")); firstGeneration = Convert.ToInt64(JSInterop.Invocations["otDialog.activate"][0].Arguments[2]); try { cut.Render(p => p.Add(x => x.Visible, false)); cut.Render(p => p.Add(x => x.Visible, true).Add(x => x.Target, "B")); JSInterop.Invocations["otDialog.activate"].Count.ShouldBe(1); } finally { activation.SetResult(true); } // JS-only OnAfterRender continuations do not require another component render. Observe the // actual replacement invocation, then inspect its recorded calls on the renderer dispatcher. await openedReplacement.Task.WaitAsync(TimeSpan.FromSeconds(5), Xunit.TestContext.Current.CancellationToken); await cut.InvokeAsync(() => { JSInterop.Invocations["otDialog.activate"].Count.ShouldBe(2); var generations = JSInterop.Invocations["otDialog.activate"].Select(call => Convert.ToInt64(call.Arguments[2])).ToArray(); generations[1].ShouldBeGreaterThan(generations[0]); JSInterop.Invocations["otDialog.deactivate"].Count.ShouldBeGreaterThan(0); }); } /// A stale Escape cannot close a different reopened target. [Fact] public async Task Frame_StaleNativeCloseCannotCancelReopenedPresentation() { JSInterop.Setup("otDialog.activate", _ => true).SetResult(true); var closes = 0; var cut = Render(p => p.Add(x => x.Visible, true).Add(x => x.Target, "A").Add(x => x.OnClose, () => closes++)); var oldGeneration = Convert.ToInt64(JSInterop.Invocations["otDialog.activate"][0].Arguments[2]); cut.Render(p => p.Add(x => x.Visible, false)); cut.Render(p => p.Add(x => x.Visible, true).Add(x => x.Target, "B")); var currentGeneration = Convert.ToInt64(JSInterop.Invocations["otDialog.activate"].Last().Arguments[2]); await cut.Instance.RequestCloseFromDom(oldGeneration); closes.ShouldBe(0); await cut.Instance.RequestCloseFromDom(currentGeneration); closes.ShouldBe(1); } /// Busy-only updates preserve the presentation and never move focus back to its start. [Fact] public void Frame_BusyOnlyRenderUpdatesWithoutReactivation() { JSInterop.Setup("otDialog.activate", _ => true).SetResult(true); var cut = Render(p => p.Add(x => x.Visible, true).Add(x => x.Target, "A")); cut.Render(p => p.Add(x => x.Busy, true)); cut.Render(p => p.Add(x => x.Busy, false)); JSInterop.Invocations["otDialog.activate"].Count.ShouldBe(1); JSInterop.Invocations["otDialog.update"].Count.ShouldBe(2); } /// An old operation cannot close or clear the busy state of a replacement confirmation. [Theory] [InlineData(false)] [InlineData(true)] public async Task ConfirmDialog_ReplacedTargetIgnoresOldCompletionUi(bool sharedCaption) { JSInterop.Setup("otDialog.activate", _ => true).SetResult(true); var enteredA = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var enteredB = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var doneA = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var doneB = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var callsA = 0; var callsB = 0; var cut = Render(p => p.Add(x => x.Visible, true).Add(x => x.Title, sharedCaption ? "Delete selected row" : "Delete A").Add(x => x.Message, sharedCaption ? "Confirm selected target" : "Target A").Add(x => x.Target, "A") .Add(x => x.OnConfirm, async () => { callsA++; enteredA.TrySetResult(true); await doneA.Task; })); var first = cut.Find(".modal-footer .btn-danger").TriggerEventAsync("onclick", new Microsoft.AspNetCore.Components.Web.MouseEventArgs()); await enteredA.Task; Task? second = null; try { cut.Render(p => p.Add(x => x.Title, sharedCaption ? "Delete selected row" : "Delete B").Add(x => x.Message, sharedCaption ? "Confirm selected target" : "Target B").Add(x => x.Target, "B") .Add(x => x.OnConfirm, async () => { callsB++; enteredB.TrySetResult(true); await doneB.Task; })); cut.Find(".modal-footer .btn-danger").HasAttribute("disabled").ShouldBeFalse(); second = cut.Find(".modal-footer .btn-danger").TriggerEventAsync("onclick", new Microsoft.AspNetCore.Components.Web.MouseEventArgs()); await enteredB.Task; await cut.InvokeAsync(() => doneA.TrySetResult(true)); await first; cut.Find("[role=dialog]").TextContent.ShouldContain(sharedCaption ? "Delete selected row" : "Delete B"); cut.Find(".modal-footer .btn-danger").HasAttribute("disabled").ShouldBeTrue(); callsA.ShouldBe(1); callsB.ShouldBe(1); } finally { await cut.InvokeAsync(() => { doneA.TrySetResult(true); doneB.TrySetResult(true); }); await first; if (second is not null) await second; } cut.FindAll("[role=dialog]").ShouldBeEmpty(); } /// A queued control from an earlier render never invokes a newer target's callback. [Fact] public async Task ConfirmDialog_StaleRenderedActionCannotInvokeReplacementCallback() { JSInterop.Setup("otDialog.activate", _ => true).SetResult(true); var callsA = 0; var callsB = 0; var cut = Render(p => p.Add(x => x.Visible, true).Add(x => x.Title, "Delete A").Add(x => x.OnConfirm, () => callsA++)); var staleFooter = cut.FindComponent().Instance.Footer!; cut.Render(p => p.Add(x => x.Title, "Delete B").Add(x => x.OnConfirm, () => callsB++)); var stale = Render>(p => p.Add(x => x.Value, new object()).Add(x => x.ChildContent, staleFooter)); await stale.Find(".btn-danger").TriggerEventAsync("onclick", new Microsoft.AspNetCore.Components.Web.MouseEventArgs()); callsA.ShouldBe(0); callsB.ShouldBe(0); cut.Find("[role=dialog]").TextContent.ShouldContain("Delete B"); } /// A dispatched operation continues after disposal without invoking stale visibility UI. [Fact] public async Task ConfirmDialog_DisposalDoesNotCancelOperationOrCloseAnotherUi() { JSInterop.Setup("otDialog.activate", _ => true).SetResult(true); var entered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var done = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var calls = 0; var closes = 0; var cut = Render(p => p.Add(x => x.Visible, true).Add(x => x.OnConfirm, async () => { calls++; entered.TrySetResult(true); await done.Task; }).Add(x => x.VisibleChanged, _ => closes++)); var confirming = cut.Find(".modal-footer .btn-danger").TriggerEventAsync("onclick", new Microsoft.AspNetCore.Components.Web.MouseEventArgs()); await entered.Task; await DisposeComponentsAsync(); done.TrySetResult(true); await confirming; calls.ShouldBe(1); closes.ShouldBe(0); } }