28
28
import android .net .ConnectivityManager ;
29
29
import android .net .Network ;
30
30
import android .net .NetworkInfo ;
31
+ import androidx .test .core .app .ApplicationProvider ;
31
32
import io .grpc .CallOptions ;
32
33
import io .grpc .ClientCall ;
33
34
import io .grpc .ManagedChannel ;
39
40
import org .junit .Test ;
40
41
import org .junit .runner .RunWith ;
41
42
import org .robolectric .RobolectricTestRunner ;
42
- import org .robolectric .RuntimeEnvironment ;
43
43
import org .robolectric .annotation .Config ;
44
44
import org .robolectric .annotation .Implementation ;
45
45
import org .robolectric .annotation .Implements ;
54
54
public final class AndroidChannelBuilderTest {
55
55
private final NetworkInfo WIFI_CONNECTED =
56
56
ShadowNetworkInfo .newInstance (
57
- NetworkInfo .DetailedState .CONNECTED , ConnectivityManager .TYPE_WIFI , 0 , true , true );
57
+ NetworkInfo .DetailedState .CONNECTED ,
58
+ ConnectivityManager .TYPE_WIFI ,
59
+ 0 ,
60
+ true ,
61
+ NetworkInfo .State .CONNECTED );
58
62
private final NetworkInfo WIFI_DISCONNECTED =
59
63
ShadowNetworkInfo .newInstance (
60
- NetworkInfo .DetailedState .DISCONNECTED , ConnectivityManager .TYPE_WIFI , 0 , true , false );
64
+ NetworkInfo .DetailedState .DISCONNECTED ,
65
+ ConnectivityManager .TYPE_WIFI ,
66
+ 0 ,
67
+ true ,
68
+ NetworkInfo .State .DISCONNECTED );
61
69
private final NetworkInfo MOBILE_CONNECTED =
62
70
ShadowNetworkInfo .newInstance (
63
71
NetworkInfo .DetailedState .CONNECTED ,
64
72
ConnectivityManager .TYPE_MOBILE ,
65
73
ConnectivityManager .TYPE_MOBILE_MMS ,
66
74
true ,
67
- true );
75
+ NetworkInfo . State . CONNECTED );
68
76
private final NetworkInfo MOBILE_DISCONNECTED =
69
77
ShadowNetworkInfo .newInstance (
70
78
NetworkInfo .DetailedState .DISCONNECTED ,
71
79
ConnectivityManager .TYPE_MOBILE ,
72
80
ConnectivityManager .TYPE_MOBILE_MMS ,
73
81
true ,
74
- false );
82
+ NetworkInfo . State . DISCONNECTED );
75
83
76
84
private ConnectivityManager connectivityManager ;
77
85
78
86
@ Before
79
87
public void setUp () {
80
88
connectivityManager =
81
89
(ConnectivityManager )
82
- RuntimeEnvironment .application .getSystemService (Context .CONNECTIVITY_SERVICE );
90
+ ApplicationProvider
91
+ .getApplicationContext ()
92
+ .getSystemService (Context .CONNECTIVITY_SERVICE );
83
93
}
84
94
85
95
@ Test
@@ -103,8 +113,9 @@ public void nullContextDoesNotThrow_api23() {
103
113
104
114
// Network change and shutdown should be no-op for the channel without an Android Context
105
115
shadowOf (connectivityManager ).setActiveNetworkInfo (WIFI_CONNECTED );
106
- RuntimeEnvironment .application .sendBroadcast (
107
- new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
116
+ ApplicationProvider
117
+ .getApplicationContext ()
118
+ .sendBroadcast (new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
108
119
androidChannel .shutdown ();
109
120
110
121
assertThat (delegateChannel .resetCount ).isEqualTo (0 );
@@ -131,48 +142,55 @@ public void resetConnectBackoff_api23() {
131
142
TestChannel delegateChannel = new TestChannel ();
132
143
ManagedChannel androidChannel =
133
144
new AndroidChannelBuilder .AndroidChannel (
134
- delegateChannel , RuntimeEnvironment . application .getApplicationContext ());
145
+ delegateChannel , ApplicationProvider .getApplicationContext ());
135
146
assertThat (delegateChannel .resetCount ).isEqualTo (0 );
136
147
137
148
// On API levels < 24, the broadcast receiver will invoke resetConnectBackoff() on the first
138
149
// connectivity action broadcast regardless of previous connection status
139
150
shadowOf (connectivityManager ).setActiveNetworkInfo (WIFI_CONNECTED );
140
- RuntimeEnvironment .application .sendBroadcast (
141
- new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
151
+ ApplicationProvider
152
+ .getApplicationContext ()
153
+ .sendBroadcast (new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
142
154
assertThat (delegateChannel .resetCount ).isEqualTo (1 );
143
155
144
156
// The broadcast receiver may fire when the active network status has not actually changed
145
- RuntimeEnvironment .application .sendBroadcast (
146
- new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
157
+ ApplicationProvider
158
+ .getApplicationContext ()
159
+ .sendBroadcast (new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
147
160
assertThat (delegateChannel .resetCount ).isEqualTo (1 );
148
161
149
162
// Drop the connection
150
163
shadowOf (connectivityManager ).setActiveNetworkInfo (null );
151
- RuntimeEnvironment .application .sendBroadcast (
152
- new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
164
+ ApplicationProvider
165
+ .getApplicationContext ()
166
+ .sendBroadcast (new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
153
167
assertThat (delegateChannel .resetCount ).isEqualTo (1 );
154
168
155
169
// Notify that a new but not connected network is available
156
170
shadowOf (connectivityManager ).setActiveNetworkInfo (MOBILE_DISCONNECTED );
157
- RuntimeEnvironment .application .sendBroadcast (
158
- new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
171
+ ApplicationProvider
172
+ .getApplicationContext ()
173
+ .sendBroadcast (new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
159
174
assertThat (delegateChannel .resetCount ).isEqualTo (1 );
160
175
161
176
// Establish a connection
162
177
shadowOf (connectivityManager ).setActiveNetworkInfo (MOBILE_CONNECTED );
163
- RuntimeEnvironment .application .sendBroadcast (
164
- new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
178
+ ApplicationProvider
179
+ .getApplicationContext ()
180
+ .sendBroadcast (new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
165
181
assertThat (delegateChannel .resetCount ).isEqualTo (2 );
166
182
167
183
// Disconnect, then shutdown the channel and verify that the broadcast receiver has been
168
184
// unregistered
169
185
shadowOf (connectivityManager ).setActiveNetworkInfo (null );
170
- RuntimeEnvironment .application .sendBroadcast (
171
- new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
186
+ ApplicationProvider
187
+ .getApplicationContext ()
188
+ .sendBroadcast (new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
172
189
androidChannel .shutdown ();
173
190
shadowOf (connectivityManager ).setActiveNetworkInfo (MOBILE_CONNECTED );
174
- RuntimeEnvironment .application .sendBroadcast (
175
- new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
191
+ ApplicationProvider
192
+ .getApplicationContext ()
193
+ .sendBroadcast (new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
176
194
177
195
assertThat (delegateChannel .resetCount ).isEqualTo (2 );
178
196
// enterIdle is not called on API levels < 24
@@ -186,7 +204,7 @@ public void resetConnectBackoffAndEnterIdle_api24() {
186
204
TestChannel delegateChannel = new TestChannel ();
187
205
ManagedChannel androidChannel =
188
206
new AndroidChannelBuilder .AndroidChannel (
189
- delegateChannel , RuntimeEnvironment . application .getApplicationContext ());
207
+ delegateChannel , ApplicationProvider .getApplicationContext ());
190
208
assertThat (delegateChannel .resetCount ).isEqualTo (0 );
191
209
assertThat (delegateChannel .enterIdleCount ).isEqualTo (0 );
192
210
@@ -227,7 +245,7 @@ public void newChannelWithConnection_entersIdleOnSecondConnectionChange_api24()
227
245
TestChannel delegateChannel = new TestChannel ();
228
246
ManagedChannel androidChannel =
229
247
new AndroidChannelBuilder .AndroidChannel (
230
- delegateChannel , RuntimeEnvironment . application .getApplicationContext ());
248
+ delegateChannel , ApplicationProvider .getApplicationContext ());
231
249
232
250
// The first onAvailable() may just signal that the device was connected when the callback is
233
251
// registered, rather than indicating a changed network, so we do not enter idle.
@@ -248,15 +266,17 @@ public void shutdownNowUnregistersBroadcastReceiver_api23() {
248
266
TestChannel delegateChannel = new TestChannel ();
249
267
ManagedChannel androidChannel =
250
268
new AndroidChannelBuilder .AndroidChannel (
251
- delegateChannel , RuntimeEnvironment . application .getApplicationContext ());
269
+ delegateChannel , ApplicationProvider .getApplicationContext ());
252
270
253
271
shadowOf (connectivityManager ).setActiveNetworkInfo (null );
254
- RuntimeEnvironment .application .sendBroadcast (
255
- new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
272
+ ApplicationProvider
273
+ .getApplicationContext ()
274
+ .sendBroadcast (new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
256
275
androidChannel .shutdownNow ();
257
276
shadowOf (connectivityManager ).setActiveNetworkInfo (WIFI_CONNECTED );
258
- RuntimeEnvironment .application .sendBroadcast (
259
- new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
277
+ ApplicationProvider
278
+ .getApplicationContext ()
279
+ .sendBroadcast (new Intent (ConnectivityManager .CONNECTIVITY_ACTION ));
260
280
261
281
assertThat (delegateChannel .resetCount ).isEqualTo (0 );
262
282
}
@@ -268,7 +288,7 @@ public void shutdownNowUnregistersNetworkCallback_api24() {
268
288
TestChannel delegateChannel = new TestChannel ();
269
289
ManagedChannel androidChannel =
270
290
new AndroidChannelBuilder .AndroidChannel (
271
- delegateChannel , RuntimeEnvironment . application .getApplicationContext ());
291
+ delegateChannel , ApplicationProvider .getApplicationContext ());
272
292
273
293
androidChannel .shutdownNow ();
274
294
shadowOf (connectivityManager ).setActiveNetworkInfo (WIFI_CONNECTED );
@@ -318,6 +338,7 @@ private void notifyDefaultNetworkCallbacksOnLost(Network network) {
318
338
}
319
339
320
340
@ Implementation (minSdk = N )
341
+ @ Override
321
342
protected void registerDefaultNetworkCallback (
322
343
ConnectivityManager .NetworkCallback networkCallback ) {
323
344
defaultNetworkCallbacks .add (networkCallback );
0 commit comments