11using System ;
2+ using System . Collections . Generic ;
23using System . Threading ;
34using System . Threading . Tasks ;
45using Buttplug . Components . WebsocketServer ;
56using Buttplug . Core ;
7+ using Buttplug . Core . Messages ;
68using Buttplug . Server ;
7- using Xunit ;
9+ using NUnit . Framework ;
10+ using Buttplug . Server . Test ;
811
912namespace Buttplug . Client . Test
1013{
14+ [ TestFixture ]
1115 public class ButtplugClientTests : IButtplugServerFactory
1216 {
13- public ButtplugServer GetServer ( )
14- {
15- return new ButtplugServer ( "Test server" , 200 ) ;
16- }
17-
1817 private class ButtplugTestClient : ButtplugWSClient
1918 {
2019 public ButtplugTestClient ( string aClientName )
@@ -28,17 +27,48 @@ public async Task<ButtplugMessage> SendMsg(ButtplugMessage aMsg)
2827 }
2928 }
3029
31- [ Fact ]
32- public async void TestConnection ( )
30+ private ButtplugTestClient _client ;
31+ private ButtplugWebsocketServer _server ;
32+ private TestDeviceSubtypeManager _subtypeMgr ;
33+ private DeviceManager _devMgr ;
34+ private ButtplugLogManager _logMgr ;
35+
36+ public ButtplugServer GetServer ( )
37+ {
38+ return new TestServer ( 200 , _devMgr , false ) ;
39+ }
40+
41+ [ OneTimeSetUp ]
42+ public void OneTimeSetUp ( )
43+ {
44+ SynchronizationContext . SetSynchronizationContext ( new SynchronizationContext ( ) ) ;
45+ _logMgr = new ButtplugLogManager ( ) ;
46+ _devMgr = new DeviceManager ( new ButtplugLogManager ( ) ) ;
47+ _subtypeMgr = new TestDeviceSubtypeManager ( ) ;
48+ _devMgr . AddDeviceSubtypeManager ( _subtypeMgr ) ;
49+ }
50+
51+ [ TearDown ]
52+ public void CleanUp ( )
3353 {
34- var server = new ButtplugWebsocketServer ( ) ;
35- server . StartServer ( this ) ;
54+ _client ? . Disconnect ( ) ;
55+ _server ? . Disconnect ( ) ;
56+ }
57+
58+ [ Test ]
59+ public void TestConnection ( )
60+ {
61+ AutoResetEvent eEvent = new AutoResetEvent ( false ) ;
3662
37- var client = new ButtplugTestClient ( "Test client" ) ;
38- await client . Connect ( new Uri ( "ws://localhost:12345/buttplug" ) ) ;
63+ _subtypeMgr . AddDevice ( new TestDevice ( _logMgr , "A" , "1" ) ) ;
64+ _server = new ButtplugWebsocketServer ( ) ;
65+ _server . StartServer ( this ) ;
3966
40- var msgId = client . nextMsgId ;
41- var res = await client . SendMsg ( new Core . Messages . Test ( "Test string" , msgId ) ) ;
67+ _client = new ButtplugTestClient ( "Test client" ) ;
68+ _client . Connect ( new Uri ( "ws://localhost:12345/buttplug" ) ) . Wait ( ) ;
69+
70+ var msgId = _client . nextMsgId ;
71+ var res = _client . SendMsg ( new Core . Messages . Test ( "Test string" , msgId ) ) . GetAwaiter ( ) . GetResult ( ) ;
4272 Assert . True ( res != null ) ;
4373 Assert . True ( res is Core . Messages . Test ) ;
4474 Assert . True ( ( ( Core . Messages . Test ) res ) . TestString == "Test string" ) ;
@@ -47,39 +77,96 @@ public async void TestConnection()
4777 // Check ping is working
4878 Thread . Sleep ( 400 ) ;
4979
50- msgId = client . nextMsgId ;
51- res = await client . SendMsg ( new Core . Messages . Test ( "Test string" , msgId ) ) ;
80+ msgId = _client . nextMsgId ;
81+ res = _client . SendMsg ( new Core . Messages . Test ( "Test string" , msgId ) ) . GetAwaiter ( ) . GetResult ( ) ;
5282 Assert . True ( res != null ) ;
5383 Assert . True ( res is Core . Messages . Test ) ;
5484 Assert . True ( ( ( Core . Messages . Test ) res ) . TestString == "Test string" ) ;
5585 Assert . True ( ( ( Core . Messages . Test ) res ) . Id > msgId ) ;
5686
57- res = await client . SendMsg ( new Core . Messages . Test ( "Test string" ) ) ;
87+ res = _client . SendMsg ( new Core . Messages . Test ( "Test string" ) ) . GetAwaiter ( ) . GetResult ( ) ;
5888 Assert . True ( res != null ) ;
5989 Assert . True ( res is Core . Messages . Test ) ;
6090 Assert . True ( ( ( Core . Messages . Test ) res ) . TestString == "Test string" ) ;
6191 Assert . True ( ( ( Core . Messages . Test ) res ) . Id > msgId ) ;
6292
63- Assert . True ( client . nextMsgId > 5 ) ;
93+ Assert . True ( _client . nextMsgId > 5 ) ;
94+
95+ // Test that events are raised
96+ bool scanningFinished = false ;
97+ ButtplugClientDevice lastAdded = null ;
98+ ButtplugClientDevice lastRemoved = null ;
99+ _client . ScanningFinished += ( aSender , aArg ) =>
100+ {
101+ scanningFinished = true ;
102+ eEvent . Set ( ) ;
103+ } ;
104+
105+ _client . DeviceAdded += ( aSender , aArg ) =>
106+ {
107+ lastAdded = aArg . Device ;
108+ eEvent . Set ( ) ;
109+ } ;
110+
111+ _client . DeviceRemoved += ( aSender , aArg ) =>
112+ {
113+ lastRemoved = aArg . Device ;
114+ eEvent . Set ( ) ;
115+ } ;
116+ _client . StartScanning ( ) . Wait ( ) ;
117+ Assert . Null ( lastAdded ) ;
118+ _subtypeMgr . AddDevice ( new TestDevice ( _logMgr , "B" , "2" ) ) ;
119+ eEvent . WaitOne ( 10000 ) ;
120+ eEvent . Reset ( ) ;
121+ Assert . NotNull ( lastAdded ) ;
122+ Assert . AreEqual ( "B" , lastAdded . Name ) ;
123+
124+ Assert . True ( ! scanningFinished ) ;
125+ _client . StopScanning ( ) . Wait ( ) ;
126+ eEvent . WaitOne ( 10000 ) ;
127+ eEvent . Reset ( ) ;
128+ Assert . True ( scanningFinished ) ;
129+
130+ Assert . AreEqual ( 1 , _client . getDevices ( ) . Length ) ;
131+ Assert . AreEqual ( "B" , _client . getDevices ( ) [ 0 ] . Name ) ;
132+ _client . RequestDeviceList ( ) . Wait ( ) ;
133+ Assert . AreEqual ( 2 , _client . getDevices ( ) . Length ) ;
134+ Assert . AreEqual ( "A" , _client . getDevices ( ) [ 0 ] . Name ) ;
135+ Assert . AreEqual ( "B" , _client . getDevices ( ) [ 1 ] . Name ) ;
136+
137+ eEvent . Reset ( ) ;
138+ Assert . Null ( lastRemoved ) ;
139+ foreach ( var dev in _devMgr . _devices . Values )
140+ {
141+ if ( ( dev as TestDevice ) ? . Identifier == "2" )
142+ {
143+ ( dev as TestDevice ) . RemoveDevice ( ) ;
144+ }
145+ }
64146
65- await client . RequestDeviceList ( ) ;
147+ eEvent . WaitOne ( 10000 ) ;
148+ eEvent . Reset ( ) ;
149+ Assert . NotNull ( lastRemoved ) ;
150+ Assert . AreEqual ( "B" , lastRemoved . Name ) ;
151+ Assert . AreEqual ( 1 , _client . getDevices ( ) . Length ) ;
152+ Assert . AreEqual ( "A" , _client . getDevices ( ) [ 0 ] . Name ) ;
66153
67154 // Shut it down
68- await client . Disconnect ( ) ;
69- server . StopServer ( ) ;
155+ _client . Disconnect ( ) . Wait ( ) ;
156+ _server . StopServer ( ) ;
70157 }
71158
72- [ Fact ]
73- public async void TestSSLConnection ( )
159+ [ Test ]
160+ public void TestSSLConnection ( )
74161 {
75- var server = new ButtplugWebsocketServer ( ) ;
76- server . StartServer ( this , 12346 , true , true ) ;
162+ _server = new ButtplugWebsocketServer ( ) ;
163+ _server . StartServer ( this , 12346 , true , true ) ;
77164
78- var client = new ButtplugTestClient ( "Test client" ) ;
79- await client . Connect ( new Uri ( "wss://localhost:12346/buttplug" ) , true ) ;
165+ _client = new ButtplugTestClient ( "Test client" ) ;
166+ _client . Connect ( new Uri ( "wss://localhost:12346/buttplug" ) , true ) . Wait ( ) ;
80167
81- var msgId = client . nextMsgId ;
82- var res = await client . SendMsg ( new Core . Messages . Test ( "Test string" , msgId ) ) ;
168+ var msgId = _client . nextMsgId ;
169+ var res = _client . SendMsg ( new Core . Messages . Test ( "Test string" , msgId ) ) . GetAwaiter ( ) . GetResult ( ) ;
83170 Assert . True ( res != null ) ;
84171 Assert . True ( res is Core . Messages . Test ) ;
85172 Assert . True ( ( ( Core . Messages . Test ) res ) . TestString == "Test string" ) ;
@@ -88,20 +175,20 @@ public async void TestSSLConnection()
88175 // Check ping is working
89176 Thread . Sleep ( 400 ) ;
90177
91- msgId = client . nextMsgId ;
92- res = await client . SendMsg ( new Core . Messages . Test ( "Test string" , msgId ) ) ;
178+ msgId = _client . nextMsgId ;
179+ res = _client . SendMsg ( new Core . Messages . Test ( "Test string" , msgId ) ) . GetAwaiter ( ) . GetResult ( ) ;
93180 Assert . True ( res != null ) ;
94181 Assert . True ( res is Core . Messages . Test ) ;
95182 Assert . True ( ( ( Core . Messages . Test ) res ) . TestString == "Test string" ) ;
96183 Assert . True ( ( ( Core . Messages . Test ) res ) . Id > msgId ) ;
97184
98- Assert . True ( client . nextMsgId > 4 ) ;
185+ Assert . True ( _client . nextMsgId > 4 ) ;
99186
100- await client . RequestDeviceList ( ) ;
187+ _client . RequestDeviceList ( ) . Wait ( ) ;
101188
102189 // Shut it down
103- await client . Disconnect ( ) ;
104- server . StopServer ( ) ;
190+ _client . Disconnect ( ) . Wait ( ) ;
191+ _server . StopServer ( ) ;
105192 }
106193 }
107194}
0 commit comments