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

Commit 0f5d402c authored by Jason Monk's avatar Jason Monk
Browse files

Protect against weak pointer crash.

Because assuming GC won't run within any period of time is not a
valid testing strategy.

Test: runtest systemui
Change-Id: Id29ab777a70ca27b6ca33bd2a44cfb4c38f0fd09
Fixes: 64581449
parent e33e796d
Loading
Loading
Loading
Loading
+7 −4
Original line number Original line Diff line number Diff line
@@ -296,12 +296,15 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa


        @Override
        @Override
        public void run() {
        public void run() {
            mBondState = mDevice.get().getBondState();
            CachedBluetoothDevice device = mDevice.get();
            mMaxConnectionState = mDevice.get().getMaxConnectionState();
            if (device != null) {
                mBondState = device.getBondState();
                mMaxConnectionState = device.getMaxConnectionState();
                mUiHandler.removeMessages(H.MSG_PAIRED_DEVICES_CHANGED);
                mUiHandler.removeMessages(H.MSG_PAIRED_DEVICES_CHANGED);
                mUiHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED);
                mUiHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED);
            }
            }
        }
        }
    }


    private final class H extends Handler {
    private final class H extends Handler {
        private final ArrayList<BluetoothController.Callback> mCallbacks = new ArrayList<>();
        private final ArrayList<BluetoothController.Callback> mCallbacks = new ArrayList<>();
+22 −0
Original line number Original line Diff line number Diff line
@@ -137,4 +137,26 @@ public class BluetoothControllerImplTest extends SysuiTestCase {
        verify(callback).onBluetoothDevicesChanged();
        verify(callback).onBluetoothDevicesChanged();
        mainLooper.destroy();
        mainLooper.destroy();
    }
    }

    @Test
    public void testNullAsync_DoesNotCrash() throws Exception {
        CachedBluetoothDevice device = mock(CachedBluetoothDevice.class);
        when(device.getMaxConnectionState()).thenReturn(BluetoothProfile.STATE_CONNECTED);
        BluetoothController.Callback callback = mock(BluetoothController.Callback.class);
        mBluetoothControllerImpl.addCallback(callback);

        // Grab the main looper, we'll need it later.
        TestableLooper mainLooper = new TestableLooper(Looper.getMainLooper());

        try {
            // Trigger the state getting.
            assertEquals(BluetoothProfile.STATE_DISCONNECTED,
                    mBluetoothControllerImpl.getMaxConnectionState(null));

            mTestableLooper.processMessages(1);
            mainLooper.processAllMessages();
        } finally {
            mainLooper.destroy();
        }
    }
}
}