Merge "Replace usages of littlemock with mockito"

am: 0a11c83016

Change-Id: I1456ae3d07d396080a817de01796eefc71230981
This commit is contained in:
Paul Duffin
2017-03-08 14:59:26 +00:00
committed by android-build-merger
5 changed files with 59 additions and 57 deletions

View File

@@ -29,7 +29,6 @@ LOCAL_STATIC_JAVA_LIBRARIES := \
frameworks-core-util-lib \ frameworks-core-util-lib \
mockwebserver \ mockwebserver \
guava \ guava \
littlemock \
android-support-test \ android-support-test \
mockito-target \ mockito-target \
espresso-core \ espresso-core \

View File

@@ -19,7 +19,7 @@ package android.net;
import com.android.org.conscrypt.ClientSessionContext; import com.android.org.conscrypt.ClientSessionContext;
import com.android.org.conscrypt.SSLClientSessionCache; import com.android.org.conscrypt.SSLClientSessionCache;
import com.google.testing.littlemock.LittleMock; import org.mockito.Mockito;
import junit.framework.TestCase; import junit.framework.TestCase;
@@ -39,25 +39,25 @@ public class SSLSessionCacheTest extends TestCase {
public void testInstall_compatibleContext() throws Exception { public void testInstall_compatibleContext() throws Exception {
final SSLContext ctx = SSLContext.getDefault(); 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(); final ClientSessionContext clientCtx = (ClientSessionContext) ctx.getClientSessionContext();
try { try {
SSLSessionCache.install(new SSLSessionCache(mock), ctx); SSLSessionCache.install(new SSLSessionCache(mock), ctx);
clientCtx.getSession("www.foogle.com", 443); clientCtx.getSession("www.foogle.com", 443);
LittleMock.verify(mock).getSessionData(LittleMock.anyString(), LittleMock.anyInt()); Mockito.verify(mock).getSessionData(Mockito.anyString(), Mockito.anyInt());
} finally { } finally {
// Restore cacheless behaviour. // Restore cacheless behaviour.
SSLSessionCache.install(null, ctx); SSLSessionCache.install(null, ctx);
clientCtx.getSession("www.foogle.com", 443); clientCtx.getSession("www.foogle.com", 443);
LittleMock.verifyNoMoreInteractions(mock); Mockito.verifyNoMoreInteractions(mock);
} }
} }
public void testInstall_incompatibleContext() { public void testInstall_incompatibleContext() {
try { try {
SSLSessionCache.install( SSLSessionCache.install(
new SSLSessionCache(LittleMock.mock(SSLClientSessionCache.class)), new SSLSessionCache(Mockito.mock(SSLClientSessionCache.class)),
new FakeSSLContext()); new FakeSSLContext());
fail(); fail();
} catch (IllegalArgumentException expected) {} } catch (IllegalArgumentException expected) {}
@@ -102,7 +102,7 @@ public class SSLSessionCacheTest extends TestCase {
@Override @Override
protected SSLSessionContext engineGetClientSessionContext() { protected SSLSessionContext engineGetClientSessionContext() {
return LittleMock.mock(SSLSessionContext.class); return Mockito.mock(SSLSessionContext.class);
} }
} }
} }

View File

@@ -16,8 +16,8 @@
package android.widget.focus; package android.widget.focus;
import static com.google.testing.littlemock.LittleMock.inOrder; import static org.mockito.Mockito.inOrder;
import static com.google.testing.littlemock.LittleMock.mock; import static org.mockito.Mockito.mock;
import android.os.Handler; import android.os.Handler;
import android.test.ActivityInstrumentationTestCase2; import android.test.ActivityInstrumentationTestCase2;
@@ -31,7 +31,7 @@ import android.view.ViewTreeObserver.OnGlobalFocusChangeListener;
import android.widget.Button; import android.widget.Button;
import com.android.frameworks.coretests.R; 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 * {@link RequestFocusTest} is set up to exercise cases where the views that

View File

@@ -20,7 +20,7 @@ LOCAL_MODULE_TAGS := tests
LOCAL_SRC_FILES := $(call all-subdir-java-files) 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_JAVA_LIBRARIES := android.test.runner
LOCAL_PACKAGE_NAME := TtsTests LOCAL_PACKAGE_NAME := TtsTests

