Merge "Add unbind if bind call returns false" into tm-dev

This commit is contained in:
Fabian Kozynski
2022-03-01 15:35:18 +00:00
committed by Android (Google) Code Review
4 changed files with 55 additions and 6 deletions

View File

@@ -97,7 +97,11 @@ class ControlsProviderLifecycleManager(
}
bindTryCount++
try {
context.bindServiceAsUser(intent, serviceConnection, BIND_FLAGS, user)
val bound = context
.bindServiceAsUser(intent, serviceConnection, BIND_FLAGS, user)
if (!bound) {
context.unbindService(serviceConnection)
}
} catch (e: SecurityException) {
Log.e(TAG, "Failed to bind to service", e)
}

View File

@@ -204,6 +204,9 @@ public class TileLifecycleManager extends BroadcastReceiver implements
| Context.BIND_ALLOW_BACKGROUND_ACTIVITY_STARTS
| Context.BIND_WAIVE_PRIORITY,
mUser);
if (!mIsBound) {
mContext.unbindService(this);
}
} catch (SecurityException e) {
Log.e(TAG, "Failed to bind to service", e);
mIsBound = false;

View File

@@ -210,4 +210,25 @@ class ControlsProviderLifecycleManagerTest : SysuiTestCase() {
eq(actionCallbackService))
assertEquals(action, wrapperCaptor.getValue().getWrappedAction())
}
@Test
fun testFalseBindCallsUnbind() {
val falseContext = mock(Context::class.java)
`when`(falseContext.bindServiceAsUser(any(), any(), anyInt(), any())).thenReturn(false)
val manager = ControlsProviderLifecycleManager(
falseContext,
executor,
actionCallbackService,
UserHandle.of(0),
componentName
)
manager.bindService()
executor.runAllReady()
val captor = ArgumentCaptor.forClass(
ServiceConnection::class.java
)
verify(falseContext).bindServiceAsUser(any(), captor.capture(), anyInt(), any())
verify(falseContext).unbindService(captor.value)
}
}

View File

@@ -23,6 +23,7 @@ import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -34,6 +35,7 @@ import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo;
import android.content.pm.ServiceInfo;
import android.net.Uri;
@@ -56,7 +58,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.ArgumentCaptor;
@SmallTest
@RunWith(AndroidJUnit4.class)
@@ -64,10 +66,10 @@ public class TileLifecycleManagerTest extends SysuiTestCase {
private static final int TEST_FAIL_TIMEOUT = 5000;
private final PackageManagerAdapter mMockPackageManagerAdapter =
Mockito.mock(PackageManagerAdapter.class);
mock(PackageManagerAdapter.class);
private final BroadcastDispatcher mMockBroadcastDispatcher =
Mockito.mock(BroadcastDispatcher.class);
private final IQSTileService.Stub mMockTileService = Mockito.mock(IQSTileService.Stub.class);
mock(BroadcastDispatcher.class);
private final IQSTileService.Stub mMockTileService = mock(IQSTileService.Stub.class);
private ComponentName mTileServiceComponentName;
private Intent mTileServiceIntent;
private UserHandle mUser;
@@ -95,7 +97,7 @@ public class TileLifecycleManagerTest extends SysuiTestCase {
mThread.start();
mHandler = Handler.createAsync(mThread.getLooper());
mStateManager = new TileLifecycleManager(mHandler, mWrappedContext,
Mockito.mock(IQSService.class),
mock(IQSService.class),
mMockPackageManagerAdapter,
mMockBroadcastDispatcher,
mTileServiceIntent,
@@ -269,6 +271,25 @@ public class TileLifecycleManagerTest extends SysuiTestCase {
assertTrue(mStateManager.isToggleableTile());
}
@Test
public void testFalseBindCallsUnbind() {
Context falseContext = mock(Context.class);
when(falseContext.bindServiceAsUser(any(), any(), anyInt(), any())).thenReturn(false);
TileLifecycleManager manager = new TileLifecycleManager(mHandler, falseContext,
mock(IQSService.class),
mMockPackageManagerAdapter,
mMockBroadcastDispatcher,
mTileServiceIntent,
mUser);
manager.setBindService(true);
ArgumentCaptor<ServiceConnection> captor = ArgumentCaptor.forClass(ServiceConnection.class);
verify(falseContext).bindServiceAsUser(any(), captor.capture(), anyInt(), any());
verify(falseContext).unbindService(captor.getValue());
}
private static class TestContextWrapper extends ContextWrapper {
private IntentFilter mLastIntentFilter;
private int mLastFlag;