Fix PhoneAccount parceling (2/2)

Bug: 18343731
Change-Id: Ib3867448e7731160bc478659d0297a2044050972
diff --git a/tests/src/com/android/server/telecom/tests/unit/PhoneAccountRegistrarTest.java b/tests/src/com/android/server/telecom/tests/unit/PhoneAccountRegistrarTest.java
index 2610720..8c839a9 100644
--- a/tests/src/com/android/server/telecom/tests/unit/PhoneAccountRegistrarTest.java
+++ b/tests/src/com/android/server/telecom/tests/unit/PhoneAccountRegistrarTest.java
@@ -29,6 +29,7 @@
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.net.Uri;
+import android.os.Parcel;
 import android.telecom.PhoneAccount;
 import android.telecom.PhoneAccountHandle;
 import android.test.AndroidTestCase;
@@ -39,9 +40,11 @@
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.util.Arrays;
 
 public class PhoneAccountRegistrarTest extends AndroidTestCase {
 
+    private static final int MAX_VERSION = Integer.MAX_VALUE;
     private static final String FILE_NAME = "phone-account-registrar-test.xml";
     private PhoneAccountRegistrar mRegistrar;
 
@@ -58,12 +61,12 @@
 
     public void testPhoneAccountHandle() throws Exception {
         PhoneAccountHandle input = new PhoneAccountHandle(new ComponentName("pkg0", "cls0"), "id0");
-        PhoneAccountHandle result = roundTrip(this, input,
+        PhoneAccountHandle result = roundTripXml(this, input,
                 PhoneAccountRegistrar.sPhoneAccountHandleXml, mContext);
         assertPhoneAccountHandleEquals(input, result);
 
         PhoneAccountHandle inputN = new PhoneAccountHandle(new ComponentName("pkg0", "cls0"), null);
-        PhoneAccountHandle resultN = roundTrip(this, inputN,
+        PhoneAccountHandle resultN = roundTripXml(this, inputN,
                 PhoneAccountRegistrar.sPhoneAccountHandleXml, mContext);
         Log.i(this, "inputN = %s, resultN = %s", inputN, resultN);
         assertPhoneAccountHandleEquals(inputN, resultN);
@@ -74,14 +77,15 @@
                 .addSupportedUriScheme(PhoneAccount.SCHEME_TEL)
                 .addSupportedUriScheme(PhoneAccount.SCHEME_VOICEMAIL)
                 .build();
-        PhoneAccount result = roundTrip(this, input, PhoneAccountRegistrar.sPhoneAccountXml,
+        PhoneAccount result = roundTripXml(this, input, PhoneAccountRegistrar.sPhoneAccountXml,
                 mContext);
         assertPhoneAccountEquals(input, result);
     }
 
     public void testState() throws Exception {
         PhoneAccountRegistrar.State input = makeQuickState();
-        PhoneAccountRegistrar.State result = roundTrip(this, input, PhoneAccountRegistrar.sStateXml,
+        PhoneAccountRegistrar.State result = roundTripXml(this, input,
+                PhoneAccountRegistrar.sStateXml,
                 mContext);
         assertStateEquals(input, result);
     }
@@ -184,6 +188,38 @@
         assertNull(mRegistrar.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL));
     }
 
+    public void testPhoneAccountParceling() throws Exception {
+        PhoneAccountHandle handle = makeQuickAccountHandle("foo");
+        roundTripPhoneAccount(new PhoneAccount.Builder(handle, null).build());
+        roundTripPhoneAccount(new PhoneAccount.Builder(handle, "foo").build());
+        roundTripPhoneAccount(
+                new PhoneAccount.Builder(handle, "foo")
+                        .setAddress(Uri.parse("tel:123456"))
+                        .setCapabilities(23)
+                        .setHighlightColor(0xf0f0f0)
+                        .setIcon(
+                                "com.android.server.telecom.tests",
+                                R.drawable.stat_sys_phone_call,
+                                0xfefefe)
+                        .setShortDescription("short description")
+                        .setSubscriptionAddress(Uri.parse("tel:2345678"))
+                        .setSupportedUriSchemes(Arrays.asList("tel", "sip"))
+                        .build());
+        roundTripPhoneAccount(
+                new PhoneAccount.Builder(handle, "foo")
+                        .setAddress(Uri.parse("tel:123456"))
+                        .setCapabilities(23)
+                        .setHighlightColor(0xf0f0f0)
+                        .setIcon(
+                                BitmapFactory.decodeResource(
+                                        getContext().getResources(),
+                                        R.drawable.stat_sys_phone_call))
+                        .setShortDescription("short description")
+                        .setSubscriptionAddress(Uri.parse("tel:2345678"))
+                        .setSupportedUriSchemes(Arrays.asList("tel", "sip"))
+                        .build());
+    }
+
     private static PhoneAccountHandle makeQuickAccountHandle(String id) {
         return new PhoneAccountHandle(
                 new ComponentName(
@@ -209,7 +245,21 @@
                 .build();
     }
 
-    private static <T> T roundTrip(
+    private static void roundTripPhoneAccount(PhoneAccount original) throws Exception {
+        PhoneAccount copy = null;
+
+        {
+            Parcel parcel = Parcel.obtain();
+            parcel.writeParcelable(original, 0);
+            parcel.setDataPosition(0);
+            copy = parcel.readParcelable(PhoneAccountRegistrarTest.class.getClassLoader());
+            parcel.recycle();
+        }
+
+        assertPhoneAccountEquals(original, copy);
+    }
+
+    private static <T> T roundTripXml(
             Object self,
             T input,
             PhoneAccountRegistrar.XmlSerialization<T> xml,
@@ -234,7 +284,7 @@
             XmlPullParser parser = Xml.newPullParser();
             parser.setInput(new BufferedInputStream(new ByteArrayInputStream(data)), null);
             parser.nextTag();
-            result = xml.readFromXml(parser, 0, context);
+            result = xml.readFromXml(parser, MAX_VERSION, context);
         }
 
         Log.d(self, "result = " + result);