Merge "Add a common test library."
diff --git a/packages/NetworkStack/tests/lib/Android.bp b/packages/NetworkStack/tests/lib/Android.bp
new file mode 100644
index 0000000..f45a81c
--- /dev/null
+++ b/packages/NetworkStack/tests/lib/Android.bp
@@ -0,0 +1,26 @@
+//
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+java_library {
+    name: "net-tests-utils",
+    srcs: [
+        "src/**/*.java",
+        "src/**/*.kt",
+    ],
+    static_libs: [
+        "kotlin-test",
+    ],
+}
diff --git a/packages/NetworkStack/tests/lib/src/com/android/testutils/HandlerUtils.kt b/packages/NetworkStack/tests/lib/src/com/android/testutils/HandlerUtils.kt
new file mode 100644
index 0000000..3dce5a5
--- /dev/null
+++ b/packages/NetworkStack/tests/lib/src/com/android/testutils/HandlerUtils.kt
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.testutils
+
+import android.os.ConditionVariable
+import android.os.Handler
+import android.os.HandlerThread
+import java.util.concurrent.Executor
+import kotlin.test.fail
+
+/**
+ * Block until the specified Handler or HandlerThread becomes idle, or until timeoutMs has passed.
+ */
+fun Handler.waitForIdle(timeoutMs: Long) = waitForIdleHandler(this, timeoutMs)
+fun HandlerThread.waitForIdle(timeoutMs: Long) = waitForIdleHandler(this.threadHandler, timeoutMs)
+fun waitForIdleHandler(handler: HandlerThread, timeoutMs: Long) {
+    waitForIdleHandler(handler.threadHandler, timeoutMs)
+}
+fun waitForIdleHandler(handler: Handler, timeoutMs: Long) {
+    val cv = ConditionVariable(false)
+    handler.post(cv::open)
+    if (!cv.block(timeoutMs)) {
+        fail("Handler did not become idle after ${timeoutMs}ms")
+    }
+}
+
+/**
+ * Block until the given Serial Executor becomes idle, or until timeoutMs has passed.
+ */
+fun waitForIdleSerialExecutor(executor: Executor, timeoutMs: Long) {
+    val cv = ConditionVariable()
+    executor.execute(cv::open)
+    if (!cv.block(timeoutMs)) {
+        fail("Executor did not become idle after ${timeoutMs}ms")
+    }
+}
diff --git a/packages/NetworkStack/tests/unit/Android.bp b/packages/NetworkStack/tests/unit/Android.bp
index 85951eb..48b13b0 100644
--- a/packages/NetworkStack/tests/unit/Android.bp
+++ b/packages/NetworkStack/tests/unit/Android.bp
@@ -23,6 +23,7 @@
     static_libs: [
         "androidx.test.rules",
         "mockito-target-extended-minus-junit4",
+        "net-tests-utils",
         "NetworkStackBase",
         "testables",
     ],
diff --git a/packages/NetworkStack/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java b/packages/NetworkStack/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java
index 262641d..e4c1d17 100644
--- a/packages/NetworkStack/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java
+++ b/packages/NetworkStack/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java
@@ -92,6 +92,7 @@
 import com.android.networkstack.R;
 import com.android.networkstack.metrics.DataStallDetectionStats;
 import com.android.networkstack.metrics.DataStallStatsUtils;
+import com.android.testutils.HandlerUtilsKt;
 
 import org.junit.After;
 import org.junit.Before;
@@ -420,7 +421,7 @@
         final WrappedNetworkMonitor nm = new WrappedNetworkMonitor();
         nm.start();
         setNetworkCapabilities(nm, nc);
-        waitForIdle(nm.getHandler());
+        HandlerUtilsKt.waitForIdle(nm.getHandler(), HANDLER_TIMEOUT_MS);
         mCreatedNetworkMonitors.add(nm);
         return nm;
     }
@@ -437,15 +438,7 @@
 
     private void setNetworkCapabilities(NetworkMonitor nm, NetworkCapabilities nc) {
         nm.notifyNetworkCapabilitiesChanged(nc);
-        waitForIdle(nm.getHandler());
-    }
-
-    private void waitForIdle(Handler handler) {
-        final ConditionVariable cv = new ConditionVariable(false);
-        handler.post(cv::open);
-        if (!cv.block(HANDLER_TIMEOUT_MS)) {
-            fail("Timed out waiting for handler");
-        }
+        HandlerUtilsKt.waitForIdle(nm.getHandler(), HANDLER_TIMEOUT_MS);
     }
 
     @Test
