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

Commit c4421b9e authored by Automerger Merge Worker's avatar Automerger Merge Worker Committed by Android (Google) Code Review
Browse files

Merge "Merge "Handle bindService errors in ObservableServiceConnection" into...

Merge "Merge "Handle bindService errors in ObservableServiceConnection" into tm-qpr-dev am: 5846d277 am: 0b3460ca am: 5b5ad203 am: 79b19266"
parents 417fb393 a62506f9
Loading
Loading
Loading
Loading
+12 −6
Original line number Diff line number Diff line
@@ -143,7 +143,13 @@ public class ObservableServiceConnection<T> implements ServiceConnection {
     * @return {@code true} if initiating binding succeed, {@code false} otherwise.
     */
    public boolean bind() {
        final boolean bindResult = mContext.bindService(mServiceIntent, mFlags, mExecutor, this);
        boolean bindResult = false;
        try {
            bindResult = mContext.bindService(mServiceIntent, mFlags, mExecutor, this);
        } 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);
+17 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.systemui.util.service;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.clearInvocations;
@@ -169,4 +171,19 @@ public class ObservableServiceConnectionTest extends SysuiTestCase {
        verify(mCallback).onDisconnected(eq(connection),
                eq(ObservableServiceConnection.DISCONNECT_REASON_UNBIND));
    }

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

        when(mContext.bindService(eq(mIntent), anyInt(), eq(mExecutor), eq(connection)))
                .thenThrow(new SecurityException());

        // Verify that the exception was caught and that bind returns false, and we properly
        // unbind.
        assertThat(connection.bind()).isFalse();
        verify(mContext).unbindService(connection);
    }
}