From aaf8e1f3f4873756d9752c6abd52346551a03dd3 Mon Sep 17 00:00:00 2001 From: Julia Reynolds Date: Fri, 17 Sep 2021 16:00:53 -0400 Subject: [PATCH] Handle onNullBinding According to the docs, an onNullBinding requires the service to be manually unbound Test: A test NLS service that returns null from onBind; ensure that the ServiceRecord does not appear in the bugreport when that app has NLS permission Bug: 192475653 Change-Id: I784910865fa11a5f88c4e6821007564d3aea7ec9 --- .../server/notification/ManagedServices.java | 2 +- .../server/notification/ManagedServicesTest.java | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/services/core/java/com/android/server/notification/ManagedServices.java b/services/core/java/com/android/server/notification/ManagedServices.java index bccc52fe350a6..ddaaa1eeff4a0 100644 --- a/services/core/java/com/android/server/notification/ManagedServices.java +++ b/services/core/java/com/android/server/notification/ManagedServices.java @@ -1536,7 +1536,7 @@ abstract public class ManagedServices { @Override public void onNullBinding(ComponentName name) { Slog.v(TAG, "onNullBinding() called with: name = [" + name + "]"); - mServicesBound.remove(servicesBindingTag); + mContext.unbindService(this); } }; if (!mContext.bindServiceAsUser(intent, diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ManagedServicesTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ManagedServicesTest.java index f9663f200b56d..987236c7c98c3 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/ManagedServicesTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/ManagedServicesTest.java @@ -65,6 +65,7 @@ import com.google.android.collect.Lists; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.invocation.InvocationOnMock; @@ -1320,16 +1321,15 @@ public class ManagedServicesTest extends UiServiceTestCase { APPROVAL_BY_COMPONENT); ComponentName cn = ComponentName.unflattenFromString("a/a"); - service.registerSystemService(cn, 0); - when(context.bindServiceAsUser(any(), any(), anyInt(), any())).thenAnswer(invocation -> { - Object[] args = invocation.getArguments(); - ServiceConnection sc = (ServiceConnection) args[1]; - sc.onNullBinding(cn); - return true; - }); + ArgumentCaptor captor = ArgumentCaptor.forClass(ServiceConnection.class); + when(context.bindServiceAsUser(any(), captor.capture(), anyInt(), any())) + .thenAnswer(invocation -> { + captor.getValue().onNullBinding(cn); + return true; + }); service.registerSystemService(cn, 0); - assertFalse(service.isBound(cn, 0)); + verify(context).unbindService(captor.getValue()); } @Test