-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBluetoothStatusTrackerTests.cs
More file actions
98 lines (76 loc) · 3.15 KB
/
Copy pathBluetoothStatusTrackerTests.cs
File metadata and controls
98 lines (76 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using SwitchifyPc.Core.Bluetooth;
namespace SwitchifyPc.Tests;
public sealed class BluetoothStatusTrackerTests
{
[Fact]
public void SystemStatusSetsCheckedAndChangedTimestamps()
{
double now = 1000;
BluetoothStatusTracker tracker = new(() => now);
BluetoothStatus status = tracker.SetSystemStatus(new BluetoothSystemStatusEvent(true, "on", true, true));
Assert.Equal(1000, status.System.LastCheckedAt);
Assert.Equal(1000, status.System.LastChangedAt);
}
[Fact]
public void IdenticalSystemStatusUpdatesCheckedButPreservesChangedTimestamp()
{
double now = 1000;
BluetoothStatusTracker tracker = new(() => now);
tracker.SetSystemStatus(new BluetoothSystemStatusEvent(true, "on", true, true));
now = 2000;
BluetoothStatus status = tracker.SetSystemStatus(new BluetoothSystemStatusEvent(true, "on", true, true));
Assert.Equal(2000, status.System.LastCheckedAt);
Assert.Equal(1000, status.System.LastChangedAt);
}
[Fact]
public void RadioOffRemovesConnectionsAndMarksAdapterOff()
{
double now = 1000;
BluetoothStatusTracker tracker = new(() => now);
tracker.SetReady();
tracker.AddConnection("ble");
BluetoothStatus status = tracker.SetSystemStatus(new BluetoothSystemStatusEvent(true, "off", true, true));
Assert.Equal("unavailable", status.Status);
Assert.Equal("adapter_off", status.Reason);
Assert.Equal(0, status.ConnectedClientCount);
Assert.Equal("adapter_off", status.LastDisconnectReason);
}
[Fact]
public void RadioOnAfterAdapterOffMovesToStarting()
{
BluetoothStatusTracker tracker = new(() => 1000);
tracker.SetSystemStatus(new BluetoothSystemStatusEvent(true, "off", true, true));
BluetoothStatus status = tracker.SetSystemStatus(new BluetoothSystemStatusEvent(true, "on", true, true));
Assert.Equal("starting", status.Status);
Assert.Null(status.Reason);
}
[Fact]
public void AdapterMissingReportsUnsupported()
{
BluetoothStatusTracker tracker = new(() => 1000);
BluetoothStatus status = tracker.SetSystemStatus(new BluetoothSystemStatusEvent(false, "unknown", null, null));
Assert.Equal("unavailable", status.Status);
Assert.Equal("unsupported", status.Reason);
}
[Fact]
public void DiagnosticsKeepNewestFiveEvents()
{
double now = 1000;
BluetoothStatusTracker tracker = new(() => now++);
foreach (string diagnosticEvent in new[] { "one", "two", "three", "four", "five", "six" })
{
tracker.RecordDiagnostic(diagnosticEvent);
}
Assert.Equal("six", tracker.Status.LastEvent);
Assert.Equal(["two", "three", "four", "five", "six"], tracker.Status.RecentEvents.Select(record => record.Event).ToArray());
}
[Fact]
public void NotifiesOnStatusChange()
{
List<BluetoothStatus> changes = [];
BluetoothStatusTracker tracker = new(() => 1000, changes.Add);
tracker.SetReady();
Assert.Single(changes);
Assert.Equal("ready", changes[0].Status);
}
}