New @TestApi: AutofillId.equalsIgnoreSession

Test: atest FrameworksCoreTests:android.view.autofill.AutofillIdTest
Bug: 113593220

Change-Id: I72c5727638558d3b8dc0c57045a8e43c1d506069
This commit is contained in:
Felipe Leme
2019-05-01 16:52:48 -07:00
parent 2f8f43514f
commit 7b307ea7b2
3 changed files with 63 additions and 0 deletions

View File

@@ -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 {

View File

@@ -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);

View File

@@ -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();
}
}