All test are now executable (but all are not working).

This commit is contained in:
Dennis Guse
2019-08-14 18:03:48 +02:00
parent 08825fcf04
commit 01ef5cfa6f
3 changed files with 123 additions and 120 deletions
@@ -16,10 +16,13 @@
package de.dennisguse.opentracks.services.tasks; package de.dennisguse.opentracks.services.tasks;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
/** /**
@@ -30,9 +33,13 @@ import org.junit.runner.RunWith;
*/ */
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class AnnouncementPeriodicTaskFactoryTest { public class AnnouncementPeriodicTaskFactoryTest {
private Context context = ApplicationProvider.getApplicationContext();
@Test
public void testCreate() { public void testCreate() {
PeriodicTaskFactory factory = new AnnouncementPeriodicTaskFactory(); PeriodicTaskFactory factory = new AnnouncementPeriodicTaskFactory();
PeriodicTask task = factory.create(InstrumentationRegistry.getInstrumentation().getContext()); PeriodicTask task = factory.create(context);
Assert.assertTrue(task instanceof AnnouncementPeriodicTask); Assert.assertTrue(task instanceof AnnouncementPeriodicTask);
} }
} }
@@ -21,24 +21,31 @@ import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener; import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.telephony.PhoneStateListener; import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.test.AndroidTestCase;
import de.dennisguse.opentracks.stats.TripStatistics; import androidx.test.core.app.ApplicationProvider;
import de.dennisguse.opentracks.util.StringUtils;
import com.google.android.testing.mocking.AndroidMock;
import com.google.android.testing.mocking.UsesMocks;
import org.easymock.Capture; import org.junit.After;
import org.easymock.EasyMock; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import static com.google.android.testing.mocking.AndroidMock.capture; import de.dennisguse.opentracks.stats.TripStatistics;
import static com.google.android.testing.mocking.AndroidMock.eq;
import static com.google.android.testing.mocking.AndroidMock.expect; import static org.mockito.Mockito.any;
import static com.google.android.testing.mocking.AndroidMock.same; import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.same;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/** /**
* Tests for {@link AnnouncementPeriodicTask}. * Tests for {@link AnnouncementPeriodicTask}.
@@ -47,28 +54,31 @@ import static com.google.android.testing.mocking.AndroidMock.same;
* *
* @author Rodrigo Damazio * @author Rodrigo Damazio
*/ */
public class AnnouncementPeriodicTaskTest extends AndroidTestCase { @RunWith(MockitoJUnitRunner.class)
public class AnnouncementPeriodicTaskTest {
// Use something other than our hardcoded value // Use something other than our hardcoded value
private static final Locale DEFAULT_LOCALE = Locale.KOREAN; private static final Locale DEFAULT_LOCALE = Locale.KOREAN;
private static final String ANNOUNCEMENT = "I can haz cheeseburger?"; private static final String ANNOUNCEMENT = "I can haz cheeseburger?";
private Locale oldDefaultLocale; private Locale oldDefaultLocale;
private Context context = ApplicationProvider.getApplicationContext();
private AnnouncementPeriodicTask task; private AnnouncementPeriodicTask task;
@Mock
private AnnouncementPeriodicTask mockTask; private AnnouncementPeriodicTask mockTask;
private Capture<OnInitListener> initListenerCapture; private ArgumentCaptor<OnInitListener> initListenerCapture = ArgumentCaptor.forClass(OnInitListener.class);
private Capture<PhoneStateListener> phoneListenerCapture; private ArgumentCaptor<PhoneStateListener> phoneListenerCapture = ArgumentCaptor.forClass(PhoneStateListener.class);
private TextToSpeechDelegate ttsDelegate; private TextToSpeechDelegate ttsDelegate;
@Mock
private TextToSpeechInterface tts; private TextToSpeechInterface tts;
@UsesMocks({ // @UsesMocks({
AnnouncementPeriodicTask.class, // StringUtils.class,
StringUtils.class, // })
})
@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
oldDefaultLocale = Locale.getDefault(); oldDefaultLocale = Locale.getDefault();
Locale.setDefault(DEFAULT_LOCALE); Locale.setDefault(DEFAULT_LOCALE);
@@ -84,7 +94,7 @@ public class AnnouncementPeriodicTaskTest extends AndroidTestCase {
} }
}; };
ttsDelegate = new TextToSpeechDelegate(getContext(), blockingListener); ttsDelegate = new TextToSpeechDelegate(context, blockingListener);
// Wait for all async operations done in the constructor to finish. // Wait for all async operations done in the constructor to finish.
synchronized (blockingListener) { synchronized (blockingListener) {
@@ -94,18 +104,10 @@ public class AnnouncementPeriodicTaskTest extends AndroidTestCase {
} }
} }
// Phew, done, now we can start forwarding calls
tts = AndroidMock.createMock(TextToSpeechInterface.class);
initListenerCapture = new Capture<OnInitListener>();
phoneListenerCapture = new Capture<PhoneStateListener>();
// Create a partial forwarding mock // Create a partial forwarding mock
mockTask = AndroidMock.createMock(AnnouncementPeriodicTask.class, getContext()); task = new AnnouncementPeriodicTask(context) {
task = new AnnouncementPeriodicTask(getContext()) {
@Override @Override
protected TextToSpeech newTextToSpeech(Context ctx, protected TextToSpeech newTextToSpeech(Context ctx, OnInitListener onInitListener) {
OnInitListener onInitListener) {
return mockTask.newTextToSpeech(ctx, onInitListener); return mockTask.newTextToSpeech(ctx, onInitListener);
} }
@@ -115,116 +117,109 @@ public class AnnouncementPeriodicTaskTest extends AndroidTestCase {
} }
@Override @Override
protected void listenToPhoneState( protected void listenToPhoneState(PhoneStateListener listener, int events) {
PhoneStateListener listener, int events) {
mockTask.listenToPhoneState(listener, events); mockTask.listenToPhoneState(listener, events);
} }
}; };
} }
@Override @After
protected void tearDown() { public void tearDown() {
Locale.setDefault(oldDefaultLocale); Locale.setDefault(oldDefaultLocale);
} }
@Test
public void testStart() { public void testStart() {
doStart(); doStart();
OnInitListener ttsInitListener = initListenerCapture.getValue(); OnInitListener ttsInitListener = initListenerCapture.getValue();
assertNotNull(ttsInitListener); Assert.assertNotNull(ttsInitListener);
AndroidMock.replay(tts);
ttsInitListener.onInit(TextToSpeech.SUCCESS); ttsInitListener.onInit(TextToSpeech.SUCCESS);
//TODO
AndroidMock.verify(mockTask, tts); //verify(mockTask, tts);
} }
@Test
public void testStart_notReady() { public void testStart_notReady() {
doStart(); doStart();
OnInitListener ttsInitListener = initListenerCapture.getValue(); OnInitListener ttsInitListener = initListenerCapture.getValue();
assertNotNull(ttsInitListener); Assert.assertNotNull(ttsInitListener);
AndroidMock.replay(tts);
ttsInitListener.onInit(TextToSpeech.ERROR); ttsInitListener.onInit(TextToSpeech.ERROR);
//TODO
AndroidMock.verify(mockTask, tts); //AndroidMock.verify(mockTask, tts);
} }
@Test
public void testShutdown() { public void testShutdown() {
// First, start // First, start
doStart(); doStart();
AndroidMock.verify(mockTask); verify(mockTask);
AndroidMock.reset(mockTask); reset(mockTask);
// Then, shut down // Then, shut down
PhoneStateListener phoneListener = phoneListenerCapture.getValue(); PhoneStateListener phoneListener = phoneListenerCapture.getValue();
mockTask.listenToPhoneState( mockTask.listenToPhoneState(
same(phoneListener), eq(PhoneStateListener.LISTEN_NONE)); same(phoneListener), eq(PhoneStateListener.LISTEN_NONE));
tts.shutdown(); tts.shutdown();
AndroidMock.replay(mockTask, tts);
task.shutdown(); task.shutdown();
AndroidMock.verify(mockTask, tts); //TODO
//AndroidMock.verify(mockTask, tts);
} }
public void testRun() throws Exception { @Test
// Expect service data calls public void testRun() {
// when service data calls
TripStatistics stats = new TripStatistics(); TripStatistics stats = new TripStatistics();
// Expect announcement building call // when announcement building call
expect(mockTask.getAnnouncement(same(stats))).andStubReturn(ANNOUNCEMENT); when(mockTask.getAnnouncement(same(stats))).thenReturn(ANNOUNCEMENT);
// Put task in "ready" state // Put task in "ready" state
startTask(TextToSpeech.SUCCESS); startTask(TextToSpeech.SUCCESS);
expect(tts.isLanguageAvailable(DEFAULT_LOCALE)).andStubReturn(TextToSpeech.LANG_AVAILABLE); when(tts.isLanguageAvailable(DEFAULT_LOCALE)).thenReturn(TextToSpeech.LANG_AVAILABLE);
expect(tts.setLanguage(DEFAULT_LOCALE)).andReturn(TextToSpeech.LANG_AVAILABLE); when(tts.setLanguage(DEFAULT_LOCALE)).thenReturn(TextToSpeech.LANG_AVAILABLE);
expect(tts.setSpeechRate(AnnouncementPeriodicTask.TTS_SPEECH_RATE)).andReturn( when(tts.setSpeechRate(AnnouncementPeriodicTask.TTS_SPEECH_RATE)).thenReturn(TextToSpeech.SUCCESS);
TextToSpeech.SUCCESS); when(tts.setOnUtteranceCompletedListener((OnUtteranceCompletedListener) any())).thenReturn(0);
expect(tts.setOnUtteranceCompletedListener((OnUtteranceCompletedListener) EasyMock.anyObject()))
.andReturn(0);
// Expect actual announcement call
expect(
tts.speak(eq(ANNOUNCEMENT), eq(TextToSpeech.QUEUE_FLUSH),
eq(AnnouncementPeriodicTask.SPEECH_PARAMS))).andReturn(0);
// Run the announcement // Run the announcement
AndroidMock.replay(tts);
task.announce(stats); task.announce(stats);
AndroidMock.verify(mockTask, tts); //TODO
//AndroidMock.verify(mockTask, tts);
} }
public void testRun_notReady() throws Exception { @Test
public void testRun_notReady() {
// Put task in "not ready" state // Put task in "not ready" state
startTask(TextToSpeech.ERROR); startTask(TextToSpeech.ERROR);
// Run the announcement // Run the announcement
AndroidMock.replay(tts);
task.run(null); task.run(null);
AndroidMock.verify(mockTask, tts); //TODO
//verify(mockTask, tts);
} }
public void testRun_duringCall() throws Exception { @Test
public void testRun_duringCall() {
startTask(TextToSpeech.SUCCESS); startTask(TextToSpeech.SUCCESS);
expect(tts.isSpeaking()).andStubReturn(false); when(tts.isSpeaking()).thenReturn(false);
// Run the announcement // Run the announcement
AndroidMock.replay(tts);
PhoneStateListener phoneListener = phoneListenerCapture.getValue(); PhoneStateListener phoneListener = phoneListenerCapture.getValue();
phoneListener.onCallStateChanged(TelephonyManager.CALL_STATE_OFFHOOK, null); phoneListener.onCallStateChanged(TelephonyManager.CALL_STATE_OFFHOOK, null);
task.run(null); task.run(null);
AndroidMock.verify(mockTask, tts); //TODO
//verify(mockTask, tts);
} }
public void testRun_ringWhileSpeaking() throws Exception { @Test
public void testRun_ringWhileSpeaking() {
startTask(TextToSpeech.SUCCESS); startTask(TextToSpeech.SUCCESS);
expect(tts.isSpeaking()).andStubReturn(true); when(tts.isSpeaking()).thenReturn(true);
expect(tts.stop()).andReturn(TextToSpeech.SUCCESS); when(tts.stop()).thenReturn(TextToSpeech.SUCCESS);
AndroidMock.replay(tts);
// Update the state to ringing - this should stop the current announcement. // Update the state to ringing - this should stop the current announcement.
PhoneStateListener phoneListener = phoneListenerCapture.getValue(); PhoneStateListener phoneListener = phoneListenerCapture.getValue();
@@ -232,111 +227,118 @@ public class AnnouncementPeriodicTaskTest extends AndroidTestCase {
// Run the announcement - this should do nothing. // Run the announcement - this should do nothing.
task.run(null); task.run(null);
//TODO
AndroidMock.verify(mockTask, tts); //AndroidMock.verify(mockTask, tts);
} }
public void testRun_whileRinging() throws Exception { @Test
public void testRun_whileRinging() {
startTask(TextToSpeech.SUCCESS); startTask(TextToSpeech.SUCCESS);
expect(tts.isSpeaking()).andStubReturn(false); when(tts.isSpeaking()).thenReturn(false);
// Run the announcement // Run the announcement
AndroidMock.replay(tts);
PhoneStateListener phoneListener = phoneListenerCapture.getValue(); PhoneStateListener phoneListener = phoneListenerCapture.getValue();
phoneListener.onCallStateChanged(TelephonyManager.CALL_STATE_RINGING, null); phoneListener.onCallStateChanged(TelephonyManager.CALL_STATE_RINGING, null);
task.run(null); task.run(null);
AndroidMock.verify(mockTask, tts); //TODO
//AndroidMock.verify(mockTask, tts);
} }
public void testRun_noService() throws Exception { @Test
public void testRun_noService() {
startTask(TextToSpeech.SUCCESS); startTask(TextToSpeech.SUCCESS);
// Run the announcement // Run the announcement
AndroidMock.replay(tts);
task.run(null); task.run(null);
AndroidMock.verify(mockTask, tts); //TODO
//AndroidMock.verify(mockTask, tts);
} }
public void testRun_noStats() throws Exception { @Test
// Expect service data calls public void testRun_noStats() {
// when service data calls
startTask(TextToSpeech.SUCCESS); startTask(TextToSpeech.SUCCESS);
// Run the announcement // Run the announcement
AndroidMock.replay(tts);
task.run(null); task.run(null);
AndroidMock.verify(mockTask, tts); //TODO
//AndroidMock.verify(mockTask, tts);
} }
/** /**
* Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with time zero. * Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with time zero.
*/ */
@Test
public void testGetAnnounceTime_time_zero() { public void testGetAnnounceTime_time_zero() {
long time = 0; // 0 seconds long time = 0; // 0 seconds
assertEquals("0 minutes 0 seconds", task.getAnnounceTime(time)); Assert.assertEquals("0 minutes 0 seconds", task.getAnnounceTime(time));
} }
/** /**
* Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with time one. * Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with time one.
*/ */
@Test
public void testGetAnnounceTime_time_one() { public void testGetAnnounceTime_time_one() {
long time = 1 * 1000; // 1 second long time = 1 * 1000; // 1 second
assertEquals("0 minutes 1 second", task.getAnnounceTime(time)); Assert.assertEquals("0 minutes 1 second", task.getAnnounceTime(time));
} }
/** /**
* Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with singular * Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with singular
* numbers with the hour unit. * numbers with the hour unit.
*/ */
@Test
public void testGetAnnounceTime_singular_has_hour() { public void testGetAnnounceTime_singular_has_hour() {
long time = (1 * 60 * 60 * 1000) + (1 * 60 * 1000) + (1 * 1000); // 1 hour 1 minute 1 second long time = (1 * 60 * 60 * 1000) + (1 * 60 * 1000) + (1 * 1000); // 1 hour 1 minute 1 second
assertEquals("1 hour 1 minute 1 second", task.getAnnounceTime(time)); Assert.assertEquals("1 hour 1 minute 1 second", task.getAnnounceTime(time));
} }
/** /**
* Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with plural numbers * Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with plural numbers
* with the hour unit. * with the hour unit.
*/ */
@Test
public void testGetAnnounceTime_plural_has_hour() { public void testGetAnnounceTime_plural_has_hour() {
long time = (2 * 60 * 60 * 1000) + (2 * 60 * 1000) + (2 * 1000); // 2 hours 2 minutes 2 seconds long time = (2 * 60 * 60 * 1000) + (2 * 60 * 1000) + (2 * 1000); // 2 hours 2 minutes 2 seconds
assertEquals("2 hours 2 minutes 2 seconds", task.getAnnounceTime(time)); Assert.assertEquals("2 hours 2 minutes 2 seconds", task.getAnnounceTime(time));
} }
/** /**
* Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with singular * Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with singular
* numbers without the hour unit. * numbers without the hour unit.
*/ */
@Test
public void testGetAnnounceTime_singular_no_hour() { public void testGetAnnounceTime_singular_no_hour() {
long time = (1 * 60 * 1000) + (1 * 1000); // 1 minute 1 second long time = (1 * 60 * 1000) + (1 * 1000); // 1 minute 1 second
assertEquals("1 minute 1 second", task.getAnnounceTime(time)); Assert.assertEquals("1 minute 1 second", task.getAnnounceTime(time));
} }
/** /**
* Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with plural numbers * Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with plural numbers
* without the hour unit. * without the hour unit.
*/ */
@Test
public void testGetAnnounceTime_plural_no_hour() { public void testGetAnnounceTime_plural_no_hour() {
long time = (2 * 60 * 1000) + (2 * 1000); // 2 minutes 2 seconds long time = (2 * 60 * 1000) + (2 * 1000); // 2 minutes 2 seconds
assertEquals("2 minutes 2 seconds", task.getAnnounceTime(time)); Assert.assertEquals("2 minutes 2 seconds", task.getAnnounceTime(time));
} }
private void startTask(int state) { private void startTask(int state) {
AndroidMock.resetToNice(tts); reset(tts);
AndroidMock.replay(tts);
doStart(); doStart();
OnInitListener ttsInitListener = initListenerCapture.getValue(); OnInitListener ttsInitListener = initListenerCapture.getValue();
ttsInitListener.onInit(state); ttsInitListener.onInit(state);
AndroidMock.resetToDefault(tts); //TODO
reset(tts);
//AndroidMock.resetToDefault(tts);
} }
private void doStart() { private void doStart() {
mockTask.listenToPhoneState(capture(phoneListenerCapture), mockTask.listenToPhoneState(phoneListenerCapture.capture(), eq(PhoneStateListener.LISTEN_CALL_STATE));
eq(PhoneStateListener.LISTEN_CALL_STATE)); when(mockTask.newTextToSpeech(same(context), initListenerCapture.capture())).thenReturn(ttsDelegate);
expect(mockTask.newTextToSpeech(
same(getContext()), capture(initListenerCapture)))
.andStubReturn(ttsDelegate);
AndroidMock.replay(mockTask);
task.start(); task.start();
} }
@@ -401,8 +403,7 @@ public class AnnouncementPeriodicTaskTest extends AndroidTestCase {
* set) will go up to the original class, but after tts is set we'll forward * set) will go up to the original class, but after tts is set we'll forward
* them all to the mock. * them all to the mock.
*/ */
private class TextToSpeechDelegate private class TextToSpeechDelegate extends TextToSpeech implements TextToSpeechInterface {
extends TextToSpeech implements TextToSpeechInterface {
public TextToSpeechDelegate(Context context, OnInitListener listener) { public TextToSpeechDelegate(Context context, OnInitListener listener) {
super(context, listener); super(context, listener);
} }
@@ -464,8 +465,7 @@ public class AnnouncementPeriodicTaskTest extends AndroidTestCase {
} }
@Override @Override
public int playEarcon(String earcon, int queueMode, public int playEarcon(String earcon, int queueMode, HashMap<String, String> params) {
HashMap<String, String> params) {
if (tts == null) { if (tts == null) {
return super.playEarcon(earcon, queueMode, params); return super.playEarcon(earcon, queueMode, params);
} }
@@ -473,8 +473,7 @@ public class AnnouncementPeriodicTaskTest extends AndroidTestCase {
} }
@Override @Override
public int playSilence(long durationInMs, int queueMode, public int playSilence(long durationInMs, int queueMode, HashMap<String, String> params) {
HashMap<String, String> params) {
if (tts == null) { if (tts == null) {
return super.playSilence(durationInMs, queueMode, params); return super.playSilence(durationInMs, queueMode, params);
} }
@@ -490,8 +489,7 @@ public class AnnouncementPeriodicTaskTest extends AndroidTestCase {
} }
@Override @Override
public int setOnUtteranceCompletedListener( public int setOnUtteranceCompletedListener(OnUtteranceCompletedListener listener) {
OnUtteranceCompletedListener listener) {
if (tts == null) { if (tts == null) {
return super.setOnUtteranceCompletedListener(listener); return super.setOnUtteranceCompletedListener(listener);
} }
@@ -541,8 +539,7 @@ public class AnnouncementPeriodicTaskTest extends AndroidTestCase {
} }
@Override @Override
public int synthesizeToFile(String text, HashMap<String, String> params, public int synthesizeToFile(String text, HashMap<String, String> params, String filename) {
String filename) {
if (tts == null) { if (tts == null) {
return super.synthesizeToFile(text, params, filename); return super.synthesizeToFile(text, params, filename);
} }
-1
View File
@@ -1 +0,0 @@
/apikeys.xml