Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Pages/ModalAccessibilityTests.cs
T

214 lines
12 KiB
C#

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;
/// <summary>Confirmation names and cancellation preserve the existing destructive callback contract.</summary>
[Trait("Category", "Unit")]
public sealed class ModalAccessibilityTests : BunitContext
{
/// <summary>Only the defined local focus-adapter calls are configured.</summary>
public ModalAccessibilityTests()
{
JSInterop.SetupVoid("otDialog.update", _ => true).SetVoidResult();
JSInterop.SetupVoid("otDialog.deactivate", _ => true).SetVoidResult();
}
/// <summary>Cancel closes a named confirmation without invoking the destructive action.</summary>
[Fact]
public void ConfirmDialog_IsNamedAndCancelDoesNotInvokeDelete()
{
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
var deletes = 0;
var cut = Render<RawConfirmDialog>(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();
}
/// <summary>Closing while confirmation is in flight cannot imply the operation was cancelled.</summary>
[Fact]
public async Task ConfirmDialog_BusyClosePreservesInFlightOperation()
{
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
var entered = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var pending = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var calls = 0;
var cut = Render<RawConfirmDialog>(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();
}
/// <summary>A delayed old activation is cleaned before a later presentation is activated.</summary>
[Fact]
public void Frame_HideDuringDeferredActivation_ReopenActivatesFreshPresentation()
{
var activation = JSInterop.Setup<bool>("otDialog.activate", _ => true);
var cut = Render<ModalFrame>(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));
}
/// <summary>Disposal waits for a pending activation and cleans any late native state.</summary>
[Fact]
public async Task Frame_DisposeDuringDeferredActivation_CleansLateActivation()
{
var activation = JSInterop.Setup<bool>("otDialog.activate", _ => true);
var cut = Render<ModalFrame>(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));
}
/// <summary>A new target queued during activation opens only after the old native generation is cleaned.</summary>
[Fact]
public void Frame_TargetSwitchDuringDeferredActivation_IsSerialized()
{
var activation = JSInterop.Setup<bool>("otDialog.activate", _ => true);
var cut = Render<ModalFrame>(p => p.Add(x => x.Visible, true).Add(x => x.Target, "A"));
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); }
cut.WaitForAssertion(() => 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);
}
/// <summary>A stale Escape cannot close a different reopened target.</summary>
[Fact]
public async Task Frame_StaleNativeCloseCannotCancelReopenedPresentation()
{
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
var closes = 0;
var cut = Render<ModalFrame>(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);
}
/// <summary>Busy-only updates preserve the presentation and never move focus back to its start.</summary>
[Fact]
public void Frame_BusyOnlyRenderUpdatesWithoutReactivation()
{
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
var cut = Render<ModalFrame>(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);
}
/// <summary>An old operation cannot close or clear the busy state of a replacement confirmation.</summary>
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task ConfirmDialog_ReplacedTargetIgnoresOldCompletionUi(bool sharedCaption)
{
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
var enteredA = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var enteredB = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var doneA = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var doneB = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var callsA = 0; var callsB = 0;
var cut = Render<RawConfirmDialog>(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();
}
/// <summary>A queued control from an earlier render never invokes a newer target's callback.</summary>
[Fact]
public async Task ConfirmDialog_StaleRenderedActionCannotInvokeReplacementCallback()
{
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
var callsA = 0; var callsB = 0;
var cut = Render<RawConfirmDialog>(p => p.Add(x => x.Visible, true).Add(x => x.Title, "Delete A").Add(x => x.OnConfirm, () => callsA++));
var staleFooter = cut.FindComponent<ModalFrame>().Instance.Footer!;
cut.Render(p => p.Add(x => x.Title, "Delete B").Add(x => x.OnConfirm, () => callsB++));
var stale = Render<CascadingValue<object>>(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");
}
/// <summary>A dispatched operation continues after disposal without invoking stale visibility UI.</summary>
[Fact]
public async Task ConfirmDialog_DisposalDoesNotCancelOperationOrCloseAnotherUi()
{
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
var entered = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var done = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var calls = 0; var closes = 0;
var cut = Render<RawConfirmDialog>(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);
}
}