diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/AdaptiveLocationListenerPolicy.java b/MyTracks/src/com/google/android/apps/mytracks/services/AdaptiveLocationListenerPolicy.java index eab300f73..7d630e995 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/AdaptiveLocationListenerPolicy.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/AdaptiveLocationListenerPolicy.java @@ -16,6 +16,7 @@ package com.google.android.apps.mytracks.services; + /** * A LocationListenerPolicy that will change based on how long the user has been * stationary. @@ -39,7 +40,7 @@ public class AdaptiveLocationListenerPolicy implements LocationListenerPolicy { private final long maxInterval; private final int minDistance; - + /** * The time the user has been at the current location, in milliseconds. */ diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/ControlRecordingService.java b/MyTracks/src/com/google/android/apps/mytracks/services/ControlRecordingService.java index 11e6b2a13..7b7b9f378 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/ControlRecordingService.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/ControlRecordingService.java @@ -20,6 +20,7 @@ import static com.google.android.apps.mytracks.Constants.TAG; import com.google.android.apps.mytracks.widgets.TrackWidgetProvider; import com.google.android.maps.mytracks.R; +import com.google.common.annotations.VisibleForTesting; import android.app.IntentService; import android.content.ComponentName; @@ -94,6 +95,18 @@ public class ControlRecordingService extends IntentService implements ServiceCon @Override protected void onHandleIntent(Intent intent) { waitConnected(); + onHandleIntent(intent, trackRecordingService); + } + + + /** + * Handles the intent to start or stop a recording. + * + * @param intent to be handled + * @param trackRecordingService the trackRecordingService + */ + @VisibleForTesting + void onHandleIntent(Intent intent, @SuppressWarnings("hiding") ITrackRecordingService trackRecordingService) { String action = intent.getAction(); if (action != null) { try { diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/services/AdaptiveLocationListenerPolicyTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/services/AdaptiveLocationListenerPolicyTest.java new file mode 100644 index 000000000..22741cad5 --- /dev/null +++ b/MyTracksTest/src/com/google/android/apps/mytracks/services/AdaptiveLocationListenerPolicyTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2012 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 + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.android.apps.mytracks.services; + +import junit.framework.TestCase; + +/** + * Tests the {@link AdaptiveLocationListenerPolicy}. + * + * @author youtaol + */ +public class AdaptiveLocationListenerPolicyTest extends TestCase { + + private AdaptiveLocationListenerPolicy adocationListenerPolicy; + private static final long MIN = 1000; + private static final long MAX = 3000; + private static final int MIN_DISTANCE = 10; + private static final long NEW_IDLE_TIME_BIG = 10000; + private static final long NEW_IDLE_TIME_NORMAL = 5000; + private static final long NEW_IDLE_TIME_SMALL = 2000; + private static final long NEW_IDLE_TIME_LESS_THAN_MIN = 500; + + @Override + protected void setUp() throws Exception { + super.setUp(); + adocationListenerPolicy = new AdaptiveLocationListenerPolicy(MIN, MAX, MIN_DISTANCE); + } + + /** + * Tests the + * {@link AdaptiveLocationListenerPolicy#getDesiredPollingInterval()} in four + * situations. + * + */ + public void testGetDesiredPollingInterval() { + adocationListenerPolicy.updateIdleTime(NEW_IDLE_TIME_BIG); + assertEquals(MAX, adocationListenerPolicy.getDesiredPollingInterval()); + + adocationListenerPolicy.updateIdleTime(NEW_IDLE_TIME_NORMAL); + // First get the half of NEW_IDLE_TIME_NORMAL, and then round it to the + // nearest second. + assertEquals((NEW_IDLE_TIME_NORMAL / 2 / 1000) * 1000, + adocationListenerPolicy.getDesiredPollingInterval()); + + adocationListenerPolicy.updateIdleTime(NEW_IDLE_TIME_SMALL); + assertEquals(MIN, adocationListenerPolicy.getDesiredPollingInterval()); + + adocationListenerPolicy.updateIdleTime(NEW_IDLE_TIME_LESS_THAN_MIN); + assertEquals(MIN, adocationListenerPolicy.getDesiredPollingInterval()); + } + + /** + * Tests the method {@link AdaptiveLocationListenerPolicy#getMinDistance()}. + */ + public void testGetMinDistance() { + assertEquals(MIN_DISTANCE, adocationListenerPolicy.getMinDistance()); + } +} diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/services/ControlRecordingServiceTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/services/ControlRecordingServiceTest.java new file mode 100644 index 000000000..e7c887a5f --- /dev/null +++ b/MyTracksTest/src/com/google/android/apps/mytracks/services/ControlRecordingServiceTest.java @@ -0,0 +1,105 @@ +/* + * Copyright 2012 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 + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.android.apps.mytracks.services; + +import com.google.android.maps.mytracks.R; +import com.google.android.testing.mocking.UsesMocks; + +import android.content.Context; +import android.content.Intent; +import android.os.RemoteException; +import android.test.ServiceTestCase; + +import org.easymock.EasyMock; + +/** + * Tests {@link ControlRecordingService}. + * + * @author Youtao Liu + */ +public class ControlRecordingServiceTest extends ServiceTestCase { + + private Context context; + private ControlRecordingService controlRecordingService; + + public ControlRecordingServiceTest() { + super(ControlRecordingService.class); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + context = getContext(); + } + + /** + * Tests the start of {@link ControlRecordingService} and tests the method + * {@link ControlRecordingService#onHandleIntent(Intent, ITrackRecordingService)} + * to start a track recording. + */ + @UsesMocks(ITrackRecordingService.class) + public void testStartRecording() { + assertNull(controlRecordingService); + Intent intent = startControlRecordingService(context.getString(R.string.track_action_start)); + assertNotNull(controlRecordingService); + + ITrackRecordingService iTrackRecordingServiceMock = EasyMock + .createStrictMock(ITrackRecordingService.class); + try { + EasyMock.expect(iTrackRecordingServiceMock.startNewTrack()).andReturn(1L); + EasyMock.replay(iTrackRecordingServiceMock); + controlRecordingService.onHandleIntent(intent, iTrackRecordingServiceMock); + EasyMock.verify(iTrackRecordingServiceMock); + } catch (RemoteException e) { + fail(); + } + } + + /** + * Tests the method + * {@link ControlRecordingService#onHandleIntent(Intent, ITrackRecordingService)} + * to stop a track recording. + */ + @UsesMocks(ITrackRecordingService.class) + public void testStopRecording() { + Intent intent = startControlRecordingService(context.getString(R.string.track_action_end)); + + ITrackRecordingService iTrackRecordingServiceMock = EasyMock + .createStrictMock(ITrackRecordingService.class); + try { + iTrackRecordingServiceMock.endCurrentTrack(); + EasyMock.replay(iTrackRecordingServiceMock); + controlRecordingService.onHandleIntent(intent, iTrackRecordingServiceMock); + EasyMock.verify(iTrackRecordingServiceMock); + } catch (RemoteException e) { + fail(); + } + } + + /** + * Starts a ControlRecordingService with a specified action. + * + * @param action the action string in the start intent + */ + private Intent startControlRecordingService(String action) { + Intent intent = new Intent(context, ControlRecordingService.class); + intent.setAction(action); + startService(intent); + controlRecordingService = getService(); + return intent; + } + +}