Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Pages/CommonControlAccessibilityTests.cs
T
2026-11-13 21:52:00 -05:00

129 lines
6.1 KiB
C#

using AngleSharp.Dom;
using Bunit;
using Bunit.TestDoubles;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using ZB.MOM.WW.OtOpcUa.AdminUI.Browsing;
using ZB.MOM.WW.OtOpcUa.Security.Auth;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.Pickers;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages;
/// <summary>Common editor bodies retain native names and stable repeated-instance associations.</summary>
[Trait("Category", "Unit")]
public sealed class CommonControlAccessibilityTests : BunitContext
{
private readonly Action _allowBrowse;
private readonly RefusingBrowserProxy _browser;
public CommonControlAccessibilityTests()
{
var auth = this.AddAuthorization(); auth.SetAuthorized("fixture-reader");
_allowBrowse = () => auth.SetPolicies(AdminUiPolicies.DriverOperator);
var browser = DispatchProxy.Create<IBrowserSessionService, RefusingBrowserProxy>();
_browser = (RefusingBrowserProxy)(object)browser;
Services.AddSingleton(browser);
}
[Theory]
[InlineData(typeof(AbCipAddressPickerBody))]
[InlineData(typeof(TwinCATAddressPickerBody))]
[InlineData(typeof(FOCASAddressPickerBody))]
[InlineData(typeof(GalaxyAddressPickerBody))]
[InlineData(typeof(SqlAddressPickerBody))]
[InlineData(typeof(OpcUaClientAddressPickerBody))]
[InlineData(typeof(ModbusAddressPickerBody))]
[InlineData(typeof(AbLegacyAddressPickerBody))]
[InlineData(typeof(S7AddressPickerBody))]
[InlineData(typeof(DriverIdentitySection))]
[InlineData(typeof(DriverResilienceSection))]
public void CommonFields_TwoInstancesHaveResolvedNames(Type type)
{
AssertRelationships(RenderFields(type));
_browser.Calls.ShouldBe(0);
}
/// <summary>Operator-only drafts and conditional address spaces remain named without opening a session.</summary>
[Theory]
[InlineData(typeof(SqlAddressPickerBody), "WideRow")]
[InlineData(typeof(FOCASAddressPickerBody), "Alarm")]
[InlineData(typeof(FOCASAddressPickerBody), "Parameter")]
[InlineData(typeof(FOCASAddressPickerBody), "Macro")]
public void ConditionalPickerFields_RemainNamedWithoutBrowserOperations(Type type,string branch)
{
_allowBrowse();
var cut=RenderFields(type);
cut.Find("select").Change(branch);
AssertRelationships(cut);
if (type == typeof(SqlAddressPickerBody))
{
var label=cut.FindAll("label").First(e=>e.TextContent.Trim()=="Row selector");
cut.FindAll("select").Single(e=>e.Id==label.GetAttribute("for")).Change("Newest");
AssertRelationships(cut);
}
if (type == typeof(FOCASAddressPickerBody) && branch == "Alarm")
{
var label=cut.FindAll("label").First(e=>e.TextContent.Trim()=="Alarms");
var select=cut.FindAll("select").Single(e=>e.Id==label.GetAttribute("for"));
select.Change(select.QuerySelectorAll("option").First(e=>e.GetAttribute("value") is not ("*" or "OTHER")).GetAttribute("value"));
AssertRelationships(cut);
cut.FindAll("label").ShouldContain(e=>e.TextContent.Contains("Through number"));
}
_browser.Calls.ShouldBe(0);
}
private IRenderedComponent<CascadingValue<EditContext>> RenderFields(Type type)
{
var cut = Render<CascadingValue<EditContext>>(p => p.Add(x => x.Value,new EditContext(new object()))
.Add(x => x.ChildContent,(RenderFragment)(builder =>
{
for (int i=0;i<2;i++)
{
builder.OpenComponent(0,type);builder.SetKey(i);
if(type==typeof(DriverIdentitySection))
{
builder.AddAttribute(1,"Model",new DriverIdentitySection.DriverIdentityModel());
builder.AddAttribute(2,"IsNew",true);builder.AddAttribute(3,"ShowDriverType",true);
}
builder.CloseComponent();
}
})));
return cut;
}
private static void AssertRelationships(IRenderedComponent<CascadingValue<EditContext>> cut)
{
var ids=cut.FindAll("[id]").Select(e=>e.Id).ToArray();
ids.Distinct(StringComparer.Ordinal).Count().ShouldBe(ids.Length);
var inputs=cut.FindAll("input,select,textarea");inputs.ShouldNotBeEmpty();
foreach(var input in inputs)
{
var name=input.GetAttribute("aria-label") ?? string.Join(" ",cut.FindAll("label").Where(e=>e.GetAttribute("for")==input.Id&&!string.IsNullOrEmpty(input.Id)).Select(e=>e.TextContent));
if(string.IsNullOrWhiteSpace(name)&&input.GetAttribute("aria-labelledby") is {} labelledBy)
name=string.Join(" ",labelledBy.Split(' ',StringSplitOptions.RemoveEmptyEntries).Select(id=>Find(cut,id).TextContent));
if(string.IsNullOrWhiteSpace(name)) name=input.Closest("label")?.TextContent;
name.ShouldNotBeNullOrWhiteSpace(input.OuterHtml);
foreach(var id in (input.GetAttribute("aria-describedby")??"").Split(' ',StringSplitOptions.RemoveEmptyEntries)) Find(cut,id).ShouldNotBeNull();
}
foreach(var label in cut.FindAll("label[for]"))Find(cut,label.GetAttribute("for")!).ShouldNotBeNull();
}
private static IElement Find(IRenderedComponent<CascadingValue<EditContext>> cut,string id)=>cut.FindAll("[id]").Single(e=>e.Id==id);
/// <summary>No browse session or external connection may be opened by a field fixture.</summary>
public class RefusingBrowserProxy : DispatchProxy
{
public int Calls { get; private set; }
protected override object? Invoke(MethodInfo? method, object?[]? args)
{
if (method?.Name == nameof(IBrowserSessionService.CanBrowse)) return true; // Static capability only; no session/connection.
Calls++;
throw new NotSupportedException("Field fixture refuses browser operations: " + method?.Name);
}
}
}