Fix NPE when connect timeout

If user click connect button and then receive timeout event after leave
detail page, the NPE happened. Ignore the timeout event if activity
already gone.

Bug: 131141866
Test: manual test
Test: make RunSettingsRoboTests -j32 ROBOTEST_FILTER=com.android.settings.wifi.details.WifiDetailPreferenceControllerTest
Change-Id: Ic22c22027386d4a6de5d693eafcd9cd463ed3415
This commit is contained in:
clownshen
2019-04-24 13:52:06 +08:00
committed by Clown SHEN
parent b70f2b58e0
commit dd4764b7f4
2 changed files with 37 additions and 0 deletions

View File

@@ -1109,6 +1109,10 @@ public class WifiDetailPreferenceController extends AbstractPreferenceController
}
@Override
public void onFinish() {
if (mFragment == null || mFragment.getActivity() == null) {
Log.d(TAG, "Ignore timeout since activity not exist!");
return;
}
Log.e(TAG, "Timeout for state:" + mConnectingState);
if (mConnectingState == STATE_ENABLE_WIFI) {
updateConnectingState(STATE_ENABLE_WIFI_FAILED);

View File

@@ -1623,6 +1623,39 @@ public class WifiDetailPreferenceControllerTest {
mContext.getString(R.string.wifi_failed_connect_message));
}
@Test
public void testConnectButton_clickConnectAndBackKey_ignoreTimeoutEvent() {
setUpForDisconnectedNetwork();
when(mockWifiManager.isWifiEnabled()).thenReturn(true);
InOrder inOrder = inOrder(mockButtonsPref);
setUpForToast();
displayAndResume();
// check connect button exist
verifyConnectBtnSetUpAsVisible(inOrder);
// click connect button
mController.connectNetwork();
// check display button as connecting
verify(mockWifiManager, times(1)).connect(anyInt(), any(WifiManager.ActionListener.class));
verifyConnectBtnSetUpAsConnecting(inOrder);
// leave detail page
when(mockFragment.getActivity()).thenReturn(null);
// timeout happened
mController.mTimer.onFinish();
// check connect button visible, be init as default and toast failed message
inOrder.verify(mockButtonsPref, never()).setButton3Text(R.string.wifi_connect);
inOrder.verify(mockButtonsPref, never()).setButton3Icon(R.drawable.ic_settings_wireless);
inOrder.verify(mockButtonsPref, never()).setButton3Enabled(true);
inOrder.verify(mockButtonsPref, never()).setButton3Visible(true);
assertThat(ShadowToast.shownToastCount()).isEqualTo(0);
}
@Test
public void updateAccessPoint_returnFalseForNothingChanged() {
setUpForDisconnectedNetwork();