summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorRemi NGUYEN VAN <reminv@google.com>2021-03-11 23:51:49 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2021-03-11 23:51:49 +0000
commit1c22944c5e57d12375d1b025ae5d990c7fe92c01 (patch)
treed68b49788c2d5fcee2002685c69c81c2d2b3f15f /common
parent78e2f0d1241a0cf777f2681bd3b88139f70229e8 (diff)
parent209bb7d5b84bf552a0f834f223589256ff237935 (diff)
downloadnet-1c22944c5e57d12375d1b025ae5d990c7fe92c01.tar.gz
Merge "Rename StringNetworkSpecifier to Ethernet"
Diffstat (limited to 'common')
-rw-r--r--common/devicetests/com/android/testutils/CompatUtil.kt53
-rw-r--r--common/devicetests/com/android/testutils/TestNetworkTracker.kt3
2 files changed, 54 insertions, 2 deletions
diff --git a/common/devicetests/com/android/testutils/CompatUtil.kt b/common/devicetests/com/android/testutils/CompatUtil.kt
new file mode 100644
index 00000000..4bb90a80
--- /dev/null
+++ b/common/devicetests/com/android/testutils/CompatUtil.kt
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2021 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.net.NetworkSpecifier
+import android.os.Build
+
+/**
+ * Test utility to create [NetworkSpecifier]s on different SDK versions.
+ */
+object CompatUtil {
+ @JvmStatic
+ fun makeTestNetworkSpecifier(ifName: String): NetworkSpecifier {
+ // Until R, there was no TestNetworkSpecifier, StringNetworkSpecifier was used instead
+ if (isDevSdkInRange(minExclusive = null, maxInclusive = Build.VERSION_CODES.R)) {
+ makeNetworkSpecifierInternal("android.net.StringNetworkSpecifier", ifName)
+ }
+ // TestNetworkSpecifier is not part of the SDK in some branches using this utility
+ // TODO: replace with a direct call to the constructor
+ return makeNetworkSpecifierInternal("android.net.TestNetworkSpecifier", ifName)
+ }
+
+ @JvmStatic
+ fun makeEthernetNetworkSpecifier(ifName: String): NetworkSpecifier {
+ // Until R, there was no EthernetNetworkSpecifier, StringNetworkSpecifier was used instead
+ if (isDevSdkInRange(minExclusive = null, maxInclusive = Build.VERSION_CODES.R)) {
+ makeNetworkSpecifierInternal("android.net.StringNetworkSpecifier", ifName)
+ }
+ // EthernetNetworkSpecifier is not part of the SDK in some branches using this utility
+ // TODO: replace with a direct call to the constructor
+ return makeNetworkSpecifierInternal("android.net.EthernetNetworkSpecifier", ifName)
+ }
+
+ private fun makeNetworkSpecifierInternal(clazz: String, specifier: String): NetworkSpecifier {
+ // StringNetworkSpecifier was removed after R (and was hidden API before that)
+ return Class.forName(clazz)
+ .getConstructor(String::class.java).newInstance(specifier) as NetworkSpecifier
+ }
+} \ No newline at end of file
diff --git a/common/devicetests/com/android/testutils/TestNetworkTracker.kt b/common/devicetests/com/android/testutils/TestNetworkTracker.kt
index 4bd9ae8e..5b978617 100644
--- a/common/devicetests/com/android/testutils/TestNetworkTracker.kt
+++ b/common/devicetests/com/android/testutils/TestNetworkTracker.kt
@@ -23,7 +23,6 @@ import android.net.LinkAddress
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
-import android.net.StringNetworkSpecifier
import android.net.TestNetworkInterface
import android.net.TestNetworkManager
import android.os.Binder
@@ -68,7 +67,7 @@ class TestNetworkTracker internal constructor(
// Test networks do not have NOT_VPN or TRUSTED capabilities by default
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED)
- .setNetworkSpecifier(StringNetworkSpecifier(iface.interfaceName))
+ .setNetworkSpecifier(CompatUtil.makeTestNetworkSpecifier(iface.interfaceName))
.build()
networkCallback = object : NetworkCallback() {
override fun onAvailable(network: Network) {