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

Commit e079c300 authored by Will Leshner's avatar Will Leshner
Browse files

Fix an issue with unbinding in ObservableServiceConnection.

Bug: 322977114
Test: atest ObservableServiceConnectionTest
Flag: NA
Change-Id: Iccceb08690952cef7926cd4e959318bc4d96d19c
parent 12adb88b
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -158,11 +158,11 @@ public class ObservableServiceConnection<T> implements ServiceConnection {
        try {
        try {
            bindResult = mContext.bindServiceAsUser(mServiceIntent, this, mFlags,
            bindResult = mContext.bindServiceAsUser(mServiceIntent, this, mFlags,
                    mUserTracker.getUserHandle());
                    mUserTracker.getUserHandle());
            mBoundCalled = true;
        } catch (SecurityException e) {
        } catch (SecurityException e) {
            Log.d(TAG, "Could not bind to service", e);
            Log.d(TAG, "Could not bind to service", e);
            mContext.unbindService(this);
            mContext.unbindService(this);
        }
        }
        mBoundCalled = true;
        if (DEBUG) {
        if (DEBUG) {
            Log.d(TAG, "bind. bound:" + bindResult);
            Log.d(TAG, "bind. bound:" + bindResult);
        }
        }
+21 −0
Original line number Original line Diff line number Diff line
@@ -200,4 +200,25 @@ public class ObservableServiceConnectionTest extends SysuiTestCase {
        assertThat(connection.bind()).isFalse();
        assertThat(connection.bind()).isFalse();
        verify(mContext).unbindService(connection);
        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));
    }
}
}