forked from upstream-mirrors/OpenTracks
Merge with head.
This commit is contained in:
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
package com.google.android.apps.mytracks;
|
||||
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.SendDialog;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.app.ProgressDialog;
|
||||
import android.test.ActivityInstrumentationTestCase2;
|
||||
|
||||
/**
|
||||
* @author Sandor Dornbush
|
||||
*/
|
||||
public class DialogManagerTest
|
||||
extends ActivityInstrumentationTestCase2<MyTracks> {
|
||||
|
||||
public DialogManagerTest() {
|
||||
super(MyTracks.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
MyTracks.clearInstance();
|
||||
assertNull(MyTracks.getInstance());
|
||||
}
|
||||
|
||||
public void test_onCreateChartSettings() {
|
||||
Dialog d =
|
||||
getActivity().onCreateDialog(DialogManager.DIALOG_CHART_SETTINGS, null);
|
||||
assertNotNull(d);
|
||||
assertTrue(d instanceof ChartSettingsDialog);
|
||||
}
|
||||
|
||||
public void test_onCreateImportProgress() {
|
||||
Dialog d =
|
||||
getActivity().onCreateDialog(
|
||||
DialogManager.DIALOG_IMPORT_PROGRESS, null);
|
||||
assertNotNull(d);
|
||||
assertTrue(d instanceof ProgressDialog);
|
||||
}
|
||||
|
||||
public void test_onCreateProgress() {
|
||||
Dialog d =
|
||||
getActivity().onCreateDialog(DialogManager.DIALOG_PROGRESS, null);
|
||||
assertNotNull(d);
|
||||
assertTrue(d instanceof ProgressDialog);
|
||||
ProgressDialog pd = (ProgressDialog) d;
|
||||
assertEquals(100, pd.getMax());
|
||||
assertEquals(10, pd.getProgress());
|
||||
}
|
||||
|
||||
public void test_onCreateSendToGoogle() {
|
||||
Dialog d =
|
||||
getActivity().onCreateDialog(DialogManager.DIALOG_SEND_TO_GOOGLE, null);
|
||||
assertNotNull(d);
|
||||
assertTrue(d instanceof SendDialog);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.google.android.apps.mytracks;
|
||||
|
||||
import com.google.android.apps.mytracks.services.ServiceUtils;
|
||||
import com.google.android.apps.mytracks.services.TrackRecordingServiceConnection;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
|
||||
import android.app.Activity;
|
||||
@@ -32,60 +34,53 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* A unit test for {@link MyTracks} activity.
|
||||
*
|
||||
*
|
||||
* @author Bartlomiej Niechwiej
|
||||
*/
|
||||
public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
|
||||
private SharedPreferences sharedPreferences;
|
||||
private TrackRecordingServiceConnection serviceConnection;
|
||||
|
||||
public MyTracksTest() {
|
||||
super(MyTracks.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
MyTracks.clearInstance();
|
||||
assertNull(MyTracks.getInstance());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
clearSelectedAndRecordingTracks();
|
||||
waitForIdle();
|
||||
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
|
||||
public void testInitialization_mainAction() {
|
||||
// Make sure we can start MyTracks and the activity doesn't start recording.
|
||||
assertNotNull(getActivity());
|
||||
assertNotNull(MyTracks.getInstance());
|
||||
assertNotNull(getActivity().getSharedPreferences());
|
||||
|
||||
assertInitialized();
|
||||
|
||||
// Check if not recording.
|
||||
assertFalse(getActivity().isRecording());
|
||||
assertEquals(-1, getActivity().getRecordingTrackId());
|
||||
long selectedTrackId = getActivity().getSharedPreferences().getLong(
|
||||
assertFalse(isRecording());
|
||||
assertEquals(-1, getRecordingTrackId());
|
||||
long selectedTrackId = getSharedPreferences().getLong(
|
||||
getActivity().getString(R.string.selected_track_key), -1);
|
||||
assertEquals(selectedTrackId, getActivity().getSelectedTrackId());
|
||||
}
|
||||
|
||||
|
||||
public void testInitialization_viewActionWithNoData() {
|
||||
// Simulate start with ACTION_VIEW intent.
|
||||
Intent startIntent = new Intent();
|
||||
startIntent.setAction(Intent.ACTION_VIEW);
|
||||
setActivityIntent(startIntent);
|
||||
|
||||
assertNotNull(getActivity());
|
||||
assertNotNull(MyTracks.getInstance());
|
||||
assertNotNull(getActivity().getSharedPreferences());
|
||||
|
||||
|
||||
assertInitialized();
|
||||
|
||||
// Check if not recording.
|
||||
assertFalse(getActivity().isRecording());
|
||||
assertEquals(-1, getActivity().getRecordingTrackId());
|
||||
long selectedTrackId = getActivity().getSharedPreferences().getLong(
|
||||
assertFalse(isRecording());
|
||||
assertEquals(-1, getRecordingTrackId());
|
||||
long selectedTrackId = getSharedPreferences().getLong(
|
||||
getActivity().getString(R.string.selected_track_key), -1);
|
||||
assertEquals(selectedTrackId, getActivity().getSelectedTrackId());
|
||||
}
|
||||
|
||||
|
||||
public void testInitialization_viewActionWithValidData() throws Exception {
|
||||
// Simulate start with ACTION_VIEW intent.
|
||||
Intent startIntent = new Intent();
|
||||
@@ -93,24 +88,22 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
|
||||
Uri uri = Uri.fromFile(File.createTempFile("valid", ".gpx"));
|
||||
|
||||
// TODO: Add a valid GPX.
|
||||
|
||||
|
||||
startIntent.setData(uri);
|
||||
setActivityIntent(startIntent);
|
||||
|
||||
assertNotNull(getActivity());
|
||||
assertNotNull(MyTracks.getInstance());
|
||||
assertNotNull(getActivity().getSharedPreferences());
|
||||
|
||||
|
||||
assertInitialized();
|
||||
|
||||
// Check if not recording.
|
||||
assertFalse(getActivity().isRecording());
|
||||
assertEquals(-1, getActivity().getRecordingTrackId());
|
||||
long selectedTrackId = getActivity().getSharedPreferences().getLong(
|
||||
assertFalse(isRecording());
|
||||
assertEquals(-1, getRecordingTrackId());
|
||||
long selectedTrackId = getSharedPreferences().getLong(
|
||||
getActivity().getString(R.string.selected_track_key), -1);
|
||||
assertEquals(selectedTrackId, getActivity().getSelectedTrackId());
|
||||
|
||||
// TODO: Finish this test.
|
||||
|
||||
// TODO: Finish this test.
|
||||
}
|
||||
|
||||
|
||||
public void testInitialization_viewActionWithInvalidData() throws Exception {
|
||||
// Simulate start with ACTION_VIEW intent.
|
||||
Intent startIntent = new Intent();
|
||||
@@ -118,66 +111,62 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
|
||||
Uri uri = Uri.fromFile(File.createTempFile("invalid", ".gpx"));
|
||||
startIntent.setData(uri);
|
||||
setActivityIntent(startIntent);
|
||||
|
||||
assertNotNull(getActivity());
|
||||
assertNotNull(MyTracks.getInstance());
|
||||
assertNotNull(getActivity().getSharedPreferences());
|
||||
|
||||
|
||||
assertInitialized();
|
||||
|
||||
// Check if not recording.
|
||||
assertFalse(getActivity().isRecording());
|
||||
assertEquals(-1, getActivity().getRecordingTrackId());
|
||||
long selectedTrackId = getActivity().getSharedPreferences().getLong(
|
||||
assertFalse(isRecording());
|
||||
assertEquals(-1, getRecordingTrackId());
|
||||
long selectedTrackId = getSharedPreferences().getLong(
|
||||
getActivity().getString(R.string.selected_track_key), -1);
|
||||
assertEquals(selectedTrackId, getActivity().getSelectedTrackId());
|
||||
|
||||
|
||||
// TODO: Finish this test.
|
||||
}
|
||||
|
||||
|
||||
public void testRecording_startAndStop() throws Exception {
|
||||
// Make sure we can start MyTracks and the activity doesn't start recording.
|
||||
assertNotNull(getActivity());
|
||||
assertNotNull(MyTracks.getInstance());
|
||||
assertNotNull(getActivity().getSharedPreferences());
|
||||
assertInitialized();
|
||||
|
||||
// Check if not recording.
|
||||
clearSelectedAndRecordingTracks();
|
||||
clearSelectedAndRecordingTracks();
|
||||
waitForIdle();
|
||||
|
||||
assertFalse(getActivity().isRecording());
|
||||
assertEquals(-1, getActivity().getRecordingTrackId());
|
||||
long selectedTrackId = getActivity().getSharedPreferences().getLong(
|
||||
assertFalse(isRecording());
|
||||
assertEquals(-1, getRecordingTrackId());
|
||||
long selectedTrackId = getSharedPreferences().getLong(
|
||||
getActivity().getString(R.string.selected_track_key), -1);
|
||||
assertEquals(selectedTrackId, getActivity().getSelectedTrackId());
|
||||
|
||||
// Start a new track.
|
||||
getActivity().startRecording();
|
||||
serviceConnection.bindIfRunning();
|
||||
long recordingTrackId = awaitRecordingStatus(5000, true);
|
||||
assertTrue(recordingTrackId >= 0);
|
||||
|
||||
|
||||
// Wait until we are done and make sure that selectedTrack = recordingTrack.
|
||||
waitForIdle();
|
||||
assertEquals(recordingTrackId, getActivity().getSharedPreferences().getLong(
|
||||
assertEquals(recordingTrackId, getSharedPreferences().getLong(
|
||||
getActivity().getString(R.string.recording_track_key), -1));
|
||||
selectedTrackId = getActivity().getSharedPreferences().getLong(
|
||||
selectedTrackId = getSharedPreferences().getLong(
|
||||
getActivity().getString(R.string.selected_track_key), -1);
|
||||
assertEquals(recordingTrackId, selectedTrackId);
|
||||
assertEquals(selectedTrackId, getActivity().getSelectedTrackId());
|
||||
|
||||
// Watch for MyTracksDetails activity.
|
||||
|
||||
// Watch for MyTracksDetails activity.
|
||||
ActivityMonitor monitor = getInstrumentation().addMonitor(
|
||||
TrackDetails.class.getName(), null, false);
|
||||
|
||||
// Now, stop the track and make sure that it is still selected, but
|
||||
// no longer recording.
|
||||
getActivity().stopRecording();
|
||||
|
||||
// Check if we got back MyTracksDetails activity.
|
||||
|
||||
// Check if we got back MyTracksDetails activity.
|
||||
Activity activity = getInstrumentation().waitForMonitor(monitor);
|
||||
assertTrue(activity instanceof TrackDetails);
|
||||
|
||||
|
||||
// TODO: Update track name and other properties and test if they were
|
||||
// properly saved.
|
||||
|
||||
|
||||
// Simulate a click on Save button.
|
||||
Button save = (Button) activity.findViewById(R.id.trackdetails_save);
|
||||
save.performClick();
|
||||
@@ -185,18 +174,24 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
|
||||
// Check the remaining properties.
|
||||
recordingTrackId = awaitRecordingStatus(5000, false);
|
||||
assertEquals(-1, recordingTrackId);
|
||||
assertEquals(recordingTrackId, getActivity().getRecordingTrackId());
|
||||
assertEquals(recordingTrackId, getActivity().getSharedPreferences().getLong(
|
||||
assertEquals(recordingTrackId, getRecordingTrackId());
|
||||
assertEquals(recordingTrackId, getSharedPreferences().getLong(
|
||||
getActivity().getString(R.string.recording_track_key), -1));
|
||||
// Make sure this is the same track as the last recording track ID.
|
||||
assertEquals(selectedTrackId, getActivity().getSelectedTrackId());
|
||||
}
|
||||
|
||||
|
||||
private void assertInitialized() {
|
||||
assertNotNull(getActivity());
|
||||
|
||||
serviceConnection = new TrackRecordingServiceConnection(getActivity(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits until the UI thread becomes idle.
|
||||
*/
|
||||
private void waitForIdle() throws InterruptedException {
|
||||
// Note: We can't use getInstrumentation().waitForIdleSync() here.
|
||||
// Note: We can't use getInstrumentation().waitForIdleSync() here.
|
||||
final Object semaphore = new Object();
|
||||
synchronized (semaphore) {
|
||||
final AtomicBoolean isIdle = new AtomicBoolean();
|
||||
@@ -216,20 +211,20 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears {selected,recording}TrackId in the {@link SharedPreferences}.
|
||||
* Clears {selected,recording}TrackId in the {@link getSharedPreferences()}.
|
||||
*/
|
||||
private void clearSelectedAndRecordingTracks() {
|
||||
Editor editor = getActivity().getSharedPreferences().edit();
|
||||
Editor editor = getSharedPreferences().edit();
|
||||
editor.putLong(getActivity().getString(R.string.selected_track_key), -1);
|
||||
editor.putLong(getActivity().getString(R.string.recording_track_key), -1);
|
||||
|
||||
|
||||
editor.clear();
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Waits until the recording state changes to the given status.
|
||||
*
|
||||
*
|
||||
* @param timeout the maximum time to wait, in milliseconds.
|
||||
* @param isRecording the final status to await.
|
||||
* @return the recording track ID.
|
||||
@@ -237,14 +232,30 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
|
||||
private long awaitRecordingStatus(long timeout, boolean isRecording)
|
||||
throws TimeoutException, InterruptedException {
|
||||
long startTime = System.nanoTime();
|
||||
while (getActivity().isRecording() != isRecording) {
|
||||
while (isRecording() != isRecording) {
|
||||
if (System.nanoTime() - startTime > timeout * 1000000) {
|
||||
throw new TimeoutException("Timeout while waiting for recording!");
|
||||
}
|
||||
Thread.sleep(20);
|
||||
}
|
||||
waitForIdle();
|
||||
assertEquals(isRecording, getActivity().isRecording());
|
||||
return getActivity().getRecordingTrackId();
|
||||
assertEquals(isRecording, isRecording());
|
||||
return getRecordingTrackId();
|
||||
}
|
||||
|
||||
private long getRecordingTrackId() {
|
||||
return getSharedPreferences().getLong(getActivity().getString(R.string.recording_track_key), -1);
|
||||
}
|
||||
|
||||
private SharedPreferences getSharedPreferences() {
|
||||
if (sharedPreferences == null) {
|
||||
sharedPreferences = getActivity().getSharedPreferences(Constants.SETTINGS_NAME, 0);
|
||||
}
|
||||
return sharedPreferences;
|
||||
}
|
||||
|
||||
private boolean isRecording() {
|
||||
return ServiceUtils.isRecording(getActivity(),
|
||||
serviceConnection.getServiceIfBound(), getSharedPreferences());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,478 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011 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;
|
||||
|
||||
import static com.google.android.testing.mocking.AndroidMock.anyInt;
|
||||
import static com.google.android.testing.mocking.AndroidMock.capture;
|
||||
import static com.google.android.testing.mocking.AndroidMock.eq;
|
||||
import static com.google.android.testing.mocking.AndroidMock.expect;
|
||||
import static com.google.android.testing.mocking.AndroidMock.isA;
|
||||
import static com.google.android.testing.mocking.AndroidMock.leq;
|
||||
import static com.google.android.testing.mocking.AndroidMock.same;
|
||||
|
||||
import com.google.android.apps.mytracks.TrackDataHub.ListenerDataType;
|
||||
import com.google.android.apps.mytracks.TrackDataListener.ProviderState;
|
||||
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
|
||||
import com.google.android.apps.mytracks.content.Track;
|
||||
import com.google.android.apps.mytracks.content.TracksColumns;
|
||||
import com.google.android.apps.mytracks.content.Waypoint;
|
||||
import com.google.android.apps.mytracks.content.WaypointsColumns;
|
||||
import com.google.android.apps.mytracks.services.TrackRecordingServiceTest.MockContext;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
import com.google.android.testing.mocking.AndroidMock;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||
import android.database.ContentObserver;
|
||||
import android.database.Cursor;
|
||||
import android.database.MatrixCursor;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.provider.BaseColumns;
|
||||
import android.test.AndroidTestCase;
|
||||
import android.test.RenamingDelegatingContext;
|
||||
import android.test.mock.MockContentResolver;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.EnumSet;
|
||||
|
||||
import org.easymock.Capture;
|
||||
import org.easymock.IAnswer;
|
||||
|
||||
/**
|
||||
* Tests for {@link TrackDataHub}.
|
||||
*
|
||||
* @author Rodrigo Damazio
|
||||
*/
|
||||
public class TrackDataHubTest extends AndroidTestCase {
|
||||
|
||||
private static final long TRACK_ID = 42;
|
||||
private MyTracksProviderUtils providerUtils;
|
||||
private TrackDataHub hub;
|
||||
private TrackDataSources dataSources;
|
||||
private SharedPreferences prefs;
|
||||
private TrackDataListener listener1;
|
||||
private TrackDataListener listener2;
|
||||
private Capture<OnSharedPreferenceChangeListener> preferenceListenerCapture =
|
||||
new Capture<SharedPreferences.OnSharedPreferenceChangeListener>();
|
||||
private MockContext context;
|
||||
private float declination;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
MockContentResolver mockContentResolver = new MockContentResolver();
|
||||
RenamingDelegatingContext targetContext = new RenamingDelegatingContext(
|
||||
getContext(), getContext(), "test.");
|
||||
context = new MockContext(mockContentResolver, targetContext);
|
||||
|
||||
prefs = context.getSharedPreferences(Constants.SETTINGS_NAME, 0);
|
||||
providerUtils = AndroidMock.createMock("providerUtils", MyTracksProviderUtils.class);
|
||||
dataSources = AndroidMock.createNiceMock("dataSources", TrackDataSources.class);
|
||||
|
||||
hub = new TrackDataHub(context, dataSources, prefs, providerUtils) {
|
||||
@Override
|
||||
protected void runInListenerThread(Runnable runnable) {
|
||||
// Run everything in the same thread.
|
||||
runnable.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected float getDeclinationFor(Location location, long timestamp) {
|
||||
return declination;
|
||||
}
|
||||
};
|
||||
|
||||
listener1 = AndroidMock.createMock("listener1", TrackDataListener.class);
|
||||
listener2 = AndroidMock.createMock("listener2", TrackDataListener.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
AndroidMock.reset(dataSources);
|
||||
|
||||
// Expect everything to be unregistered.
|
||||
if (preferenceListenerCapture.hasCaptured()) {
|
||||
dataSources.unregisterOnSharedPreferenceChangeListener(preferenceListenerCapture.getValue());
|
||||
}
|
||||
dataSources.removeLocationUpdates(isA(LocationListener.class));
|
||||
dataSources.unregisterSensorListener(isA(SensorEventListener.class));
|
||||
dataSources.unregisterContentObserver(isA(ContentObserver.class));
|
||||
AndroidMock.expectLastCall().times(3);
|
||||
|
||||
AndroidMock.replay(dataSources);
|
||||
|
||||
hub.stop();
|
||||
hub.destroy();
|
||||
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
public void testTrackListen() {
|
||||
Capture<ContentObserver> observerCapture = new Capture<ContentObserver>();
|
||||
Track track = new Track();
|
||||
prefs.edit().putLong("recordingTrack", TRACK_ID)
|
||||
.putLong("selectedTrack", TRACK_ID).commit();
|
||||
expect(providerUtils.getTrack(TRACK_ID)).andStubReturn(track);
|
||||
expectStart();
|
||||
dataSources.registerContentObserver(
|
||||
eq(TracksColumns.CONTENT_URI), eq(false), capture(observerCapture));
|
||||
|
||||
// Expect the initial loading.
|
||||
// Both listeners (registered before and after start) should get the same data.
|
||||
listener1.onTrackUpdated(track);
|
||||
listener2.onTrackUpdated(track);
|
||||
|
||||
AndroidMock.replay(dataSources, providerUtils, listener1, listener2);
|
||||
|
||||
hub.registerTrackDataListener(listener1, EnumSet.of(ListenerDataType.TRACK_UPDATES));
|
||||
hub.start();
|
||||
hub.registerTrackDataListener(listener2, EnumSet.of(ListenerDataType.TRACK_UPDATES));
|
||||
|
||||
AndroidMock.verify(listener1, listener2, dataSources);
|
||||
AndroidMock.reset(listener1, listener2, dataSources);
|
||||
|
||||
ContentObserver observer = observerCapture.getValue();
|
||||
|
||||
// Now expect an update.
|
||||
listener1.onTrackUpdated(track);
|
||||
listener2.onTrackUpdated(track);
|
||||
|
||||
AndroidMock.replay(listener1, listener2, dataSources);
|
||||
|
||||
observer.onChange(false);
|
||||
|
||||
AndroidMock.verify(listener1, listener2, dataSources);
|
||||
AndroidMock.reset(listener1, listener2, dataSources);
|
||||
|
||||
// Unregister one, get another update.
|
||||
listener2.onTrackUpdated(track);
|
||||
|
||||
AndroidMock.replay(listener1, listener2, dataSources);
|
||||
|
||||
hub.unregisterTrackDataListener(listener1);
|
||||
|
||||
observer.onChange(false);
|
||||
|
||||
AndroidMock.verify(listener1, listener2, dataSources);
|
||||
AndroidMock.reset(listener1, listener2, dataSources);
|
||||
|
||||
// Unregister the other, expect internal unregistration
|
||||
dataSources.unregisterContentObserver(observer);
|
||||
|
||||
AndroidMock.replay(listener1, listener2, dataSources);
|
||||
|
||||
hub.unregisterTrackDataListener(listener2);
|
||||
observer.onChange(false);
|
||||
|
||||
AndroidMock.verify(listener1, listener2, dataSources);
|
||||
}
|
||||
|
||||
private static class FixedSizeCursorAnswer implements IAnswer<Cursor> {
|
||||
private final int size;
|
||||
|
||||
public FixedSizeCursorAnswer(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor answer() throws Throwable {
|
||||
MatrixCursor cursor = new MatrixCursor(new String[] { BaseColumns._ID });
|
||||
for (long i = 1; i <= size; i++) {
|
||||
cursor.addRow(new Object[] { i });
|
||||
}
|
||||
return cursor;
|
||||
}
|
||||
}
|
||||
|
||||
public void testWaypointListen() {
|
||||
Capture<ContentObserver> observerCapture = new Capture<ContentObserver>();
|
||||
prefs.edit().putLong("recordingTrack", TRACK_ID)
|
||||
.putLong("selectedTrack", TRACK_ID).commit();
|
||||
|
||||
Waypoint wpt1 = new Waypoint(),
|
||||
wpt2 = new Waypoint(),
|
||||
wpt3 = new Waypoint(),
|
||||
wpt4 = new Waypoint();
|
||||
Location loc = new Location("gps");
|
||||
loc.setLatitude(10.0);
|
||||
loc.setLongitude(8.0);
|
||||
wpt1.setLocation(loc);
|
||||
wpt2.setLocation(loc);
|
||||
wpt3.setLocation(loc);
|
||||
wpt4.setLocation(loc);
|
||||
|
||||
expect(providerUtils.getWaypointsCursor(
|
||||
eq(TRACK_ID), leq(0L), eq(Constants.MAX_DISPLAYED_WAYPOINTS_POINTS)))
|
||||
.andStubAnswer(new FixedSizeCursorAnswer(2));
|
||||
expect(providerUtils.createWaypoint(isA(Cursor.class)))
|
||||
.andReturn(wpt1)
|
||||
.andReturn(wpt2)
|
||||
.andReturn(wpt1)
|
||||
.andReturn(wpt2);
|
||||
|
||||
expectStart();
|
||||
dataSources.registerContentObserver(
|
||||
eq(WaypointsColumns.CONTENT_URI), eq(false), capture(observerCapture));
|
||||
|
||||
// Expect the initial loading.
|
||||
// Both listeners (registered before and after start) should get the same data.
|
||||
listener1.clearWaypoints();
|
||||
listener1.onNewWaypoint(wpt1);
|
||||
listener1.onNewWaypoint(wpt2);
|
||||
listener1.onNewWaypointsDone();
|
||||
listener2.clearWaypoints();
|
||||
listener2.onNewWaypoint(wpt1);
|
||||
listener2.onNewWaypoint(wpt2);
|
||||
listener2.onNewWaypointsDone();
|
||||
|
||||
AndroidMock.replay(dataSources, providerUtils, listener1, listener2);
|
||||
|
||||
hub.registerTrackDataListener(listener1, EnumSet.of(ListenerDataType.WAYPOINT_UPDATES));
|
||||
hub.start();
|
||||
hub.registerTrackDataListener(listener2, EnumSet.of(ListenerDataType.WAYPOINT_UPDATES));
|
||||
|
||||
AndroidMock.verify(listener1, listener2, dataSources, providerUtils);
|
||||
AndroidMock.reset(listener1, listener2, dataSources, providerUtils);
|
||||
|
||||
ContentObserver observer = observerCapture.getValue();
|
||||
|
||||
expect(providerUtils.getWaypointsCursor(
|
||||
eq(TRACK_ID), leq(0L), eq(Constants.MAX_DISPLAYED_WAYPOINTS_POINTS)))
|
||||
.andStubAnswer(new FixedSizeCursorAnswer(3));
|
||||
expect(providerUtils.createWaypoint(isA(Cursor.class)))
|
||||
.andReturn(wpt1)
|
||||
.andReturn(wpt2)
|
||||
.andReturn(wpt3);
|
||||
|
||||
// Now expect an update.
|
||||
listener1.clearWaypoints();
|
||||
listener2.clearWaypoints();
|
||||
listener1.onNewWaypoint(wpt1);
|
||||
listener2.onNewWaypoint(wpt1);
|
||||
listener1.onNewWaypoint(wpt2);
|
||||
listener2.onNewWaypoint(wpt2);
|
||||
listener1.onNewWaypoint(wpt3);
|
||||
listener2.onNewWaypoint(wpt3);
|
||||
listener1.onNewWaypointsDone();
|
||||
listener2.onNewWaypointsDone();
|
||||
|
||||
AndroidMock.replay(listener1, listener2, dataSources, providerUtils);
|
||||
|
||||
observer.onChange(false);
|
||||
|
||||
AndroidMock.verify(listener1, listener2, dataSources, providerUtils);
|
||||
AndroidMock.reset(listener1, listener2, dataSources, providerUtils);
|
||||
|
||||
// Unregister one, get another update.
|
||||
expect(providerUtils.getWaypointsCursor(
|
||||
eq(TRACK_ID), leq(0L), eq(Constants.MAX_DISPLAYED_WAYPOINTS_POINTS)))
|
||||
.andStubAnswer(new FixedSizeCursorAnswer(4));
|
||||
expect(providerUtils.createWaypoint(isA(Cursor.class)))
|
||||
.andReturn(wpt1)
|
||||
.andReturn(wpt2)
|
||||
.andReturn(wpt3)
|
||||
.andReturn(wpt4);
|
||||
|
||||
// Now expect an update.
|
||||
listener2.clearWaypoints();
|
||||
listener2.onNewWaypoint(wpt1);
|
||||
listener2.onNewWaypoint(wpt2);
|
||||
listener2.onNewWaypoint(wpt3);
|
||||
listener2.onNewWaypoint(wpt4);
|
||||
listener2.onNewWaypointsDone();
|
||||
|
||||
AndroidMock.replay(listener1, listener2, dataSources, providerUtils);
|
||||
|
||||
hub.unregisterTrackDataListener(listener1);
|
||||
|
||||
observer.onChange(false);
|
||||
|
||||
AndroidMock.verify(listener1, listener2, dataSources, providerUtils);
|
||||
AndroidMock.reset(listener1, listener2, dataSources, providerUtils);
|
||||
|
||||
// Unregister the other, expect internal unregistration
|
||||
dataSources.unregisterContentObserver(observer);
|
||||
|
||||
AndroidMock.replay(listener1, listener2, dataSources, providerUtils);
|
||||
|
||||
hub.unregisterTrackDataListener(listener2);
|
||||
observer.onChange(false);
|
||||
|
||||
AndroidMock.verify(listener1, listener2, dataSources, providerUtils);
|
||||
}
|
||||
|
||||
public void testPointsListen() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
public void testPointsListen_resample() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
public void testLocationListen() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
public void testCompassListen() throws Exception {
|
||||
Sensor compass = newSensor();
|
||||
expect(dataSources.getSensor(Sensor.TYPE_ORIENTATION)).andReturn(compass);
|
||||
Capture<SensorEventListener> listenerCapture = new Capture<SensorEventListener>();
|
||||
dataSources.registerSensorListener(capture(listenerCapture), same(compass), anyInt());
|
||||
|
||||
Capture<LocationListener> locationListenerCapture = new Capture<LocationListener>();
|
||||
dataSources.requestLocationUpdates(capture(locationListenerCapture));
|
||||
|
||||
SensorEvent event = newSensorEvent();
|
||||
event.sensor = compass;
|
||||
|
||||
// First, get a dummy heading update.
|
||||
listener1.onCurrentHeadingChanged(0.0);
|
||||
|
||||
// Then, get a heading update without a known location (thus can't calculate declination).
|
||||
listener1.onCurrentHeadingChanged(42.0f);
|
||||
|
||||
// Also expect location updates which are not relevant to us.
|
||||
listener1.onProviderStateChange(isA(ProviderState.class));
|
||||
AndroidMock.expectLastCall().anyTimes();
|
||||
|
||||
AndroidMock.replay(dataSources, providerUtils, listener1);
|
||||
|
||||
hub.registerTrackDataListener(listener1,
|
||||
EnumSet.of(ListenerDataType.COMPASS_UPDATES, ListenerDataType.LOCATION_UPDATES));
|
||||
hub.start();
|
||||
|
||||
SensorEventListener sensorListener = listenerCapture.getValue();
|
||||
LocationListener locationListener = locationListenerCapture.getValue();
|
||||
event.values[0] = 42.0f;
|
||||
sensorListener.onSensorChanged(event);
|
||||
|
||||
AndroidMock.verify(dataSources, providerUtils, listener1);
|
||||
AndroidMock.reset(dataSources, providerUtils, listener1);
|
||||
|
||||
// Expect the heading update to include declination.
|
||||
listener1.onCurrentHeadingChanged(52.0);
|
||||
|
||||
// Also expect location updates which are not relevant to us.
|
||||
listener1.onProviderStateChange(isA(ProviderState.class));
|
||||
AndroidMock.expectLastCall().anyTimes();
|
||||
listener1.onCurrentLocationChanged(isA(Location.class));
|
||||
AndroidMock.expectLastCall().anyTimes();
|
||||
|
||||
AndroidMock.replay(dataSources, providerUtils, listener1);
|
||||
|
||||
// Now try injecting a location update, triggering a declination update.
|
||||
Location location = new Location("gps");
|
||||
location.setLatitude(10.0);
|
||||
location.setLongitude(20.0);
|
||||
location.setAltitude(30.0);
|
||||
declination = 10.0f;
|
||||
locationListener.onLocationChanged(location);
|
||||
sensorListener.onSensorChanged(event);
|
||||
|
||||
AndroidMock.verify(dataSources, providerUtils, listener1);
|
||||
AndroidMock.reset(dataSources, providerUtils, listener1);
|
||||
|
||||
listener1.onCurrentHeadingChanged(52.0);
|
||||
|
||||
AndroidMock.replay(dataSources, providerUtils, listener1);
|
||||
|
||||
// Now try changing the known declination - it should still return the old declination, since
|
||||
// updates only happen sparsely.
|
||||
declination = 20.0f;
|
||||
sensorListener.onSensorChanged(event);
|
||||
|
||||
AndroidMock.verify(dataSources, providerUtils, listener1);
|
||||
}
|
||||
|
||||
private Sensor newSensor() throws Exception {
|
||||
Constructor<Sensor> constructor = Sensor.class.getDeclaredConstructor();
|
||||
constructor.setAccessible(true);
|
||||
return constructor.newInstance();
|
||||
}
|
||||
|
||||
private SensorEvent newSensorEvent() throws Exception {
|
||||
Constructor<SensorEvent> constructor = SensorEvent.class.getDeclaredConstructor(int.class);
|
||||
constructor.setAccessible(true);
|
||||
return constructor.newInstance(3);
|
||||
}
|
||||
|
||||
public void testDisplayPreferencesListen() throws Exception {
|
||||
String metricUnitsKey = context.getString(R.string.metric_units_key);
|
||||
String speedKey = context.getString(R.string.report_speed_key);
|
||||
|
||||
prefs.edit()
|
||||
.putBoolean(metricUnitsKey, true)
|
||||
.putBoolean(speedKey, true)
|
||||
.commit();
|
||||
|
||||
Capture<OnSharedPreferenceChangeListener> listenerCapture =
|
||||
new Capture<OnSharedPreferenceChangeListener>();
|
||||
dataSources.registerOnSharedPreferenceChangeListener(capture(listenerCapture));
|
||||
|
||||
expect(listener1.onUnitsChanged(true)).andReturn(false);
|
||||
expect(listener2.onUnitsChanged(true)).andReturn(false);
|
||||
expect(listener1.onReportSpeedChanged(true)).andReturn(false);
|
||||
expect(listener2.onReportSpeedChanged(true)).andReturn(false);
|
||||
|
||||
AndroidMock.replay(dataSources, providerUtils, listener1, listener2);
|
||||
|
||||
hub.registerTrackDataListener(listener1, EnumSet.of(ListenerDataType.DISPLAY_PREFERENCES));
|
||||
hub.start();
|
||||
hub.registerTrackDataListener(listener2, EnumSet.of(ListenerDataType.DISPLAY_PREFERENCES));
|
||||
|
||||
AndroidMock.verify(dataSources, providerUtils, listener1, listener2);
|
||||
AndroidMock.reset(dataSources, providerUtils, listener1, listener2);
|
||||
|
||||
expect(listener1.onReportSpeedChanged(false)).andReturn(false);
|
||||
expect(listener2.onReportSpeedChanged(false)).andReturn(false);
|
||||
|
||||
AndroidMock.replay(dataSources, providerUtils, listener1, listener2);
|
||||
|
||||
prefs.edit()
|
||||
.putBoolean(speedKey, false)
|
||||
.commit();
|
||||
OnSharedPreferenceChangeListener listener = listenerCapture.getValue();
|
||||
listener.onSharedPreferenceChanged(prefs, speedKey);
|
||||
|
||||
AndroidMock.verify(dataSources, providerUtils, listener1, listener2);
|
||||
AndroidMock.reset(dataSources, providerUtils, listener1, listener2);
|
||||
|
||||
expect(listener1.onUnitsChanged(false)).andReturn(false);
|
||||
expect(listener2.onUnitsChanged(false)).andReturn(false);
|
||||
|
||||
AndroidMock.replay(dataSources, providerUtils, listener1, listener2);
|
||||
|
||||
prefs.edit()
|
||||
.putBoolean(metricUnitsKey, false)
|
||||
.commit();
|
||||
listener.onSharedPreferenceChanged(prefs, metricUnitsKey);
|
||||
|
||||
AndroidMock.verify(dataSources, providerUtils, listener1, listener2);
|
||||
}
|
||||
|
||||
public void testFullListener() {
|
||||
// TODO: test loading a track, getting updates, loading another, unloading
|
||||
}
|
||||
|
||||
private void expectStart() {
|
||||
dataSources.registerOnSharedPreferenceChangeListener(capture(preferenceListenerCapture));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,875 @@
|
||||
/*
|
||||
* Copyright 2011 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.content;
|
||||
|
||||
import static com.google.android.testing.mocking.AndroidMock.anyInt;
|
||||
import static com.google.android.testing.mocking.AndroidMock.capture;
|
||||
import static com.google.android.testing.mocking.AndroidMock.eq;
|
||||
import static com.google.android.testing.mocking.AndroidMock.expect;
|
||||
import static com.google.android.testing.mocking.AndroidMock.isA;
|
||||
import static com.google.android.testing.mocking.AndroidMock.leq;
|
||||
import static com.google.android.testing.mocking.AndroidMock.same;
|
||||
|
||||
import com.google.android.apps.mytracks.Constants;
|
||||
import com.google.android.apps.mytracks.content.MyTracksProviderUtils.LocationFactory;
|
||||
import com.google.android.apps.mytracks.content.MyTracksProviderUtils.LocationIterator;
|
||||
import com.google.android.apps.mytracks.content.TrackDataHub.ListenerDataType;
|
||||
import com.google.android.apps.mytracks.content.TrackDataListener.ProviderState;
|
||||
import com.google.android.apps.mytracks.services.TrackRecordingServiceTest.MockContext;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
import com.google.android.testing.mocking.AndroidMock;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||
import android.database.ContentObserver;
|
||||
import android.database.Cursor;
|
||||
import android.database.MatrixCursor;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.provider.BaseColumns;
|
||||
import android.test.AndroidTestCase;
|
||||
import android.test.RenamingDelegatingContext;
|
||||
import android.test.mock.MockContentResolver;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.easymock.Capture;
|
||||
import org.easymock.IAnswer;
|
||||
|
||||
/**
|
||||
* Tests for {@link TrackDataHub}.
|
||||
*
|
||||
* @author Rodrigo Damazio
|
||||
*/
|
||||
public class TrackDataHubTest extends AndroidTestCase {
|
||||
|
||||
private static final long TRACK_ID = 42L;
|
||||
private static final int TARGET_POINTS = 50;
|
||||
|
||||
private MyTracksProviderUtils providerUtils;
|
||||
private TrackDataHub hub;
|
||||
private TrackDataListeners listeners;
|
||||
private DataSourcesWrapper dataSources;
|
||||
private SharedPreferences prefs;
|
||||
private TrackDataListener listener1;
|
||||
private TrackDataListener listener2;
|
||||
private Capture<OnSharedPreferenceChangeListener> preferenceListenerCapture =
|
||||
new Capture<SharedPreferences.OnSharedPreferenceChangeListener>();
|
||||
private MockContext context;
|
||||
private float declination;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
MockContentResolver mockContentResolver = new MockContentResolver();
|
||||
RenamingDelegatingContext targetContext = new RenamingDelegatingContext(
|
||||
getContext(), getContext(), "test.");
|
||||
context = new MockContext(mockContentResolver, targetContext);
|
||||
|
||||
prefs = context.getSharedPreferences(Constants.SETTINGS_NAME, 0);
|
||||
providerUtils = AndroidMock.createMock("providerUtils", MyTracksProviderUtils.class);
|
||||
dataSources = AndroidMock.createNiceMock("dataSources", DataSourcesWrapper.class);
|
||||
|
||||
listeners = new TrackDataListeners();
|
||||
hub = new TrackDataHub(context, listeners, prefs, providerUtils, TARGET_POINTS) {
|
||||
@Override
|
||||
protected DataSourcesWrapper newDataSources() {
|
||||
return dataSources;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runInListenerThread(Runnable runnable) {
|
||||
// Run everything in the same thread.
|
||||
runnable.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected float getDeclinationFor(Location location, long timestamp) {
|
||||
return declination;
|
||||
}
|
||||
};
|
||||
|
||||
listener1 = AndroidMock.createStrictMock("listener1", TrackDataListener.class);
|
||||
listener2 = AndroidMock.createStrictMock("listener2", TrackDataListener.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
AndroidMock.reset(dataSources);
|
||||
|
||||
// Expect everything to be unregistered.
|
||||
if (preferenceListenerCapture.hasCaptured()) {
|
||||
dataSources.unregisterOnSharedPreferenceChangeListener(preferenceListenerCapture.getValue());
|
||||
}
|
||||
dataSources.removeLocationUpdates(isA(LocationListener.class));
|
||||
dataSources.unregisterSensorListener(isA(SensorEventListener.class));
|
||||
dataSources.unregisterContentObserver(isA(ContentObserver.class));
|
||||
AndroidMock.expectLastCall().times(3);
|
||||
|
||||
AndroidMock.replay(dataSources);
|
||||
|
||||
hub.stop();
|
||||
hub = null;
|
||||
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
public void testTrackListen() {
|
||||
Capture<ContentObserver> observerCapture = new Capture<ContentObserver>();
|
||||
Track track = new Track();
|
||||
prefs.edit().putLong("recordingTrack", TRACK_ID)
|
||||
.putLong("selectedTrack", TRACK_ID).commit();
|
||||
expect(providerUtils.getTrack(TRACK_ID)).andStubReturn(track);
|
||||
expectStart();
|
||||
dataSources.registerContentObserver(
|
||||
eq(TracksColumns.CONTENT_URI), eq(false), capture(observerCapture));
|
||||
|
||||
// Expect the initial loading.
|
||||
// Both listeners (registered before and after start) should get the same data.
|
||||
listener1.onTrackUpdated(track);
|
||||
listener2.onTrackUpdated(track);
|
||||
|
||||
replay();
|
||||
|
||||
hub.registerTrackDataListener(listener1, EnumSet.of(ListenerDataType.TRACK_UPDATES));
|
||||
hub.start();
|
||||
hub.registerTrackDataListener(listener2, EnumSet.of(ListenerDataType.TRACK_UPDATES));
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
ContentObserver observer = observerCapture.getValue();
|
||||
expect(providerUtils.getTrack(TRACK_ID)).andStubReturn(track);
|
||||
|
||||
// Now expect an update.
|
||||
listener1.onTrackUpdated(track);
|
||||
listener2.onTrackUpdated(track);
|
||||
|
||||
replay();
|
||||
|
||||
observer.onChange(false);
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
// Unregister one, get another update.
|
||||
expect(providerUtils.getTrack(TRACK_ID)).andStubReturn(track);
|
||||
listener2.onTrackUpdated(track);
|
||||
|
||||
replay();
|
||||
|
||||
hub.unregisterTrackDataListener(listener1);
|
||||
|
||||
observer.onChange(false);
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
// Unregister the other, expect internal unregistration
|
||||
dataSources.unregisterContentObserver(observer);
|
||||
|
||||
replay();
|
||||
|
||||
hub.unregisterTrackDataListener(listener2);
|
||||
observer.onChange(false);
|
||||
|
||||
verifyAndReset();
|
||||
}
|
||||
|
||||
private static class FixedSizeCursorAnswer implements IAnswer<Cursor> {
|
||||
private final int size;
|
||||
|
||||
public FixedSizeCursorAnswer(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor answer() throws Throwable {
|
||||
MatrixCursor cursor = new MatrixCursor(new String[] { BaseColumns._ID });
|
||||
for (long i = 1; i <= size; i++) {
|
||||
cursor.addRow(new Object[] { i });
|
||||
}
|
||||
return cursor;
|
||||
}
|
||||
}
|
||||
|
||||
private static class FixedSizeLocationIterator implements LocationIterator {
|
||||
private final long startId;
|
||||
private final Location[] locs;
|
||||
private final Set<Integer> splitIndexSet = new HashSet<Integer>();
|
||||
private int currentIdx = -1;
|
||||
|
||||
public FixedSizeLocationIterator(long startId, int size) {
|
||||
this(startId, size, null);
|
||||
}
|
||||
|
||||
public FixedSizeLocationIterator(long startId, int size, int... splitIndices) {
|
||||
this.startId = startId;
|
||||
this.locs = new Location[size];
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
Location loc = new Location("gps");
|
||||
loc.setLatitude(-15.0 + i / 1000.0);
|
||||
loc.setLongitude(37 + i / 1000.0);
|
||||
loc.setAltitude(i);
|
||||
|
||||
locs[i] = loc;
|
||||
}
|
||||
|
||||
if (splitIndices != null) {
|
||||
for (int splitIdx : splitIndices) {
|
||||
splitIndexSet.add(splitIdx);
|
||||
|
||||
Location splitLoc = locs[splitIdx];
|
||||
splitLoc.setLatitude(100.0);
|
||||
splitLoc.setLongitude(200.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void expectLocationsDelivered(TrackDataListener listener) {
|
||||
for (int i = 0; i < locs.length; i++) {
|
||||
if (splitIndexSet.contains(i)) {
|
||||
listener.onSegmentSplit();
|
||||
} else {
|
||||
listener.onNewTrackPoint(locs[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void expectSampledLocationsDelivered(
|
||||
TrackDataListener listener, int sampleFrequency, boolean includeSampledOut) {
|
||||
for (int i = 0; i < locs.length; i++) {
|
||||
if (splitIndexSet.contains(i)) {
|
||||
listener.onSegmentSplit();
|
||||
} else if (i % sampleFrequency == 0) {
|
||||
listener.onNewTrackPoint(locs[i]);
|
||||
} else if (includeSampledOut) {
|
||||
listener.onSampledOutTrackPoint(locs[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return currentIdx < (locs.length - 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location next() {
|
||||
currentIdx++;
|
||||
return locs[currentIdx];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLocationId() {
|
||||
return startId + currentIdx;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
|
||||
public void testWaypointListen() {
|
||||
Capture<ContentObserver> observerCapture = new Capture<ContentObserver>();
|
||||
prefs.edit().putLong("recordingTrack", TRACK_ID)
|
||||
.putLong("selectedTrack", TRACK_ID).commit();
|
||||
|
||||
Waypoint wpt1 = new Waypoint(),
|
||||
wpt2 = new Waypoint(),
|
||||
wpt3 = new Waypoint(),
|
||||
wpt4 = new Waypoint();
|
||||
Location loc = new Location("gps");
|
||||
loc.setLatitude(10.0);
|
||||
loc.setLongitude(8.0);
|
||||
wpt1.setLocation(loc);
|
||||
wpt2.setLocation(loc);
|
||||
wpt3.setLocation(loc);
|
||||
wpt4.setLocation(loc);
|
||||
|
||||
expect(providerUtils.getWaypointsCursor(
|
||||
eq(TRACK_ID), leq(0L), eq(Constants.MAX_DISPLAYED_WAYPOINTS_POINTS)))
|
||||
.andStubAnswer(new FixedSizeCursorAnswer(2));
|
||||
expect(providerUtils.createWaypoint(isA(Cursor.class)))
|
||||
.andReturn(wpt1)
|
||||
.andReturn(wpt2)
|
||||
.andReturn(wpt1)
|
||||
.andReturn(wpt2);
|
||||
|
||||
expectStart();
|
||||
dataSources.registerContentObserver(
|
||||
eq(WaypointsColumns.CONTENT_URI), eq(false), capture(observerCapture));
|
||||
|
||||
// Expect the initial loading.
|
||||
// Both listeners (registered before and after start) should get the same data.
|
||||
listener1.clearWaypoints();
|
||||
listener1.onNewWaypoint(wpt1);
|
||||
listener1.onNewWaypoint(wpt2);
|
||||
listener1.onNewWaypointsDone();
|
||||
listener2.clearWaypoints();
|
||||
listener2.onNewWaypoint(wpt1);
|
||||
listener2.onNewWaypoint(wpt2);
|
||||
listener2.onNewWaypointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
hub.registerTrackDataListener(listener1, EnumSet.of(ListenerDataType.WAYPOINT_UPDATES));
|
||||
hub.start();
|
||||
hub.registerTrackDataListener(listener2, EnumSet.of(ListenerDataType.WAYPOINT_UPDATES));
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
ContentObserver observer = observerCapture.getValue();
|
||||
|
||||
expect(providerUtils.getWaypointsCursor(
|
||||
eq(TRACK_ID), leq(0L), eq(Constants.MAX_DISPLAYED_WAYPOINTS_POINTS)))
|
||||
.andStubAnswer(new FixedSizeCursorAnswer(3));
|
||||
expect(providerUtils.createWaypoint(isA(Cursor.class)))
|
||||
.andReturn(wpt1)
|
||||
.andReturn(wpt2)
|
||||
.andReturn(wpt3);
|
||||
|
||||
// Now expect an update.
|
||||
listener1.clearWaypoints();
|
||||
listener2.clearWaypoints();
|
||||
listener1.onNewWaypoint(wpt1);
|
||||
listener2.onNewWaypoint(wpt1);
|
||||
listener1.onNewWaypoint(wpt2);
|
||||
listener2.onNewWaypoint(wpt2);
|
||||
listener1.onNewWaypoint(wpt3);
|
||||
listener2.onNewWaypoint(wpt3);
|
||||
listener1.onNewWaypointsDone();
|
||||
listener2.onNewWaypointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
observer.onChange(false);
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
// Unregister one, get another update.
|
||||
expect(providerUtils.getWaypointsCursor(
|
||||
eq(TRACK_ID), leq(0L), eq(Constants.MAX_DISPLAYED_WAYPOINTS_POINTS)))
|
||||
.andStubAnswer(new FixedSizeCursorAnswer(4));
|
||||
expect(providerUtils.createWaypoint(isA(Cursor.class)))
|
||||
.andReturn(wpt1)
|
||||
.andReturn(wpt2)
|
||||
.andReturn(wpt3)
|
||||
.andReturn(wpt4);
|
||||
|
||||
// Now expect an update.
|
||||
listener2.clearWaypoints();
|
||||
listener2.onNewWaypoint(wpt1);
|
||||
listener2.onNewWaypoint(wpt2);
|
||||
listener2.onNewWaypoint(wpt3);
|
||||
listener2.onNewWaypoint(wpt4);
|
||||
listener2.onNewWaypointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
hub.unregisterTrackDataListener(listener1);
|
||||
|
||||
observer.onChange(false);
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
// Unregister the other, expect internal unregistration
|
||||
dataSources.unregisterContentObserver(observer);
|
||||
|
||||
replay();
|
||||
|
||||
hub.unregisterTrackDataListener(listener2);
|
||||
observer.onChange(false);
|
||||
|
||||
verifyAndReset();
|
||||
}
|
||||
|
||||
public void testPointsListen() {
|
||||
Capture<ContentObserver> observerCapture = new Capture<ContentObserver>();
|
||||
prefs.edit().putLong("recordingTrack", TRACK_ID)
|
||||
.putLong("selectedTrack", TRACK_ID).commit();
|
||||
|
||||
expectStart();
|
||||
dataSources.registerContentObserver(
|
||||
eq(TrackPointsColumns.CONTENT_URI), eq(false), capture(observerCapture));
|
||||
|
||||
FixedSizeLocationIterator locationIterator = new FixedSizeLocationIterator(1, 10, 5);
|
||||
expect(providerUtils.getLocationIterator(
|
||||
eq(TRACK_ID), eq(0L), eq(false), isA(LocationFactory.class)))
|
||||
.andReturn(locationIterator);
|
||||
expect(providerUtils.getLastLocationId(TRACK_ID)).andReturn(10L);
|
||||
|
||||
listener1.clearTrackPoints();
|
||||
locationIterator.expectLocationsDelivered(listener1);
|
||||
listener1.onNewTrackPointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
hub.start();
|
||||
hub.registerTrackDataListener(listener1, EnumSet.of(ListenerDataType.POINT_UPDATES));
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
// Register a second listener - it will get the same points as the previous one
|
||||
locationIterator = new FixedSizeLocationIterator(1, 10, 5);
|
||||
expect(providerUtils.getLocationIterator(
|
||||
eq(TRACK_ID), eq(0L), eq(false), isA(LocationFactory.class)))
|
||||
.andReturn(locationIterator);
|
||||
expect(providerUtils.getLastLocationId(TRACK_ID)).andReturn(10L);
|
||||
|
||||
listener2.clearTrackPoints();
|
||||
locationIterator.expectLocationsDelivered(listener2);
|
||||
listener2.onNewTrackPointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
hub.start();
|
||||
hub.registerTrackDataListener(listener2, EnumSet.of(ListenerDataType.POINT_UPDATES));
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
// Deliver more points - should go to both listeners, without clearing.
|
||||
ContentObserver observer = observerCapture.getValue();
|
||||
|
||||
locationIterator = new FixedSizeLocationIterator(11, 10, 1);
|
||||
expect(providerUtils.getLocationIterator(
|
||||
eq(TRACK_ID), eq(11L), eq(false), isA(LocationFactory.class)))
|
||||
.andReturn(locationIterator);
|
||||
expect(providerUtils.getLastLocationId(TRACK_ID)).andReturn(20L);
|
||||
|
||||
locationIterator.expectLocationsDelivered(listener1);
|
||||
locationIterator.expectLocationsDelivered(listener2);
|
||||
listener1.onNewTrackPointsDone();
|
||||
listener2.onNewTrackPointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
observer.onChange(false);
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
// Unregister listener1, switch tracks to ensure data is cleared/reloaded.
|
||||
locationIterator = new FixedSizeLocationIterator(101, 10);
|
||||
expect(providerUtils.getLocationIterator(
|
||||
eq(TRACK_ID + 1), eq(0L), eq(false), isA(LocationFactory.class)))
|
||||
.andReturn(locationIterator);
|
||||
expect(providerUtils.getLastLocationId(TRACK_ID + 1)).andReturn(110L);
|
||||
|
||||
listener2.clearTrackPoints();
|
||||
locationIterator.expectLocationsDelivered(listener2);
|
||||
listener2.onNewTrackPointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
hub.unregisterTrackDataListener(listener1);
|
||||
hub.loadTrack(TRACK_ID + 1);
|
||||
|
||||
verifyAndReset();
|
||||
}
|
||||
|
||||
public void testPointsListen_beforeStart() {
|
||||
|
||||
}
|
||||
|
||||
public void testPointsListen_reRegister() {
|
||||
Capture<ContentObserver> observerCapture = new Capture<ContentObserver>();
|
||||
prefs.edit().putLong("recordingTrack", TRACK_ID)
|
||||
.putLong("selectedTrack", TRACK_ID).commit();
|
||||
|
||||
expectStart();
|
||||
dataSources.registerContentObserver(
|
||||
eq(TrackPointsColumns.CONTENT_URI), eq(false), capture(observerCapture));
|
||||
|
||||
FixedSizeLocationIterator locationIterator = new FixedSizeLocationIterator(1, 10, 5);
|
||||
expect(providerUtils.getLocationIterator(
|
||||
eq(TRACK_ID), eq(0L), eq(false), isA(LocationFactory.class)))
|
||||
.andReturn(locationIterator);
|
||||
expect(providerUtils.getLastLocationId(TRACK_ID)).andReturn(10L);
|
||||
|
||||
listener1.clearTrackPoints();
|
||||
locationIterator.expectLocationsDelivered(listener1);
|
||||
listener1.onNewTrackPointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
hub.start();
|
||||
hub.registerTrackDataListener(listener1, EnumSet.of(ListenerDataType.POINT_UPDATES));
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
// Unregister
|
||||
ContentObserver observer = observerCapture.getValue();
|
||||
dataSources.unregisterContentObserver(observer);
|
||||
|
||||
replay();
|
||||
|
||||
hub.unregisterTrackDataListener(listener1);
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
// Register again, except only points since unregistered.
|
||||
dataSources.registerContentObserver(
|
||||
eq(TrackPointsColumns.CONTENT_URI), eq(false), capture(observerCapture));
|
||||
|
||||
locationIterator = new FixedSizeLocationIterator(11, 10);
|
||||
expect(providerUtils.getLocationIterator(
|
||||
eq(TRACK_ID), eq(11L), eq(false), isA(LocationFactory.class)))
|
||||
.andReturn(locationIterator);
|
||||
expect(providerUtils.getLastLocationId(TRACK_ID)).andReturn(20L);
|
||||
|
||||
locationIterator.expectLocationsDelivered(listener1);
|
||||
listener1.onNewTrackPointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
hub.registerTrackDataListener(listener1, EnumSet.of(ListenerDataType.POINT_UPDATES));
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
// Deliver more points - should still be incremental.
|
||||
locationIterator = new FixedSizeLocationIterator(21, 10, 1);
|
||||
expect(providerUtils.getLocationIterator(
|
||||
eq(TRACK_ID), eq(21L), eq(false), isA(LocationFactory.class)))
|
||||
.andReturn(locationIterator);
|
||||
expect(providerUtils.getLastLocationId(TRACK_ID)).andReturn(30L);
|
||||
|
||||
locationIterator.expectLocationsDelivered(listener1);
|
||||
listener1.onNewTrackPointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
observer.onChange(false);
|
||||
|
||||
verifyAndReset();
|
||||
}
|
||||
|
||||
public void testPointsListen_reRegisterTrackChanged() {
|
||||
Capture<ContentObserver> observerCapture = new Capture<ContentObserver>();
|
||||
prefs.edit().putLong("recordingTrack", TRACK_ID)
|
||||
.putLong("selectedTrack", TRACK_ID).commit();
|
||||
|
||||
expectStart();
|
||||
dataSources.registerContentObserver(
|
||||
eq(TrackPointsColumns.CONTENT_URI), eq(false), capture(observerCapture));
|
||||
|
||||
FixedSizeLocationIterator locationIterator = new FixedSizeLocationIterator(1, 10, 5);
|
||||
expect(providerUtils.getLocationIterator(
|
||||
eq(TRACK_ID), eq(0L), eq(false), isA(LocationFactory.class)))
|
||||
.andReturn(locationIterator);
|
||||
expect(providerUtils.getLastLocationId(TRACK_ID)).andReturn(10L);
|
||||
|
||||
listener1.clearTrackPoints();
|
||||
locationIterator.expectLocationsDelivered(listener1);
|
||||
listener1.onNewTrackPointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
hub.start();
|
||||
hub.registerTrackDataListener(listener1, EnumSet.of(ListenerDataType.POINT_UPDATES));
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
// Unregister
|
||||
ContentObserver observer = observerCapture.getValue();
|
||||
dataSources.unregisterContentObserver(observer);
|
||||
|
||||
replay();
|
||||
|
||||
hub.unregisterTrackDataListener(listener1);
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
// Register again after track changed, expect all points.
|
||||
dataSources.registerContentObserver(
|
||||
eq(TrackPointsColumns.CONTENT_URI), eq(false), capture(observerCapture));
|
||||
|
||||
locationIterator = new FixedSizeLocationIterator(1, 10);
|
||||
expect(providerUtils.getLocationIterator(
|
||||
eq(TRACK_ID + 1), eq(0L), eq(false), isA(LocationFactory.class)))
|
||||
.andReturn(locationIterator);
|
||||
expect(providerUtils.getLastLocationId(TRACK_ID + 1)).andReturn(10L);
|
||||
|
||||
listener1.clearTrackPoints();
|
||||
locationIterator.expectLocationsDelivered(listener1);
|
||||
listener1.onNewTrackPointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
hub.loadTrack(TRACK_ID + 1);
|
||||
hub.registerTrackDataListener(listener1, EnumSet.of(ListenerDataType.POINT_UPDATES));
|
||||
|
||||
verifyAndReset();
|
||||
}
|
||||
|
||||
public void testPointsListen_largeTrackSampling() {
|
||||
Capture<ContentObserver> observerCapture = new Capture<ContentObserver>();
|
||||
prefs.edit().putLong("recordingTrack", TRACK_ID)
|
||||
.putLong("selectedTrack", TRACK_ID).commit();
|
||||
|
||||
expectStart();
|
||||
dataSources.registerContentObserver(
|
||||
eq(TrackPointsColumns.CONTENT_URI), eq(false), capture(observerCapture));
|
||||
|
||||
FixedSizeLocationIterator locationIterator = new FixedSizeLocationIterator(1, 200, 4, 25, 71, 120);
|
||||
expect(providerUtils.getLocationIterator(
|
||||
eq(TRACK_ID), eq(0L), eq(false), isA(LocationFactory.class)))
|
||||
.andReturn(locationIterator);
|
||||
expect(providerUtils.getLastLocationId(TRACK_ID)).andReturn(200L);
|
||||
|
||||
listener1.clearTrackPoints();
|
||||
listener2.clearTrackPoints();
|
||||
locationIterator.expectSampledLocationsDelivered(listener1, 4, false);
|
||||
locationIterator.expectSampledLocationsDelivered(listener2, 4, true);
|
||||
listener1.onNewTrackPointsDone();
|
||||
listener2.onNewTrackPointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
hub.registerTrackDataListener(listener1,
|
||||
EnumSet.of(ListenerDataType.POINT_UPDATES));
|
||||
hub.registerTrackDataListener(listener2,
|
||||
EnumSet.of(ListenerDataType.POINT_UPDATES, ListenerDataType.SAMPLED_OUT_POINT_UPDATES));
|
||||
hub.start();
|
||||
|
||||
verifyAndReset();
|
||||
}
|
||||
|
||||
public void testPointsListen_resampling() {
|
||||
Capture<ContentObserver> observerCapture = new Capture<ContentObserver>();
|
||||
prefs.edit().putLong("recordingTrack", TRACK_ID)
|
||||
.putLong("selectedTrack", TRACK_ID).commit();
|
||||
|
||||
expectStart();
|
||||
dataSources.registerContentObserver(
|
||||
eq(TrackPointsColumns.CONTENT_URI), eq(false), capture(observerCapture));
|
||||
|
||||
// Deliver 30 points (no sampling happens)
|
||||
FixedSizeLocationIterator locationIterator = new FixedSizeLocationIterator(1, 30, 5);
|
||||
expect(providerUtils.getLocationIterator(
|
||||
eq(TRACK_ID), eq(0L), eq(false), isA(LocationFactory.class)))
|
||||
.andReturn(locationIterator);
|
||||
expect(providerUtils.getLastLocationId(TRACK_ID)).andReturn(30L);
|
||||
|
||||
listener1.clearTrackPoints();
|
||||
locationIterator.expectLocationsDelivered(listener1);
|
||||
listener1.onNewTrackPointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
hub.start();
|
||||
hub.registerTrackDataListener(listener1, EnumSet.of(ListenerDataType.POINT_UPDATES));
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
// Now deliver 30 more (incrementally sampled)
|
||||
ContentObserver observer = observerCapture.getValue();
|
||||
locationIterator = new FixedSizeLocationIterator(31, 30);
|
||||
expect(providerUtils.getLocationIterator(
|
||||
eq(TRACK_ID), eq(31L), eq(false), isA(LocationFactory.class)))
|
||||
.andReturn(locationIterator);
|
||||
expect(providerUtils.getLastLocationId(TRACK_ID)).andReturn(60L);
|
||||
|
||||
locationIterator.expectSampledLocationsDelivered(listener1, 2, false);
|
||||
listener1.onNewTrackPointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
observer.onChange(false);
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
// Now another 30 (triggers resampling)
|
||||
locationIterator = new FixedSizeLocationIterator(1, 90);
|
||||
expect(providerUtils.getLocationIterator(
|
||||
eq(TRACK_ID), eq(0L), eq(false), isA(LocationFactory.class)))
|
||||
.andReturn(locationIterator);
|
||||
expect(providerUtils.getLastLocationId(TRACK_ID)).andReturn(90L);
|
||||
|
||||
listener1.clearTrackPoints();
|
||||
locationIterator.expectSampledLocationsDelivered(listener1, 2, false);
|
||||
listener1.onNewTrackPointsDone();
|
||||
|
||||
replay();
|
||||
|
||||
observer.onChange(false);
|
||||
|
||||
verifyAndReset();
|
||||
}
|
||||
|
||||
public void testLocationListen() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
public void testCompassListen() throws Exception {
|
||||
AndroidMock.resetToDefault(listener1);
|
||||
|
||||
Sensor compass = newSensor();
|
||||
expect(dataSources.getSensor(Sensor.TYPE_ORIENTATION)).andReturn(compass);
|
||||
Capture<SensorEventListener> listenerCapture = new Capture<SensorEventListener>();
|
||||
dataSources.registerSensorListener(capture(listenerCapture), same(compass), anyInt());
|
||||
|
||||
Capture<LocationListener> locationListenerCapture = new Capture<LocationListener>();
|
||||
dataSources.requestLocationUpdates(capture(locationListenerCapture));
|
||||
|
||||
SensorEvent event = newSensorEvent();
|
||||
event.sensor = compass;
|
||||
|
||||
// First, get a dummy heading update.
|
||||
listener1.onCurrentHeadingChanged(0.0);
|
||||
|
||||
// Then, get a heading update without a known location (thus can't calculate declination).
|
||||
listener1.onCurrentHeadingChanged(42.0f);
|
||||
|
||||
// Also expect location updates which are not relevant to us.
|
||||
listener1.onProviderStateChange(isA(ProviderState.class));
|
||||
AndroidMock.expectLastCall().anyTimes();
|
||||
|
||||
replay();
|
||||
|
||||
hub.registerTrackDataListener(listener1,
|
||||
EnumSet.of(ListenerDataType.COMPASS_UPDATES, ListenerDataType.LOCATION_UPDATES));
|
||||
hub.start();
|
||||
|
||||
SensorEventListener sensorListener = listenerCapture.getValue();
|
||||
LocationListener locationListener = locationListenerCapture.getValue();
|
||||
event.values[0] = 42.0f;
|
||||
sensorListener.onSensorChanged(event);
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
// Expect the heading update to include declination.
|
||||
listener1.onCurrentHeadingChanged(52.0);
|
||||
|
||||
// Also expect location updates which are not relevant to us.
|
||||
listener1.onProviderStateChange(isA(ProviderState.class));
|
||||
AndroidMock.expectLastCall().anyTimes();
|
||||
listener1.onCurrentLocationChanged(isA(Location.class));
|
||||
AndroidMock.expectLastCall().anyTimes();
|
||||
|
||||
replay();
|
||||
|
||||
// Now try injecting a location update, triggering a declination update.
|
||||
Location location = new Location("gps");
|
||||
location.setLatitude(10.0);
|
||||
location.setLongitude(20.0);
|
||||
location.setAltitude(30.0);
|
||||
declination = 10.0f;
|
||||
locationListener.onLocationChanged(location);
|
||||
sensorListener.onSensorChanged(event);
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
listener1.onCurrentHeadingChanged(52.0);
|
||||
|
||||
replay();
|
||||
|
||||
// Now try changing the known declination - it should still return the old declination, since
|
||||
// updates only happen sparsely.
|
||||
declination = 20.0f;
|
||||
sensorListener.onSensorChanged(event);
|
||||
|
||||
verifyAndReset();
|
||||
}
|
||||
|
||||
private Sensor newSensor() throws Exception {
|
||||
Constructor<Sensor> constructor = Sensor.class.getDeclaredConstructor();
|
||||
constructor.setAccessible(true);
|
||||
return constructor.newInstance();
|
||||
}
|
||||
|
||||
private SensorEvent newSensorEvent() throws Exception {
|
||||
Constructor<SensorEvent> constructor = SensorEvent.class.getDeclaredConstructor(int.class);
|
||||
constructor.setAccessible(true);
|
||||
return constructor.newInstance(3);
|
||||
}
|
||||
|
||||
public void testDisplayPreferencesListen() throws Exception {
|
||||
String metricUnitsKey = context.getString(R.string.metric_units_key);
|
||||
String speedKey = context.getString(R.string.report_speed_key);
|
||||
|
||||
prefs.edit()
|
||||
.putBoolean(metricUnitsKey, true)
|
||||
.putBoolean(speedKey, true)
|
||||
.commit();
|
||||
|
||||
Capture<OnSharedPreferenceChangeListener> listenerCapture =
|
||||
new Capture<OnSharedPreferenceChangeListener>();
|
||||
dataSources.registerOnSharedPreferenceChangeListener(capture(listenerCapture));
|
||||
|
||||
expect(listener1.onUnitsChanged(true)).andReturn(false);
|
||||
expect(listener2.onUnitsChanged(true)).andReturn(false);
|
||||
expect(listener1.onReportSpeedChanged(true)).andReturn(false);
|
||||
expect(listener2.onReportSpeedChanged(true)).andReturn(false);
|
||||
|
||||
replay();
|
||||
|
||||
hub.registerTrackDataListener(listener1, EnumSet.of(ListenerDataType.DISPLAY_PREFERENCES));
|
||||
hub.start();
|
||||
hub.registerTrackDataListener(listener2, EnumSet.of(ListenerDataType.DISPLAY_PREFERENCES));
|
||||
|
||||
verifyAndReset();
|
||||
|
||||
expect(listener1.onReportSpeedChanged(false)).andReturn(false);
|
||||
expect(listener2.onReportSpeedChanged(false)).andReturn(false);
|
||||
|
||||
replay();
|
||||
|
||||
prefs.edit()
|
||||
.putBoolean(speedKey, false)
|
||||
.commit();
|
||||
OnSharedPreferenceChangeListener listener = listenerCapture.getValue();
|
||||
listener.onSharedPreferenceChanged(prefs, speedKey);
|
||||
|
||||
AndroidMock.verify(dataSources, providerUtils, listener1, listener2);
|
||||
AndroidMock.reset(dataSources, providerUtils, listener1, listener2);
|
||||
|
||||
expect(listener1.onUnitsChanged(false)).andReturn(false);
|
||||
expect(listener2.onUnitsChanged(false)).andReturn(false);
|
||||
|
||||
replay();
|
||||
|
||||
prefs.edit()
|
||||
.putBoolean(metricUnitsKey, false)
|
||||
.commit();
|
||||
listener.onSharedPreferenceChanged(prefs, metricUnitsKey);
|
||||
|
||||
verifyAndReset();
|
||||
}
|
||||
|
||||
private void expectStart() {
|
||||
dataSources.registerOnSharedPreferenceChangeListener(capture(preferenceListenerCapture));
|
||||
}
|
||||
|
||||
private void replay() {
|
||||
AndroidMock.replay(dataSources, providerUtils, listener1, listener2);
|
||||
}
|
||||
|
||||
private void verifyAndReset() {
|
||||
AndroidMock.verify(listener1, listener2, dataSources, providerUtils);
|
||||
AndroidMock.reset(listener1, listener2, dataSources, providerUtils);
|
||||
}
|
||||
}
|
||||
@@ -110,9 +110,6 @@ public class DatabaseDumperTest extends TestCase {
|
||||
// Verify the number of rows
|
||||
assertEquals(FAKE_DATA.length, reader.readInt());
|
||||
|
||||
// Verify the row contents
|
||||
byte[] blob = new byte[4];
|
||||
|
||||
// Row 0 -- everything populated
|
||||
assertEquals(EXPECTED_FIELD_SETS[0], reader.readLong());
|
||||
assertEquals(42, reader.readInt());
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package com.google.android.apps.mytracks.io.docs;
|
||||
|
||||
import com.google.android.apps.mytracks.io.gdata.GDataWrapper;
|
||||
import com.google.wireless.gdata.client.GDataServiceClient;
|
||||
|
||||
import android.content.Context;
|
||||
import android.test.mock.MockContext;
|
||||
@@ -43,7 +44,7 @@ public class DocsHelperTest extends TestCase {
|
||||
// touch AndroidMock at all to get this failure.
|
||||
// The bug is filed with Android Mock as
|
||||
// http://code.google.com/p/android-mock/issues/detail?id=3
|
||||
private class MockGDataWrapper extends GDataWrapper {
|
||||
private class MockGDataWrapper extends GDataWrapper<GDataServiceClient> {
|
||||
private final boolean returnValue;
|
||||
|
||||
MockGDataWrapper(boolean returnValue) {
|
||||
@@ -51,7 +52,7 @@ public class DocsHelperTest extends TestCase {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean runQuery(QueryFunction queryFunction) {
|
||||
public boolean runQuery(QueryFunction<GDataServiceClient> queryFunction) {
|
||||
return returnValue;
|
||||
}
|
||||
}
|
||||
|
||||
+6
-9
@@ -13,10 +13,7 @@
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
package com.google.android.apps.mytracks;
|
||||
|
||||
import com.google.android.apps.mytracks.io.file.MockTrackWriter;
|
||||
import com.google.android.apps.mytracks.io.file.TrackWriter;
|
||||
package com.google.android.apps.mytracks.io.file;
|
||||
|
||||
import android.app.ProgressDialog;
|
||||
import android.test.ActivityInstrumentationTestCase2;
|
||||
@@ -29,9 +26,9 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
*
|
||||
* @author Matthew Simmons
|
||||
*/
|
||||
public class WriteProgressControllerTest extends ActivityInstrumentationTestCase2<MyTracks> {
|
||||
public class WriteProgressControllerTest extends ActivityInstrumentationTestCase2<SaveActivity> {
|
||||
public WriteProgressControllerTest() {
|
||||
super(MyTracks.class);
|
||||
super(SaveActivity.class);
|
||||
}
|
||||
|
||||
private static void assertProgress(ProgressDialog dialog, int expectedProgress,
|
||||
@@ -57,15 +54,15 @@ public class WriteProgressControllerTest extends ActivityInstrumentationTestCase
|
||||
};
|
||||
|
||||
WriteProgressController controller = new WriteProgressController(
|
||||
getActivity(), mockWriter);
|
||||
getActivity(), mockWriter, SaveActivity.PROGRESS_DIALOG);
|
||||
controller.setOnCompletionListener(new WriteProgressController.OnCompletionListener() {
|
||||
@Override
|
||||
public void onComplete(TrackWriter writer) {
|
||||
public void onComplete() {
|
||||
controllerDoneRef.set(true);
|
||||
}
|
||||
});
|
||||
|
||||
dialogRef.set(controller.getDialog());
|
||||
dialogRef.set(controller.createProgressDialog());
|
||||
|
||||
controller.startWrite();
|
||||
|
||||
+6
-7
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package com.google.android.apps.mytracks.io.sendtogoogle;
|
||||
|
||||
import com.google.android.apps.mytracks.MyTracks;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
@@ -33,9 +32,9 @@ import java.util.List;
|
||||
*
|
||||
* @author Matthew Simmons
|
||||
*/
|
||||
public class ResultDialogFactoryTest extends ActivityInstrumentationTestCase2<MyTracks> {
|
||||
public class ResultDialogFactoryTest extends ActivityInstrumentationTestCase2<SendActivity> {
|
||||
public ResultDialogFactoryTest() {
|
||||
super(MyTracks.class);
|
||||
super(SendActivity.class);
|
||||
}
|
||||
|
||||
private List<SendResult> makeResults(SendResult... results) {
|
||||
@@ -55,7 +54,7 @@ public class ResultDialogFactoryTest extends ActivityInstrumentationTestCase2<My
|
||||
List<SendResult> results = makeResults(new SendResult(SendType.MYMAPS, true),
|
||||
new SendResult(SendType.DOCS, true));
|
||||
|
||||
AlertDialog dialog = ResultDialogFactory.makeDialog(getActivity(), results, clickListener, null);
|
||||
AlertDialog dialog = ResultDialogFactory.makeDialog(getActivity(), results, clickListener, null, null);
|
||||
dialog.show();
|
||||
|
||||
ListView listView = (ListView) dialog.findViewById(R.id.send_to_google_result_list);
|
||||
@@ -87,7 +86,7 @@ public class ResultDialogFactoryTest extends ActivityInstrumentationTestCase2<My
|
||||
new SendResult(SendType.DOCS, true));
|
||||
|
||||
AlertDialog dialog = ResultDialogFactory.makeDialog(getActivity(), results,
|
||||
clickListener, clickListener);
|
||||
clickListener, clickListener, null);
|
||||
dialog.show();
|
||||
|
||||
assertEquals(View.VISIBLE, dialog.getButton(AlertDialog.BUTTON_POSITIVE).getVisibility());
|
||||
@@ -98,7 +97,7 @@ public class ResultDialogFactoryTest extends ActivityInstrumentationTestCase2<My
|
||||
List<SendResult> results = makeResults(new SendResult(SendType.MYMAPS, true),
|
||||
new SendResult(SendType.DOCS, false));
|
||||
|
||||
AlertDialog dialog = ResultDialogFactory.makeDialog(getActivity(), results, clickListener, null);
|
||||
AlertDialog dialog = ResultDialogFactory.makeDialog(getActivity(), results, clickListener, null, null);
|
||||
dialog.show();
|
||||
|
||||
assertEquals(View.GONE,
|
||||
@@ -112,7 +111,7 @@ public class ResultDialogFactoryTest extends ActivityInstrumentationTestCase2<My
|
||||
new SendResult(SendType.DOCS, false));
|
||||
|
||||
AlertDialog dialog = ResultDialogFactory.makeDialog(getActivity(), results,
|
||||
clickListener, clickListener);
|
||||
clickListener, clickListener, null);
|
||||
dialog.show();
|
||||
|
||||
assertEquals(View.VISIBLE, dialog.getButton(AlertDialog.BUTTON_POSITIVE).getVisibility());
|
||||
|
||||
+39
-112
@@ -49,9 +49,9 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* Tests for the MyTracks track recording service.
|
||||
*
|
||||
*
|
||||
* @author Bartlomiej Niechwiej
|
||||
*
|
||||
*
|
||||
* TODO: The original class, ServiceTestCase, has a few limitations, e.g.
|
||||
* it's not possible to properly shutdown the service, unless tearDown()
|
||||
* is called, which prevents from testing multiple scenarios in a single
|
||||
@@ -63,7 +63,7 @@ public class TrackRecordingServiceTest
|
||||
private Context context;
|
||||
private MyTracksProviderUtils providerUtils;
|
||||
private SharedPreferences sharedPreferences;
|
||||
|
||||
|
||||
/*
|
||||
* In order to support starting and binding to the service in the same
|
||||
* unit test, we provide a workaround, as the original class doesn't allow
|
||||
@@ -72,19 +72,19 @@ public class TrackRecordingServiceTest
|
||||
|
||||
private boolean bound;
|
||||
private Intent serviceIntent;
|
||||
|
||||
|
||||
public TrackRecordingServiceTest() {
|
||||
super(TrackRecordingService.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* A context wrapper with the user provided {@link ContentResolver}.
|
||||
*
|
||||
*
|
||||
* TODO: Move to test utils package.
|
||||
*/
|
||||
public static class MockContext extends ContextWrapper {
|
||||
private final ContentResolver contentResolver;
|
||||
|
||||
|
||||
public MockContext(ContentResolver contentResolver, Context base) {
|
||||
super(base);
|
||||
this.contentResolver = contentResolver;
|
||||
@@ -108,7 +108,7 @@ public class TrackRecordingServiceTest
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected IBinder bindService(Intent intent) {
|
||||
if (getService() != null) {
|
||||
@@ -190,7 +190,7 @@ public class TrackRecordingServiceTest
|
||||
startService(startIntent);
|
||||
assertNotNull(getService());
|
||||
|
||||
// We expect to resume the previous track.
|
||||
// We expect to resume the previous track.
|
||||
assertTrue(getService().isRecording());
|
||||
ITrackRecordingService service = bindAndGetService(createStartIntent());
|
||||
assertEquals(123, service.getRecordingTrackId());
|
||||
@@ -293,7 +293,7 @@ public class TrackRecordingServiceTest
|
||||
assertTrue(tracks.isEmpty());
|
||||
|
||||
ITrackRecordingService service = bindAndGetService(createStartIntent());
|
||||
// Test if we start in no-recording mode by default.
|
||||
// Test if we start in no-recording mode by default.
|
||||
assertFalse(service.isRecording());
|
||||
assertEquals(-1, service.getRecordingTrackId());
|
||||
}
|
||||
@@ -463,92 +463,19 @@ public class TrackRecordingServiceTest
|
||||
context.getString(R.string.recording_track_key), 0));
|
||||
assertEquals(-1, service.getRecordingTrackId());
|
||||
}
|
||||
|
||||
|
||||
@MediumTest
|
||||
public void testIntegration_completeRecordingSession() throws Exception {
|
||||
List<Track> tracks = providerUtils.getAllTracks();
|
||||
assertTrue(tracks.isEmpty());
|
||||
fullRecordingSession();
|
||||
}
|
||||
|
||||
@MediumTest
|
||||
public void testDeleteAllTracks_noRecording() throws Exception {
|
||||
createDummyTrack(123, -1, false);
|
||||
assertEquals(1, providerUtils.getAllTracks().size());
|
||||
|
||||
ITrackRecordingService service = bindAndGetService(createStartIntent());
|
||||
assertFalse(service.isRecording());
|
||||
|
||||
// Deleting all tracks should succeed.
|
||||
service.deleteAllTracks();
|
||||
assertFalse(service.isRecording());
|
||||
assertTrue(providerUtils.getAllTracks().isEmpty());
|
||||
}
|
||||
|
||||
@MediumTest
|
||||
public void testDeleteAllTracks_noTracks() throws Exception {
|
||||
assertTrue(providerUtils.getAllTracks().isEmpty());
|
||||
|
||||
ITrackRecordingService service = bindAndGetService(createStartIntent());
|
||||
assertFalse(service.isRecording());
|
||||
|
||||
// Deleting all tracks should succeed.
|
||||
service.deleteAllTracks();
|
||||
assertFalse(service.isRecording());
|
||||
assertTrue(providerUtils.getAllTracks().isEmpty());
|
||||
}
|
||||
|
||||
@MediumTest
|
||||
public void testDeleteAllTracks_trackInProgress() throws Exception {
|
||||
createDummyTrack(123, -1, true);
|
||||
assertEquals(1, providerUtils.getAllTracks().size());
|
||||
|
||||
ITrackRecordingService service = bindAndGetService(createStartIntent());
|
||||
assertTrue(service.isRecording());
|
||||
|
||||
// Since we have a track in progress, we expect to fail.
|
||||
try {
|
||||
service.deleteAllTracks();
|
||||
fail("Expecting IllegalStateException");
|
||||
} catch (IllegalStateException e) {
|
||||
// Expected.
|
||||
}
|
||||
assertTrue(service.isRecording());
|
||||
assertEquals(1, providerUtils.getAllTracks().size());
|
||||
}
|
||||
|
||||
@MediumTest
|
||||
public void testHasRecorded_noTracks() throws Exception {
|
||||
ITrackRecordingService service = bindAndGetService(createStartIntent());
|
||||
assertFalse(service.isRecording());
|
||||
assertFalse(service.hasRecorded());
|
||||
}
|
||||
|
||||
@MediumTest
|
||||
public void testHasRecorded_trackInProgress() throws Exception {
|
||||
createDummyTrack(123, -1, true);
|
||||
assertEquals(1, providerUtils.getAllTracks().size());
|
||||
|
||||
ITrackRecordingService service = bindAndGetService(createStartIntent());
|
||||
assertTrue(service.isRecording());
|
||||
assertTrue(service.hasRecorded());
|
||||
}
|
||||
|
||||
@MediumTest
|
||||
public void testHasRecorded_oldTracks() throws Exception {
|
||||
createDummyTrack(123, -1, false);
|
||||
assertEquals(1, providerUtils.getAllTracks().size());
|
||||
|
||||
ITrackRecordingService service = bindAndGetService(createStartIntent());
|
||||
assertFalse(service.isRecording());
|
||||
assertTrue(service.hasRecorded());
|
||||
}
|
||||
|
||||
@MediumTest
|
||||
public void testInsertStatisticsMarker_noRecordingTrack() throws Exception {
|
||||
ITrackRecordingService service = bindAndGetService(createStartIntent());
|
||||
assertFalse(service.isRecording());
|
||||
|
||||
|
||||
try {
|
||||
service.insertWaypoint(WaypointCreationRequest.DEFAULT_STATISTICS);
|
||||
fail("Expecting IllegalStateException");
|
||||
@@ -556,17 +483,17 @@ public class TrackRecordingServiceTest
|
||||
// Expected.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@MediumTest
|
||||
public void testInsertStatisticsMarker_validLocation() throws Exception {
|
||||
createDummyTrack(123, -1, true);
|
||||
|
||||
|
||||
ITrackRecordingService service = bindAndGetService(createStartIntent());
|
||||
assertTrue(service.isRecording());
|
||||
|
||||
|
||||
assertEquals(1, service.insertWaypoint(WaypointCreationRequest.DEFAULT_STATISTICS));
|
||||
assertEquals(2, service.insertWaypoint(WaypointCreationRequest.DEFAULT_STATISTICS));
|
||||
|
||||
|
||||
Waypoint wpt = providerUtils.getWaypoint(1);
|
||||
assertEquals(getContext().getString(R.string.stats_icon_url),
|
||||
wpt.getIcon());
|
||||
@@ -586,7 +513,7 @@ public class TrackRecordingServiceTest
|
||||
public void testInsertWaypointMarker_noRecordingTrack() throws Exception {
|
||||
ITrackRecordingService service = bindAndGetService(createStartIntent());
|
||||
assertFalse(service.isRecording());
|
||||
|
||||
|
||||
try {
|
||||
service.insertWaypoint(WaypointCreationRequest.DEFAULT_MARKER);
|
||||
fail("Expecting IllegalStateException");
|
||||
@@ -594,14 +521,14 @@ public class TrackRecordingServiceTest
|
||||
// Expected.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@MediumTest
|
||||
public void testInsertWaypointMarker_validWaypoint() throws Exception {
|
||||
createDummyTrack(123, -1, true);
|
||||
|
||||
|
||||
ITrackRecordingService service = bindAndGetService(createStartIntent());
|
||||
assertTrue(service.isRecording());
|
||||
|
||||
|
||||
assertEquals(1, service.insertWaypoint(WaypointCreationRequest.DEFAULT_MARKER));
|
||||
Waypoint wpt = providerUtils.getWaypoint(1);
|
||||
assertEquals(getContext().getString(R.string.waypoint_icon_url),
|
||||
@@ -614,7 +541,7 @@ public class TrackRecordingServiceTest
|
||||
assertNotNull(wpt.getLocation());
|
||||
assertNull(wpt.getStatistics());
|
||||
}
|
||||
|
||||
|
||||
@MediumTest
|
||||
public void testWithProperties_noAnnouncementFreq() throws Exception {
|
||||
functionalTest(R.string.announcement_frequency_key, (Object) null);
|
||||
@@ -624,7 +551,7 @@ public class TrackRecordingServiceTest
|
||||
public void testWithProperties_defaultAnnouncementFreq() throws Exception {
|
||||
functionalTest(R.string.announcement_frequency_key, 1);
|
||||
}
|
||||
|
||||
|
||||
@MediumTest
|
||||
public void testWithProperties_noMaxRecordingDist() throws Exception {
|
||||
functionalTest(R.string.max_recording_distance_key, (Object) null);
|
||||
@@ -654,7 +581,7 @@ public class TrackRecordingServiceTest
|
||||
public void testWithProperties_defaultSplitFreqByDist() throws Exception {
|
||||
functionalTest(R.string.split_frequency_key, 5);
|
||||
}
|
||||
|
||||
|
||||
@MediumTest
|
||||
public void testWithProperties_defaultSplitFreqByTime() throws Exception {
|
||||
functionalTest(R.string.split_frequency_key, -2);
|
||||
@@ -674,7 +601,7 @@ public class TrackRecordingServiceTest
|
||||
public void testWithProperties_metricUnitsDisabled() throws Exception {
|
||||
functionalTest(R.string.metric_units_key, false);
|
||||
}
|
||||
|
||||
|
||||
@MediumTest
|
||||
public void testWithProperties_noMinRecordingInterval() throws Exception {
|
||||
functionalTest(R.string.min_recording_interval_key, (Object) null);
|
||||
@@ -690,23 +617,23 @@ public class TrackRecordingServiceTest
|
||||
public void testWithProperties_noMinRequiredAccuracy() throws Exception {
|
||||
functionalTest(R.string.min_required_accuracy_key, (Object) null);
|
||||
}
|
||||
|
||||
|
||||
@MediumTest
|
||||
public void testWithProperties_defaultMinRequiredAccuracy() throws Exception {
|
||||
functionalTest(R.string.min_required_accuracy_key, 500);
|
||||
}
|
||||
|
||||
|
||||
@MediumTest
|
||||
public void testWithProperties_noSensorType() throws Exception {
|
||||
functionalTest(R.string.sensor_type_key, (Object) null);
|
||||
}
|
||||
|
||||
|
||||
@MediumTest
|
||||
public void testWithProperties_zephyrSensorType() throws Exception {
|
||||
functionalTest(R.string.sensor_type_key,
|
||||
context.getString(R.string.zephyr_sensor_type));
|
||||
context.getString(R.string.zephyr_sensor_type));
|
||||
}
|
||||
|
||||
|
||||
private ITrackRecordingService bindAndGetService(Intent intent) {
|
||||
ITrackRecordingService service = ITrackRecordingService.Stub.asInterface(
|
||||
bindService(intent));
|
||||
@@ -724,7 +651,7 @@ public class TrackRecordingServiceTest
|
||||
addTrack(dummyTrack, isRecording);
|
||||
return dummyTrack;
|
||||
}
|
||||
|
||||
|
||||
private void updateAutoResumePrefs(int attempts, int timeoutMins) {
|
||||
Editor editor = sharedPreferences.edit();
|
||||
editor.putInt(context.getString(
|
||||
@@ -733,20 +660,20 @@ public class TrackRecordingServiceTest
|
||||
R.string.auto_resume_track_timeout_key), timeoutMins);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
|
||||
private Intent createStartIntent() {
|
||||
Intent startIntent = new Intent();
|
||||
startIntent.setClass(context, TrackRecordingService.class);
|
||||
return startIntent;
|
||||
}
|
||||
|
||||
|
||||
private void addTrack(Track track, boolean isRecording) {
|
||||
assertTrue(track.getId() >= 0);
|
||||
providerUtils.insertTrack(track);
|
||||
assertEquals(track.getId(), providerUtils.getTrack(track.getId()).getId());
|
||||
setRecordingTrack(isRecording ? track.getId() : -1);
|
||||
}
|
||||
|
||||
|
||||
private void setRecordingTrack(long id) {
|
||||
Editor editor = sharedPreferences.edit();
|
||||
editor.putLong(context.getString(R.string.recording_track_key), id);
|
||||
@@ -760,11 +687,11 @@ public class TrackRecordingServiceTest
|
||||
throws Exception {
|
||||
final String key = context.getString(resourceId);
|
||||
for (Object value : values) {
|
||||
// Remove all properties and set the property for the given key.
|
||||
// Remove all properties and set the property for the given key.
|
||||
Editor editor = sharedPreferences.edit();
|
||||
editor.clear();
|
||||
if (value instanceof String) {
|
||||
editor.putString(key, (String) value);
|
||||
editor.putString(key, (String) value);
|
||||
} else if (value instanceof Long) {
|
||||
editor.putLong(key, (Long) value);
|
||||
} else if (value instanceof Integer) {
|
||||
@@ -775,15 +702,15 @@ public class TrackRecordingServiceTest
|
||||
// Do nothing, as clear above has already removed this property.
|
||||
}
|
||||
editor.commit();
|
||||
|
||||
|
||||
fullRecordingSession();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void fullRecordingSession() throws Exception {
|
||||
ITrackRecordingService service = bindAndGetService(createStartIntent());
|
||||
assertFalse(service.isRecording());
|
||||
|
||||
|
||||
// Start a track.
|
||||
long id = service.startNewTrack();
|
||||
assertTrue(id >= 0);
|
||||
@@ -806,14 +733,14 @@ public class TrackRecordingServiceTest
|
||||
loc.setTime(startTime + i * 10000);
|
||||
loc.setBearing(3.0f);
|
||||
service.recordLocation(loc);
|
||||
|
||||
|
||||
if (i % 10 == 0) {
|
||||
service.insertWaypoint(WaypointCreationRequest.DEFAULT_STATISTICS);
|
||||
} else if (i % 7 == 0) {
|
||||
service.insertWaypoint(WaypointCreationRequest.DEFAULT_MARKER);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Stop the track. Validate if it has correct data.
|
||||
service.endCurrentTrack();
|
||||
assertFalse(service.isRecording());
|
||||
|
||||
+22
-3
@@ -9,21 +9,40 @@ public class ZephyrMessageParserTest extends TestCase {
|
||||
ZephyrMessageParser parser = new ZephyrMessageParser();
|
||||
|
||||
public void testIsValid() {
|
||||
byte[] buf = new byte[60];
|
||||
byte[] smallBuf = new byte[59];
|
||||
assertFalse(parser.isValid(smallBuf));
|
||||
// A complete and valid Zephyr HxM packet
|
||||
byte[] buf = { 2,38,55,26,0,49,101,80,0,49,98,100,42,113,120,-53,-24,-60,-123,-61,117,-69,42,-75,74,-78,51,-79,27,-83,28,-88,28,-93,29,-98,25,-103,26,-108,26,-113,59,-118,0,0,0,0,0,0,-22,3,125,1,48,0,96,4,30,0 };
|
||||
// Make buffer invalid
|
||||
buf[0] = buf[58] = buf[59] = 0;
|
||||
assertFalse(parser.isValid(buf));
|
||||
buf[0] = 0x02;
|
||||
assertFalse(parser.isValid(buf));
|
||||
buf[58] = 0x1E;
|
||||
assertFalse(parser.isValid(buf));
|
||||
buf[59] = 0x03;
|
||||
assertTrue(parser.isValid(buf));
|
||||
}
|
||||
|
||||
public void testParseBuffer() {
|
||||
byte[] buf = new byte[60];
|
||||
buf[12] = 50;
|
||||
// Heartrate (-1 =^ 255 unsigned byte)
|
||||
buf[12] = -1;
|
||||
// Power
|
||||
buf[11] = 51;
|
||||
// Cadence (=^ 255*16 strides/min)
|
||||
buf[56] = -1;
|
||||
buf[57] = 15;
|
||||
Sensor.SensorDataSet sds = parser.parseBuffer(buf);
|
||||
assertTrue(sds.hasHeartRate());
|
||||
assertTrue(sds.getHeartRate().getState() == Sensor.SensorState.SENDING);
|
||||
assertEquals(50, sds.getHeartRate().getValue());
|
||||
assertEquals(255, sds.getHeartRate().getValue());
|
||||
assertTrue(sds.hasPower());
|
||||
assertTrue(sds.getPower().getState() == Sensor.SensorState.SENDING);
|
||||
assertEquals(51, sds.getPower().getValue());
|
||||
assertTrue(sds.hasCadence());
|
||||
assertTrue(sds.getCadence().getState() == Sensor.SensorState.SENDING);
|
||||
assertEquals(255, sds.getCadence().getValue());
|
||||
}
|
||||
|
||||
public void testFindNextAlignment() {
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
package com.google.android.apps.mytracks.services.sensors;
|
||||
package com.google.android.apps.mytracks.services.sensors.ant;
|
||||
|
||||
import com.dsi.ant.AntMesg;
|
||||
import com.google.android.apps.mytracks.Constants;
|
||||
+1
-1
@@ -13,7 +13,7 @@
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
package com.google.android.apps.mytracks.services.sensors;
|
||||
package com.google.android.apps.mytracks.services.sensors.ant;
|
||||
|
||||
import com.google.android.apps.mytracks.services.sensors.ant.AntSensorManager;
|
||||
|
||||
Reference in New Issue
Block a user