Skip to content

Commit fbf8488

Browse files
committed
test: 更新单元测试
1 parent 9ccdc0a commit fbf8488

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

src/BootstrapBlazor/Services/Bluetooth/BluetoothDevice.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public BluetoothDevice(JSModule module, string clientId, string[] args)
5454
/// <returns></returns>
5555
public async Task<bool> Connect(CancellationToken token = default)
5656
{
57-
if (Connected == false && _module != null)
57+
if (Connected == false)
5858
{
5959
ErrorMessage = null;
6060
Connected = await _module.InvokeAsync<bool>("connect", token, _clientId, _interop, nameof(OnError));
@@ -69,7 +69,7 @@ public async Task<bool> Connect(CancellationToken token = default)
6969
public async Task<bool> Disconnect(CancellationToken token = default)
7070
{
7171
var ret = false;
72-
if (Connected && _module != null)
72+
if (Connected)
7373
{
7474
ErrorMessage = null;
7575
ret = await _module.InvokeAsync<bool>("disconnect", token, _clientId, _interop, nameof(OnError));
@@ -88,7 +88,7 @@ public async Task<bool> Disconnect(CancellationToken token = default)
8888
public async Task<byte[]?> ReadValue(string serviceName, string characteristicName, CancellationToken token = default)
8989
{
9090
byte[]? ret = null;
91-
if (Connected && _module != null)
91+
if (Connected)
9292
{
9393
ErrorMessage = null;
9494
ret = await _module.InvokeAsync<byte[]?>("readValue", token, _clientId, serviceName, characteristicName, _interop, nameof(OnError));

src/BootstrapBlazor/Services/Bluetooth/DefaultBluetoothService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private async Task<JSModule> LoadModule()
4545
{
4646
var module = await _runtime.LoadModule("./_content/BootstrapBlazor/modules/bt.js");
4747

48-
IsSupport = await module.InvokeAsync<bool>("init", _interop, nameof(OnError));
48+
IsSupport = await module.InvokeAsync<bool>("init");
4949
return module;
5050
}
5151

@@ -77,6 +77,7 @@ public async Task<bool> GetAvailability(CancellationToken token = default)
7777
BluetoothDevice? device = null;
7878
if (IsSupport)
7979
{
80+
ErrorMessage = null;
8081
var parameters = await _module.InvokeAsync<string[]?>("requestDevice", token, _deviceId, optionalServices, _interop, nameof(OnError));
8182
if (parameters != null)
8283
{
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 BluetoothServiceTest : BootstrapBlazorTestBase
8+
{
9+
[Fact]
10+
public async Task RequestDevice_Ok()
11+
{
12+
Context.JSInterop.Setup<bool>("init", matcher => matcher.Arguments.Count == 0).SetResult(true);
13+
Context.JSInterop.Setup<bool>("getAvailability", matcher => matcher.Arguments.Count == 0).SetResult(true);
14+
Context.JSInterop.Setup<string[]?>("requestDevice", matcher => matcher.Arguments.Count == 4 && (matcher.Arguments[0]?.ToString()?.StartsWith("bb_bt_") ?? false)).SetResult(["test", "id_1234"]);
15+
Context.JSInterop.Setup<bool>("connect", matcher => matcher.Arguments.Count == 3 && (matcher.Arguments[0]?.ToString()?.StartsWith("bb_bt_") ?? false)).SetResult(true);
16+
Context.JSInterop.Setup<byte[]>("readValue", matcher => matcher.Arguments.Count == 5 && (matcher.Arguments[0]?.ToString()?.StartsWith("bb_bt_") ?? false)).SetResult([0x31]);
17+
Context.JSInterop.Setup<bool>("disconnect", matcher => matcher.Arguments.Count == 3 && (matcher.Arguments[0]?.ToString()?.StartsWith("bb_bt_") ?? false)).SetResult(true);
18+
19+
var bluetoothService = Context.Services.GetRequiredService<IBluetoothService>();
20+
var device = await bluetoothService.RequestDevice(["battery_service"]);
21+
Assert.NotNull(device);
22+
Assert.Equal("test", device.Name);
23+
Assert.Equal("id_1234", device.Id);
24+
Assert.Null(device.ErrorMessage);
25+
26+
await device.Connect();
27+
Assert.True(device.Connected);
28+
29+
var val = await device.ReadValue("battery_service", "battery_level");
30+
Assert.Equal([0x31], val);
31+
32+
var mi = device.GetType().GetMethod("OnError");
33+
Assert.NotNull(mi);
34+
mi.Invoke(device, ["test"]);
35+
Assert.Equal("test", device.ErrorMessage);
36+
37+
await device.Disconnect();
38+
Assert.False(device.Connected);
39+
40+
await device.DisposeAsync();
41+
}
42+
43+
[Fact]
44+
public async Task GetAvailability_Ok()
45+
{
46+
Context.JSInterop.Setup<bool>("init", matcher => matcher.Arguments.Count == 0).SetResult(true);
47+
Context.JSInterop.Setup<bool>("getAvailability", matcher => matcher.Arguments.Count == 0).SetResult(true);
48+
var bluetoothService = Context.Services.GetRequiredService<IBluetoothService>();
49+
50+
await bluetoothService.GetAvailability();
51+
Assert.True(bluetoothService.IsSupport);
52+
Assert.True(bluetoothService.IsAvailable);
53+
Assert.Null(bluetoothService.ErrorMessage);
54+
55+
var mi = bluetoothService.GetType().GetMethod("OnError");
56+
Assert.NotNull(mi);
57+
mi.Invoke(bluetoothService, ["test"]);
58+
Assert.Equal("test", bluetoothService.ErrorMessage);
59+
}
60+
}

0 commit comments

Comments
 (0)