|
19 | 19 | import static android.content.Context.BIND_AUTO_CREATE; |
20 | 20 | import static android.os.Looper.getMainLooper; |
21 | 21 | import static com.google.common.truth.Truth.assertThat; |
| 22 | +import static org.junit.Assert.assertThrows; |
22 | 23 | import static org.junit.Assert.fail; |
23 | 24 | import static org.robolectric.Shadows.shadowOf; |
24 | 25 |
|
|
35 | 36 | import androidx.test.core.app.ApplicationProvider; |
36 | 37 | import io.grpc.Status; |
37 | 38 | import io.grpc.Status.Code; |
| 39 | +import io.grpc.StatusException; |
38 | 40 | import io.grpc.binder.BinderChannelCredentials; |
39 | 41 | import io.grpc.binder.internal.Bindable.Observer; |
40 | 42 | import java.util.Arrays; |
@@ -79,6 +81,7 @@ public void setUp() { |
79 | 81 |
|
80 | 82 | // Don't call onServiceDisconnected() upon unbindService(), just like the real Android doesn't. |
81 | 83 | shadowApplication.setUnbindServiceCallsOnServiceDisconnected(false); |
| 84 | + shadowApplication.setBindServiceCallsOnServiceConnectedDirectly(false); |
82 | 85 |
|
83 | 86 | binding = newBuilder().build(); |
84 | 87 | shadowOf(getMainLooper()).idle(); |
@@ -283,18 +286,30 @@ public void testBindWithTargetUserHandle() throws Exception { |
283 | 286 |
|
284 | 287 | @Test |
285 | 288 | public void testResolve() throws Exception { |
| 289 | + serviceInfo.processName = "x"; // ServiceInfo has no equals() so look for one distinctive field. |
| 290 | + shadowOf(appContext.getPackageManager()).addOrUpdateService(serviceInfo); |
286 | 291 | ServiceInfo resolvedServiceInfo = binding.resolve(); |
287 | | - assertThat(resolvedServiceInfo.packageName).isEqualTo(serviceInfo.packageName); |
288 | | - assertThat(resolvedServiceInfo.name).isEqualTo(serviceInfo.name); |
| 292 | + assertThat(resolvedServiceInfo.processName).isEqualTo(serviceInfo.processName); |
289 | 293 | } |
290 | 294 |
|
291 | 295 | @Test |
292 | 296 | @Config(sdk = 33) |
293 | 297 | public void testResolveWithTargetUserHandle() throws Exception { |
| 298 | + serviceInfo.processName = "x"; // ServiceInfo has no equals() so look for one distinctive field. |
| 299 | + // Robolectric just ignores the user arg to resolveServiceAsUser() so this is all we can do. |
| 300 | + shadowOf(appContext.getPackageManager()).addOrUpdateService(serviceInfo); |
294 | 301 | binding = newBuilder().setTargetUserHandle(generateUserHandle(/* userId= */ 0)).build(); |
295 | 302 | ServiceInfo resolvedServiceInfo = binding.resolve(); |
296 | | - assertThat(resolvedServiceInfo.packageName).isEqualTo(serviceInfo.packageName); |
297 | | - assertThat(resolvedServiceInfo.name).isEqualTo(serviceInfo.name); |
| 303 | + assertThat(resolvedServiceInfo.processName).isEqualTo(serviceInfo.processName); |
| 304 | + } |
| 305 | + |
| 306 | + @Test |
| 307 | + public void testResolveNonExistentServiceThrows() throws Exception { |
| 308 | + ComponentName doesNotExistService = new ComponentName("does.not.exist", "NoService"); |
| 309 | + binding = newBuilder().setTargetComponent(doesNotExistService).build(); |
| 310 | + StatusException statusException = assertThrows(StatusException.class, binding::resolve); |
| 311 | + assertThat(statusException.getStatus().getCode()).isEqualTo(Code.UNIMPLEMENTED); |
| 312 | + assertThat(statusException.getStatus().getDescription()).contains("does.not.exist"); |
298 | 313 | } |
299 | 314 |
|
300 | 315 | @Test |
|
0 commit comments