View File

@@ -22,9 +22,12 @@ import android.speech.tts.TextToSpeech;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import com.android.speech.tts.MockableTextToSpeechService.IDelegate; import com.android.speech.tts.MockableTextToSpeechService.IDelegate;
import com.google.testing.littlemock.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import com.google.testing.littlemock.Behaviour; import org.mockito.Mockito;
import com.google.testing.littlemock.LittleMock; 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 junit.framework.Assert;
import java.util.Locale; import java.util.Locale;
@@ -40,16 +43,16 @@ public class TextToSpeechTests extends InstrumentationTestCase {
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
IDelegate passThrough = LittleMock.mock(IDelegate.class); IDelegate passThrough = Mockito.mock(IDelegate.class);
MockableTextToSpeechService.setMocker(passThrough); MockableTextToSpeechService.setMocker(passThrough);
// For the default voice selection // For the default voice selection
LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(passThrough) Mockito.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(passThrough)
.onIsLanguageAvailable( .onIsLanguageAvailable(
LittleMock.anyString(), LittleMock.anyString(), LittleMock.anyString()); Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(passThrough) Mockito.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(passThrough)
.onLoadLanguage( .onLoadLanguage(
LittleMock.anyString(), LittleMock.anyString(), LittleMock.anyString()); Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
blockingInitAndVerify(MOCK_ENGINE, TextToSpeech.SUCCESS); blockingInitAndVerify(MOCK_ENGINE, TextToSpeech.SUCCESS);
assertEquals(MOCK_ENGINE, mTts.getCurrentEngine()); assertEquals(MOCK_ENGINE, mTts.getCurrentEngine());
@@ -71,42 +74,42 @@ public class TextToSpeechTests extends InstrumentationTestCase {
} }
public void testSetLanguage_delegation() { public void testSetLanguage_delegation() {
IDelegate delegate = LittleMock.mock(IDelegate.class); IDelegate delegate = Mockito.mock(IDelegate.class);
MockableTextToSpeechService.setMocker(delegate); 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"); "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"); "eng", "USA", "variant");
// Test 1 :Tests that calls to onLoadLanguage( ) are delegated through to the // Test 1 :Tests that calls to onLoadLanguage( ) are delegated through to the
// service without any caching or intermediate steps. // service without any caching or intermediate steps.
assertEquals(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE, mTts.setLanguage(new Locale("eng", "USA", "variant"))); 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"); "eng", "USA", "variant");
LittleMock.verify(delegate, LittleMock.anyTimes()).onLoadLanguage( Mockito.verify(delegate, Mockito.atLeast(0)).onLoadLanguage(
"eng", "USA", "variant"); "eng", "USA", "variant");
} }
public void testSetLanguage_availableLanguage() throws Exception { public void testSetLanguage_availableLanguage() throws Exception {
IDelegate delegate = LittleMock.mock(IDelegate.class); IDelegate delegate = Mockito.mock(IDelegate.class);
MockableTextToSpeechService.setMocker(delegate); MockableTextToSpeechService.setMocker(delegate);
// --------------------------------------------------------- // ---------------------------------------------------------
// Test 2 : Tests that when the language is successfully set // Test 2 : Tests that when the language is successfully set
// like above (returns LANG_COUNTRY_AVAILABLE). That the // like above (returns LANG_COUNTRY_AVAILABLE). That the
// request language changes from that point on. // 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"); "eng", "USA", "variant");
LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(delegate).onIsLanguageAvailable( Mockito.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(delegate).onIsLanguageAvailable(
"eng", "USA", ""); "eng", "USA", "");
LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(delegate).onLoadLanguage( Mockito.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(delegate).onLoadLanguage(
"eng", "USA", ""); "eng", "USA", "");
mTts.setLanguage(new Locale("eng", "USA", "variant")); mTts.setLanguage(new Locale("eng", "USA", "variant"));
blockingCallSpeak("foo bar", delegate); blockingCallSpeak("foo bar", delegate);
ArgumentCaptor<SynthesisRequest> req = LittleMock.createCaptor(); ArgumentCaptor<SynthesisRequest> req = ArgumentCaptor.forClass(SynthesisRequest.class);
LittleMock.verify(delegate, LittleMock.times(1)).onSynthesizeText(req.capture(), Mockito.verify(delegate, Mockito.times(1)).onSynthesizeText(req.capture(),
LittleMock.<SynthesisCallback>anyObject()); Mockito.<SynthesisCallback>anyObject());
assertEquals("eng", req.getValue().getLanguage()); assertEquals("eng", req.getValue().getLanguage());
assertEquals("USA", req.getValue().getCountry()); assertEquals("USA", req.getValue().getCountry());
@@ -115,21 +118,21 @@ public class TextToSpeechTests extends InstrumentationTestCase {
} }
public void testSetLanguage_unavailableLanguage() throws Exception { public void testSetLanguage_unavailableLanguage() throws Exception {
IDelegate delegate = LittleMock.mock(IDelegate.class); IDelegate delegate = Mockito.mock(IDelegate.class);
MockableTextToSpeechService.setMocker(delegate); MockableTextToSpeechService.setMocker(delegate);
// --------------------------------------------------------- // ---------------------------------------------------------
// TEST 3 : Tests that the language that is set does not change when the // TEST 3 : Tests that the language that is set does not change when the
// engine reports it could not load the specified language. // 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", ""); delegate).onIsLanguageAvailable("fra", "FRA", "");
LittleMock.doReturn(TextToSpeech.LANG_NOT_SUPPORTED).when( Mockito.doReturn(TextToSpeech.LANG_NOT_SUPPORTED).when(
delegate).onLoadLanguage("fra", "FRA", ""); delegate).onLoadLanguage("fra", "FRA", "");
mTts.setLanguage(Locale.FRANCE); mTts.setLanguage(Locale.FRANCE);
blockingCallSpeak("le fou barre", delegate); blockingCallSpeak("le fou barre", delegate);
ArgumentCaptor<SynthesisRequest> req2 = LittleMock.createCaptor(); ArgumentCaptor<SynthesisRequest> req2 = ArgumentCaptor.forClass(SynthesisRequest.class);
LittleMock.verify(delegate, LittleMock.times(1)).onSynthesizeText(req2.capture(), Mockito.verify(delegate, Mockito.times(1)).onSynthesizeText(req2.capture(),
LittleMock.<SynthesisCallback>anyObject()); Mockito.<SynthesisCallback>anyObject());
// The params are basically unchanged. // The params are basically unchanged.
assertEquals("eng", req2.getValue().getLanguage()); assertEquals("eng", req2.getValue().getLanguage());
@@ -139,41 +142,41 @@ public class TextToSpeechTests extends InstrumentationTestCase {
} }
public void testIsLanguageAvailable() { public void testIsLanguageAvailable() {
IDelegate delegate = LittleMock.mock(IDelegate.class); IDelegate delegate = Mockito.mock(IDelegate.class);
MockableTextToSpeechService.setMocker(delegate); MockableTextToSpeechService.setMocker(delegate);
// Test1: Simple end to end test. // Test1: Simple end to end test.
LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when( Mockito.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(
delegate).onIsLanguageAvailable("eng", "USA", ""); delegate).onIsLanguageAvailable("eng", "USA", "");
assertEquals(TextToSpeech.LANG_COUNTRY_AVAILABLE, mTts.isLanguageAvailable(Locale.US)); 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", ""); "eng", "USA", "");
} }
public void testDefaultLanguage_setsVoiceName() throws Exception { public void testDefaultLanguage_setsVoiceName() throws Exception {
IDelegate delegate = LittleMock.mock(IDelegate.class); IDelegate delegate = Mockito.mock(IDelegate.class);
MockableTextToSpeechService.setMocker(delegate); MockableTextToSpeechService.setMocker(delegate);
Locale defaultLocale = Locale.getDefault(); Locale defaultLocale = Locale.getDefault();
// --------------------------------------------------------- // ---------------------------------------------------------
// Test that default language also sets the default voice // Test that default language also sets the default voice
// name // name
LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE). Mockito.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).
when(delegate).onIsLanguageAvailable( when(delegate).onIsLanguageAvailable(
defaultLocale.getISO3Language(), defaultLocale.getISO3Language(),
defaultLocale.getISO3Country().toUpperCase(), defaultLocale.getISO3Country().toUpperCase(),
defaultLocale.getVariant()); defaultLocale.getVariant());
LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE). Mockito.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).
when(delegate).onLoadLanguage( when(delegate).onLoadLanguage(
defaultLocale.getISO3Language(), defaultLocale.getISO3Language(),
defaultLocale.getISO3Country(), defaultLocale.getISO3Country(),
defaultLocale.getVariant()); defaultLocale.getVariant());
blockingCallSpeak("foo bar", delegate); blockingCallSpeak("foo bar", delegate);
ArgumentCaptor<SynthesisRequest> req = LittleMock.createCaptor(); ArgumentCaptor<SynthesisRequest> req = ArgumentCaptor.forClass(SynthesisRequest.class);
LittleMock.verify(delegate, LittleMock.times(1)).onSynthesizeText(req.capture(), Mockito.verify(delegate, Mockito.times(1)).onSynthesizeText(req.capture(),
LittleMock.<SynthesisCallback>anyObject()); Mockito.<SynthesisCallback>anyObject());
assertEquals(defaultLocale.getISO3Language(), req.getValue().getLanguage()); assertEquals(defaultLocale.getISO3Language(), req.getValue().getLanguage());
assertEquals(defaultLocale.getISO3Country(), req.getValue().getCountry()); assertEquals(defaultLocale.getISO3Country(), req.getValue().getCountry());
@@ -185,8 +188,8 @@ public class TextToSpeechTests extends InstrumentationTestCase {
private void blockingCallSpeak(String speech, IDelegate mock) throws private void blockingCallSpeak(String speech, IDelegate mock) throws
InterruptedException { InterruptedException {
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
doCountDown(latch).when(mock).onSynthesizeText(LittleMock.<SynthesisRequest>anyObject(), doCountDown(latch).when(mock).onSynthesizeText(Mockito.<SynthesisRequest>anyObject(),
LittleMock.<SynthesisCallback>anyObject()); Mockito.<SynthesisCallback>anyObject());
mTts.speak(speech, TextToSpeech.QUEUE_ADD, null); mTts.speak(speech, TextToSpeech.QUEUE_ADD, null);
awaitCountDown(latch, 5, TimeUnit.SECONDS); awaitCountDown(latch, 5, TimeUnit.SECONDS);
@@ -194,7 +197,7 @@ public class TextToSpeechTests extends InstrumentationTestCase {
private void blockingInitAndVerify(final String engine, int errorCode) throws private void blockingInitAndVerify(final String engine, int errorCode) throws
InterruptedException { InterruptedException {
TextToSpeech.OnInitListener listener = LittleMock.mock( TextToSpeech.OnInitListener listener = Mockito.mock(
TextToSpeech.OnInitListener.class); TextToSpeech.OnInitListener.class);
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
@@ -206,18 +209,18 @@ public class TextToSpeechTests extends InstrumentationTestCase {
awaitCountDown(latch, 5, TimeUnit.SECONDS); 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. */ /** 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) { public static CountDownBehaviour doCountDown(final CountDownLatch latch) {
return new CountDownBehaviour() { return new CountDownBehaviour() {
@Override @Override
public <T> T when(T mock) { public <T> T when(T mock) {
return LittleMock.doAnswer(new Callable<Void>() { return Mockito.doAnswer(new Answer<Void>() {
@Override @Override
public Void call() throws Exception { public Void answer(InvocationOnMock invocation) throws Exception {
latch.countDown(); latch.countDown();
return null; return null;
} }
@@ -225,13 +228,13 @@ public class TextToSpeechTests extends InstrumentationTestCase {
} }
@Override @Override
public Behaviour andReturn(final Object result) { public Stubber andReturn(final Object result) {
return new Behaviour() { return new StubberImpl() {
@Override @Override
public <T> T when(T mock) { public <T> T when(T mock) {
return LittleMock.doAnswer(new Callable<Object>() { return Mockito.doAnswer(new Answer<Object>() {
@Override @Override
public Object call() throws Exception { public Object answer(InvocationOnMock invocation) throws Exception {
latch.countDown(); latch.countDown();
return result; return result;
} }