3737import org .junit .Test ;
3838import org .mockito .ArgumentCaptor ;
3939import org .slf4j .Logger ;
40+ import org .slf4j .LoggerFactory ;
4041
4142import java .util .concurrent .TimeUnit ;
4243import java .util .concurrent .TimeoutException ;
4344
45+ import static android .app .Service .START_FLAG_REDELIVERY ;
4446import static junit .framework .Assert .assertEquals ;
4547import static junit .framework .Assert .assertTrue ;
4648import static junit .framework .Assert .fail ;
5759public class DatafileServiceTest {
5860
5961 private ListeningExecutorService executor ;
62+ private static final int MAX_ITERATION = 100 ;
6063
6164 @ Rule
6265 public final ServiceTestRule mServiceRule = new ServiceTestRule ();
@@ -68,11 +71,16 @@ public void setup() {
6871
6972 @ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
7073 @ Test
71- @ Ignore
7274 public void testBinding () throws TimeoutException {
7375 Context context = InstrumentationRegistry .getTargetContext ();
7476 Intent intent = new Intent (context , DatafileService .class );
75- IBinder binder = mServiceRule .bindService (intent );
77+ IBinder binder = null ;
78+ int it = 0 ;
79+
80+ while ((binder = mServiceRule .bindService (intent )) == null && it < MAX_ITERATION ){
81+ it ++;
82+ }
83+
7684 final Context targetContext = InstrumentationRegistry .getTargetContext ();
7785 Logger logger = mock (Logger .class );
7886 DatafileCache datafileCache = new DatafileCache ("1" , new Cache (targetContext , logger ), logger );
@@ -90,25 +98,36 @@ public void testBinding() throws TimeoutException {
9098
9199 @ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
92100 @ Test
93- @ Ignore
94101 public void testValidStart () throws TimeoutException {
95102 Context context = InstrumentationRegistry .getTargetContext ();
96103 Intent intent = new Intent (context , DatafileService .class );
97- IBinder binder = mServiceRule .bindService (intent );
104+ IBinder binder = null ;
105+ int it = 0 ;
106+
107+ while ((binder = mServiceRule .bindService (intent )) == null && it < MAX_ITERATION ){
108+ it ++;
109+ }
110+
98111 intent .putExtra (DatafileService .EXTRA_PROJECT_ID , "1" );
99112 DatafileService datafileService = ((DatafileService .LocalBinder ) binder ).getService ();
100113 Logger logger = mock (Logger .class );
101114 datafileService .logger = logger ;
102- datafileService .onStartCommand (intent , 0 , 0 );
103- verify ( logger ). info ( "Started watching project {} in the background" , "1" );
115+ int val = datafileService .onStartCommand (intent , 0 , 0 );
116+ assertEquals ( val , START_FLAG_REDELIVERY );
104117 }
105118
106119 @ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
107120 @ Test
108121 public void testNullIntentStart () throws TimeoutException {
109122 Context context = InstrumentationRegistry .getTargetContext ();
110123 Intent intent = new Intent (context , DatafileService .class );
111- IBinder binder = mServiceRule .bindService (intent );
124+ IBinder binder = null ;
125+ int it = 0 ;
126+
127+ while ((binder = mServiceRule .bindService (intent )) == null && it < MAX_ITERATION ){
128+ it ++;
129+ }
130+ mServiceRule .bindService (intent );
112131 DatafileService datafileService = ((DatafileService .LocalBinder ) binder ).getService ();
113132 Logger logger = mock (Logger .class );
114133 datafileService .logger = logger ;
@@ -118,11 +137,16 @@ public void testNullIntentStart() throws TimeoutException {
118137
119138 @ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
120139 @ Test
121- @ Ignore
122140 public void testNoProjectIdIntentStart () throws TimeoutException {
123141 Context context = InstrumentationRegistry .getTargetContext ();
124142 Intent intent = new Intent (context , DatafileService .class );
125- IBinder binder = mServiceRule .bindService (intent );
143+ IBinder binder = null ;
144+ int it = 0 ;
145+
146+ while ((binder = mServiceRule .bindService (intent )) == null && it < MAX_ITERATION ){
147+ it ++;
148+ }
149+
126150 DatafileService datafileService = ((DatafileService .LocalBinder ) binder ).getService ();
127151 Logger logger = mock (Logger .class );
128152 datafileService .logger = logger ;
@@ -132,6 +156,27 @@ public void testNoProjectIdIntentStart() throws TimeoutException {
132156
133157 @ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
134158 @ Test
159+ public void testUnbind () throws TimeoutException {
160+ Context context = InstrumentationRegistry .getTargetContext ();
161+ Intent intent = new Intent (context , DatafileService .class );
162+ IBinder binder = null ;
163+ int it = 0 ;
164+
165+ while ((binder = mServiceRule .bindService (intent )) == null && it < MAX_ITERATION ){
166+ it ++;
167+ }
168+
169+ DatafileService datafileService = ((DatafileService .LocalBinder ) binder ).getService ();
170+ Logger logger = mock (Logger .class );
171+ datafileService .logger = logger ;
172+
173+ datafileService .onUnbind (intent );
174+ verify (logger ).info ("All clients are unbound from data file service" );
175+ }
176+
177+ @ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
178+ @ Test
179+ @ Ignore
135180 public void testIntentExtraData (){
136181 Context context = mock (Context .class );
137182 when (context .getPackageName ()).thenReturn ("com.optly" );
0 commit comments