@@ -1125,7 +1118,7 @@
         } catch (RemoteException e) {
             fail("Unexpected exception: " + e);
         }
-        waitForIdle(monitor.getHandler());
+        HandlerUtilsKt.waitForIdle(monitor.getHandler(), HANDLER_TIMEOUT_MS);
 
         return monitor;
     }
diff --git a/tests/net/Android.bp b/tests/net/Android.bp
index 306cc51..eb25acf 100644
--- a/tests/net/Android.bp
+++ b/tests/net/Android.bp
@@ -10,6 +10,7 @@
         "framework-protos",
         "androidx.test.rules",
         "mockito-target-minus-junit4",
+        "net-tests-utils",
         "platform-test-annotations",
         "services.core",
         "services.net",
diff --git a/tests/net/java/android/net/nsd/NsdManagerTest.java b/tests/net/java/android/net/nsd/NsdManagerTest.java
index 2d2bccb..cf7587a 100644
--- a/tests/net/java/android/net/nsd/NsdManagerTest.java
+++ b/tests/net/java/android/net/nsd/NsdManagerTest.java
@@ -16,8 +16,6 @@
 
 package android.net.nsd;
 
-import static com.android.internal.util.TestUtils.waitForIdleHandler;
-
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
@@ -40,6 +38,7 @@
 import androidx.test.runner.AndroidJUnit4;
 
 import com.android.internal.util.AsyncChannel;
+import com.android.testutils.HandlerUtilsKt;
 
 import org.junit.After;
 import org.junit.Before;
