From b16b6e6d2c3b35ef5cffc6c9e397117555093030 Mon Sep 17 00:00:00 2001 From: Quang Anh Luong Date: Mon, 27 Jan 2025 19:42:40 +0900 Subject: [PATCH] Add coverage for StopTetheringCallback methods Bug: 385014495 Bug: 385278741 Test: atest TetheringManagerTest Change-Id: I176e3339a80a1efe5410092589f459fd7e94728a --- .../net/cts/util/CtsTetheringUtils.java | 78 +++++++++++++++++++ .../tethering/cts/TetheringManagerTest.java | 10 +-- 2 files changed, 80 insertions(+), 8 deletions(-) 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 243cd2739f..82a295d851 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 @@ -126,6 +126,67 @@ public final class CtsTetheringUtils { } } + public static class StopTetheringCallback implements TetheringManager.StopTetheringCallback { + private static final int TIMEOUT_MS = 30_000; + public static class CallbackValue { + public final int error; + + private CallbackValue(final int e) { + error = e; + } + + public static class OnStopTetheringSucceeded extends CallbackValue { + OnStopTetheringSucceeded() { + super(TETHER_ERROR_NO_ERROR); + } + } + + public static class OnStopTetheringFailed extends CallbackValue { + OnStopTetheringFailed(final int error) { + super(error); + } + } + + @Override + public String toString() { + return String.format("%s(%d)", getClass().getSimpleName(), error); + } + } + + private final ArrayTrackRecord.ReadHead mHistory = + new ArrayTrackRecord().newReadHead(); + + @Override + public void onStopTetheringSucceeded() { + mHistory.add(new CallbackValue.OnStopTetheringSucceeded()); + } + + @Override + public void onStopTetheringFailed(final int error) { + mHistory.add(new CallbackValue.OnStopTetheringFailed(error)); + } + + /** + * Verifies that {@link #onStopTetheringSucceeded()} was called + */ + public void verifyStopTetheringSucceeded() { + final CallbackValue cv = mHistory.poll(TIMEOUT_MS, c -> true); + assertNotNull("No onStopTetheringSucceeded after " + TIMEOUT_MS + " ms", cv); + assertTrue("Fail stop tethering:" + cv, + cv instanceof CallbackValue.OnStopTetheringSucceeded); + } + + /** + * Verifies that {@link #onStopTetheringFailed(int)} was called + */ + public void expectStopTetheringFailed(final int expected) { + final CallbackValue cv = mHistory.poll(TIMEOUT_MS, c -> true); + assertNotNull("No onStopTetheringFailed after " + TIMEOUT_MS + " ms", cv); + assertTrue("Expect fail with error code " + expected + ", but received: " + cv, + (cv instanceof CallbackValue.OnStopTetheringFailed) && (cv.error == expected)); + } + } + private static boolean isRegexMatch(final String[] ifaceRegexs, String iface) { if (ifaceRegexs == null) fail("ifaceRegexs should not be null"); @@ -574,6 +635,23 @@ public final class CtsTetheringUtils { expectSoftApDisabled(); } + /** + * Calls {@link TetheringManager#stopTethering(TetheringRequest, Executor, + * TetheringManager.StopTetheringCallback)} and verifies it throws an + * UnsupportedOperationException. + */ + public void stopTethering(final TetheringRequest request) { + final StopTetheringCallback callback = new StopTetheringCallback(); + runAsShell(TETHER_PRIVILEGED, () -> { + try { + mTm.stopTethering(request, Runnable::run /* Executor */, callback); + fail("stopTethering should throw UnsupportedOperationException"); + } catch (UnsupportedOperationException expected) { + // Success. + } + }); + } + public void stopAllTethering() { final TestTetheringEventCallback callback = registerTetheringEventCallback(); try { diff --git a/tests/cts/tethering/src/android/tethering/cts/TetheringManagerTest.java b/tests/cts/tethering/src/android/tethering/cts/TetheringManagerTest.java index d103f75d01..a2cac697ff 100644 --- a/tests/cts/tethering/src/android/tethering/cts/TetheringManagerTest.java +++ b/tests/cts/tethering/src/android/tethering/cts/TetheringManagerTest.java @@ -101,7 +101,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Executor; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; @@ -458,14 +457,9 @@ public class TetheringManagerTest { @Test public void testStopTetheringRequest() throws Exception { + assumeTrue(isTetheringWithSoftApConfigEnabled()); TetheringRequest request = new TetheringRequest.Builder(TETHERING_WIFI).build(); - Executor executor = Runnable::run; - TetheringManager.StopTetheringCallback callback = - new TetheringManager.StopTetheringCallback() {}; - try { - mTM.stopTethering(request, executor, callback); - fail("stopTethering should throw UnsupportedOperationException"); - } catch (UnsupportedOperationException expect) { } + mCtsTetheringUtils.stopTethering(request); } private boolean isTetheringWithSoftApConfigEnabled() {