From fa711cf142beb3bf76f0e276842e3fc8e4d4af6a Mon Sep 17 00:00:00 2001 From: Sandor Dornbush Date: Sun, 19 Dec 2010 22:11:41 -0800 Subject: [PATCH] Address comments + add more tests. --- .../content/WaypointCreationRequest.java | 29 +++------- .../content/WaypointCreationRequestTest.java | 56 +++++++++++++++++++ 2 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 MyTracksTest/src/com/google/android/apps/mytracks/content/WaypointCreationRequestTest.java diff --git a/MyTracksLib/src/com/google/android/apps/mytracks/content/WaypointCreationRequest.java b/MyTracksLib/src/com/google/android/apps/mytracks/content/WaypointCreationRequest.java index 66c3692ac..e1b8463d9 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/content/WaypointCreationRequest.java +++ b/MyTracksLib/src/com/google/android/apps/mytracks/content/WaypointCreationRequest.java @@ -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 { + @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; } diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/content/WaypointCreationRequestTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/content/WaypointCreationRequestTest.java new file mode 100644 index 000000000..b79b57270 --- /dev/null +++ b/MyTracksTest/src/com/google/android/apps/mytracks/content/WaypointCreationRequestTest.java @@ -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()); + } +}