summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChalard Jean <jchalard@google.com>2019-05-28 16:48:32 +0900
committerChalard Jean <jchalard@google.com>2019-05-28 22:20:51 +0900
commit0d0cc8ccb563686d6cf2d54c9613ea0167ea6ecb (patch)
treeaa190e98f1a1ce5544d3b1142814d8327b4ce78f
parentf108e1a9b0e55c3862991384e2747f22846b5165 (diff)
Add a common test library.
This is the most common test library for Connectivity tests. It is meant to be usable in framework tests, network stack tests, CTS, GTS. To achieve that, it can only depend on framework classes. Bug: none Test: NetworkMonitorTest Test: NsdManagerTest Test: ConnectivityServiceTest Test: OffloadControllerTest Test: NetworkStatsObserversTest Test: NetworkStatsServiceTest (all the touched classes) Change-Id: Ic47cbe7ba0e407145fa6bc49bb2adb3c5937dbc4
-rw-r--r--tests/lib/Android.bp26
-rw-r--r--tests/lib/src/com/android/testutils/HandlerUtils.kt50
-rw-r--r--tests/unit/Android.bp1
-rw-r--r--tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java15
4 files changed, 81 insertions, 11 deletions
diff --git a/tests/lib/Android.bp b/tests/lib/Android.bp
new file mode 100644
index 0000000..f45a81c
--- /dev/null
+++ b/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/tests/lib/src/com/android/testutils/HandlerUtils.kt b/tests/lib/src/com/android/testutils/HandlerUtils.kt
new file mode 100644
index 0000000..3dce5a5
--- /dev/null
+++ b/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/tests/unit/Android.bp b/tests/unit/Android.bp
index 85951eb..48b13b0 100644
--- a/tests/unit/Android.bp
+++ b/tests/unit/Android.bp
@@ -23,6 +23,7 @@ android_test {
static_libs: [
"androidx.test.rules",
"mockito-target-extended-minus-junit4",
+ "net-tests-utils",
"NetworkStackBase",
"testables",
],
diff --git a/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java b/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java
index 262641d..e4c1d17 100644
--- a/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java
+++ b/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java
@@ -92,6 +92,7 @@ import androidx.test.runner.AndroidJUnit4;
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 @@ public class NetworkMonitorTest {
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 @@ public class NetworkMonitorTest {
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 @@ public class NetworkMonitorTest {
} catch (RemoteException e) {
fail("Unexpected exception: " + e);
}
- waitForIdle(monitor.getHandler());
+ HandlerUtilsKt.waitForIdle(monitor.getHandler(), HANDLER_TIMEOUT_MS);
return monitor;
}