From 109d2969a5a722eba725d84b79e838a1fc8e7f45 Mon Sep 17 00:00:00 2001 From: Matthew Simmons Date: Fri, 11 Mar 2011 16:55:10 -0500 Subject: [PATCH] Fix test failures by removing the need to mock TrackRecordingService. Android mock appears to have a problem with TRS and Context. When it tries to mock either class, it ends up trying (and failing) to mock DatabaseErrorHandler, which for some reason always fails. --- .../services/StatusAnnouncerTask.java | 34 ++++++++--- .../services/StatusAnnouncerTaskTest.java | 61 +++++++------------ 2 files changed, 45 insertions(+), 50 deletions(-) diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/StatusAnnouncerTask.java b/MyTracks/src/com/google/android/apps/mytracks/services/StatusAnnouncerTask.java index 756f1393e..bed0e1185 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/StatusAnnouncerTask.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/StatusAnnouncerTask.java @@ -1,12 +1,12 @@ /* * Copyright 2009 Google Inc. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -128,6 +128,25 @@ public class StatusAnnouncerTask implements PeriodicTask { */ @Override public void run(TrackRecordingService service) { + if (service == null) { + Log.e(MyTracksConstants.TAG, "StatusAnnouncer TrackRecordingService not initialized"); + return; + } + + runWithStatistics(service.getTripStatistics()); + } + + /** + * This method exists as a convenience for testing code, allowing said code + * to avoid needing to instantiate an entire {@link TrackRecordingService} + * just to test the announcer. + */ + protected void runWithStatistics(TripStatistics statistics) { + if (statistics == null) { + Log.e(MyTracksConstants.TAG, "StatusAnnouncer stats not initialized."); + return; + } + if (!ready || tts == null) { Log.e(MyTracksConstants.TAG, "StatusAnnouncer Tts not ready."); return; @@ -139,12 +158,7 @@ public class StatusAnnouncerTask implements PeriodicTask { return; } - if (service == null || service.getTripStatistics() == null) { - Log.e(MyTracksConstants.TAG, "StatusAnnouncer stats not initialized."); - return; - } - - String announcement = getAnnouncement(service.getTripStatistics()); + String announcement = getAnnouncement(statistics); Log.d(MyTracksConstants.TAG, "Announcement: " + announcement); speakAnnouncment(announcement); } @@ -165,7 +179,7 @@ public class StatusAnnouncerTask implements PeriodicTask { boolean metricUnits = true; boolean reportSpeed = true; if (preferences != null) { - metricUnits = + metricUnits = preferences.getBoolean(context.getString(R.string.metric_units_key), true); reportSpeed = diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/services/StatusAnnouncerTaskTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/services/StatusAnnouncerTaskTest.java index 59ef6853c..4bc7a3664 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/services/StatusAnnouncerTaskTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/services/StatusAnnouncerTaskTest.java @@ -1,12 +1,12 @@ /* * Copyright 2010 Google Inc. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -255,7 +255,6 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { @UsesMocks({ StatusAnnouncerTask.class, StringUtils.class, - TrackRecordingService.class, }) @Override protected void setUp() throws Exception { @@ -390,9 +389,6 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { public void testRun() throws Exception { // Expect service data calls TripStatistics stats = new TripStatistics(); - TrackRecordingService service = - AndroidMock.createMock(TrackRecordingService.class); - expect(service.getTripStatistics()).andStubReturn(stats); // Expect announcement building call expect(mockTask.getAnnouncement(same(stats))).andStubReturn(ANNOUNCEMENT); @@ -407,75 +403,63 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { .andReturn(TextToSpeech.SUCCESS); // Run the announcement - AndroidMock.replay(tts, stringUtils, service); - task.run(service); - AndroidMock.verify(mockTask, tts, stringUtils, service); + AndroidMock.replay(tts, stringUtils); + task.runWithStatistics(stats); + AndroidMock.verify(mockTask, tts, stringUtils); } public void testRun_notReady() throws Exception { - TrackRecordingService service = - AndroidMock.createMock(TrackRecordingService.class); - // Put task in "not ready" state startTask(TextToSpeech.ERROR); // Run the announcement - AndroidMock.replay(tts, stringUtils, service); - task.run(service); - AndroidMock.verify(mockTask, tts, stringUtils, service); + AndroidMock.replay(tts, stringUtils); + task.runWithStatistics(null); + AndroidMock.verify(mockTask, tts, stringUtils); } public void testRun_duringCall() throws Exception { - TrackRecordingService service = - AndroidMock.createMock(TrackRecordingService.class); - startTask(TextToSpeech.SUCCESS); expect(tts.isSpeaking()).andStubReturn(false); // Run the announcement - AndroidMock.replay(tts, stringUtils, service); + AndroidMock.replay(tts, stringUtils); PhoneStateListener phoneListener = phoneListenerCapture.getValue(); phoneListener.onCallStateChanged(TelephonyManager.CALL_STATE_OFFHOOK, null); - task.run(service); - AndroidMock.verify(mockTask, tts, stringUtils, service); + task.runWithStatistics(null); + AndroidMock.verify(mockTask, tts, stringUtils); } public void testRun_ringWhileSpeaking() throws Exception { - TrackRecordingService service = - AndroidMock.createMock(TrackRecordingService.class); - startTask(TextToSpeech.SUCCESS); expect(tts.isSpeaking()).andStubReturn(true); expect(tts.stop()).andReturn(TextToSpeech.SUCCESS); - AndroidMock.replay(tts, stringUtils, service); + AndroidMock.replay(tts, stringUtils); // Update the state to ringing - this should stop the current announcement. PhoneStateListener phoneListener = phoneListenerCapture.getValue(); phoneListener.onCallStateChanged(TelephonyManager.CALL_STATE_RINGING, null); // Run the announcement - this should do nothing. - task.run(service); + task.runWithStatistics(null); - AndroidMock.verify(mockTask, tts, stringUtils, service); + AndroidMock.verify(mockTask, tts, stringUtils); } public void testRun_whileRinging() throws Exception { - TrackRecordingService service = - AndroidMock.createMock(TrackRecordingService.class); - startTask(TextToSpeech.SUCCESS); expect(tts.isSpeaking()).andStubReturn(false); // Run the announcement - AndroidMock.replay(tts, stringUtils, service); + AndroidMock.replay(tts, stringUtils); PhoneStateListener phoneListener = phoneListenerCapture.getValue(); phoneListener.onCallStateChanged(TelephonyManager.CALL_STATE_RINGING, null); - task.run(service); - AndroidMock.verify(mockTask, tts, stringUtils, service); + task.runWithStatistics(null); + AndroidMock.verify(mockTask, tts, stringUtils); } public void testRun_noService() throws Exception { @@ -489,16 +473,13 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { public void testRun_noStats() throws Exception { // Expect service data calls - TrackRecordingService service = - AndroidMock.createMock(TrackRecordingService.class); - expect(service.getTripStatistics()).andStubReturn(null); startTask(TextToSpeech.SUCCESS); // Run the announcement - AndroidMock.replay(tts, stringUtils, service); - task.run(service); - AndroidMock.verify(mockTask, tts, stringUtils, service); + AndroidMock.replay(tts, stringUtils); + task.runWithStatistics(null); + AndroidMock.verify(mockTask, tts, stringUtils); } private void startTask(int state) {