@@ -74,7 +73,7 @@
 
     @After
     public void tearDown() throws Exception {
-        mServiceHandler.waitForIdle(mTimeoutMs);
+        HandlerUtilsKt.waitForIdle(mServiceHandler, mTimeoutMs);
         mServiceHandler.chan.disconnect();
         mServiceHandler.stop();
         if (mManager != null) {
@@ -334,7 +333,7 @@
     }
 
     int verifyRequest(int expectedMessageType) {
-        mServiceHandler.waitForIdle(mTimeoutMs);
+        HandlerUtilsKt.waitForIdle(mServiceHandler, mTimeoutMs);
         verify(mServiceHandler, timeout(mTimeoutMs)).handleMessage(any());
         reset(mServiceHandler);
         Message received = mServiceHandler.getLastMessage();
@@ -366,10 +365,6 @@
             lastMessage.copyFrom(msg);
         }
 
-        void waitForIdle(long timeoutMs) {
-            waitForIdleHandler(this, timeoutMs);
-        }
-
         @Override
         public void handleMessage(Message msg) {
             setLastMessage(msg);
diff --git a/tests/net/java/com/android/server/ConnectivityServiceTest.java b/tests/net/java/com/android/server/ConnectivityServiceTest.java
index cb774ba..1a8d905 100644
--- a/tests/net/java/com/android/server/ConnectivityServiceTest.java
+++ b/tests/net/java/com/android/server/ConnectivityServiceTest.java
@@ -68,10 +68,6 @@
 import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
 import static android.net.RouteInfo.RTN_UNREACHABLE;
 
-import static com.android.internal.util.TestUtils.waitForIdleHandler;
-import static com.android.internal.util.TestUtils.waitForIdleLooper;
-import static com.android.internal.util.TestUtils.waitForIdleSerialExecutor;
-
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -196,6 +192,7 @@
 import com.android.server.connectivity.Vpn;
 import com.android.server.net.NetworkPinner;
 import com.android.server.net.NetworkPolicyManagerInternal;
+import com.android.testutils.HandlerUtilsKt;
 
 import org.junit.After;
 import org.junit.Before;
@@ -375,19 +372,19 @@
 
     public void waitForIdle(int timeoutMsAsInt) {
         long timeoutMs = timeoutMsAsInt;
-        waitForIdleHandler(mService.mHandlerThread, timeoutMs);
+        HandlerUtilsKt.waitForIdle(mService.mHandlerThread, timeoutMs);
         waitForIdle(mCellNetworkAgent, timeoutMs);
         waitForIdle(mWiFiNetworkAgent, timeoutMs);
         waitForIdle(mEthernetNetworkAgent, timeoutMs);
-        waitForIdleHandler(mService.mHandlerThread, timeoutMs);
-        waitForIdleLooper(ConnectivityThread.getInstanceLooper(), timeoutMs);
+        HandlerUtilsKt.waitForIdle(mService.mHandlerThread, timeoutMs);
+        HandlerUtilsKt.waitForIdle(ConnectivityThread.get(), timeoutMs);
     }
 
     public void waitForIdle(MockNetworkAgent agent, long timeoutMs) {
         if (agent == null) {
             return;
         }
-        waitForIdleHandler(agent.mHandlerThread, timeoutMs);
+        HandlerUtilsKt.waitForIdle(agent.mHandlerThread, timeoutMs);
     }
 
     private void waitForIdle() {
@@ -1220,7 +1217,7 @@
         }
 
         public void waitForIdle(int timeoutMs) {
-            waitForIdleHandler(mHandlerThread, timeoutMs);
+            HandlerUtilsKt.waitForIdle(mHandlerThread, timeoutMs);
         }
 
         public void waitForIdle() {
@@ -4107,7 +4104,7 @@
         }
 
         public void assertNoCallback() {
-            waitForIdleSerialExecutor(mExecutor, TIMEOUT_MS);
+            HandlerUtilsKt.waitForIdleSerialExecutor(mExecutor, TIMEOUT_MS);
             CallbackValue cv = mCallbacks.peek();
             assertNull("Unexpected callback: " + cv, cv);
         }
diff --git a/tests/net/java/com/android/server/connectivity/tethering/OffloadControllerTest.java b/tests/net/java/com/android/server/connectivity/tethering/OffloadControllerTest.java
index be54b1a..762b76c 100644
--- a/tests/net/java/com/android/server/connectivity/tethering/OffloadControllerTest.java
+++ b/tests/net/java/com/android/server/connectivity/tethering/OffloadControllerTest.java
@@ -51,7 +51,6 @@
 import android.net.NetworkStats;
 import android.net.RouteInfo;
 import android.net.util.SharedLog;
-import android.os.ConditionVariable;
 import android.os.Handler;
 import android.os.INetworkManagementService;
 import android.os.Looper;
@@ -63,6 +62,7 @@
 import androidx.test.runner.AndroidJUnit4;
 
 import com.android.internal.util.test.FakeSettingsProvider;
+import com.android.testutils.HandlerUtilsKt;
 
 import org.junit.After;
 import org.junit.Before;
@@ -90,6 +90,7 @@
     private static final String IPV6_DISCARD_PREFIX = "100::/64";
     private static final String USB_PREFIX = "192.168.42.0/24";
     private static final String WIFI_PREFIX = "192.168.43.0/24";
+    private static final long WAIT_FOR_IDLE_TIMEOUT = 2 * 1000;
 
     @Mock private OffloadHardwareInterface mHardware;
     @Mock private ApplicationInfo mApplicationInfo;
@@ -131,9 +132,7 @@
     }
 
     private void waitForIdle() {
-        ConditionVariable cv = new ConditionVariable();
-        new Handler(Looper.getMainLooper()).post(() -> { cv.open(); });
-        cv.block();
+        HandlerUtilsKt.waitForIdle(new Handler(Looper.getMainLooper()), WAIT_FOR_IDLE_TIMEOUT);
     }
 
     private OffloadController makeOffloadController() throws Exception {
diff --git a/tests/net/java/com/android/server/net/NetworkStatsObserversTest.java b/tests/net/java/com/android/server/net/NetworkStatsObserversTest.java
index 43a3803..9a47f35 100644
--- a/tests/net/java/com/android/server/net/NetworkStatsObserversTest.java
+++ b/tests/net/java/com/android/server/net/NetworkStatsObserversTest.java
@@ -28,8 +28,6 @@
 import static android.net.TrafficStats.MB_IN_BYTES;
 import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
 
-import static com.android.internal.util.TestUtils.waitForIdleHandler;
-
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.any;
@@ -56,6 +54,7 @@
 
 import com.android.internal.net.VpnInfo;
 import com.android.server.net.NetworkStatsServiceTest.LatchedHandler;
+import com.android.testutils.HandlerUtilsKt;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -457,7 +456,7 @@
     }
 
     private void waitForObserverToIdle() {
-        waitForIdleHandler(mObserverHandlerThread, WAIT_TIMEOUT_MS);
-        waitForIdleHandler(mHandler, WAIT_TIMEOUT_MS);
+        HandlerUtilsKt.waitForIdle(mObserverHandlerThread, WAIT_TIMEOUT_MS);
+        HandlerUtilsKt.waitForIdle(mHandler, WAIT_TIMEOUT_MS);
     }
 }
