From 7b307ea7b2db82e4881ae66bb3fc805185da936e Mon Sep 17 00:00:00 2001 From: Felipe Leme Date: Wed, 1 May 2019 16:52:48 -0700 Subject: [PATCH] New @TestApi: AutofillId.equalsIgnoreSession Test: atest FrameworksCoreTests:android.view.autofill.AutofillIdTest Bug: 113593220 Change-Id: I72c5727638558d3b8dc0c57045a8e43c1d506069 --- api/test-current.txt | 1 + .../android/view/autofill/AutofillId.java | 12 +++++ .../android/view/autofill/AutofillIdTest.java | 50 +++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/api/test-current.txt b/api/test-current.txt index aacf2c1fa9a6c..62d8b206511e1 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -3262,6 +3262,7 @@ package android.view.autofill { ctor public AutofillId(@NonNull android.view.autofill.AutofillId, int); ctor public AutofillId(int, int); ctor public AutofillId(@NonNull android.view.autofill.AutofillId, long, int); + method public boolean equalsIgnoreSession(@Nullable android.view.autofill.AutofillId); } public final class AutofillManager { diff --git a/core/java/android/view/autofill/AutofillId.java b/core/java/android/view/autofill/AutofillId.java index 2c79299390530..8acfd38df5e98 100644 --- a/core/java/android/view/autofill/AutofillId.java +++ b/core/java/android/view/autofill/AutofillId.java @@ -16,6 +16,7 @@ package android.view.autofill; import android.annotation.NonNull; +import android.annotation.Nullable; import android.annotation.TestApi; import android.os.Parcel; import android.os.Parcelable; @@ -172,6 +173,17 @@ public final class AutofillId implements Parcelable { return true; } + /** @hide */ + @TestApi + public boolean equalsIgnoreSession(@Nullable AutofillId other) { + if (this == other) return true; + if (other == null) return false; + if (mViewId != other.mViewId) return false; + if (mVirtualIntId != other.mVirtualIntId) return false; + if (mVirtualLongId != other.mVirtualLongId) return false; + return true; + } + @Override public String toString() { final StringBuilder builder = new StringBuilder().append(mViewId); diff --git a/core/tests/coretests/src/android/view/autofill/AutofillIdTest.java b/core/tests/coretests/src/android/view/autofill/AutofillIdTest.java index 2f17b32370f46..d900682a3a208 100644 --- a/core/tests/coretests/src/android/view/autofill/AutofillIdTest.java +++ b/core/tests/coretests/src/android/view/autofill/AutofillIdTest.java @@ -17,6 +17,7 @@ package android.view.autofill; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; import static org.testng.Assert.assertThrows; @@ -129,46 +130,81 @@ public class AutofillIdTest { final AutofillId realIdSame = new AutofillId(42); assertThat(realId).isEqualTo(realIdSame); assertThat(realIdSame).isEqualTo(realId); + assertEqualsIgnoreSession(realId, realIdSame); + assertEqualsIgnoreSession(realIdSame, realId); assertThat(realId.hashCode()).isEqualTo(realIdSame.hashCode()); final AutofillId realIdDifferent = new AutofillId(108); assertThat(realId).isNotEqualTo(realIdDifferent); assertThat(realIdDifferent).isNotEqualTo(realId); + assertNotEqualsIgnoreSession(realId, realIdDifferent); + assertNotEqualsIgnoreSession(realIdDifferent, realId); + assertThat(realId.hashCode()).isNotEqualTo(realIdDifferent.hashCode()); final AutofillId virtualId = new AutofillId(42, 1); final AutofillId virtualIdSame = new AutofillId(42, 1); assertThat(virtualId).isEqualTo(virtualIdSame); assertThat(virtualIdSame).isEqualTo(virtualId); + assertEqualsIgnoreSession(virtualId, virtualIdSame); + assertEqualsIgnoreSession(virtualIdSame, virtualId); assertThat(virtualId.hashCode()).isEqualTo(virtualIdSame.hashCode()); assertThat(virtualId).isNotEqualTo(realId); assertThat(realId).isNotEqualTo(virtualId); + assertNotEqualsIgnoreSession(realId, virtualId); + assertNotEqualsIgnoreSession(virtualId, realId); final AutofillId virtualIdDifferentChild = new AutofillId(42, 2); assertThat(virtualIdDifferentChild).isNotEqualTo(virtualId); assertThat(virtualId).isNotEqualTo(virtualIdDifferentChild); + assertNotEqualsIgnoreSession(virtualIdDifferentChild, virtualId); + assertNotEqualsIgnoreSession(virtualId, virtualIdDifferentChild); assertThat(virtualIdDifferentChild).isNotEqualTo(realId); assertThat(realId).isNotEqualTo(virtualIdDifferentChild); + assertNotEqualsIgnoreSession(virtualIdDifferentChild, realId); + assertNotEqualsIgnoreSession(realId, virtualIdDifferentChild); final AutofillId virtualIdDifferentParent = new AutofillId(108, 1); assertThat(virtualIdDifferentParent).isNotEqualTo(virtualId); assertThat(virtualId).isNotEqualTo(virtualIdDifferentParent); + assertNotEqualsIgnoreSession(virtualIdDifferentParent, virtualId); + assertNotEqualsIgnoreSession(virtualId, virtualIdDifferentParent); assertThat(virtualIdDifferentParent).isNotEqualTo(virtualIdDifferentChild); assertThat(virtualIdDifferentChild).isNotEqualTo(virtualIdDifferentParent); + assertNotEqualsIgnoreSession(virtualIdDifferentParent, virtualIdDifferentChild); + assertNotEqualsIgnoreSession(virtualIdDifferentChild, virtualIdDifferentParent); final AutofillId virtualIdDifferentSession = new AutofillId(new AutofillId(42), 1L, 108); assertThat(virtualIdDifferentSession).isNotEqualTo(virtualId); assertThat(virtualId).isNotEqualTo(virtualIdDifferentSession); + if (false) { // TODO: doesn't work because one object uses int virtual ids, other uses long + assertEqualsIgnoreSession(virtualIdDifferentSession, virtualId); + assertEqualsIgnoreSession(virtualId, virtualIdDifferentSession); + } assertThat(virtualIdDifferentSession).isNotEqualTo(realId); assertThat(realId).isNotEqualTo(virtualIdDifferentSession); + assertNotEqualsIgnoreSession(virtualIdDifferentSession, realId); + assertNotEqualsIgnoreSession(realId, virtualIdDifferentSession); final AutofillId sameVirtualIdDifferentSession = new AutofillId(new AutofillId(42), 1L, 108); assertThat(sameVirtualIdDifferentSession).isEqualTo(virtualIdDifferentSession); assertThat(virtualIdDifferentSession).isEqualTo(sameVirtualIdDifferentSession); + assertEqualsIgnoreSession(sameVirtualIdDifferentSession, virtualIdDifferentSession); + assertEqualsIgnoreSession(virtualIdDifferentSession, sameVirtualIdDifferentSession); assertThat(sameVirtualIdDifferentSession.hashCode()) .isEqualTo(virtualIdDifferentSession.hashCode()); } + @Test + public void testEqualsIgnoreSession() { + final AutofillId id1 = new AutofillId(new AutofillId(42), 1L, 108); + final AutofillId id2 = new AutofillId(new AutofillId(42), 1L, 666); + assertThat(id1).isNotEqualTo(id2); + assertThat(id2).isNotEqualTo(id1); + assertEqualsIgnoreSession(id1, id2); + assertEqualsIgnoreSession(id2, id1); + } + private AutofillId cloneThroughParcel(AutofillId id) { Parcel parcel = Parcel.obtain(); @@ -186,4 +222,18 @@ public class AutofillIdTest { parcel.recycle(); } } + + public static void assertEqualsIgnoreSession(AutofillId id1, AutofillId id2) { + assertWithMessage("id1 is null").that(id1).isNotNull(); + assertWithMessage("id2 is null").that(id2).isNotNull(); + assertWithMessage("%s is not equal to %s", id1, id2).that(id1.equalsIgnoreSession(id2)) + .isTrue(); + } + + public static void assertNotEqualsIgnoreSession(AutofillId id1, AutofillId id2) { + assertWithMessage("id1 is null").that(id1).isNotNull(); + assertWithMessage("id2 is null").that(id2).isNotNull(); + assertWithMessage("%s is not equal to %s", id1, id2).that(id1.equalsIgnoreSession(id2)) + .isFalse(); + } }