Dreamine.FullKit.Wpf.Tests 1.0.0.0
Dreamine.FullKit.Wpf.Tests 기능을 검증하는 자동화 테스트 프로젝트입니다.
로딩중...
검색중...
일치하는것 없음
PlcWpfTests.cs
이 파일의 문서화 페이지로 가기
1using System.Globalization;
2using System.Windows.Data;
3using System.Windows.Media;
4using Dreamine.MVVM.ViewModels;
5using Dreamine.PLC.Abstractions.Connections;
6using Dreamine.PLC.Abstractions.Devices;
7using Dreamine.PLC.Wpf.Commands;
8using Dreamine.PLC.Wpf.Converters;
9using Dreamine.PLC.Wpf.Models;
10using Dreamine.PLC.Wpf.Services;
11
13
22public sealed class PlcWpfTests
23{
32 [Fact]
34 {
35 var count = 0;
36 var command = new RelayCommand(() => count++, () => false);
37
38 command.Execute(null);
39
40 Assert.False(command.CanExecute(null));
41 Assert.Equal(0, count);
42 }
43
60 [Fact]
62 {
63 var entered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
64 var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
65 var command = new AsyncRelayCommand(async () =>
66 {
67 entered.SetResult();
68 await release.Task;
69 });
70
71 command.Execute(null);
72 await entered.Task.WaitAsync(TimeSpan.FromSeconds(2));
73
74 Assert.False(command.CanExecute(null));
75
76 release.SetResult();
77 await Task.Delay(50);
78
79 Assert.True(command.CanExecute(null));
80 }
81
98 [Fact]
100 {
101 var failed = new TaskCompletionSource<Exception>(TaskCreationOptions.RunContinuationsAsynchronously);
102 var command = new AsyncRelayCommand(() => throw new InvalidOperationException("boom"));
103 command.ExecutionFailed += (_, ex) => failed.SetResult(ex);
104
105 command.Execute(null);
106 var exception = await failed.Task.WaitAsync(TimeSpan.FromSeconds(2));
107
108 Assert.IsType<InvalidOperationException>(exception);
109 Assert.Same(exception, command.LastException);
110 Assert.True(command.CanExecute(null));
111 }
112
121 [Fact]
123 {
124 object? received = null;
125 var command = new DelegateCommand(parameter => received = parameter, parameter => Equals(parameter, "ok"));
126
127 command.Execute("no");
128 command.Execute("ok");
129
130 Assert.Equal("ok", received);
131 }
132
141 [Fact]
143 {
144 var converter = new PlcConnectionStateBrushConverter();
145
146 Assert.Same(Brushes.ForestGreen, converter.Convert(PlcConnectionState.Connected, typeof(Brush), null!, CultureInfo.InvariantCulture));
147 Assert.Same(Brushes.DarkOrange, converter.Convert(PlcConnectionState.Connecting, typeof(Brush), null!, CultureInfo.InvariantCulture));
148 Assert.Same(Brushes.Firebrick, converter.Convert(PlcConnectionState.Faulted, typeof(Brush), null!, CultureInfo.InvariantCulture));
149 Assert.Same(Brushes.Gray, converter.Convert(PlcConnectionState.Disconnected, typeof(Brush), null!, CultureInfo.InvariantCulture));
150 Assert.Equal(Binding.DoNothing, converter.ConvertBack(Brushes.Gray, typeof(PlcConnectionState), null!, CultureInfo.InvariantCulture));
151 }
152
161 [Fact]
163 {
164 var item = new PlcOperationLogItem
165 {
166 Operation = "Read",
167 Address = "D100",
168 Values = "1,2",
169 IsSuccess = true,
170 Message = "OK"
171 };
172
173 Assert.Equal("Read", item.Operation);
174 Assert.Equal("D100", item.Address);
175 Assert.True(item.IsSuccess);
176 }
177
186 [Fact]
188 {
189 var address = new PlcAddressViewItem
190 {
191 DeviceType = PlcDeviceType.M,
192 Offset = 10,
193 BitOffset = 2,
194 DisplayName = "Motor ready"
195 };
196 var channel = new PlcChannelViewItem
197 {
198 Name = "Line 1",
199 State = PlcConnectionState.Connected,
200 Description = "Main PLC"
201 };
202 using var service = new TestAsyncDisposable(new PlcMonitorService());
203
204 Assert.Equal(new PlcAddress(PlcDeviceType.M, 10, 2), address.ToAddress());
205 Assert.Equal("Line 1", channel.Name);
206 Assert.Equal(PlcConnectionState.Connected, channel.State);
207 Assert.NotNull(service.Value.ViewModel);
208 }
209
218 private sealed class TestAsyncDisposable : IDisposable
219 {
236 public TestAsyncDisposable(PlcMonitorService value)
237 {
238 Value = value;
239 }
240
249 public PlcMonitorService Value { get; }
250
259 public void Dispose()
260 {
261 Value.DisposeAsync().AsTask().GetAwaiter().GetResult();
262 }
263 }
264}