|
| 1 | +package com.appunite.websocket; |
| 2 | + |
| 3 | +import android.net.Uri; |
| 4 | +import android.test.AndroidTestCase; |
| 5 | + |
| 6 | +import com.appunite.socketio.NotConnectedException; |
| 7 | + |
| 8 | +import org.hamcrest.Matchers; |
| 9 | +import org.json.JSONObject; |
| 10 | +import org.mockito.ArgumentCaptor; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 14 | +import java.util.concurrent.atomic.AtomicReference; |
| 15 | + |
| 16 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 17 | +import static org.hamcrest.Matchers.*; |
| 18 | +import static org.mockito.Matchers.argThat; |
| 19 | +import static org.mockito.Mockito.mock; |
| 20 | +import static org.mockito.Mockito.reset; |
| 21 | +import static org.mockito.Mockito.timeout; |
| 22 | +import static org.mockito.Mockito.verify; |
| 23 | + |
| 24 | +public class WebsocketTest extends AndroidTestCase { |
| 25 | + |
| 26 | + private Uri mUri; |
| 27 | + private WebSocketListener mListener; |
| 28 | + |
| 29 | + @Override |
| 30 | + public void setUp() throws Exception { |
| 31 | + super.setUp(); |
| 32 | + mUri = Uri.parse("wss://some.test.website.com/"); |
| 33 | + mListener = mock(WebSocketListener.class); |
| 34 | + } |
| 35 | + |
| 36 | + public void testPreconditions() throws Exception { |
| 37 | + assertThat(mUri, is(notNullValue())); |
| 38 | + } |
| 39 | + |
| 40 | + public void testNoError() throws Exception { |
| 41 | + final WebSocket webSocket = new WebSocket(mListener); |
| 42 | + final AtomicReference<Exception> reference = new AtomicReference<Exception>(); |
| 43 | + new Thread(new Runnable() { |
| 44 | + @Override |
| 45 | + public void run() { |
| 46 | + try { |
| 47 | + webSocket.connect(mUri); |
| 48 | + } catch (IOException e) { |
| 49 | + reference.set(e); |
| 50 | + } catch (WrongWebsocketResponse e) { |
| 51 | + reference.set(e); |
| 52 | + } catch (InterruptedException e) { |
| 53 | + } |
| 54 | + } |
| 55 | + }).start(); |
| 56 | + |
| 57 | + Thread.sleep(2000); |
| 58 | + final Exception exception = reference.get(); |
| 59 | + if (exception != null) { |
| 60 | + throw exception; |
| 61 | + } |
| 62 | + |
| 63 | + webSocket.interrupt(); |
| 64 | + } |
| 65 | + |
| 66 | + public void testConnection() throws Exception { |
| 67 | + final WebSocket webSocket = new WebSocket(mListener); |
| 68 | + |
| 69 | + final AtomicBoolean interrupted = new AtomicBoolean(false); |
| 70 | + new Thread(new Runnable() { |
| 71 | + @Override |
| 72 | + public void run() { |
| 73 | + try { |
| 74 | + webSocket.connect(mUri); |
| 75 | + } catch (IOException ignore) { |
| 76 | + } catch (WrongWebsocketResponse ignore) { |
| 77 | + } catch (InterruptedException e) { |
| 78 | + interrupted.set(true); |
| 79 | + } |
| 80 | + } |
| 81 | + }).start(); |
| 82 | + |
| 83 | + verify(mListener, timeout(2000)).onConnected(); |
| 84 | + |
| 85 | + webSocket.interrupt(); |
| 86 | + Thread.sleep(1000); |
| 87 | + |
| 88 | + assertThat(interrupted.get(), Matchers.is(equalTo(true))); |
| 89 | + } |
| 90 | + |
| 91 | + public void testSendData() throws Exception { |
| 92 | + final WebSocket webSocket = authorize(); |
| 93 | + |
| 94 | + webSocket.interrupt(); |
| 95 | + } |
| 96 | + |
| 97 | + public void testRequestUsers() throws Exception { |
| 98 | + final WebSocket webSocket = authorize(); |
| 99 | + |
| 100 | + webSocket.sendStringMessage("{\"action\": \"observe\", \"users\": [12354]}"); |
| 101 | + verify(mListener, timeout(2000)).onStringMessage(argThat(containsString("\"12354\""))); |
| 102 | + |
| 103 | + webSocket.interrupt(); |
| 104 | + } |
| 105 | + |
| 106 | + private WebSocket connectWebsocket() throws IOException, InterruptedException, NotConnectedException { |
| 107 | + final WebSocket webSocket = new WebSocket(mListener); |
| 108 | + |
| 109 | + new Thread(new Runnable() { |
| 110 | + @Override |
| 111 | + public void run() { |
| 112 | + try { |
| 113 | + webSocket.connect(mUri); |
| 114 | + } catch (IOException ignore) { |
| 115 | + } catch (WrongWebsocketResponse ignore) { |
| 116 | + } catch (InterruptedException ignore) { |
| 117 | + } |
| 118 | + } |
| 119 | + }).start(); |
| 120 | + |
| 121 | + verify(mListener, timeout(2000)).onConnected(); |
| 122 | + reset(mListener); |
| 123 | + return webSocket; |
| 124 | + } |
| 125 | + |
| 126 | + private WebSocket authorize() throws IOException, InterruptedException, NotConnectedException { |
| 127 | + final WebSocket webSocket = connectWebsocket(); |
| 128 | + |
| 129 | + webSocket.sendStringMessage("{\"action\": \"auth\", \"auth_token\": \"1ZizC3fpskE3YEHexfgX\"}"); |
| 130 | + |
| 131 | + final ArgumentCaptor<String> responseMessage = ArgumentCaptor.forClass(String.class); |
| 132 | + verify(mListener, timeout(2000)).onStringMessage(responseMessage.capture()); |
| 133 | + reset(mListener); |
| 134 | + |
| 135 | + assertThat(responseMessage.getValue(), is(containsString("auth"))); |
| 136 | + assertThat(responseMessage.getValue(), is(containsString("OK"))); |
| 137 | + return webSocket; |
| 138 | + } |
| 139 | +} |
0 commit comments