Add CTS test for ExternalTimeSuggestion

Bug: 182275086
Bug: 157504928
Bug: 177079827
Test: CtsTimeTestCases
Change-Id: If4006e087a62ada3ea9710da567b810fce2267f3
diff --git a/tests/tests/time/src/android/time/cts/TimeManagerTest.java b/tests/tests/time/src/android/time/cts/TimeManagerTest.java
index 94ab334..4719d82 100644
--- a/tests/tests/time/src/android/time/cts/TimeManagerTest.java
+++ b/tests/tests/time/src/android/time/cts/TimeManagerTest.java
@@ -18,16 +18,20 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import android.app.time.Capabilities;
+import android.app.time.ExternalTimeSuggestion;
 import android.app.time.TimeManager;
 import android.app.time.TimeZoneCapabilities;
 import android.app.time.TimeZoneCapabilitiesAndConfig;
 import android.app.time.TimeZoneConfiguration;
 import android.content.Context;
 import android.location.LocationManager;
+import android.os.Parcel;
+import android.os.Parcelable;
 import android.os.UserHandle;
 
 import org.junit.Ignore;
@@ -38,6 +42,7 @@
 
 import com.android.compatibility.common.util.AdoptShellPermissionsRule;
 
+import java.lang.reflect.Field;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -190,4 +195,66 @@
         }
         assertEquals(expectedValue, actualValue.get());
     }
+
+    @Test
+    public void testExternalTimeSuggestionEquals() {
+        long referenceTimeMillis = 1111;
+        long currentTimeMillis = 2222;
+        ExternalTimeSuggestion one = new ExternalTimeSuggestion(
+                referenceTimeMillis,
+                currentTimeMillis);
+        assertEquals(one, one);
+
+        ExternalTimeSuggestion two = new ExternalTimeSuggestion(
+                referenceTimeMillis,
+                currentTimeMillis);
+        assertEquals(one, two);
+        assertEquals(two, one);
+
+        ExternalTimeSuggestion three = new ExternalTimeSuggestion(
+                referenceTimeMillis + 1,
+                currentTimeMillis);
+        assertNotEquals(one, three);
+        assertNotEquals(three, one);
+
+        // DebugInfo must not be considered in equals().
+        one.addDebugInfo("Debug info 1");
+        two.addDebugInfo("Debug info 2");
+        assertEquals(one, two);
+    }
+
+
+    /** Returns the result of parceling and unparceling the argument. */
+    @SuppressWarnings("unchecked")
+    public static ExternalTimeSuggestion roundTripParcelable(ExternalTimeSuggestion parcelable) {
+        Parcel parcel = Parcel.obtain();
+        parcel.writeTypedObject(parcelable, 0);
+        parcel.setDataPosition(0);
+
+        Parcelable.Creator<ExternalTimeSuggestion> creator;
+        try {
+            Field creatorField = parcelable.getClass().getField("CREATOR");
+            creator = (Parcelable.Creator<ExternalTimeSuggestion>) creatorField.get(null);
+        } catch (NoSuchFieldException | IllegalAccessException e) {
+            throw new AssertionError(e);
+        }
+        ExternalTimeSuggestion toReturn = parcel.readTypedObject(creator);
+        parcel.recycle();
+        return toReturn;
+    }
+
+    @Test
+    public void testExternalTimeSuggestionParcelable() {
+        long referenceTimeMillis = 1111;
+        long currentTimeMillis = 2222;
+        ExternalTimeSuggestion suggestion = new ExternalTimeSuggestion(
+                referenceTimeMillis,
+                currentTimeMillis);
+        assertEquals(suggestion, roundTripParcelable(suggestion));
+
+        // DebugInfo should also be stored (but is not checked by equals())
+        suggestion.addDebugInfo("This is debug info");
+        ExternalTimeSuggestion rtSuggestion = roundTripParcelable(suggestion);
+        assertEquals(suggestion.getDebugInfo(), rtSuggestion.getDebugInfo());
+    }
 }