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

Commit 25c50380 authored by Etan Cohen's avatar Etan Cohen Committed by Android (Google) Code Review
Browse files

Merge "Add Executor to the TestLooper"

parents d64db3a8 cff2fe4f
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>