Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 77f574df authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "New @TestApi: AutofillId.equalsIgnoreSession" into qt-dev

parents d7d7980d 7b307ea7
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -3262,6 +3262,7 @@ package android.view.autofill {
    ctor public AutofillId(@NonNull android.view.autofill.AutofillId, int);
    ctor public AutofillId(@NonNull android.view.autofill.AutofillId, int);
    ctor public AutofillId(int, int);
    ctor public AutofillId(int, int);
    ctor public AutofillId(@NonNull android.view.autofill.AutofillId, long, int);
    ctor public AutofillId(@NonNull android.view.autofill.AutofillId, long, int);
    method public boolean equalsIgnoreSession(@Nullable android.view.autofill.AutofillId);
  }
  }


  public final class AutofillManager {
  public final class AutofillManager {
+12 −0
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@
package android.view.autofill;
package android.view.autofill;


import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.TestApi;
import android.annotation.TestApi;
import android.os.Parcel;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable;
@@ -172,6 +173,17 @@ public final class AutofillId implements Parcelable {
        return true;
        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
    @Override
    public String toString() {
    public String toString() {
        final StringBuilder builder = new StringBuilder().append(mViewId);
        final StringBuilder builder = new StringBuilder().append(mViewId);
+50 −0
Original line number Original line Diff line number Diff line
@@ -17,6 +17,7 @@
package android.view.autofill;
package android.view.autofill;


import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;


import static org.testng.Assert.assertThrows;
import static org.testng.Assert.assertThrows;


@@ -129,46 +130,81 @@ public class AutofillIdTest {
        final AutofillId realIdSame = new AutofillId(42);
        final AutofillId realIdSame = new AutofillId(42);
        assertThat(realId).isEqualTo(realIdSame);
        assertThat(realId).isEqualTo(realIdSame);
        assertThat(realIdSame).isEqualTo(realId);
        assertThat(realIdSame).isEqualTo(realId);
        assertEqualsIgnoreSession(realId, realIdSame);
        assertEqualsIgnoreSession(realIdSame, realId);
        assertThat(realId.hashCode()).isEqualTo(realIdSame.hashCode());
        assertThat(realId.hashCode()).isEqualTo(realIdSame.hashCode());


        final AutofillId realIdDifferent = new AutofillId(108);
        final AutofillId realIdDifferent = new AutofillId(108);
        assertThat(realId).isNotEqualTo(realIdDifferent);
        assertThat(realId).isNotEqualTo(realIdDifferent);
        assertThat(realIdDifferent).isNotEqualTo(realId);
        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 virtualId = new AutofillId(42, 1);
        final AutofillId virtualIdSame = new AutofillId(42, 1);
        final AutofillId virtualIdSame = new AutofillId(42, 1);
        assertThat(virtualId).isEqualTo(virtualIdSame);
        assertThat(virtualId).isEqualTo(virtualIdSame);
        assertThat(virtualIdSame).isEqualTo(virtualId);
        assertThat(virtualIdSame).isEqualTo(virtualId);
        assertEqualsIgnoreSession(virtualId, virtualIdSame);
        assertEqualsIgnoreSession(virtualIdSame, virtualId);
        assertThat(virtualId.hashCode()).isEqualTo(virtualIdSame.hashCode());
        assertThat(virtualId.hashCode()).isEqualTo(virtualIdSame.hashCode());
        assertThat(virtualId).isNotEqualTo(realId);
        assertThat(virtualId).isNotEqualTo(realId);
        assertThat(realId).isNotEqualTo(virtualId);
        assertThat(realId).isNotEqualTo(virtualId);
        assertNotEqualsIgnoreSession(realId, virtualId);
        assertNotEqualsIgnoreSession(virtualId, realId);


        final AutofillId virtualIdDifferentChild = new AutofillId(42, 2);
        final AutofillId virtualIdDifferentChild = new AutofillId(42, 2);
        assertThat(virtualIdDifferentChild).isNotEqualTo(virtualId);
        assertThat(virtualIdDifferentChild).isNotEqualTo(virtualId);
        assertThat(virtualId).isNotEqualTo(virtualIdDifferentChild);
        assertThat(virtualId).isNotEqualTo(virtualIdDifferentChild);
        assertNotEqualsIgnoreSession(virtualIdDifferentChild, virtualId);
        assertNotEqualsIgnoreSession(virtualId, virtualIdDifferentChild);
        assertThat(virtualIdDifferentChild).isNotEqualTo(realId);
        assertThat(virtualIdDifferentChild).isNotEqualTo(realId);
        assertThat(realId).isNotEqualTo(virtualIdDifferentChild);
        assertThat(realId).isNotEqualTo(virtualIdDifferentChild);
        assertNotEqualsIgnoreSession(virtualIdDifferentChild, realId);
        assertNotEqualsIgnoreSession(realId, virtualIdDifferentChild);


        final AutofillId virtualIdDifferentParent = new AutofillId(108, 1);
        final AutofillId virtualIdDifferentParent = new AutofillId(108, 1);
        assertThat(virtualIdDifferentParent).isNotEqualTo(virtualId);
        assertThat(virtualIdDifferentParent).isNotEqualTo(virtualId);
        assertThat(virtualId).isNotEqualTo(virtualIdDifferentParent);
        assertThat(virtualId).isNotEqualTo(virtualIdDifferentParent);
        assertNotEqualsIgnoreSession(virtualIdDifferentParent, virtualId);
        assertNotEqualsIgnoreSession(virtualId, virtualIdDifferentParent);
        assertThat(virtualIdDifferentParent).isNotEqualTo(virtualIdDifferentChild);
        assertThat(virtualIdDifferentParent).isNotEqualTo(virtualIdDifferentChild);
        assertThat(virtualIdDifferentChild).isNotEqualTo(virtualIdDifferentParent);
        assertThat(virtualIdDifferentChild).isNotEqualTo(virtualIdDifferentParent);
        assertNotEqualsIgnoreSession(virtualIdDifferentParent, virtualIdDifferentChild);
        assertNotEqualsIgnoreSession(virtualIdDifferentChild, virtualIdDifferentParent);


        final AutofillId virtualIdDifferentSession = new AutofillId(new AutofillId(42), 1L, 108);
        final AutofillId virtualIdDifferentSession = new AutofillId(new AutofillId(42), 1L, 108);
        assertThat(virtualIdDifferentSession).isNotEqualTo(virtualId);
        assertThat(virtualIdDifferentSession).isNotEqualTo(virtualId);
        assertThat(virtualId).isNotEqualTo(virtualIdDifferentSession);
        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(virtualIdDifferentSession).isNotEqualTo(realId);
        assertThat(realId).isNotEqualTo(virtualIdDifferentSession);
        assertThat(realId).isNotEqualTo(virtualIdDifferentSession);
        assertNotEqualsIgnoreSession(virtualIdDifferentSession, realId);
        assertNotEqualsIgnoreSession(realId, virtualIdDifferentSession);


        final AutofillId sameVirtualIdDifferentSession =
        final AutofillId sameVirtualIdDifferentSession =
                new AutofillId(new AutofillId(42), 1L, 108);
                new AutofillId(new AutofillId(42), 1L, 108);
        assertThat(sameVirtualIdDifferentSession).isEqualTo(virtualIdDifferentSession);
        assertThat(sameVirtualIdDifferentSession).isEqualTo(virtualIdDifferentSession);
        assertThat(virtualIdDifferentSession).isEqualTo(sameVirtualIdDifferentSession);
        assertThat(virtualIdDifferentSession).isEqualTo(sameVirtualIdDifferentSession);
        assertEqualsIgnoreSession(sameVirtualIdDifferentSession, virtualIdDifferentSession);
        assertEqualsIgnoreSession(virtualIdDifferentSession, sameVirtualIdDifferentSession);
        assertThat(sameVirtualIdDifferentSession.hashCode())
        assertThat(sameVirtualIdDifferentSession.hashCode())
                .isEqualTo(virtualIdDifferentSession.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) {
    private AutofillId cloneThroughParcel(AutofillId id) {
        Parcel parcel = Parcel.obtain();
        Parcel parcel = Parcel.obtain();


@@ -186,4 +222,18 @@ public class AutofillIdTest {
            parcel.recycle();
            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();
    }
}
}