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

Commit cff2fe4f authored by Etan Cohen's avatar Etan Cohen
Browse files

Add Executor to the TestLooper

Add a method to obtain an Executor from a TestLooper: the executor
executes on a new Handler on the test looper.

Bug: 73088768
Test: new unit test for executor
Change-Id: Ib0aa10011116a4d998f8d3a7434939338d9b516d
parent 4577b9b1
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package android.os.test;

import static org.junit.Assert.assertTrue;

import android.os.Handler;
import android.os.HandlerExecutor;
import android.os.Looper;
import android.os.Message;
import android.os.MessageQueue;
@@ -28,6 +30,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.Executor;

/**
 * Creates a looper whose message queue can be manipulated
@@ -83,6 +86,10 @@ public class TestLooper {
        return mLooper;
    }

    public Executor getNewExecutor() {
        return new HandlerExecutor(new Handler(getLooper()));
    }

    private Message getMessageLinkedList() {
        try {
            MessageQueue queue = mLooper.getQueue();
+31 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;

@@ -37,6 +38,8 @@ import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.MockitoAnnotations;

import java.util.concurrent.Executor;

/**
 * Test TestLooperAbstractTime which provides control over "time". Note that
 * real-time is being used as well. Therefore small time increments are NOT
@@ -48,6 +51,7 @@ public class TestLooperTest {
    private TestLooper mTestLooper;
    private Handler mHandler;
    private Handler mHandlerSpy;
    private Executor mExecutor;

    @Rule
    public ErrorCollector collector = new ErrorCollector();
@@ -59,6 +63,7 @@ public class TestLooperTest {
        mTestLooper = new TestLooper();
        mHandler = new Handler(mTestLooper.getLooper());
        mHandlerSpy = spy(mHandler);
        mExecutor = mTestLooper.getNewExecutor();
    }

    /**
@@ -92,6 +97,32 @@ public class TestLooperTest {
        inOrder.verify(mHandlerSpy, never()).handleMessage(any(Message.class));
    }

    /**
     * Basic test of the Executor with no time stamps: dispatch 4 executables, check that all 4
     * executed in correct order.
     */
    @Test
    public void testNoTimeMovementExecutor() {
        final Runnable runnableA = mock(Runnable.class);
        final Runnable runnableB = mock(Runnable.class);
        final Runnable runnableC = mock(Runnable.class);

        InOrder inOrder = inOrder(runnableA, runnableB, runnableC);

        mExecutor.execute(runnableA);
        mExecutor.execute(runnableB);
        mExecutor.execute(runnableA);
        mExecutor.execute(runnableC);
        mTestLooper.dispatchAll();

        inOrder.verify(runnableA).run();
        inOrder.verify(runnableB).run();
        inOrder.verify(runnableA).run();
        inOrder.verify(runnableC).run();

        inOrder.verifyNoMoreInteractions();
    }

    /**
     * Test message sequence: A, B, C@5K, A@10K. Don't move time.
     * <p>