From 125301d5907060988698a59803a3c2020f5de4e8 Mon Sep 17 00:00:00 2001 From: Jeremy Goldman Date: Tue, 27 Oct 2020 08:06:20 +0000 Subject: [PATCH] Fix flaky JUnit test This test sometimes fails in PostSubmits because the background thread which calls the verified class does not have time to execute before the class is verified. This creates a helper function to monitor background thread completion. Test: atest -c AutoSelectPreferenceControllerTest Change-Id: I6c320abd70fcb5fe823ec122fb0dac67174ecb4a --- .../gsm/AutoSelectPreferenceController.java | 47 ++++++++++--------- .../AutoSelectPreferenceControllerTest.java | 15 +++++- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java b/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java index d98136290e1..5fa70f922b4 100644 --- a/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java +++ b/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java @@ -51,6 +51,7 @@ import com.android.settingslib.utils.ThreadUtils; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; /** @@ -147,27 +148,7 @@ public class AutoSelectPreferenceController extends TelephonyTogglePreferenceCon @Override public boolean setChecked(boolean isChecked) { if (isChecked) { - final long startMillis = SystemClock.elapsedRealtime(); - showAutoSelectProgressBar(); - mSwitchPreference.setEnabled(false); - ThreadUtils.postOnBackgroundThread(() -> { - // set network selection mode in background - mTelephonyManager.setNetworkSelectionModeAutomatic(); - final int mode = mTelephonyManager.getNetworkSelectionMode(); - - //Update UI in UI thread - final long durationMillis = SystemClock.elapsedRealtime() - startMillis; - mUiHandler.postDelayed(() -> { - mSwitchPreference.setEnabled(true); - mSwitchPreference.setChecked( - mode == TelephonyManager.NETWORK_SELECTION_MODE_AUTO); - for (OnNetworkSelectModeListener lsn : mListeners) { - lsn.onNetworkSelectModeChanged(); - } - dismissProgressBar(); - }, - Math.max(MINIMUM_DIALOG_TIME_MILLIS - durationMillis, 0)); - }); + setAutomaticSelectionMode(); return false; } else { final Bundle bundle = new Bundle(); @@ -182,6 +163,30 @@ public class AutoSelectPreferenceController extends TelephonyTogglePreferenceCon } } + @VisibleForTesting + Future setAutomaticSelectionMode() { + final long startMillis = SystemClock.elapsedRealtime(); + showAutoSelectProgressBar(); + mSwitchPreference.setEnabled(false); + return ThreadUtils.postOnBackgroundThread(() -> { + // set network selection mode in background + mTelephonyManager.setNetworkSelectionModeAutomatic(); + final int mode = mTelephonyManager.getNetworkSelectionMode(); + + //Update UI in UI thread + final long durationMillis = SystemClock.elapsedRealtime() - startMillis; + mUiHandler.postDelayed(() -> { + mSwitchPreference.setEnabled(true); + mSwitchPreference.setChecked( + mode == TelephonyManager.NETWORK_SELECTION_MODE_AUTO); + for (OnNetworkSelectModeListener lsn : mListeners) { + lsn.onNetworkSelectModeChanged(); + } + dismissProgressBar(); + }, Math.max(MINIMUM_DIALOG_TIME_MILLIS - durationMillis, 0)); + }); + } + public AutoSelectPreferenceController init(Lifecycle lifecycle, int subId) { mSubId = subId; mTelephonyManager = mContext.getSystemService(TelephonyManager.class) diff --git a/tests/unit/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceControllerTest.java index 98c71f59db0..1364b24ed04 100644 --- a/tests/unit/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceControllerTest.java +++ b/tests/unit/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceControllerTest.java @@ -18,6 +18,7 @@ package com.android.settings.network.telephony.gsm; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.fail; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; @@ -42,6 +43,9 @@ import org.mockito.Answers; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + @RunWith(AndroidJUnit4.class) public class AutoSelectPreferenceControllerTest { private static final int SUB_ID = 2; @@ -93,7 +97,14 @@ public class AutoSelectPreferenceControllerTest { when(mTelephonyManager.getNetworkSelectionMode()).thenReturn( TelephonyManager.NETWORK_SELECTION_MODE_AUTO); - assertThat(mController.setChecked(true)).isFalse(); + // Wait for asynchronous thread to finish, otherwise test will flake. + Future thread = mController.setAutomaticSelectionMode(); + try { + thread.get(); + } catch (ExecutionException | InterruptedException e) { + e.printStackTrace(); + fail("Exception during automatic selection"); + } verify(mProgressDialog).show(); verify(mTelephonyManager).setNetworkSelectionModeAutomatic(); @@ -136,4 +147,4 @@ public class AutoSelectPreferenceControllerTest { public String resourceString(String name, Object value) { return mContext.getResources().getString(resourceId("string", name), value); } -} \ No newline at end of file +}