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

Commit 9df5d4f7 authored by Paul Duffin's avatar Paul Duffin
Browse files

Replace usages of littlemock with mockito

Littlemock appears to have been created as an alternative to
Mockito that would work on Android. Given that Mockito does
now work on Android and Littlemock has had no activity since
Aug 2012 it seems prudent to switch over to use Mockito instead.

https://code.google.com/archive/p/littlemock/

This is being done as part of the work to upgrade Mockito.

Bug: 32912773
Test: Build and install affected APKs, test using am instrument
Change-Id: Id0e57880ea66c5681b664bbfb534c96410544665
parent 75dfc5bd
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ LOCAL_STATIC_JAVA_LIBRARIES := \
    frameworks-core-util-lib \
    mockwebserver \
    guava \
    littlemock \
    android-support-test \
    mockito-target \
    espresso-core \
+6 −6
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ package android.net;
import com.android.org.conscrypt.ClientSessionContext;
import com.android.org.conscrypt.SSLClientSessionCache;

import com.google.testing.littlemock.LittleMock;
import org.mockito.Mockito;

import junit.framework.TestCase;

@@ -39,25 +39,25 @@ public class SSLSessionCacheTest extends TestCase {

    public void testInstall_compatibleContext() throws Exception {
        final SSLContext ctx = SSLContext.getDefault();
        final SSLClientSessionCache mock = LittleMock.mock(SSLClientSessionCache.class);
        final SSLClientSessionCache mock = Mockito.mock(SSLClientSessionCache.class);
        final ClientSessionContext clientCtx = (ClientSessionContext) ctx.getClientSessionContext();

        try {
            SSLSessionCache.install(new SSLSessionCache(mock), ctx);
            clientCtx.getSession("www.foogle.com", 443);
            LittleMock.verify(mock).getSessionData(LittleMock.anyString(), LittleMock.anyInt());
            Mockito.verify(mock).getSessionData(Mockito.anyString(), Mockito.anyInt());
        } finally {
            // Restore cacheless behaviour.
            SSLSessionCache.install(null, ctx);
            clientCtx.getSession("www.foogle.com", 443);
            LittleMock.verifyNoMoreInteractions(mock);
            Mockito.verifyNoMoreInteractions(mock);
        }
    }

    public void testInstall_incompatibleContext() {
        try {
            SSLSessionCache.install(
                    new SSLSessionCache(LittleMock.mock(SSLClientSessionCache.class)),
                    new SSLSessionCache(Mockito.mock(SSLClientSessionCache.class)),
                    new FakeSSLContext());
            fail();
        } catch (IllegalArgumentException expected) {}
@@ -102,7 +102,7 @@ public class SSLSessionCacheTest extends TestCase {

        @Override
        protected SSLSessionContext engineGetClientSessionContext() {
            return LittleMock.mock(SSLSessionContext.class);
            return Mockito.mock(SSLSessionContext.class);
        }
    }
}
+3 −3
Original line number Diff line number Diff line
@@ -16,8 +16,8 @@

package android.widget.focus;

import static com.google.testing.littlemock.LittleMock.inOrder;
import static com.google.testing.littlemock.LittleMock.mock;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;

import android.os.Handler;
import android.test.ActivityInstrumentationTestCase2;
@@ -31,7 +31,7 @@ import android.view.ViewTreeObserver.OnGlobalFocusChangeListener;
import android.widget.Button;

import com.android.frameworks.coretests.R;
import com.google.testing.littlemock.LittleMock.InOrder;
import org.mockito.InOrder;

/**
 * {@link RequestFocusTest} is set up to exercise cases where the views that
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ LOCAL_MODULE_TAGS := tests

LOCAL_SRC_FILES := $(call all-subdir-java-files)

LOCAL_STATIC_JAVA_LIBRARIES := littlemock junit legacy-android-test
LOCAL_STATIC_JAVA_LIBRARIES := mockito-target legacy-android-test
LOCAL_JAVA_LIBRARIES := android.test.runner

LOCAL_PACKAGE_NAME := TtsTests
+49 −46
Original line number Diff line number Diff line
@@ -22,9 +22,12 @@ import android.speech.tts.TextToSpeech;
import android.test.InstrumentationTestCase;

import com.android.speech.tts.MockableTextToSpeechService.IDelegate;
import com.google.testing.littlemock.ArgumentCaptor;
import com.google.testing.littlemock.Behaviour;
import com.google.testing.littlemock.LittleMock;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.internal.stubbing.StubberImpl;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.Stubber;
import junit.framework.Assert;

import java.util.Locale;
@@ -40,16 +43,16 @@ public class TextToSpeechTests extends InstrumentationTestCase {

    @Override
    public void setUp() throws Exception {
        IDelegate passThrough = LittleMock.mock(IDelegate.class);
        IDelegate passThrough = Mockito.mock(IDelegate.class);
        MockableTextToSpeechService.setMocker(passThrough);

        // For the default voice selection
        LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(passThrough)
        Mockito.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(passThrough)
            .onIsLanguageAvailable(
                    LittleMock.anyString(), LittleMock.anyString(), LittleMock.anyString());
        LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(passThrough)
                    Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
        Mockito.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(passThrough)
            .onLoadLanguage(
                    LittleMock.anyString(), LittleMock.anyString(), LittleMock.anyString());
                    Mockito.anyString(), Mockito.anyString(), Mockito.anyString());

        blockingInitAndVerify(MOCK_ENGINE, TextToSpeech.SUCCESS);
        assertEquals(MOCK_ENGINE, mTts.getCurrentEngine());
@@ -71,42 +74,42 @@ public class TextToSpeechTests extends InstrumentationTestCase {
    }

    public void testSetLanguage_delegation() {
        IDelegate delegate = LittleMock.mock(IDelegate.class);
        IDelegate delegate = Mockito.mock(IDelegate.class);
        MockableTextToSpeechService.setMocker(delegate);

        LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE).when(delegate).onIsLanguageAvailable(
        Mockito.doReturn(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE).when(delegate).onIsLanguageAvailable(
                "eng", "USA", "variant");
        LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE).when(delegate).onLoadLanguage(
        Mockito.doReturn(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE).when(delegate).onLoadLanguage(
                "eng", "USA", "variant");

        // Test 1 :Tests that calls to onLoadLanguage( ) are delegated through to the
        // service without any caching or intermediate steps.
        assertEquals(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE, mTts.setLanguage(new Locale("eng", "USA", "variant")));
        LittleMock.verify(delegate, LittleMock.anyTimes()).onIsLanguageAvailable(
        Mockito.verify(delegate, Mockito.atLeast(0)).onIsLanguageAvailable(
            "eng", "USA", "variant");
        LittleMock.verify(delegate, LittleMock.anyTimes()).onLoadLanguage(
        Mockito.verify(delegate, Mockito.atLeast(0)).onLoadLanguage(
            "eng", "USA", "variant");
    }

    public void testSetLanguage_availableLanguage() throws Exception {
        IDelegate delegate = LittleMock.mock(IDelegate.class);
        IDelegate delegate = Mockito.mock(IDelegate.class);
        MockableTextToSpeechService.setMocker(delegate);

        // ---------------------------------------------------------
        // Test 2 : Tests that when the language is successfully set
        // like above (returns LANG_COUNTRY_AVAILABLE). That the
        // request language changes from that point on.
        LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(delegate).onIsLanguageAvailable(
        Mockito.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(delegate).onIsLanguageAvailable(
                "eng", "USA", "variant");
        LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(delegate).onIsLanguageAvailable(
        Mockito.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(delegate).onIsLanguageAvailable(
                "eng", "USA", "");
        LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(delegate).onLoadLanguage(
        Mockito.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(delegate).onLoadLanguage(
                "eng", "USA", "");
        mTts.setLanguage(new Locale("eng", "USA", "variant"));
        blockingCallSpeak("foo bar", delegate);
        ArgumentCaptor<SynthesisRequest> req = LittleMock.createCaptor();
        LittleMock.verify(delegate, LittleMock.times(1)).onSynthesizeText(req.capture(),
                LittleMock.<SynthesisCallback>anyObject());
        ArgumentCaptor<SynthesisRequest> req = ArgumentCaptor.forClass(SynthesisRequest.class);
        Mockito.verify(delegate, Mockito.times(1)).onSynthesizeText(req.capture(),
                Mockito.<SynthesisCallback>anyObject());

        assertEquals("eng", req.getValue().getLanguage());
        assertEquals("USA", req.getValue().getCountry());
@@ -115,21 +118,21 @@ public class TextToSpeechTests extends InstrumentationTestCase {
    }

    public void testSetLanguage_unavailableLanguage() throws Exception {
        IDelegate delegate = LittleMock.mock(IDelegate.class);
        IDelegate delegate = Mockito.mock(IDelegate.class);
        MockableTextToSpeechService.setMocker(delegate);

        // ---------------------------------------------------------
        // TEST 3 : Tests that the language that is set does not change when the
        // engine reports it could not load the specified language.
        LittleMock.doReturn(TextToSpeech.LANG_NOT_SUPPORTED).when(
        Mockito.doReturn(TextToSpeech.LANG_NOT_SUPPORTED).when(
                delegate).onIsLanguageAvailable("fra", "FRA", "");
        LittleMock.doReturn(TextToSpeech.LANG_NOT_SUPPORTED).when(
        Mockito.doReturn(TextToSpeech.LANG_NOT_SUPPORTED).when(
                delegate).onLoadLanguage("fra", "FRA", "");
        mTts.setLanguage(Locale.FRANCE);
        blockingCallSpeak("le fou barre", delegate);
        ArgumentCaptor<SynthesisRequest> req2 = LittleMock.createCaptor();
        LittleMock.verify(delegate, LittleMock.times(1)).onSynthesizeText(req2.capture(),
                        LittleMock.<SynthesisCallback>anyObject());
        ArgumentCaptor<SynthesisRequest> req2 = ArgumentCaptor.forClass(SynthesisRequest.class);
        Mockito.verify(delegate, Mockito.times(1)).onSynthesizeText(req2.capture(),
                        Mockito.<SynthesisCallback>anyObject());

        // The params are basically unchanged.
        assertEquals("eng", req2.getValue().getLanguage());
@@ -139,41 +142,41 @@ public class TextToSpeechTests extends InstrumentationTestCase {
    }

    public void testIsLanguageAvailable() {
        IDelegate delegate = LittleMock.mock(IDelegate.class);
        IDelegate delegate = Mockito.mock(IDelegate.class);
        MockableTextToSpeechService.setMocker(delegate);

        // Test1: Simple end to end test.
        LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(
        Mockito.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(
                delegate).onIsLanguageAvailable("eng", "USA", "");

        assertEquals(TextToSpeech.LANG_COUNTRY_AVAILABLE, mTts.isLanguageAvailable(Locale.US));
        LittleMock.verify(delegate, LittleMock.times(1)).onIsLanguageAvailable(
        Mockito.verify(delegate, Mockito.times(1)).onIsLanguageAvailable(
                "eng", "USA", "");
    }

    public void testDefaultLanguage_setsVoiceName() throws Exception {
        IDelegate delegate = LittleMock.mock(IDelegate.class);
        IDelegate delegate = Mockito.mock(IDelegate.class);
        MockableTextToSpeechService.setMocker(delegate);
        Locale defaultLocale = Locale.getDefault();

        // ---------------------------------------------------------
        // Test that default language also sets the default voice
        // name
        LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).
        Mockito.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).
            when(delegate).onIsLanguageAvailable(
                defaultLocale.getISO3Language(),
                defaultLocale.getISO3Country().toUpperCase(),
                defaultLocale.getVariant());
        LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).
        Mockito.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).
            when(delegate).onLoadLanguage(
                defaultLocale.getISO3Language(),
                defaultLocale.getISO3Country(),
                defaultLocale.getVariant());

        blockingCallSpeak("foo bar", delegate);
        ArgumentCaptor<SynthesisRequest> req = LittleMock.createCaptor();
        LittleMock.verify(delegate, LittleMock.times(1)).onSynthesizeText(req.capture(),
                LittleMock.<SynthesisCallback>anyObject());
        ArgumentCaptor<SynthesisRequest> req = ArgumentCaptor.forClass(SynthesisRequest.class);
        Mockito.verify(delegate, Mockito.times(1)).onSynthesizeText(req.capture(),
                Mockito.<SynthesisCallback>anyObject());

        assertEquals(defaultLocale.getISO3Language(), req.getValue().getLanguage());
        assertEquals(defaultLocale.getISO3Country(), req.getValue().getCountry());
@@ -185,8 +188,8 @@ public class TextToSpeechTests extends InstrumentationTestCase {
    private void blockingCallSpeak(String speech, IDelegate mock) throws
            InterruptedException {
        final CountDownLatch latch = new CountDownLatch(1);
        doCountDown(latch).when(mock).onSynthesizeText(LittleMock.<SynthesisRequest>anyObject(),
                LittleMock.<SynthesisCallback>anyObject());
        doCountDown(latch).when(mock).onSynthesizeText(Mockito.<SynthesisRequest>anyObject(),
                Mockito.<SynthesisCallback>anyObject());
        mTts.speak(speech, TextToSpeech.QUEUE_ADD, null);

        awaitCountDown(latch, 5, TimeUnit.SECONDS);
@@ -194,7 +197,7 @@ public class TextToSpeechTests extends InstrumentationTestCase {

    private void blockingInitAndVerify(final String engine, int errorCode) throws
            InterruptedException {
        TextToSpeech.OnInitListener listener = LittleMock.mock(
        TextToSpeech.OnInitListener listener = Mockito.mock(
                TextToSpeech.OnInitListener.class);

        final CountDownLatch latch = new CountDownLatch(1);
@@ -206,18 +209,18 @@ public class TextToSpeechTests extends InstrumentationTestCase {
        awaitCountDown(latch, 5, TimeUnit.SECONDS);
    }

    public interface CountDownBehaviour extends Behaviour {
    public static abstract class CountDownBehaviour extends StubberImpl {
        /** Used to mock methods that return a result. */
        Behaviour andReturn(Object result);
        public abstract Stubber andReturn(Object result);
    }

    public static CountDownBehaviour doCountDown(final CountDownLatch latch) {
        return new CountDownBehaviour() {
            @Override
            public <T> T when(T mock) {
                return LittleMock.doAnswer(new Callable<Void>() {
                return Mockito.doAnswer(new Answer<Void>() {
                    @Override
                    public Void call() throws Exception {
                    public Void answer(InvocationOnMock invocation) throws Exception {
                        latch.countDown();
                        return null;
                    }
@@ -225,13 +228,13 @@ public class TextToSpeechTests extends InstrumentationTestCase {
            }

            @Override
            public Behaviour andReturn(final Object result) {
                return new Behaviour() {
            public Stubber andReturn(final Object result) {
                return new StubberImpl() {
                    @Override
                    public <T> T when(T mock) {
                        return LittleMock.doAnswer(new Callable<Object>() {
                        return Mockito.doAnswer(new Answer<Object>() {
                            @Override
                            public Object call() throws Exception {
                            public Object answer(InvocationOnMock invocation) throws Exception {
                                latch.countDown();
                                return result;
                            }