Merge "Merge "Protect against weak pointer crash." into oc-mr1-dev am: 0480580696" into oc-mr1-dev-plus-aosp

This commit is contained in:
Android Build Merger (Role)
2017-08-16 17:39:33 +00:00
committed by Android (Google) Code Review
2 changed files with 29 additions and 4 deletions

View File

@@ -296,10 +296,13 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa
@Override
public void run() {
mBondState = mDevice.get().getBondState();
mMaxConnectionState = mDevice.get().getMaxConnectionState();
mUiHandler.removeMessages(H.MSG_PAIRED_DEVICES_CHANGED);
mUiHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED);
CachedBluetoothDevice device = mDevice.get();
if (device != null) {
mBondState = device.getBondState();
mMaxConnectionState = device.getMaxConnectionState();
mUiHandler.removeMessages(H.MSG_PAIRED_DEVICES_CHANGED);
mUiHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED);
}
}
}

View File

@@ -137,4 +137,26 @@ public class BluetoothControllerImplTest extends SysuiTestCase {
verify(callback).onBluetoothDevicesChanged();
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();
}
}
}