37
37
import org .junit .Test ;
38
38
import org .mockito .ArgumentCaptor ;
39
39
import org .slf4j .Logger ;
40
+ import org .slf4j .LoggerFactory ;
40
41
41
42
import java .util .concurrent .TimeUnit ;
42
43
import java .util .concurrent .TimeoutException ;
43
44
45
+ import static android .app .Service .START_FLAG_REDELIVERY ;
44
46
import static junit .framework .Assert .assertEquals ;
45
47
import static junit .framework .Assert .assertTrue ;
46
48
import static junit .framework .Assert .fail ;
57
59
public class DatafileServiceTest {
58
60
59
61
private ListeningExecutorService executor ;
62
+ private static final int MAX_ITERATION = 100 ;
60
63
61
64
@ Rule
62
65
public final ServiceTestRule mServiceRule = new ServiceTestRule ();
@@ -68,11 +71,16 @@ public void setup() {
68
71
69
72
@ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
70
73
@ Test
71
- @ Ignore
72
74
public void testBinding () throws TimeoutException {
73
75
Context context = InstrumentationRegistry .getTargetContext ();
74
76
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
+
76
84
final Context targetContext = InstrumentationRegistry .getTargetContext ();
77
85
Logger logger = mock (Logger .class );
78
86
DatafileCache datafileCache = new DatafileCache ("1" , new Cache (targetContext , logger ), logger );
@@ -90,25 +98,36 @@ public void testBinding() throws TimeoutException {
90
98
91
99
@ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
92
100
@ Test
93
- @ Ignore
94
101
public void testValidStart () throws TimeoutException {
95
102
Context context = InstrumentationRegistry .getTargetContext ();
96
103
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
+
98
111
intent .putExtra (DatafileService .EXTRA_PROJECT_ID , "1" );
99
112
DatafileService datafileService = ((DatafileService .LocalBinder ) binder ).getService ();
100
113
Logger logger = mock (Logger .class );
101
114
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 );
104
117
}
105
118
106
119
@ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
107
120
@ Test
108
121
public void testNullIntentStart () throws TimeoutException {
109
122
Context context = InstrumentationRegistry .getTargetContext ();
110
123
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 );
112
131
DatafileService datafileService = ((DatafileService .LocalBinder ) binder ).getService ();
113
132
Logger logger = mock (Logger .class );
114
133
datafileService .logger = logger ;
@@ -118,11 +137,16 @@ public void testNullIntentStart() throws TimeoutException {
118
137
119
138
@ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
120
139
@ Test
121
- @ Ignore
122
140
public void testNoProjectIdIntentStart () throws TimeoutException {
123
141
Context context = InstrumentationRegistry .getTargetContext ();
124
142
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
+
126
150
DatafileService datafileService = ((DatafileService .LocalBinder ) binder ).getService ();
127
151
Logger logger = mock (Logger .class );
128
152
datafileService .logger = logger ;
@@ -132,6 +156,27 @@ public void testNoProjectIdIntentStart() throws TimeoutException {
132
156
133
157
@ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
134
158
@ 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
135
180
public void testIntentExtraData (){
136
181
Context context = mock (Context .class );
137
182
when (context .getPackageName ()).thenReturn ("com.optly" );
0 commit comments