diff --git a/Tethering/tests/integration/base/android/net/EthernetTetheringTestBase.java b/Tethering/tests/integration/base/android/net/EthernetTetheringTestBase.java index 9dad30188a..83fc3e4ae1 100644 --- a/Tethering/tests/integration/base/android/net/EthernetTetheringTestBase.java +++ b/Tethering/tests/integration/base/android/net/EthernetTetheringTestBase.java @@ -58,6 +58,8 @@ import android.net.TetheringManager.TetheringEventCallback; import android.net.TetheringManager.TetheringRequest; import android.net.TetheringTester.TetheredDevice; import android.net.cts.util.CtsNetUtils; +import android.net.cts.util.CtsTetheringUtils; +import android.net.cts.util.CtsTetheringUtils.TestTetheringEventCallback; import android.os.Handler; import android.os.HandlerThread; import android.os.SystemClock; @@ -75,6 +77,7 @@ import com.android.testutils.TestNetworkTracker; import org.junit.After; import org.junit.Before; +import org.junit.BeforeClass; import java.io.FileDescriptor; import java.net.Inet4Address; @@ -161,6 +164,28 @@ public abstract class EthernetTetheringTestBase { private TapPacketReader mDownstreamReader; private MyTetheringEventCallback mTetheringEventCallback; + @BeforeClass + public static void setUpOnce() throws Exception { + // The first test case may experience tethering restart with IP conflict handling. + // Tethering would cache the last upstreams so that the next enabled tethering avoids + // picking up the address that is in conflict with the upstreams. To protect subsequent + // tests, turn tethering on and off before running them. + final Context ctx = InstrumentationRegistry.getInstrumentation().getContext(); + final CtsTetheringUtils utils = new CtsTetheringUtils(ctx); + final TestTetheringEventCallback callback = utils.registerTetheringEventCallback(); + try { + if (!callback.isWifiTetheringSupported(ctx)) return; + + callback.expectNoTetheringActive(); + + utils.startWifiTethering(callback); + callback.getCurrentValidUpstream(); + utils.stopWifiTethering(callback); + } finally { + utils.unregisterTetheringEventCallback(callback); + } + } + @Before public void setUp() throws Exception { mHandlerThread = new HandlerThread(getClass().getSimpleName()); 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 f506c2366e..dffd9d5bb7 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 @@ -393,21 +393,28 @@ public final class CtsTetheringUtils { } public void assumeTetheringSupported() { + assumeTrue(isTetheringSupported()); + } + + private boolean isTetheringSupported() { final ArrayTrackRecord.ReadHead history = mHistory.newReadHead(); - assertNotNull("No onSupported callback", history.poll(TIMEOUT_MS, (cv) -> { - if (cv.callbackType != CallbackType.ON_SUPPORTED) return false; + final CallbackValue result = history.poll(TIMEOUT_MS, (cv) -> { + return cv.callbackType == CallbackType.ON_SUPPORTED; + }); - assumeTrue(cv.callbackParam2 == 1 /* supported */); - return true; - })); + assertNotNull("No onSupported callback", result); + return result.callbackParam2 == 1 /* supported */; } public void assumeWifiTetheringSupported(final Context ctx) throws Exception { - assumeTetheringSupported(); + assumeTrue(isWifiTetheringSupported(ctx)); + } - assumeTrue(!getTetheringInterfaceRegexps().getTetherableWifiRegexs().isEmpty()); - assumeTrue(isPortableHotspotSupported(ctx)); + public boolean isWifiTetheringSupported(final Context ctx) throws Exception { + return isTetheringSupported() + && !getTetheringInterfaceRegexps().getTetherableWifiRegexs().isEmpty() + && isPortableHotspotSupported(ctx); } public TetheringInterfaceRegexps getTetheringInterfaceRegexps() {