From 0acaa74538491dc626f8f31f732b819f8097d668 Mon Sep 17 00:00:00 2001 From: Chiachang Wang Date: Fri, 21 May 2021 18:26:23 +0800 Subject: [PATCH] Correct the logic for CtsTetheringUtils.isWifiTetheringSupported The existing isWifiTetheringSupported only check if tethering side supports wifi tethering or not but not wifi side. A expected behavior should include both of them, so add the wifi side check into the helper function. Also update in the existing caller side due to a new parameter added. Bug: 186061922 Test: atest MtsTetheringTestLatestSdk Megred-In: Id69ac1d30ab2bbf23e870193335b139f54672636 Change-Id: Id69ac1d30ab2bbf23e870193335b139f54672636 Ignore-AOSP-First: cherry-pick --- .../tethering/mts/TetheringModuleTest.java | 5 ++- .../net/cts/util/CtsTetheringUtils.java | 37 +++++++++++-------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/Tethering/tests/mts/src/android/tethering/mts/TetheringModuleTest.java b/Tethering/tests/mts/src/android/tethering/mts/TetheringModuleTest.java index e0fcbfad28..07aab6378f 100644 --- a/Tethering/tests/mts/src/android/tethering/mts/TetheringModuleTest.java +++ b/Tethering/tests/mts/src/android/tethering/mts/TetheringModuleTest.java @@ -15,6 +15,7 @@ */ package android.tethering.mts; +import static android.Manifest.permission.ACCESS_WIFI_STATE; import static android.Manifest.permission.MANAGE_TEST_NETWORKS; import static android.Manifest.permission.NETWORK_SETTINGS; import static android.Manifest.permission.READ_DEVICE_CONFIG; @@ -70,7 +71,7 @@ public class TetheringModuleTest { @Before public void setUp() throws Exception { mUiAutomation.adoptShellPermissionIdentity(MANAGE_TEST_NETWORKS, NETWORK_SETTINGS, - WRITE_SETTINGS, READ_DEVICE_CONFIG, TETHER_PRIVILEGED); + WRITE_SETTINGS, READ_DEVICE_CONFIG, TETHER_PRIVILEGED, ACCESS_WIFI_STATE); mContext = InstrumentationRegistry.getContext(); mTm = mContext.getSystemService(TetheringManager.class); mCtsTetheringUtils = new CtsTetheringUtils(mContext); @@ -102,7 +103,7 @@ public class TetheringModuleTest { TestNetworkTracker tnt = null; try { tetherEventCallback.assumeTetheringSupported(); - assumeTrue(isWifiTetheringSupported(tetherEventCallback)); + assumeTrue(isWifiTetheringSupported(mContext, tetherEventCallback)); tetherEventCallback.expectNoTetheringActive(); final TetheringInterface tetheredIface = diff --git a/tests/cts/net/util/java/android/net/cts/util/CtsTetheringUtils.java b/tests/cts/net/util/java/android/net/cts/util/CtsTetheringUtils.java index 1bdd5337b3..c220326145 100644 --- a/tests/cts/net/util/java/android/net/cts/util/CtsTetheringUtils.java +++ b/tests/cts/net/util/java/android/net/cts/util/CtsTetheringUtils.java @@ -388,20 +388,7 @@ public final class CtsTetheringUtils { assumeTetheringSupported(); assumeTrue(!getTetheringInterfaceRegexps().getTetherableWifiRegexs().isEmpty()); - - final PackageManager pm = ctx.getPackageManager(); - assumeTrue(pm.hasSystemFeature(PackageManager.FEATURE_WIFI)); - - WifiManager wm = ctx.getSystemService(WifiManager.class); - // Wifi feature flags only work when wifi is on. - final boolean previousWifiEnabledState = wm.isWifiEnabled(); - try { - if (!previousWifiEnabledState) SystemUtil.runShellCommand("svc wifi enable"); - waitForWifiEnabled(ctx); - assumeTrue(wm.isPortableHotspotSupported()); - } finally { - if (!previousWifiEnabledState) SystemUtil.runShellCommand("svc wifi disable"); - } + assumeTrue(isPortableHotspotSupported(ctx)); } public TetheringInterfaceRegexps getTetheringInterfaceRegexps() { @@ -453,8 +440,26 @@ public final class CtsTetheringUtils { return callback.getTetheringInterfaceRegexps().getTetherableWifiRegexs(); } - public static boolean isWifiTetheringSupported(final TestTetheringEventCallback callback) { - return !getWifiTetherableInterfaceRegexps(callback).isEmpty(); + public static boolean isWifiTetheringSupported(final Context ctx, + final TestTetheringEventCallback callback) throws Exception { + return !getWifiTetherableInterfaceRegexps(callback).isEmpty() + && isPortableHotspotSupported(ctx); + } + + /* Returns if wifi supports hotspot. */ + private static boolean isPortableHotspotSupported(final Context ctx) throws Exception { + final PackageManager pm = ctx.getPackageManager(); + if (!pm.hasSystemFeature(PackageManager.FEATURE_WIFI)) return false; + final WifiManager wm = ctx.getSystemService(WifiManager.class); + // Wifi feature flags only work when wifi is on. + final boolean previousWifiEnabledState = wm.isWifiEnabled(); + try { + if (!previousWifiEnabledState) SystemUtil.runShellCommand("svc wifi enable"); + waitForWifiEnabled(ctx); + return wm.isPortableHotspotSupported(); + } finally { + if (!previousWifiEnabledState) SystemUtil.runShellCommand("svc wifi disable"); + } } public TetheringInterface startWifiTethering(final TestTetheringEventCallback callback)