|
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 | // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone |
5 | 5 |
|
| 6 | +using System; |
6 | 7 | using System.Globalization; |
7 | 8 |
|
8 | 9 | namespace UnitTest.Services; |
@@ -115,6 +116,11 @@ public async Task ReadValue_null() |
115 | 116 | var characteristic = await service.GetCharacteristic("battery_level"); |
116 | 117 | Assert.NotNull(characteristic); |
117 | 118 |
|
| 119 | + mi = characteristic.GetType().GetMethod("OnError"); |
| 120 | + Assert.NotNull(mi); |
| 121 | + mi.Invoke(characteristic, ["test"]); |
| 122 | + Assert.Equal("test", characteristic.ErrorMessage); |
| 123 | + |
118 | 124 | v = await characteristic.ReadValue(); |
119 | 125 | Assert.Null(v); |
120 | 126 | } |
@@ -312,13 +318,43 @@ public async Task Notifications_Ok() |
312 | 318 | var characteristic = await service.GetCharacteristic("battery_level"); |
313 | 319 | Assert.NotNull(characteristic); |
314 | 320 |
|
| 321 | + byte[]? buffer = null; |
315 | 322 | var notification = await characteristic.StartNotifications(payload => |
316 | 323 | { |
| 324 | + buffer = payload; |
317 | 325 | return Task.CompletedTask; |
318 | 326 | }); |
319 | 327 | Assert.True(notification); |
320 | 328 |
|
| 329 | + await characteristic.StartNotifications(payload => { return Task.CompletedTask; }); |
| 330 | + Assert.Equal("the battery_level characteristic already started.", characteristic.ErrorMessage); |
| 331 | + |
| 332 | + // trigger notification |
| 333 | + var mi = characteristic.GetType().GetMethod("OnNotification"); |
| 334 | + Assert.NotNull(mi); |
| 335 | + Assert.Null(buffer); |
| 336 | + mi.Invoke(characteristic, ["battery_level", "1"u8.ToArray()]); |
| 337 | + Assert.NotNull(buffer); |
| 338 | + |
321 | 339 | notification = await characteristic.StopNotifications(); |
322 | 340 | Assert.True(notification); |
| 341 | + |
| 342 | + Context.JSInterop.Setup<bool?>("startNotifications", matcher => matcher.Arguments[0]?.ToString()?.StartsWith("bb_bt_") ?? false).SetResult(null); |
| 343 | + await characteristic.StartNotifications(payload => |
| 344 | + { |
| 345 | + return Task.CompletedTask; |
| 346 | + }); |
| 347 | + notification = await characteristic.StopNotifications(); |
| 348 | + Assert.False(notification); |
| 349 | + |
| 350 | + Context.JSInterop.Setup<bool?>("startNotifications", matcher => matcher.Arguments[0]?.ToString()?.StartsWith("bb_bt_") ?? false).SetResult(true); |
| 351 | + Context.JSInterop.Setup<bool?>("stopNotifications", matcher => matcher.Arguments[0]?.ToString()?.StartsWith("bb_bt_") ?? false).SetResult(null); |
| 352 | + await characteristic.StartNotifications(payload => |
| 353 | + { |
| 354 | + return Task.CompletedTask; |
| 355 | + }); |
| 356 | + notification = await characteristic.StopNotifications(); |
| 357 | + Assert.False(notification); |
| 358 | + |
323 | 359 | } |
324 | 360 | } |
0 commit comments