diff --git a/tests/net/java/com/android/server/net/NetworkStatsServiceTest.java b/tests/net/java/com/android/server/net/NetworkStatsServiceTest.java
index e35c34a..07b5ba4 100644
--- a/tests/net/java/com/android/server/net/NetworkStatsServiceTest.java
+++ b/tests/net/java/com/android/server/net/NetworkStatsServiceTest.java
@@ -52,7 +52,6 @@
 import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
 import static android.text.format.DateUtils.WEEK_IN_MILLIS;
 
-import static com.android.internal.util.TestUtils.waitForIdleHandler;
 import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL;
 
 import static org.junit.Assert.assertEquals;
@@ -105,6 +104,7 @@
 import com.android.internal.util.test.BroadcastInterceptingContext;
 import com.android.server.net.NetworkStatsService.NetworkStatsSettings;
 import com.android.server.net.NetworkStatsService.NetworkStatsSettings.Config;
+import com.android.testutils.HandlerUtilsKt;
 
 import libcore.io.IoUtils;
 
@@ -1321,8 +1321,6 @@
         expectNetworkStatsSummary(buildEmptyStats());
         expectNetworkStatsUidDetail(buildEmptyStats());
 
-
-
         // Register and verify request and that binder was called
         DataUsageRequest request =
                 mService.registerUsageCallback(mServiceContext.getOpPackageName(), inputRequest,
@@ -1334,14 +1332,11 @@
 
         // Send dummy message to make sure that any previous message has been handled
         mHandler.sendMessage(mHandler.obtainMessage(-1));
-        waitForIdleHandler(mHandler, WAIT_TIMEOUT);
-
-
+        HandlerUtilsKt.waitForIdle(mHandler, WAIT_TIMEOUT);
 
         // Make sure that the caller binder gets connected
         verify(mBinder).linkToDeath(any(IBinder.DeathRecipient.class), anyInt());
 
-
         // modify some number on wifi, and trigger poll event
         // not enough traffic to call data usage callback
         incrementCurrentTime(HOUR_IN_MILLIS);
@@ -1674,7 +1669,7 @@
         mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
         // Send dummy message to make sure that any previous message has been handled
         mHandler.sendMessage(mHandler.obtainMessage(-1));
-        waitForIdleHandler(mHandler, WAIT_TIMEOUT);
+        HandlerUtilsKt.waitForIdle(mHandler, WAIT_TIMEOUT);
     }
 
     static class LatchedHandler extends Handler {
diff --git a/tests/net/util/java/com/android/internal/util/TestUtils.java b/tests/net/util/java/com/android/internal/util/TestUtils.java
index a99cd47..daa00d2 100644
--- a/tests/net/util/java/com/android/internal/util/TestUtils.java
+++ b/tests/net/util/java/com/android/internal/util/TestUtils.java
@@ -19,57 +19,13 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
-import android.os.ConditionVariable;
-import android.os.Handler;
-import android.os.HandlerThread;
-import android.os.Looper;
 import android.os.Parcel;
 import android.os.Parcelable;
 
-import androidx.annotation.NonNull;
-
-import java.util.concurrent.Executor;
-
 public final class TestUtils {
     private TestUtils() { }
 
     /**
-     * Block until the given Handler thread becomes idle, or until timeoutMs has passed.
-     */
-    public static void waitForIdleHandler(HandlerThread handlerThread, long timeoutMs) {
-        waitForIdleLooper(handlerThread.getLooper(), timeoutMs);
-    }
-
-    /**
-     * Block until the given Looper becomes idle, or until timeoutMs has passed.
-     */
-    public static void waitForIdleLooper(Looper looper, long timeoutMs) {
-        waitForIdleHandler(new Handler(looper), timeoutMs);
-    }
-
-    /**
-     * Block until the given Handler becomes idle, or until timeoutMs has passed.
-     */
-    public static void waitForIdleHandler(Handler handler, long timeoutMs) {
-        final ConditionVariable cv = new ConditionVariable();
-        handler.post(() -> cv.open());
-        if (!cv.block(timeoutMs)) {
-            fail(handler.toString() + " did not become idle after " + timeoutMs + " ms");
-        }
-    }
-
-    /**
-     * Block until the given Serial Executor becomes idle, or until timeoutMs has passed.
-     */
-    public static void waitForIdleSerialExecutor(@NonNull Executor executor, long timeoutMs) {
-        final ConditionVariable cv = new ConditionVariable();
-        executor.execute(() -> cv.open());
-        if (!cv.block(timeoutMs)) {
-            fail(executor.toString() + " did not become idle after " + timeoutMs + " ms");
-        }
-    }
-
-    /**
      * Return a new instance of {@code T} after being parceled then unparceled.
      */
     public static <T extends Parcelable> T parcelingRoundTrip(T source) {