Add cross-platform OPC UA client stack: shared library, CLI tool, and Avalonia UI
Implements Client.Shared (IOpcUaClientService with connection lifecycle, failover, browse, read/write, subscriptions, alarms, history, redundancy), Client.CLI (8 CliFx commands mirroring tools/opcuacli-dotnet), and Client.UI (Avalonia desktop app with tree browser, read/write, subscriptions, alarms, and history tabs). All three target .NET 10 and are covered by 249 unit tests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
141
tests/ZB.MOM.WW.LmxOpcUa.Client.UI.Tests/AlarmsViewModelTests.cs
Normal file
141
tests/ZB.MOM.WW.LmxOpcUa.Client.UI.Tests/AlarmsViewModelTests.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.Shared.Models;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.Services;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.Tests.Fakes;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.ViewModels;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Client.UI.Tests;
|
||||
|
||||
public class AlarmsViewModelTests
|
||||
{
|
||||
private readonly FakeOpcUaClientService _service;
|
||||
private readonly AlarmsViewModel _vm;
|
||||
|
||||
public AlarmsViewModelTests()
|
||||
{
|
||||
_service = new FakeOpcUaClientService();
|
||||
var dispatcher = new SynchronousUiDispatcher();
|
||||
_vm = new AlarmsViewModel(_service, dispatcher);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SubscribeCommand_CannotExecute_WhenDisconnected()
|
||||
{
|
||||
_vm.IsConnected = false;
|
||||
_vm.SubscribeCommand.CanExecute(null).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SubscribeCommand_CannotExecute_WhenAlreadySubscribed()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.IsSubscribed = true;
|
||||
_vm.SubscribeCommand.CanExecute(null).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SubscribeCommand_CanExecute_WhenConnectedAndNotSubscribed()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.IsSubscribed = false;
|
||||
_vm.SubscribeCommand.CanExecute(null).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SubscribeCommand_SetsIsSubscribed()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
|
||||
await _vm.SubscribeCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.IsSubscribed.ShouldBeTrue();
|
||||
_service.SubscribeAlarmsCallCount.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnsubscribeCommand_CannotExecute_WhenNotSubscribed()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.IsSubscribed = false;
|
||||
_vm.UnsubscribeCommand.CanExecute(null).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UnsubscribeCommand_ClearsIsSubscribed()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
await _vm.SubscribeCommand.ExecuteAsync(null);
|
||||
|
||||
await _vm.UnsubscribeCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.IsSubscribed.ShouldBeFalse();
|
||||
_service.UnsubscribeAlarmsCallCount.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RefreshCommand_CallsService()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.IsSubscribed = true;
|
||||
|
||||
await _vm.RefreshCommand.ExecuteAsync(null);
|
||||
|
||||
_service.RequestConditionRefreshCallCount.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RefreshCommand_CannotExecute_WhenNotSubscribed()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.IsSubscribed = false;
|
||||
_vm.RefreshCommand.CanExecute(null).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AlarmEvent_AddsToCollection()
|
||||
{
|
||||
var alarm = new AlarmEventArgs(
|
||||
"Source1", "HighAlarm", 500, "Temperature high",
|
||||
true, true, false, DateTime.UtcNow);
|
||||
|
||||
_service.RaiseAlarmEvent(alarm);
|
||||
|
||||
_vm.AlarmEvents.Count.ShouldBe(1);
|
||||
_vm.AlarmEvents[0].SourceName.ShouldBe("Source1");
|
||||
_vm.AlarmEvents[0].ConditionName.ShouldBe("HighAlarm");
|
||||
_vm.AlarmEvents[0].Severity.ShouldBe((ushort)500);
|
||||
_vm.AlarmEvents[0].Message.ShouldBe("Temperature high");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clear_ResetsState()
|
||||
{
|
||||
_vm.IsSubscribed = true;
|
||||
_vm.AlarmEvents.Add(new AlarmEventViewModel("Src", "Cond", 100, "Msg", true, true, false, DateTime.UtcNow));
|
||||
|
||||
_vm.Clear();
|
||||
|
||||
_vm.AlarmEvents.ShouldBeEmpty();
|
||||
_vm.IsSubscribed.ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Teardown_UnhooksEventHandler()
|
||||
{
|
||||
_vm.Teardown();
|
||||
|
||||
var alarm = new AlarmEventArgs(
|
||||
"Source1", "HighAlarm", 500, "Test",
|
||||
true, true, false, DateTime.UtcNow);
|
||||
_service.RaiseAlarmEvent(alarm);
|
||||
|
||||
_vm.AlarmEvents.ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultInterval_Is1000()
|
||||
{
|
||||
_vm.Interval.ShouldBe(1000);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.Services;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.Tests.Fakes;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.ViewModels;
|
||||
using BrowseResult = ZB.MOM.WW.LmxOpcUa.Client.Shared.Models.BrowseResult;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Client.UI.Tests;
|
||||
|
||||
public class BrowseTreeViewModelTests
|
||||
{
|
||||
private readonly FakeOpcUaClientService _service;
|
||||
private readonly SynchronousUiDispatcher _dispatcher;
|
||||
private readonly BrowseTreeViewModel _vm;
|
||||
|
||||
public BrowseTreeViewModelTests()
|
||||
{
|
||||
_service = new FakeOpcUaClientService
|
||||
{
|
||||
BrowseResults = new[]
|
||||
{
|
||||
new BrowseResult("ns=2;s=Node1", "Node1", "Object", true),
|
||||
new BrowseResult("ns=2;s=Node2", "Node2", "Variable", false)
|
||||
}
|
||||
};
|
||||
_dispatcher = new SynchronousUiDispatcher();
|
||||
_vm = new BrowseTreeViewModel(_service, _dispatcher);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LoadRootsAsync_PopulatesRootNodes()
|
||||
{
|
||||
await _vm.LoadRootsAsync();
|
||||
|
||||
_vm.RootNodes.Count.ShouldBe(2);
|
||||
_vm.RootNodes[0].DisplayName.ShouldBe("Node1");
|
||||
_vm.RootNodes[1].DisplayName.ShouldBe("Node2");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LoadRootsAsync_BrowsesWithNullParent()
|
||||
{
|
||||
await _vm.LoadRootsAsync();
|
||||
|
||||
_service.BrowseCallCount.ShouldBe(1);
|
||||
_service.LastBrowseParentNodeId.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clear_RemovesAllRootNodes()
|
||||
{
|
||||
_vm.RootNodes.Add(new TreeNodeViewModel("ns=2;s=X", "X", "Object", false, _service, _dispatcher));
|
||||
_vm.Clear();
|
||||
|
||||
_vm.RootNodes.ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LoadRootsAsync_NodeWithChildren_HasPlaceholder()
|
||||
{
|
||||
await _vm.LoadRootsAsync();
|
||||
|
||||
var nodeWithChildren = _vm.RootNodes[0];
|
||||
nodeWithChildren.HasChildren.ShouldBeTrue();
|
||||
nodeWithChildren.Children.Count.ShouldBe(1);
|
||||
nodeWithChildren.Children[0].IsPlaceholder.ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LoadRootsAsync_NodeWithoutChildren_HasNoPlaceholder()
|
||||
{
|
||||
await _vm.LoadRootsAsync();
|
||||
|
||||
var leafNode = _vm.RootNodes[1];
|
||||
leafNode.HasChildren.ShouldBeFalse();
|
||||
leafNode.Children.ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TreeNode_FirstExpand_TriggersChildBrowse()
|
||||
{
|
||||
_service.BrowseResults = new[]
|
||||
{
|
||||
new BrowseResult("ns=2;s=Parent", "Parent", "Object", true)
|
||||
};
|
||||
|
||||
await _vm.LoadRootsAsync();
|
||||
|
||||
// Reset browse results for child browse
|
||||
_service.BrowseResults = new[]
|
||||
{
|
||||
new BrowseResult("ns=2;s=Child1", "Child1", "Variable", false)
|
||||
};
|
||||
|
||||
var parent = _vm.RootNodes[0];
|
||||
var initialBrowseCount = _service.BrowseCallCount;
|
||||
|
||||
parent.IsExpanded = true;
|
||||
|
||||
// Allow async operation to complete
|
||||
await Task.Delay(50);
|
||||
|
||||
_service.BrowseCallCount.ShouldBe(initialBrowseCount + 1);
|
||||
parent.Children.Count.ShouldBe(1);
|
||||
parent.Children[0].DisplayName.ShouldBe("Child1");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TreeNode_SecondExpand_DoesNotBrowseAgain()
|
||||
{
|
||||
_service.BrowseResults = new[]
|
||||
{
|
||||
new BrowseResult("ns=2;s=Parent", "Parent", "Object", true)
|
||||
};
|
||||
|
||||
await _vm.LoadRootsAsync();
|
||||
|
||||
_service.BrowseResults = new[]
|
||||
{
|
||||
new BrowseResult("ns=2;s=Child1", "Child1", "Variable", false)
|
||||
};
|
||||
|
||||
var parent = _vm.RootNodes[0];
|
||||
parent.IsExpanded = true;
|
||||
await Task.Delay(50);
|
||||
|
||||
var browseCountAfterFirst = _service.BrowseCallCount;
|
||||
|
||||
parent.IsExpanded = false;
|
||||
parent.IsExpanded = true;
|
||||
await Task.Delay(50);
|
||||
|
||||
_service.BrowseCallCount.ShouldBe(browseCountAfterFirst);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TreeNode_IsLoading_TransitionsDuringBrowse()
|
||||
{
|
||||
_service.BrowseResults = new[]
|
||||
{
|
||||
new BrowseResult("ns=2;s=Parent", "Parent", "Object", true)
|
||||
};
|
||||
|
||||
await _vm.LoadRootsAsync();
|
||||
|
||||
_service.BrowseResults = Array.Empty<BrowseResult>();
|
||||
|
||||
var parent = _vm.RootNodes[0];
|
||||
parent.IsExpanded = true;
|
||||
await Task.Delay(50);
|
||||
|
||||
// After completion, IsLoading should be false
|
||||
parent.IsLoading.ShouldBeFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
using Opc.Ua;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.Shared;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.Shared.Models;
|
||||
using BrowseResult = ZB.MOM.WW.LmxOpcUa.Client.Shared.Models.BrowseResult;
|
||||
using ConnectionState = ZB.MOM.WW.LmxOpcUa.Client.Shared.Models.ConnectionState;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Client.UI.Tests.Fakes;
|
||||
|
||||
/// <summary>
|
||||
/// Fake IOpcUaClientService for unit testing.
|
||||
/// </summary>
|
||||
public sealed class FakeOpcUaClientService : IOpcUaClientService
|
||||
{
|
||||
// Configurable responses
|
||||
public ConnectionInfo? ConnectResult { get; set; }
|
||||
public Exception? ConnectException { get; set; }
|
||||
public IReadOnlyList<BrowseResult> BrowseResults { get; set; } = Array.Empty<BrowseResult>();
|
||||
public Exception? BrowseException { get; set; }
|
||||
public DataValue ReadResult { get; set; } = new DataValue(new Variant(42), StatusCodes.Good, DateTime.UtcNow, DateTime.UtcNow);
|
||||
public Exception? ReadException { get; set; }
|
||||
public StatusCode WriteResult { get; set; } = StatusCodes.Good;
|
||||
public Exception? WriteException { get; set; }
|
||||
public RedundancyInfo? RedundancyResult { get; set; }
|
||||
public Exception? RedundancyException { get; set; }
|
||||
public IReadOnlyList<DataValue> HistoryRawResult { get; set; } = Array.Empty<DataValue>();
|
||||
public IReadOnlyList<DataValue> HistoryAggregateResult { get; set; } = Array.Empty<DataValue>();
|
||||
public Exception? HistoryException { get; set; }
|
||||
|
||||
// Call tracking
|
||||
public int ConnectCallCount { get; private set; }
|
||||
public int DisconnectCallCount { get; private set; }
|
||||
public int ReadCallCount { get; private set; }
|
||||
public int WriteCallCount { get; private set; }
|
||||
public int BrowseCallCount { get; private set; }
|
||||
public int SubscribeCallCount { get; private set; }
|
||||
public int UnsubscribeCallCount { get; private set; }
|
||||
public int SubscribeAlarmsCallCount { get; private set; }
|
||||
public int UnsubscribeAlarmsCallCount { get; private set; }
|
||||
public int RequestConditionRefreshCallCount { get; private set; }
|
||||
public int HistoryReadRawCallCount { get; private set; }
|
||||
public int HistoryReadAggregateCallCount { get; private set; }
|
||||
public int GetRedundancyInfoCallCount { get; private set; }
|
||||
|
||||
public NodeId? LastReadNodeId { get; private set; }
|
||||
public NodeId? LastWriteNodeId { get; private set; }
|
||||
public object? LastWriteValue { get; private set; }
|
||||
public NodeId? LastBrowseParentNodeId { get; private set; }
|
||||
public NodeId? LastSubscribeNodeId { get; private set; }
|
||||
public int LastSubscribeIntervalMs { get; private set; }
|
||||
public NodeId? LastUnsubscribeNodeId { get; private set; }
|
||||
public AggregateType? LastAggregateType { get; private set; }
|
||||
|
||||
public bool IsConnected { get; set; }
|
||||
public ConnectionInfo? CurrentConnectionInfo { get; set; }
|
||||
|
||||
public event EventHandler<DataChangedEventArgs>? DataChanged;
|
||||
public event EventHandler<AlarmEventArgs>? AlarmEvent;
|
||||
public event EventHandler<ConnectionStateChangedEventArgs>? ConnectionStateChanged;
|
||||
|
||||
public Task<ConnectionInfo> ConnectAsync(ConnectionSettings settings, CancellationToken ct = default)
|
||||
{
|
||||
ConnectCallCount++;
|
||||
if (ConnectException != null) throw ConnectException;
|
||||
IsConnected = true;
|
||||
CurrentConnectionInfo = ConnectResult;
|
||||
return Task.FromResult(ConnectResult!);
|
||||
}
|
||||
|
||||
public Task DisconnectAsync(CancellationToken ct = default)
|
||||
{
|
||||
DisconnectCallCount++;
|
||||
IsConnected = false;
|
||||
CurrentConnectionInfo = null;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<DataValue> ReadValueAsync(NodeId nodeId, CancellationToken ct = default)
|
||||
{
|
||||
ReadCallCount++;
|
||||
LastReadNodeId = nodeId;
|
||||
if (ReadException != null) throw ReadException;
|
||||
return Task.FromResult(ReadResult);
|
||||
}
|
||||
|
||||
public Task<StatusCode> WriteValueAsync(NodeId nodeId, object value, CancellationToken ct = default)
|
||||
{
|
||||
WriteCallCount++;
|
||||
LastWriteNodeId = nodeId;
|
||||
LastWriteValue = value;
|
||||
if (WriteException != null) throw WriteException;
|
||||
return Task.FromResult(WriteResult);
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<BrowseResult>> BrowseAsync(NodeId? parentNodeId = null, CancellationToken ct = default)
|
||||
{
|
||||
BrowseCallCount++;
|
||||
LastBrowseParentNodeId = parentNodeId;
|
||||
if (BrowseException != null) throw BrowseException;
|
||||
return Task.FromResult(BrowseResults);
|
||||
}
|
||||
|
||||
public Task SubscribeAsync(NodeId nodeId, int intervalMs = 1000, CancellationToken ct = default)
|
||||
{
|
||||
SubscribeCallCount++;
|
||||
LastSubscribeNodeId = nodeId;
|
||||
LastSubscribeIntervalMs = intervalMs;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task UnsubscribeAsync(NodeId nodeId, CancellationToken ct = default)
|
||||
{
|
||||
UnsubscribeCallCount++;
|
||||
LastUnsubscribeNodeId = nodeId;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task SubscribeAlarmsAsync(NodeId? sourceNodeId = null, int intervalMs = 1000, CancellationToken ct = default)
|
||||
{
|
||||
SubscribeAlarmsCallCount++;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task UnsubscribeAlarmsAsync(CancellationToken ct = default)
|
||||
{
|
||||
UnsubscribeAlarmsCallCount++;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task RequestConditionRefreshAsync(CancellationToken ct = default)
|
||||
{
|
||||
RequestConditionRefreshCallCount++;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<DataValue>> HistoryReadRawAsync(NodeId nodeId, DateTime startTime, DateTime endTime, int maxValues = 1000, CancellationToken ct = default)
|
||||
{
|
||||
HistoryReadRawCallCount++;
|
||||
if (HistoryException != null) throw HistoryException;
|
||||
return Task.FromResult(HistoryRawResult);
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<DataValue>> HistoryReadAggregateAsync(NodeId nodeId, DateTime startTime, DateTime endTime, AggregateType aggregate, double intervalMs = 3600000, CancellationToken ct = default)
|
||||
{
|
||||
HistoryReadAggregateCallCount++;
|
||||
LastAggregateType = aggregate;
|
||||
if (HistoryException != null) throw HistoryException;
|
||||
return Task.FromResult(HistoryAggregateResult);
|
||||
}
|
||||
|
||||
public Task<RedundancyInfo> GetRedundancyInfoAsync(CancellationToken ct = default)
|
||||
{
|
||||
GetRedundancyInfoCallCount++;
|
||||
if (RedundancyException != null) throw RedundancyException;
|
||||
return Task.FromResult(RedundancyResult!);
|
||||
}
|
||||
|
||||
// Methods to raise events from tests
|
||||
public void RaiseDataChanged(DataChangedEventArgs args) => DataChanged?.Invoke(this, args);
|
||||
public void RaiseAlarmEvent(AlarmEventArgs args) => AlarmEvent?.Invoke(this, args);
|
||||
public void RaiseConnectionStateChanged(ConnectionStateChangedEventArgs args) => ConnectionStateChanged?.Invoke(this, args);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// No-op for testing
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.Shared;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Client.UI.Tests.Fakes;
|
||||
|
||||
/// <summary>
|
||||
/// Fake factory that returns a preconfigured FakeOpcUaClientService.
|
||||
/// </summary>
|
||||
public sealed class FakeOpcUaClientServiceFactory : IOpcUaClientServiceFactory
|
||||
{
|
||||
private readonly FakeOpcUaClientService _service;
|
||||
|
||||
public FakeOpcUaClientServiceFactory(FakeOpcUaClientService service)
|
||||
{
|
||||
_service = service;
|
||||
}
|
||||
|
||||
public IOpcUaClientService Create() => _service;
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
using Opc.Ua;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.Shared.Models;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.Services;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.Tests.Fakes;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.ViewModels;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Client.UI.Tests;
|
||||
|
||||
public class HistoryViewModelTests
|
||||
{
|
||||
private readonly FakeOpcUaClientService _service;
|
||||
private readonly HistoryViewModel _vm;
|
||||
|
||||
public HistoryViewModelTests()
|
||||
{
|
||||
_service = new FakeOpcUaClientService
|
||||
{
|
||||
HistoryRawResult = new[]
|
||||
{
|
||||
new DataValue(new Variant(10), StatusCodes.Good, DateTime.UtcNow, DateTime.UtcNow),
|
||||
new DataValue(new Variant(20), StatusCodes.Good, DateTime.UtcNow, DateTime.UtcNow)
|
||||
},
|
||||
HistoryAggregateResult = new[]
|
||||
{
|
||||
new DataValue(new Variant(15.0), StatusCodes.Good, DateTime.UtcNow, DateTime.UtcNow)
|
||||
}
|
||||
};
|
||||
var dispatcher = new SynchronousUiDispatcher();
|
||||
_vm = new HistoryViewModel(_service, dispatcher);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadHistoryCommand_CannotExecute_WhenDisconnected()
|
||||
{
|
||||
_vm.IsConnected = false;
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
_vm.ReadHistoryCommand.CanExecute(null).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadHistoryCommand_CannotExecute_WhenNoNodeSelected()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.SelectedNodeId = null;
|
||||
_vm.ReadHistoryCommand.CanExecute(null).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadHistoryCommand_CanExecute_WhenConnectedAndNodeSelected()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
_vm.ReadHistoryCommand.CanExecute(null).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReadHistoryCommand_Raw_PopulatesResults()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
_vm.SelectedAggregateType = null; // Raw
|
||||
|
||||
await _vm.ReadHistoryCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.Results.Count.ShouldBe(2);
|
||||
_vm.Results[0].Value.ShouldBe("10");
|
||||
_vm.Results[1].Value.ShouldBe("20");
|
||||
_service.HistoryReadRawCallCount.ShouldBe(1);
|
||||
_service.HistoryReadAggregateCallCount.ShouldBe(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReadHistoryCommand_Aggregate_PopulatesResults()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
_vm.SelectedAggregateType = AggregateType.Average;
|
||||
|
||||
await _vm.ReadHistoryCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.Results.Count.ShouldBe(1);
|
||||
_vm.Results[0].Value.ShouldBe("15");
|
||||
_service.HistoryReadAggregateCallCount.ShouldBe(1);
|
||||
_service.LastAggregateType.ShouldBe(AggregateType.Average);
|
||||
_service.HistoryReadRawCallCount.ShouldBe(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReadHistoryCommand_ClearsResultsBefore()
|
||||
{
|
||||
_vm.Results.Add(new HistoryValueViewModel("old", "Good", "t1", "t2"));
|
||||
_vm.IsConnected = true;
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
|
||||
await _vm.ReadHistoryCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.Results.ShouldNotContain(r => r.Value == "old");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReadHistoryCommand_IsLoading_FalseAfterComplete()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
|
||||
await _vm.ReadHistoryCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.IsLoading.ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultValues_AreCorrect()
|
||||
{
|
||||
_vm.MaxValues.ShouldBe(1000);
|
||||
_vm.IntervalMs.ShouldBe(3600000);
|
||||
_vm.SelectedAggregateType.ShouldBeNull();
|
||||
_vm.IsAggregateRead.ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsAggregateRead_TrueWhenAggregateSelected()
|
||||
{
|
||||
_vm.SelectedAggregateType = AggregateType.Maximum;
|
||||
_vm.IsAggregateRead.ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AggregateTypes_ContainsNullForRaw()
|
||||
{
|
||||
_vm.AggregateTypes.ShouldContain((AggregateType?)null);
|
||||
_vm.AggregateTypes.Count.ShouldBe(7); // null + 6 enum values
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clear_ResetsState()
|
||||
{
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
_vm.Results.Add(new HistoryValueViewModel("v", "s", "t1", "t2"));
|
||||
|
||||
_vm.Clear();
|
||||
|
||||
_vm.Results.ShouldBeEmpty();
|
||||
_vm.SelectedNodeId.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReadHistoryCommand_Error_ShowsErrorInResults()
|
||||
{
|
||||
_service.HistoryException = new Exception("History not supported");
|
||||
_vm.IsConnected = true;
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
|
||||
await _vm.ReadHistoryCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.Results.Count.ShouldBe(1);
|
||||
_vm.Results[0].Value.ShouldContain("History not supported");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.Shared.Models;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.Services;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.Tests.Fakes;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.ViewModels;
|
||||
using BrowseResult = ZB.MOM.WW.LmxOpcUa.Client.Shared.Models.BrowseResult;
|
||||
using ConnectionState = ZB.MOM.WW.LmxOpcUa.Client.Shared.Models.ConnectionState;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Client.UI.Tests;
|
||||
|
||||
public class MainWindowViewModelTests
|
||||
{
|
||||
private readonly FakeOpcUaClientService _service;
|
||||
private readonly MainWindowViewModel _vm;
|
||||
|
||||
public MainWindowViewModelTests()
|
||||
{
|
||||
_service = new FakeOpcUaClientService
|
||||
{
|
||||
ConnectResult = new ConnectionInfo(
|
||||
"opc.tcp://localhost:4840",
|
||||
"TestServer",
|
||||
"None",
|
||||
"http://opcfoundation.org/UA/SecurityPolicy#None",
|
||||
"session-1",
|
||||
"TestSession"),
|
||||
BrowseResults = new[]
|
||||
{
|
||||
new BrowseResult("ns=2;s=Root", "Root", "Object", true)
|
||||
},
|
||||
RedundancyResult = new RedundancyInfo("None", 200, new[] { "urn:test" }, "urn:test")
|
||||
};
|
||||
|
||||
var factory = new FakeOpcUaClientServiceFactory(_service);
|
||||
var dispatcher = new SynchronousUiDispatcher();
|
||||
_vm = new MainWindowViewModel(factory, dispatcher);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultState_IsDisconnected()
|
||||
{
|
||||
_vm.ConnectionState.ShouldBe(ConnectionState.Disconnected);
|
||||
_vm.IsConnected.ShouldBeFalse();
|
||||
_vm.EndpointUrl.ShouldBe("opc.tcp://localhost:4840");
|
||||
_vm.StatusMessage.ShouldBe("Disconnected");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConnectCommand_CanExecute_WhenDisconnected()
|
||||
{
|
||||
_vm.ConnectCommand.CanExecute(null).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DisconnectCommand_CannotExecute_WhenDisconnected()
|
||||
{
|
||||
_vm.DisconnectCommand.CanExecute(null).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConnectCommand_TransitionsToConnected()
|
||||
{
|
||||
await _vm.ConnectCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.ConnectionState.ShouldBe(ConnectionState.Connected);
|
||||
_vm.IsConnected.ShouldBeTrue();
|
||||
_service.ConnectCallCount.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConnectCommand_LoadsRootNodes()
|
||||
{
|
||||
await _vm.ConnectCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.BrowseTree.RootNodes.Count.ShouldBe(1);
|
||||
_vm.BrowseTree.RootNodes[0].DisplayName.ShouldBe("Root");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConnectCommand_FetchesRedundancyInfo()
|
||||
{
|
||||
await _vm.ConnectCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.RedundancyInfo.ShouldNotBeNull();
|
||||
_vm.RedundancyInfo!.Mode.ShouldBe("None");
|
||||
_vm.RedundancyInfo.ServiceLevel.ShouldBe((byte)200);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConnectCommand_SetsSessionLabel()
|
||||
{
|
||||
await _vm.ConnectCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.SessionLabel.ShouldContain("TestServer");
|
||||
_vm.SessionLabel.ShouldContain("TestSession");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DisconnectCommand_TransitionsToDisconnected()
|
||||
{
|
||||
await _vm.ConnectCommand.ExecuteAsync(null);
|
||||
await _vm.DisconnectCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.ConnectionState.ShouldBe(ConnectionState.Disconnected);
|
||||
_vm.IsConnected.ShouldBeFalse();
|
||||
_service.DisconnectCallCount.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Disconnect_ClearsStateAndChildren()
|
||||
{
|
||||
await _vm.ConnectCommand.ExecuteAsync(null);
|
||||
await _vm.DisconnectCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.SessionLabel.ShouldBe(string.Empty);
|
||||
_vm.RedundancyInfo.ShouldBeNull();
|
||||
_vm.BrowseTree.RootNodes.ShouldBeEmpty();
|
||||
_vm.SubscriptionCount.ShouldBe(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConnectionStateChangedEvent_UpdatesState()
|
||||
{
|
||||
_service.RaiseConnectionStateChanged(
|
||||
new ConnectionStateChangedEventArgs(ConnectionState.Disconnected, ConnectionState.Reconnecting, "opc.tcp://localhost:4840"));
|
||||
|
||||
_vm.ConnectionState.ShouldBe(ConnectionState.Reconnecting);
|
||||
_vm.StatusMessage.ShouldBe("Reconnecting...");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SelectedTreeNode_PropagatesToChildViewModels()
|
||||
{
|
||||
await _vm.ConnectCommand.ExecuteAsync(null);
|
||||
|
||||
var node = _vm.BrowseTree.RootNodes[0];
|
||||
_vm.SelectedTreeNode = node;
|
||||
|
||||
_vm.ReadWrite.SelectedNodeId.ShouldBe(node.NodeId);
|
||||
_vm.History.SelectedNodeId.ShouldBe(node.NodeId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConnectCommand_PropagatesIsConnectedToChildViewModels()
|
||||
{
|
||||
await _vm.ConnectCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.ReadWrite.IsConnected.ShouldBeTrue();
|
||||
_vm.Subscriptions.IsConnected.ShouldBeTrue();
|
||||
_vm.Alarms.IsConnected.ShouldBeTrue();
|
||||
_vm.History.IsConnected.ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DisconnectCommand_PropagatesIsConnectedFalseToChildViewModels()
|
||||
{
|
||||
await _vm.ConnectCommand.ExecuteAsync(null);
|
||||
await _vm.DisconnectCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.ReadWrite.IsConnected.ShouldBeFalse();
|
||||
_vm.Subscriptions.IsConnected.ShouldBeFalse();
|
||||
_vm.Alarms.IsConnected.ShouldBeFalse();
|
||||
_vm.History.IsConnected.ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConnectFailure_RevertsToDisconnected()
|
||||
{
|
||||
_service.ConnectException = new Exception("Connection refused");
|
||||
|
||||
await _vm.ConnectCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.ConnectionState.ShouldBe(ConnectionState.Disconnected);
|
||||
_vm.StatusMessage.ShouldContain("Connection refused");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PropertyChanged_FiredForConnectionState()
|
||||
{
|
||||
var changed = new List<string>();
|
||||
_vm.PropertyChanged += (_, e) => changed.Add(e.PropertyName!);
|
||||
|
||||
_service.RaiseConnectionStateChanged(
|
||||
new ConnectionStateChangedEventArgs(ConnectionState.Disconnected, ConnectionState.Connected, "opc.tcp://localhost:4840"));
|
||||
|
||||
changed.ShouldContain(nameof(MainWindowViewModel.ConnectionState));
|
||||
changed.ShouldContain(nameof(MainWindowViewModel.IsConnected));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
using Opc.Ua;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.Services;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.Tests.Fakes;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.ViewModels;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Client.UI.Tests;
|
||||
|
||||
public class ReadWriteViewModelTests
|
||||
{
|
||||
private readonly FakeOpcUaClientService _service;
|
||||
private readonly ReadWriteViewModel _vm;
|
||||
|
||||
public ReadWriteViewModelTests()
|
||||
{
|
||||
_service = new FakeOpcUaClientService
|
||||
{
|
||||
ReadResult = new DataValue(new Variant("TestValue"), StatusCodes.Good, DateTime.UtcNow, DateTime.UtcNow)
|
||||
};
|
||||
var dispatcher = new SynchronousUiDispatcher();
|
||||
_vm = new ReadWriteViewModel(_service, dispatcher);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadCommand_CannotExecute_WhenDisconnected()
|
||||
{
|
||||
_vm.IsConnected = false;
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
_vm.ReadCommand.CanExecute(null).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadCommand_CannotExecute_WhenNoNodeSelected()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.SelectedNodeId = null;
|
||||
_vm.ReadCommand.CanExecute(null).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadCommand_CanExecute_WhenConnectedAndNodeSelected()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
_vm.ReadCommand.CanExecute(null).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReadCommand_UpdatesValueAndStatus()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
// auto-read fires on selection change, so reset count
|
||||
var countBefore = _service.ReadCallCount;
|
||||
|
||||
await _vm.ReadCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.CurrentValue.ShouldBe("TestValue");
|
||||
_vm.CurrentStatus.ShouldNotBeNull();
|
||||
_vm.SourceTimestamp.ShouldNotBeNull();
|
||||
_vm.ServerTimestamp.ShouldNotBeNull();
|
||||
(_service.ReadCallCount - countBefore).ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AutoRead_OnSelectionChange_WhenConnected()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
|
||||
// The auto-read fires asynchronously; give it a moment
|
||||
// In synchronous dispatcher it should fire immediately
|
||||
_service.ReadCallCount.ShouldBeGreaterThanOrEqualTo(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NullSelection_DoesNotCallService()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.SelectedNodeId = null;
|
||||
|
||||
_service.ReadCallCount.ShouldBe(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WriteCommand_UpdatesWriteStatus()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
_vm.WriteValue = "NewValue";
|
||||
|
||||
// Reset read count from auto-read
|
||||
var readCountBefore = _service.ReadCallCount;
|
||||
|
||||
await _vm.WriteCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.WriteStatus.ShouldNotBeNull();
|
||||
_service.WriteCallCount.ShouldBe(1);
|
||||
_service.LastWriteValue.ShouldBe("NewValue");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WriteCommand_CannotExecute_WhenDisconnected()
|
||||
{
|
||||
_vm.IsConnected = false;
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
_vm.WriteCommand.CanExecute(null).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReadCommand_Error_SetsErrorStatus()
|
||||
{
|
||||
_service.ReadException = new Exception("Read failed");
|
||||
_vm.IsConnected = true;
|
||||
|
||||
// We need to set SelectedNodeId and manually trigger read
|
||||
// because auto-read catches the exception too
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
|
||||
_vm.CurrentStatus.ShouldContain("Error");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clear_ResetsAllProperties()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
_vm.WriteValue = "test";
|
||||
_vm.WriteStatus = "Good";
|
||||
|
||||
_vm.Clear();
|
||||
|
||||
_vm.SelectedNodeId.ShouldBeNull();
|
||||
_vm.CurrentValue.ShouldBeNull();
|
||||
_vm.CurrentStatus.ShouldBeNull();
|
||||
_vm.SourceTimestamp.ShouldBeNull();
|
||||
_vm.ServerTimestamp.ShouldBeNull();
|
||||
_vm.WriteValue.ShouldBeNull();
|
||||
_vm.WriteStatus.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsNodeSelected_TracksSelectedNodeId()
|
||||
{
|
||||
_vm.IsNodeSelected.ShouldBeFalse();
|
||||
_vm.SelectedNodeId = "ns=2;s=SomeNode";
|
||||
_vm.IsNodeSelected.ShouldBeTrue();
|
||||
_vm.SelectedNodeId = null;
|
||||
_vm.IsNodeSelected.ShouldBeFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
using Opc.Ua;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.Shared.Models;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.Services;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.Tests.Fakes;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.UI.ViewModels;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Client.UI.Tests;
|
||||
|
||||
public class SubscriptionsViewModelTests
|
||||
{
|
||||
private readonly FakeOpcUaClientService _service;
|
||||
private readonly SubscriptionsViewModel _vm;
|
||||
|
||||
public SubscriptionsViewModelTests()
|
||||
{
|
||||
_service = new FakeOpcUaClientService();
|
||||
var dispatcher = new SynchronousUiDispatcher();
|
||||
_vm = new SubscriptionsViewModel(_service, dispatcher);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddSubscriptionCommand_CannotExecute_WhenDisconnected()
|
||||
{
|
||||
_vm.IsConnected = false;
|
||||
_vm.NewNodeIdText = "ns=2;s=SomeNode";
|
||||
_vm.AddSubscriptionCommand.CanExecute(null).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddSubscriptionCommand_CannotExecute_WhenNoNodeId()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.NewNodeIdText = null;
|
||||
_vm.AddSubscriptionCommand.CanExecute(null).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddSubscriptionCommand_AddsItem()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.NewNodeIdText = "ns=2;s=SomeNode";
|
||||
_vm.NewInterval = 500;
|
||||
|
||||
await _vm.AddSubscriptionCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.ActiveSubscriptions.Count.ShouldBe(1);
|
||||
_vm.ActiveSubscriptions[0].NodeId.ShouldBe("ns=2;s=SomeNode");
|
||||
_vm.ActiveSubscriptions[0].IntervalMs.ShouldBe(500);
|
||||
_vm.SubscriptionCount.ShouldBe(1);
|
||||
_service.SubscribeCallCount.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RemoveSubscriptionCommand_RemovesItem()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.NewNodeIdText = "ns=2;s=SomeNode";
|
||||
await _vm.AddSubscriptionCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.SelectedSubscription = _vm.ActiveSubscriptions[0];
|
||||
await _vm.RemoveSubscriptionCommand.ExecuteAsync(null);
|
||||
|
||||
_vm.ActiveSubscriptions.ShouldBeEmpty();
|
||||
_vm.SubscriptionCount.ShouldBe(0);
|
||||
_service.UnsubscribeCallCount.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveSubscriptionCommand_CannotExecute_WhenNoSelection()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.SelectedSubscription = null;
|
||||
_vm.RemoveSubscriptionCommand.CanExecute(null).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DataChanged_UpdatesMatchingRow()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.NewNodeIdText = "ns=2;s=SomeNode";
|
||||
await _vm.AddSubscriptionCommand.ExecuteAsync(null);
|
||||
|
||||
var dataValue = new DataValue(new Variant(42), StatusCodes.Good, DateTime.UtcNow);
|
||||
_service.RaiseDataChanged(new DataChangedEventArgs("ns=2;s=SomeNode", dataValue));
|
||||
|
||||
_vm.ActiveSubscriptions[0].Value.ShouldBe("42");
|
||||
_vm.ActiveSubscriptions[0].Status.ShouldNotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DataChanged_DoesNotUpdateNonMatchingRow()
|
||||
{
|
||||
_vm.IsConnected = true;
|
||||
_vm.NewNodeIdText = "ns=2;s=SomeNode";
|
||||
await _vm.AddSubscriptionCommand.ExecuteAsync(null);
|
||||
|
||||
var dataValue = new DataValue(new Variant(42), StatusCodes.Good, DateTime.UtcNow);
|
||||
_service.RaiseDataChanged(new DataChangedEventArgs("ns=2;s=OtherNode", dataValue));
|
||||
|
||||
_vm.ActiveSubscriptions[0].Value.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clear_RemovesAllSubscriptions()
|
||||
{
|
||||
_vm.ActiveSubscriptions.Add(new SubscriptionItemViewModel("ns=2;s=X", 1000));
|
||||
_vm.SubscriptionCount = 1;
|
||||
|
||||
_vm.Clear();
|
||||
|
||||
_vm.ActiveSubscriptions.ShouldBeEmpty();
|
||||
_vm.SubscriptionCount.ShouldBe(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Teardown_UnhooksEventHandler()
|
||||
{
|
||||
_vm.Teardown();
|
||||
|
||||
// After teardown, raising event should not update anything
|
||||
_vm.ActiveSubscriptions.Add(new SubscriptionItemViewModel("ns=2;s=X", 1000));
|
||||
var dataValue = new DataValue(new Variant(42), StatusCodes.Good, DateTime.UtcNow);
|
||||
_service.RaiseDataChanged(new DataChangedEventArgs("ns=2;s=X", dataValue));
|
||||
|
||||
_vm.ActiveSubscriptions[0].Value.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultInterval_Is1000()
|
||||
{
|
||||
_vm.NewInterval.ShouldBe(1000);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
<RootNamespace>ZB.MOM.WW.LmxOpcUa.Client.UI.Tests</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="xunit.v3" Version="1.1.0" />
|
||||
<PackageReference Include="Shouldly" Version="4.3.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\ZB.MOM.WW.LmxOpcUa.Client.UI\ZB.MOM.WW.LmxOpcUa.Client.UI.csproj" />
|
||||
<ProjectReference Include="..\..\src\ZB.MOM.WW.LmxOpcUa.Client.Shared\ZB.MOM.WW.LmxOpcUa.Client.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user