66import com .appunite .socketio .NotConnectedException ;
77
88import org .hamcrest .Matchers ;
9- import org .json .JSONObject ;
109import org .mockito .ArgumentCaptor ;
1110
1211import java .io .IOException ;
12+ import java .util .Arrays ;
1313import java .util .concurrent .atomic .AtomicBoolean ;
1414import java .util .concurrent .atomic .AtomicReference ;
1515
1616import static org .hamcrest .MatcherAssert .assertThat ;
1717import static org .hamcrest .Matchers .*;
18+ import static org .mockito .Matchers .any ;
1819import static org .mockito .Matchers .argThat ;
1920import static org .mockito .Mockito .mock ;
2021import static org .mockito .Mockito .reset ;
@@ -29,7 +30,7 @@ public class WebsocketTest extends AndroidTestCase {
2930 @ Override
3031 public void setUp () throws Exception {
3132 super .setUp ();
32- mUri = Uri .parse ("wss://some.test.website .com/" );
33+ mUri = Uri .parse ("wss://www.yourserver .com/" );
3334 mListener = mock (WebSocketListener .class );
3435 }
3536
@@ -49,7 +50,7 @@ public void run() {
4950 reference .set (e );
5051 } catch (WrongWebsocketResponse e ) {
5152 reference .set (e );
52- } catch (InterruptedException e ) {
53+ } catch (InterruptedException ignore ) {
5354 }
5455 }
5556 }).start ();
@@ -67,7 +68,7 @@ public void testConnection() throws Exception {
6768 final WebSocket webSocket = new WebSocket (mListener );
6869
6970 final AtomicBoolean interrupted = new AtomicBoolean (false );
70- new Thread (new Runnable () {
71+ final Thread thread = new Thread (new Runnable () {
7172 @ Override
7273 public void run () {
7374 try {
@@ -78,14 +79,106 @@ public void run() {
7879 interrupted .set (true );
7980 }
8081 }
81- }).start ();
82+ });
83+ thread .start ();
8284
8385 verify (mListener , timeout (2000 )).onConnected ();
8486
8587 webSocket .interrupt ();
86- Thread .sleep (1000 );
88+
89+ thread .join (1000 );
90+
91+ assertThat (interrupted .get (), Matchers .is (equalTo (true )));
92+ }
93+
94+ public void testMask () throws Exception {
95+ final WebSocket webSocket = new WebSocket (mListener );
96+
97+ final byte [] src = {0x01 , 0x02 , 0x03 };
98+ final byte [] toMask = src .clone ();
99+ final byte [] mask = {0x51 , 0x52 , 0x53 , 0x54 };
100+ webSocket .maskBuffer (toMask , mask );
101+
102+ assertThat (toMask , is (not (equalTo (src ))));
103+
104+ webSocket .maskBuffer (toMask , mask );
105+
106+ assertThat (toMask , is (equalTo (src )));
107+
108+ }
109+
110+ public void testPing () throws Exception {
111+ final WebSocket webSocket = new WebSocket (mListener );
112+
113+ final AtomicBoolean interrupted = new AtomicBoolean (false );
114+ final Thread thread = new Thread (new Runnable () {
115+ @ Override
116+ public void run () {
117+ try {
118+ webSocket .connect (mUri );
119+ } catch (IOException ignore ) {
120+ } catch (WrongWebsocketResponse ignore ) {
121+ } catch (InterruptedException e ) {
122+ interrupted .set (true );
123+ }
124+ }
125+ });
126+ thread .start ();
127+
128+ verify (mListener , timeout (2000 )).onConnected ();
129+ reset (mListener );
130+ final byte [] data = {0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 , 0x02 , 0x03 };
131+ webSocket .sendPingMessage (data .clone ());
132+
133+ verify (mListener , timeout (2000 )).onPong (data );
134+
135+ webSocket .interrupt ();
136+
137+ thread .join (1000 );
87138
88139 assertThat (interrupted .get (), Matchers .is (equalTo (true )));
140+
141+
142+ }
143+
144+ public void testReconnection () throws Exception {
145+ final WebSocket webSocket = new WebSocket (mListener );
146+ final Thread thread1 = new Thread (new Runnable () {
147+ @ Override
148+ public void run () {
149+ try {
150+ webSocket .connect (mUri );
151+ } catch (IOException ignore ) {
152+ } catch (WrongWebsocketResponse ignore ) {
153+ } catch (InterruptedException ignore ) {
154+ }
155+ }
156+ });
157+ thread1 .start ();
158+
159+ verify (mListener , timeout (2000 )).onConnected ();
160+ reset (mListener );
161+ webSocket .interrupt ();
162+ thread1 .join (1000 );
163+
164+
165+ final Thread thread2 = new Thread (new Runnable () {
166+ @ Override
167+ public void run () {
168+ try {
169+ webSocket .connect (mUri );
170+ } catch (IOException ignore ) {
171+ } catch (WrongWebsocketResponse ignore ) {
172+ } catch (InterruptedException ignore ) {
173+ }
174+ }
175+ });
176+ thread2 .start ();
177+
178+ verify (mListener , timeout (2000 )).onConnected ();
179+ reset (mListener );
180+ webSocket .interrupt ();
181+ thread2 .join (1000 );
89182 }
90183
91184 public void testSendData () throws Exception {
@@ -98,7 +191,7 @@ public void testRequestUsers() throws Exception {
98191 final WebSocket webSocket = authorize ();
99192
100193 webSocket .sendStringMessage ("{\" action\" : \" observe\" , \" users\" : [12354]}" );
101- verify (mListener , timeout (2000 )).onStringMessage (argThat (containsString ("\" 12354 \" " )));
194+ verify (mListener , timeout (2000 )).onStringMessage (argThat (containsString ("\" 1235664 \" " )));
102195
103196 webSocket .interrupt ();
104197 }
0 commit comments