1+ package moe .codeest .rxsocketclientdemo ;
2+
3+ import android .widget .Button ;
4+ import android .widget .TextView ;
5+ import android .widget .Toast ;
6+
7+ import org .jetbrains .annotations .NotNull ;
8+ import org .junit .Before ;
9+ import org .junit .Test ;
10+ import org .junit .runner .RunWith ;
11+ import org .robolectric .Robolectric ;
12+ import org .robolectric .RobolectricTestRunner ;
13+ import org .robolectric .annotation .Config ;
14+ import org .robolectric .shadows .ShadowLog ;
15+ import org .robolectric .shadows .ShadowToast ;
16+
17+ import java .net .URISyntaxException ;
18+ import java .util .Arrays ;
19+
20+ import moe .codeest .rxsocketclient .SocketSubscriber ;
21+
22+ import static junit .framework .Assert .assertEquals ;
23+ import static junit .framework .Assert .assertNotNull ;
24+
25+ /**
26+ * @author: Est <codeest.dev@gmail.com>
27+ * @date: 2017/7/12
28+ * @description:
29+ */
30+
31+ @ RunWith (RobolectricTestRunner .class )
32+ @ Config (constants = BuildConfig .class )
33+ public class SocketTest {
34+
35+ private JavaActivity activity ;
36+ private Button btnConnect , btnDisConnect , btnSend ;
37+ private TextView tvReceive ;
38+
39+ @ Before
40+ public void setUp () throws URISyntaxException {
41+ ShadowLog .stream = System .out ;
42+ activity = Robolectric .setupActivity (JavaActivity .class );
43+ btnConnect = (Button ) activity .findViewById (R .id .btn_connect );
44+ btnDisConnect = (Button ) activity .findViewById (R .id .btn_disconnect );
45+ btnSend = (Button ) activity .findViewById (R .id .btn_send );
46+ tvReceive = (TextView ) activity .findViewById (R .id .tv_receive );
47+ }
48+
49+ @ Test
50+ public void testActivity () {
51+ assertNotNull (activity );
52+ assertEquals ("RxSocketClientDemo" , activity .getTitle ());
53+ }
54+
55+ @ Test
56+ public void testCallback (){
57+ assertNotNull (activity );
58+ TestSocketSubscriber subscriber = new TestSocketSubscriber ();
59+ activity .connect (subscriber );
60+ subscriber .onConnected ();
61+ assertEquals ("onConnected" , ShadowToast .getTextOfLatestToast ());
62+ subscriber .onResponse (new byte []{1 , 2 , 3 });
63+ assertEquals ("[1, 2, 3]" , tvReceive .getText ());
64+ subscriber .onDisconnected ();
65+ assertEquals ("onDisConnected" , ShadowToast .getTextOfLatestToast ());
66+ }
67+
68+ @ Test
69+ public void testUI (){
70+ assertNotNull (btnConnect );
71+ assertNotNull (btnDisConnect );
72+ btnConnect .performClick ();
73+ btnDisConnect .performClick ();
74+ }
75+
76+ public class TestSocketSubscriber extends SocketSubscriber {
77+
78+ @ Override
79+ public void onConnected () {
80+ Toast .makeText (activity , "onConnected" , Toast .LENGTH_SHORT ).show ();
81+ }
82+
83+ @ Override
84+ public void onDisconnected () {
85+ Toast .makeText (activity , "onDisConnected" , Toast .LENGTH_SHORT ).show ();
86+ }
87+
88+ @ Override
89+ public void onResponse (@ NotNull byte [] data ) {
90+ tvReceive .setText (Arrays .toString (data ));
91+ }
92+ }
93+
94+ }
0 commit comments