11package org .restcomm .android .olympus ;
22
33
4+ import android .content .ComponentName ;
5+ import android .content .Context ;
46import android .content .Intent ;
7+ import android .content .ServiceConnection ;
58import android .os .Handler ;
69import android .os .HandlerThread ;
710import android .os .IBinder ;
4346import java .util .concurrent .TimeoutException ;
4447
4548@ RunWith (AndroidJUnit4 .class )
46- public class IntegrationTests implements RCDeviceListener {
49+ public class IntegrationTests implements RCDeviceListener , ServiceConnection {
4750 private static final String TAG = "IntegrationTests" ;
4851 //private Handler testHandler;
4952 static private final int TIMEOUT = 120000 ;
5053
5154 // Condition variables
5255 private boolean initialized ; // = false;
5356 private boolean released ; // = false;
57+ private boolean serviceConnected , serviceDisconnected ; // = false;
58+
59+ RCDevice .RCDeviceBinder binder ;
5460
5561
5662 //@Rule
5763 //public UiThreadTestRule uiThreadTestRule = new UiThreadTestRule();
5864
59- @ Rule
60- public final ServiceTestRule mServiceRule = new ServiceTestRule ();
65+ // @Rule
66+ // public final ServiceTestRule mServiceRule = new ServiceTestRule();
6167 //public final ServiceTestRule mServiceRule = ServiceTestRule.withTimeout(60L, TimeUnit.SECONDS);
6268
6369
70+
71+
6472 @ Before
6573 public void initialize ()
6674 {
6775 initialized = false ;
6876 released = false ;
77+ serviceConnected = false ;
78+ serviceDisconnected = false ;
6979 }
7080
7181
@@ -81,13 +91,18 @@ public void afterAction() {
8191 public void deviceInitialize_Valid () throws TimeoutException
8292 {
8393 Log .i (TAG , "------------------------------------------------------------" );
84- IBinder binder = mServiceRule .bindService (new Intent (InstrumentationRegistry .getTargetContext (), RCDevice .class ));
94+ InstrumentationRegistry .getTargetContext ().bindService (new Intent (InstrumentationRegistry .getTargetContext (), RCDevice .class ), this , Context .BIND_AUTO_CREATE );
95+
96+ await ().atMost (10 , TimeUnit .SECONDS ).until (serviceOnConnected ());
97+ //device = binder.getService();
98+ //IBinder binder = mServiceRule.bindService(new Intent(InstrumentationRegistry.getTargetContext(), RCDevice.class));
8599
86100 // Get the reference to the service, or you can call public methods on the binder directly.
87- final RCDevice device = (( RCDevice . RCDeviceBinder ) binder ) .getService ();
101+ final RCDevice device = binder .getService ();
88102
89103 HandlerThread clientHandlerThread = new HandlerThread ("client-thread" );
90104 clientHandlerThread .start ();
105+ Log .e (TAG , "---- client-thread id: " + clientHandlerThread .getId ());
91106 Handler clientHandler = new Handler (clientHandlerThread .getLooper ());
92107
93108 clientHandler .post (new Runnable () {
@@ -135,29 +150,30 @@ public void run() {
135150 //assertThat(released).isTrue();
136151
137152 Log .i (TAG , "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" );
153+
154+ InstrumentationRegistry .getTargetContext ().unbindService (this );
155+
156+ //await().atMost(15, TimeUnit.SECONDS).until(serviceOnDisconnected());
138157 }
139158
140159
141160 @ Test (timeout = TIMEOUT )
142161 // Test initializing RCDevice with proper credentials and cleartext signaling
143162 public void deviceInitialize_EncryptedSignaling_Valid () throws TimeoutException
144163 {
145- /*
146- try {
147- Thread.sleep(5000);
148- } catch (InterruptedException e) {
149- e.printStackTrace();
150- }
151- */
152- Log .i (TAG , "----------------------------------------------------------" );
153- Intent intent = new Intent (InstrumentationRegistry .getTargetContext (), RCDevice .class );
154- IBinder binder = mServiceRule .bindService (intent );
164+ Log .i (TAG , "------------------------------------------------------------" );
165+ InstrumentationRegistry .getTargetContext ().bindService (new Intent (InstrumentationRegistry .getTargetContext (), RCDevice .class ), this , Context .BIND_AUTO_CREATE );
166+
167+ await ().atMost (10 , TimeUnit .SECONDS ).until (serviceOnConnected ());
168+ //device = binder.getService();
169+ //IBinder binder = mServiceRule.bindService(new Intent(InstrumentationRegistry.getTargetContext(), RCDevice.class));
155170
156171 // Get the reference to the service, or you can call public methods on the binder directly.
157- final RCDevice device = (( RCDevice . RCDeviceBinder ) binder ) .getService ();
172+ final RCDevice device = binder .getService ();
158173
159174 HandlerThread clientHandlerThread = new HandlerThread ("client-thread-2" );
160175 clientHandlerThread .start ();
176+ Log .e (TAG , "---- client-thread-2 id: " + clientHandlerThread .getId ());
161177 Handler clientHandler = new Handler (clientHandlerThread .getLooper ());
162178
163179 clientHandler .post (new Runnable () {
@@ -200,8 +216,15 @@ public void run() {
200216 await ().atMost (10 , TimeUnit .SECONDS ).until (deviceOnReleased ());
201217
202218 clientHandlerThread .quit ();
219+
220+
203221 //assertThat(released).isTrue();
222+
204223 Log .i (TAG , "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" );
224+
225+ InstrumentationRegistry .getTargetContext ().unbindService (this );
226+
227+ //await().atMost(15, TimeUnit.SECONDS).until(serviceOnDisconnected());
205228 }
206229
207230
@@ -215,14 +238,27 @@ public Boolean call() throws Exception {
215238 }
216239 };
217240 }
218-
219241 private Callable <Boolean > deviceOnReleased () {
220242 return new Callable <Boolean >() {
221243 public Boolean call () throws Exception {
222244 return released ; // The condition that must be fulfilled
223245 }
224246 };
225247 }
248+ private Callable <Boolean > serviceOnConnected () {
249+ return new Callable <Boolean >() {
250+ public Boolean call () throws Exception {
251+ return serviceConnected ; // The condition that must be fulfilled
252+ }
253+ };
254+ }
255+ private Callable <Boolean > serviceOnDisconnected () {
256+ return new Callable <Boolean >() {
257+ public Boolean call () throws Exception {
258+ return serviceDisconnected ; // The condition that must be fulfilled
259+ }
260+ };
261+ }
226262
227263
228264 /**
@@ -264,4 +300,21 @@ public void onMessageSent(RCDevice device, int statusCode, String statusText, St
264300 {
265301 Log .i (TAG , "%% onMessageSent" );
266302 }
303+
304+ @ Override
305+ public void onServiceConnected (ComponentName name , IBinder service )
306+ {
307+ Log .i (TAG , "%% onServiceConnected" );
308+ binder = (RCDevice .RCDeviceBinder ) service ;
309+
310+ serviceConnected = true ;
311+ }
312+
313+ @ Override
314+ public void onServiceDisconnected (ComponentName name )
315+ {
316+ Log .i (TAG , "%% onServiceDisconnected" );
317+
318+ serviceDisconnected = true ;
319+ }
267320}
0 commit comments