|
| 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