|
| 1 | + // Copyright (c) Argo Zhang ([email protected]). All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | +// Website: https://www.blazor.zone or https://argozhang.github.io/ |
| 4 | + |
| 5 | +namespace UnitTest.Services; |
| 6 | + |
| 7 | +public class SerialServiceTest : BootstrapBlazorTestBase |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public async Task GetPort_Ok() |
| 11 | + { |
| 12 | + Context.JSInterop.Setup<bool>("init", matcher => matcher.Arguments.Count == 1 && (matcher.Arguments[0]?.ToString()?.StartsWith("bb_serial_") ?? false)).SetResult(true); |
| 13 | + Context.JSInterop.Setup<bool>("getPort", matcher => matcher.Arguments.Count == 1 && (matcher.Arguments[0]?.ToString()?.StartsWith("bb_serial_") ?? false)).SetResult(true); |
| 14 | + Context.JSInterop.Setup<bool>("open", matcher => matcher.Arguments.Count == 4 && (matcher.Arguments[0]?.ToString()?.StartsWith("bb_serial_") ?? false)).SetResult(true); |
| 15 | + Context.JSInterop.Setup<bool>("close", matcher => matcher.Arguments.Count == 1 && (matcher.Arguments[0]?.ToString()?.StartsWith("bb_serial_") ?? false)).SetResult(true); |
| 16 | + Context.JSInterop.Setup<bool>("write", matcher => matcher.Arguments.Count == 2 && (matcher.Arguments[0]?.ToString()?.StartsWith("bb_serial_") ?? false)).SetResult(true); |
| 17 | + var serialService = Context.Services.GetRequiredService<ISerialService>(); |
| 18 | + |
| 19 | + var serialPort = await serialService.GetPort(); |
| 20 | + Assert.True(serialService.IsSupport); |
| 21 | + Assert.NotNull(serialPort); |
| 22 | + |
| 23 | + // DataReceive |
| 24 | + serialPort.DataReceive = d => |
| 25 | + { |
| 26 | + return Task.CompletedTask; |
| 27 | + }; |
| 28 | + |
| 29 | + var option = new SerialOptions() |
| 30 | + { |
| 31 | + BaudRate = 9600, |
| 32 | + BufferSize = 1024, |
| 33 | + DataBits = 8, |
| 34 | + FlowControlType = SerialFlowControlType.Hardware, |
| 35 | + ParityType = SerialParityType.Odd, |
| 36 | + StopBits = 1 |
| 37 | + }; |
| 38 | + await serialPort.Open(option); |
| 39 | + Assert.True(serialPort.IsOpen); |
| 40 | + Assert.Equal(9600, option.BaudRate); |
| 41 | + Assert.Equal(1024, option.BufferSize); |
| 42 | + Assert.Equal(8, option.DataBits); |
| 43 | + Assert.Equal(SerialFlowControlType.Hardware, option.FlowControlType); |
| 44 | + Assert.Equal(SerialParityType.Odd, option.ParityType); |
| 45 | + Assert.Equal(1, option.StopBits); |
| 46 | + |
| 47 | + await serialPort.Write([0x31, 0x32]); |
| 48 | + |
| 49 | + var mi = serialPort.GetType().GetMethod("DataReceiveCallback"); |
| 50 | + Assert.NotNull(mi); |
| 51 | + mi.Invoke(serialPort, ["12"u8.ToArray()]); |
| 52 | + |
| 53 | + await serialPort.Close(); |
| 54 | + Assert.False(serialPort.IsOpen); |
| 55 | + |
| 56 | + await serialPort.DisposeAsync(); |
| 57 | + } |
| 58 | +} |
0 commit comments