Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 492b0507 authored by Ang Li's avatar Ang Li Committed by Android (Google) Code Review
Browse files

Merge "Add test for DeadObjectException when toggling logcat" into main

parents 70f28a76 b702cb84
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import static org.mockito.Mockito.never;
import static java.io.File.createTempFile;
import static java.nio.file.Files.createTempDirectory;

import android.os.DeadObjectException;
import android.os.IBinder;
import android.os.RemoteException;
import android.platform.test.annotations.Presubmit;
@@ -338,4 +339,32 @@ public class ProtoLogConfigurationServiceTest {
        Mockito.verify(mMockClient).toggleLogcat(eq(true),
                Mockito.argThat(it -> it.length == 1 && it[0].equals(TEST_GROUP)));
    }

    @Test
    public void doesNotThrowWhenClientDiesDuringToggle() throws RemoteException {
        // Verifies that a DeadObjectException is caught and handled gracefully.
        final var service = new ProtoLogConfigurationServiceImpl();
        final var mockPrintWriter = Mockito.mock(PrintWriter.class);

        // Register a client.
        final RegisterClientArgs args = new RegisterClientArgs();
        args.groups = new String[] { TEST_GROUP };
        args.groupsDefaultLogcatStatus = new boolean[] { false };
        service.registerClient(mMockClient, args);

        // Configure the mock client to throw DeadObjectException when toggleLogcat is called,
        // simulating a client process that has died.
        Mockito.doThrow(new DeadObjectException("Client died"))
                .when(mMockClient).toggleLogcat(anyBoolean(), any());

        // Call the method under test. This should not throw an exception.
        service.enableProtoLogToLogcat(mockPrintWriter, TEST_GROUP);

        // Verify that the service attempted to call the client.
        Mockito.verify(mMockClient).toggleLogcat(eq(true),
                Mockito.argThat(it -> it.length == 1 && it[0].equals(TEST_GROUP)));

        // Verify that the failure was reported to the PrintWriter.
        Mockito.verify(mockPrintWriter).println("- Failed (client may have died)");
    }
}