Add unit test for ThreadNetworkControllerService#SetEnabled

Bug: 321853291
Test: atest ThreadNetworkUnitTests
Flag: TEST_ONLY

Change-Id: Ifa74a629a20c87bf45d3d3e0693033d8e28b4e8f
This commit is contained in:
Yang Sun 2025-04-15 09:39:15 +00:00
commit 3fa3ca8e5b

View file

@ -652,6 +652,43 @@ public final class ThreadNetworkControllerServiceTest {
assertThat(failure.getErrorCode()).isEqualTo(ERROR_FAILED_PRECONDITION);
}
@Test
public void setEnabled_success_threadNetworkEnabled() throws Exception {
mService.initialize();
CompletableFuture<Void> setEnabledFuture = new CompletableFuture<>();
mService.setEnabled(true, newOperationReceiver(setEnabledFuture));
mTestLooper.dispatchAll();
assertThat(mFakeOtDaemon.getEnabledState()).isEqualTo(STATE_ENABLED);
}
@Test
public void setDisabled_success_threadNetworkDisabled() throws Exception {
mService.initialize();
CompletableFuture<Void> setEnabledFuture = new CompletableFuture<>();
mService.setEnabled(false, newOperationReceiver(setEnabledFuture));
mTestLooper.dispatchAll();
assertThat(mFakeOtDaemon.getEnabledState()).isEqualTo(STATE_DISABLED);
}
@Test
public void setEnabled_otDaemonRemoteFailure_returnsInternalError() throws Exception {
mService.initialize();
CompletableFuture<Void> setEnabledFuture = new CompletableFuture<>();
mFakeOtDaemon.setSetEnabledException(
new RemoteException("ot-daemon setThreadEnabled() throws"));
mService.setEnabled(true, newOperationReceiver(setEnabledFuture));
mTestLooper.dispatchAll();
var thrown = assertThrows(ExecutionException.class, () -> setEnabledFuture.get());
ThreadNetworkException failure = (ThreadNetworkException) thrown.getCause();
assertThat(failure.getErrorCode()).isEqualTo(ERROR_INTERNAL_ERROR);
}
private AtomicReference<BroadcastReceiver> captureBroadcastReceiver(String action) {
AtomicReference<BroadcastReceiver> receiverRef = new AtomicReference<>();