Merge "Add coverage for StopTetheringCallback methods" into main

This commit is contained in:
Quang Luong 2025-01-29 15:24:53 -08:00 • committed by Gerrit Code Review
commit 6bbe256bb7
2 changed files with 80 additions and 8 deletions

View file

@ -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<CallbackValue>.ReadHead mHistory =
new ArrayTrackRecord<CallbackValue>().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 {

View file

@ -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() {