Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a66b1fff authored by William Leshner's avatar William Leshner Committed by Android (Google) Code Review
Browse files

Merge "Fix an issue with unbinding in ObservableServiceConnection." into main

parents 1ff9e4d3 e079c300
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -158,11 +158,11 @@ public class ObservableServiceConnection<T> implements ServiceConnection {
        try {
            bindResult = mContext.bindServiceAsUser(mServiceIntent, this, mFlags,
                    mUserTracker.getUserHandle());
            mBoundCalled = true;
        } catch (SecurityException e) {
            Log.d(TAG, "Could not bind to service", e);
            mContext.unbindService(this);
        }
        mBoundCalled = true;
        if (DEBUG) {
            Log.d(TAG, "bind. bound:" + bindResult);
        }
+21 −0
Original line number Diff line number Diff line
@@ -200,4 +200,25 @@ public class ObservableServiceConnectionTest extends SysuiTestCase {
        assertThat(connection.bind()).isFalse();
        verify(mContext).unbindService(connection);
    }

    @Test
    public void testUnbindDoesNotCallUnbindServiceWhenBindThrowsError() {
        ObservableServiceConnection<Foo> connection = new ObservableServiceConnection<>(mContext,
                mIntent, mUserTracker, mExecutor, mTransformer);
        connection.addCallback(mCallback);

        when(mContext.bindServiceAsUser(eq(mIntent), eq(connection), anyInt(),
                eq(UserHandle.of(MAIN_USER_ID))))
                .thenThrow(new SecurityException());

        // Verify that bind returns false and we properly unbind.
        assertThat(connection.bind()).isFalse();
        verify(mContext).unbindService(connection);

        clearInvocations(mContext);

        // Ensure unbind after the failed bind has no effect.
        connection.unbind();
        verify(mContext, never()).unbindService(eq(connection));
    }
}