Skip to content

Commit 99a642c

Browse files
committed
test: MysteryVibe device tests
Added a test utility to wait until we get a value, as the MysteryVibe requires sending updates on a loop.
1 parent 4fe144e commit 99a642c

4 files changed

Lines changed: 106 additions & 1 deletion

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Buttplug.Core.Messages;
7+
using Buttplug.Server.Bluetooth.Devices;
8+
using Buttplug.Server.Test.Util;
9+
using JetBrains.Annotations;
10+
using NUnit.Framework;
11+
12+
namespace Buttplug.Server.Test.Bluetooth.Devices
13+
{
14+
[TestFixture]
15+
public class MysteryVibeTests
16+
{
17+
[NotNull] private BluetoothDeviceTestUtils<MysteryVibeBluetoothInfo> testUtil;
18+
19+
[SetUp]
20+
public void Init()
21+
{
22+
testUtil = new BluetoothDeviceTestUtils<MysteryVibeBluetoothInfo>();
23+
testUtil.SetupTest("MV Crescendo");
24+
}
25+
26+
[Test]
27+
public void TestAllowedMessages()
28+
{
29+
testUtil.TestDeviceAllowedMessages(new Dictionary<System.Type, uint>()
30+
{
31+
{ typeof(StopDeviceCmd), 0 },
32+
{ typeof(SingleMotorVibrateCmd), 0 },
33+
{ typeof(VibrateCmd), 6 },
34+
});
35+
}
36+
37+
// StopDeviceCmd noop test handled in GeneralDeviceTests
38+
39+
[Test]
40+
public void TestStopDeviceCmd()
41+
{
42+
var expected =
43+
new List<(byte[], uint)>()
44+
{
45+
(Enumerable.Repeat((byte)(MysteryVibe.MaxSpeed * 0.5), 6).ToArray(), testUtil.NoCharacteristic),
46+
};
47+
48+
testUtil.TestDeviceMessage(new SingleMotorVibrateCmd(4, 0.5), expected, false);
49+
50+
expected =
51+
new List<(byte[], uint)>()
52+
{
53+
(MysteryVibe.NullSpeed, testUtil.NoCharacteristic),
54+
};
55+
56+
testUtil.TestDeviceMessageOnWrite(new StopDeviceCmd(4), expected, false);
57+
}
58+
59+
[Test]
60+
public void TestSingleMotorVibrateCmd()
61+
{
62+
var expected =
63+
new List<(byte[], uint)>()
64+
{
65+
(Enumerable.Repeat((byte)(MysteryVibe.MaxSpeed * 0.5), 6).ToArray(), testUtil.NoCharacteristic),
66+
};
67+
68+
testUtil.TestDeviceMessage(new SingleMotorVibrateCmd(4, 0.5), expected, false);
69+
}
70+
71+
[Test]
72+
public void TestVibrateCmd()
73+
{
74+
var expected =
75+
new List<(byte[], uint)>()
76+
{
77+
(Enumerable.Repeat((byte)(MysteryVibe.MaxSpeed * 0.5), 6).ToArray(), testUtil.NoCharacteristic),
78+
};
79+
80+
testUtil.TestDeviceMessage(VibrateCmd.Create(4, 1, 0.5, 6), expected, false);
81+
}
82+
83+
[Test]
84+
public void TestInvalidVibrateCmd()
85+
{
86+
testUtil.TestInvalidVibrateCmd(6);
87+
}
88+
}
89+
}

Buttplug.Server.Test/Buttplug.Server.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<Compile Include="Bluetooth\Devices\GeneralDeviceTests.cs" />
6666
<Compile Include="Bluetooth\Devices\KiirooGen2VibeTests.cs" />
6767
<Compile Include="Bluetooth\Devices\KiirooTests.cs" />
68+
<Compile Include="Bluetooth\Devices\MysteryVibeTests.cs" />
6869
<Compile Include="Bluetooth\Devices\VorzeSATests.cs" />
6970
<Compile Include="ButtplugDeviceTests.cs" />
7071
<Compile Include="ButtplugJsonMessageParserTests.cs" />

Buttplug.Server.Test/TestBluetoothDeviceInterface.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class ReadData
4141

4242
public event EventHandler DeviceRemoved;
4343

44+
public event EventHandler ValueWritten;
45+
4446
public bool Removed;
4547

4648
public TestBluetoothDeviceInterface(string aName)
@@ -74,6 +76,7 @@ public Task<ButtplugMessage> WriteValue(uint aMsgId, uint aCharacteristic, byte[
7476
Characteristic = aCharacteristic,
7577
WriteWithResponse = aWriteWithResponse,
7678
});
79+
ValueWritten?.Invoke(this, new EventArgs());
7780
return Task.FromResult<ButtplugMessage>(new Ok(aMsgId));
7881
}
7982

Buttplug.Server.Test/Util/BluetoothDeviceTestUtils.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Security.Cryptography;
5+
using System.Threading;
56
using Buttplug.Core;
67
using Buttplug.Core.Messages;
78
using Buttplug.Server.Bluetooth;
@@ -128,6 +129,17 @@ public void TestDeviceInitialize(IEnumerable<(byte[], uint)> aExpectedBytes, boo
128129
TestPacketMatching(aExpectedBytes, aWriteWithResponse);
129130
}
130131

132+
// Testing timing with delays is a great way to get inetermittents, but here we are. Sadness.
133+
public void TestDeviceMessageOnWrite(ButtplugDeviceMessage aOutgoingMessage, IEnumerable<(byte[], uint)> aExpectedBytes, bool aWriteWithResponse)
134+
{
135+
Clear();
136+
Assert.True(bleDevice.ParseMessage(aOutgoingMessage).GetAwaiter().GetResult() is Ok);
137+
var resetEvent = new ManualResetEvent(false);
138+
bleIface.ValueWritten += (aObj, aArgs) => { resetEvent.Set(); };
139+
resetEvent.WaitOne(1000);
140+
TestPacketMatching(aExpectedBytes, aWriteWithResponse);
141+
}
142+
131143
public void TestDeviceMessage(ButtplugDeviceMessage aOutgoingMessage, IEnumerable<(byte[], uint)> aExpectedBytes, bool aWriteWithResponse)
132144
{
133145
Clear();
@@ -158,4 +170,4 @@ public void TestInvalidVibrateCmd(uint aNumVibes)
158170
}));
159171
}
160172
}
161-
}
173+
}

0 commit comments

Comments
 (0)