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

Commit ae34fd93 authored by Brad Ebinger's avatar Brad Ebinger Committed by android-build-merger
Browse files

Merge "Wait for handlers to clear in SST test before continuing"

am: f7f243ac

Change-Id: I669c099f267b59f91c8cdebb84c42e1cbb019ed7
parents 18bb5051 f7f243ac
Loading
Loading
Loading
Loading
+4 −6
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@

package com.android.internal.telephony;

import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.doReturn;
@@ -151,7 +149,7 @@ public class CarrierServiceStateTrackerTest extends TelephonyTest {
        logd(LOG_TAG + ":testSendPrefNetworkNotification()");
        Intent intent = new Intent().setAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
        mContext.sendBroadcast(intent);
        waitForMs(300);
        waitForLastHandlerAction(mCarrierServiceStateTrackerTestHandler.getThreadHandler());

        Map<Integer, CarrierServiceStateTracker.NotificationType> notificationTypeMap =
                mCarrierSST.getNotificationTypeMap();
@@ -172,7 +170,7 @@ public class CarrierServiceStateTrackerTest extends TelephonyTest {
                RILConstants.NETWORK_MODE_LTE_CDMA_EVDO);
        mSpyCarrierSST.getContentObserver().dispatchChange(false,
                Settings.Global.getUriFor(prefNetworkMode));
        waitForMs(500);
        waitForLastHandlerAction(mCarrierServiceStateTrackerTestHandler.getThreadHandler());
        verify(mNotificationManager, atLeast(1)).notify(
                eq(CarrierServiceStateTracker.PREF_NETWORK_NOTIFICATION_TAG),
                eq(SUB_ID), isA(Notification.class));
@@ -182,7 +180,7 @@ public class CarrierServiceStateTrackerTest extends TelephonyTest {
                RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA);
        mSpyCarrierSST.getContentObserver().dispatchChange(false,
                Settings.Global.getUriFor(prefNetworkMode));
        waitForMs(500);
        waitForLastHandlerAction(mCarrierServiceStateTrackerTestHandler.getThreadHandler());
        verify(mNotificationManager, atLeast(1)).cancel(
                CarrierServiceStateTracker.PREF_NETWORK_NOTIFICATION_TAG, SUB_ID);
    }
@@ -193,7 +191,7 @@ public class CarrierServiceStateTrackerTest extends TelephonyTest {
        logd(LOG_TAG + ":testSendEmergencyNetworkNotification()");
        Intent intent = new Intent().setAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
        mContext.sendBroadcast(intent);
        waitForMs(300);
        waitForLastHandlerAction(mCarrierServiceStateTrackerTestHandler.getThreadHandler());

        Map<Integer, CarrierServiceStateTracker.NotificationType> notificationTypeMap =
                mCarrierSST.getNotificationTypeMap();
+88 −90

File changed.

Preview size limit exceeded, changes collapsed.

+27 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.internal.telephony;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Matchers.anyBoolean;
@@ -708,6 +709,32 @@ public abstract class TelephonyTest {
        }
    }

    /**
     * Wait for up to 1 second for the handler message queue to clear.
     */
    protected final void waitForLastHandlerAction(Handler h) {
        CountDownLatch lock = new CountDownLatch(1);
        // Allow the handler to start work on stuff.
        h.postDelayed(lock::countDown, 100);
        int timeoutCount = 0;
        while (timeoutCount < 5) {
            try {
                if (lock.await(200, TimeUnit.MILLISECONDS)) {
                    // no messages in queue, stop waiting.
                    if (!h.hasMessagesOrCallbacks()) break;
                    lock = new CountDownLatch(1);
                    // Delay to allow the handler thread to start work on stuff.
                    h.postDelayed(lock::countDown, 100);
                }

            } catch (InterruptedException e) {
                // do nothing
            }
            timeoutCount++;
        }
        assertTrue("Handler was not empty before timeout elapsed", timeoutCount < 5);
    }

    protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) {
        final CountDownLatch lock = new CountDownLatch(1);
        h.postDelayed(lock::countDown, delayMs);