mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-02 09:33:06 +02:00
Address comments + add more tests.
This commit is contained in:
+8
-21
@@ -40,13 +40,12 @@ public class WaypointCreationRequest implements Parcelable {
|
||||
public final static WaypointCreationRequest DEFAULT_STATISTICS =
|
||||
new WaypointCreationRequest(WaypointType.STATISTICS);
|
||||
|
||||
public WaypointCreationRequest(WaypointType type) {
|
||||
private WaypointCreationRequest(WaypointType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public WaypointCreationRequest(WaypointType type, String name,
|
||||
String description, String iconUrl) {
|
||||
super();
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
@@ -55,23 +54,19 @@ public class WaypointCreationRequest implements Parcelable {
|
||||
|
||||
public static class Creator implements Parcelable.Creator<WaypointCreationRequest> {
|
||||
|
||||
@Override
|
||||
public WaypointCreationRequest createFromParcel(Parcel source) {
|
||||
int i = source.readInt();
|
||||
if (i > WaypointType.values().length) {
|
||||
throw new IllegalArgumentException("Could not find waypoint type: " + i);
|
||||
}
|
||||
WaypointCreationRequest request = new WaypointCreationRequest(WaypointType.values()[i]);
|
||||
request.description = maybeReadStringToParcel(source);
|
||||
request.iconUrl = maybeReadStringToParcel(source);
|
||||
request.name = maybeReadStringToParcel(source);
|
||||
request.description = source.readString();
|
||||
request.iconUrl = source.readString();
|
||||
request.name = source.readString();
|
||||
return request;
|
||||
}
|
||||
|
||||
private String maybeReadStringToParcel(Parcel parcel) {
|
||||
return parcel.readByte() > 0 ? parcel.readString() : null;
|
||||
}
|
||||
|
||||
|
||||
public WaypointCreationRequest[] newArray(int size) {
|
||||
return new WaypointCreationRequest[size];
|
||||
}
|
||||
@@ -87,19 +82,11 @@ public class WaypointCreationRequest implements Parcelable {
|
||||
@Override
|
||||
public void writeToParcel(Parcel parcel, int arg1) {
|
||||
parcel.writeInt(type.ordinal());
|
||||
// TODO: Pack all of available bits into one byte.
|
||||
maybeWriteStringToParcel(parcel, description);
|
||||
maybeWriteStringToParcel(parcel, iconUrl);
|
||||
maybeWriteStringToParcel(parcel, name);
|
||||
parcel.writeString(description);
|
||||
parcel.writeString(iconUrl);
|
||||
parcel.writeString(name);
|
||||
}
|
||||
|
||||
private void maybeWriteStringToParcel(Parcel parcel, String s) {
|
||||
parcel.writeByte(s == null ? (byte) 0 : (byte) 1);
|
||||
if (s != null) {
|
||||
parcel.writeString(s);
|
||||
}
|
||||
}
|
||||
|
||||
public WaypointType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.content;
|
||||
|
||||
import com.google.android.apps.mytracks.content.WaypointCreationRequest.WaypointType;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.test.AndroidTestCase;
|
||||
|
||||
/**
|
||||
* Tests for the WaypointCreationRequest class.
|
||||
*
|
||||
* @author Sandor Dornbush
|
||||
*/
|
||||
public class WaypointCreationRequestTest extends AndroidTestCase {
|
||||
|
||||
public void testTypeParceling() {
|
||||
WaypointCreationRequest original = WaypointCreationRequest.DEFAULT_MARKER;
|
||||
Parcel p = Parcel.obtain();
|
||||
original.writeToParcel(p, 0);
|
||||
p.setDataPosition(0);
|
||||
WaypointCreationRequest copy =
|
||||
WaypointCreationRequest.CREATOR.createFromParcel(p);
|
||||
assertEquals(original.getType(), copy.getType());
|
||||
assertNull(copy.getName());
|
||||
assertNull(copy.getDescription());
|
||||
assertNull(copy.getIconUrl());
|
||||
}
|
||||
|
||||
public void testAllAttributesParceling() {
|
||||
WaypointCreationRequest original =
|
||||
new WaypointCreationRequest(WaypointType.MARKER, "name", "description", "img.png");
|
||||
Parcel p = Parcel.obtain();
|
||||
original.writeToParcel(p, 0);
|
||||
p.setDataPosition(0);
|
||||
WaypointCreationRequest copy =
|
||||
WaypointCreationRequest.CREATOR.createFromParcel(p);
|
||||
assertEquals(original.getType(), copy.getType());
|
||||
assertEquals("name", copy.getName());
|
||||
assertEquals("description", copy.getDescription());
|
||||
assertEquals("img.png", copy.getIconUrl());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user