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

Commit d7d77660 authored by AI test gen's avatar AI test gen Committed by Ang Li
Browse files

Add unit test for ImeSubtypeListItem equals and hashCode

Adds a new test method, `testEqualsAndHashCode`, to verify the correctness of the `equals` and `hashCode` implementations in the `ImeSubtypeListItem` class.

The test covers the following scenarios:
- Equality with self, null, and different object types.
- Equality and hash code consistency for two equivalent items.
- Inequality for items where each of the fields is different, ensuring all fields are checked.

Please help fill out the survey for feedback: https://docs.google.com/forms/d/e/1FAIpQLSeKFKpHImCAqZIa_OR801cw72HQUreM2oGM25C3mKKT2tBFnw/viewform?usp=pp_url&entry.1586624956=ag/35402599

Please feel free to use your domain knowledge to make changes to the generated tests to make it more valuable for your team.
Original Change: ag/34763598
Test: ATP tests passed http://go/forrest-run/L87400030017394713
Bug: 431235865
Flag: TEST_ONLY

Change-Id: Ib5ac1da458196338e8bd1d0a9f9e2d11bc72b1c5
parent 3e46e14c
Loading
Loading
Loading
Loading
+99 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import static com.android.server.inputmethod.InputMethodUtils.NOT_A_SUBTYPE_INDE

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;

@@ -543,6 +544,104 @@ public final class InputMethodSubtypeSwitchingControllerTest {
                0, subtype0.compareTo(subtype1));
    }

    /** Verifies the {@code ImeSubtypeListItem.equals} and {@code ImeSubtypeListItem.hashCode}. */
    @Test
    public void testEqualsAndHashCode() {
        final var component1 = new ComponentName("com.example.ime1", "Ime");
        final var subtypes1 = new ArrayList<InputMethodSubtype>();
        subtypes1.add(createTestSubtype(SYSTEM_LOCALE));
        final var imi1 =
                createTestImi(component1, subtypes1, true /* supportsSwitchingToNextInputMethod */);

        final var item1 =
                new ImeSubtypeListItem(
                        "Ime", "Subtype", "Layout", imi1, 0 /* subtypeIndex */,
                        true /* showInImeSwitcherMenu */, false /* isAuxiliary */,
                        true /* suitableForHardware */);

        // Test equality with null.
        assertNotNull("Item should not be null.", item1);

        // Test equals and hashCode with an equivalent item.
        final var item2 =
                new ImeSubtypeListItem(
                        "Ime", "Subtype", "Layout", imi1, 0 /* subtypeIndex */,
                        true /* showInImeSwitcherMenu */, false /* isAuxiliary */,
                        true /* suitableForHardware */);
        assertEquals("Equivalent items should be equal.", item1, item2);
        assertEquals("Hash code of equivalent items should be equal.",
                item1.hashCode(), item2.hashCode());

        // Test equals with different items.
        final var diffImeName =
                new ImeSubtypeListItem(
                        "OtherIme", "Subtype", "Layout", imi1, 0 /* subtypeIndex */,
                        true /* showInImeSwitcherMenu */, false /* isAuxiliary */,
                        true /* suitableForHardware */);
        assertNotEquals("Items with different IME names should not be equal.",
                item1, diffImeName);

        final var diffSubtypeName =
                new ImeSubtypeListItem(
                        "Ime", "OtherSubtype", "Layout", imi1, 0 /* subtypeIndex */,
                        true /* showInImeSwitcherMenu */, false /* isAuxiliary */,
                        true /* suitableForHardware */);
        assertNotEquals("Items with different subtype names should not be equal.",
                item1, diffSubtypeName);

        final var diffLayoutName =
                new ImeSubtypeListItem(
                        "Ime", "Subtype", "OtherLayout", imi1, 0 /* subtypeIndex */,
                        true /* showInImeSwitcherMenu */, false /* isAuxiliary */,
                        true /* suitableForHardware */);
        assertNotEquals("Items with different layout names should not be equal.",
                item1, diffLayoutName);

        final var component2 = new ComponentName("com.example.ime2", "Ime");
        final var subtypes2 = new ArrayList<InputMethodSubtype>();
        subtypes2.add(createTestSubtype(SYSTEM_LOCALE));
        final var imi2 =
                createTestImi(component2, subtypes2, true /* supportsSwitchingToNextInputMethod */);
        final var diffImi =
                new ImeSubtypeListItem(
                        "Ime", "Subtype", "Layout", imi2, 0 /* subtypeIndex */,
                        true /* showInImeSwitcherMenu */, false /* isAuxiliary */,
                        true /* suitableForHardware */);
        assertNotEquals("Items with different IMEs should not be equal.", item1, diffImi);

        final var diffSubtypeIndex =
                new ImeSubtypeListItem(
                        "Ime", "Subtype", "Layout", imi1, 1 /* subtypeIndex */,
                        true /* showInImeSwitcherMenu */, false /* isAuxiliary */,
                        true /* suitableForHardware */);
        assertNotEquals("Items with different subtype indices should not be equal.",
                item1, diffSubtypeIndex);

        final var diffShowInMenu =
                new ImeSubtypeListItem(
                        "Ime", "Subtype", "Layout", imi1, 0 /* subtypeIndex */,
                        false /* showInImeSwitcherMenu */, false /* isAuxiliary */,
                        true /* suitableForHardware */);
        assertNotEquals("Items with different show in menu flags should not be equal.",
                item1, diffShowInMenu);

        final var diffAuxiliary =
                new ImeSubtypeListItem(
                        "Ime", "Subtype", "Layout", imi1, 0 /* subtypeIndex */,
                        true /* showInImeSwitcherMenu */, true /* isAuxiliary */,
                        true /* suitableForHardware */);
        assertNotEquals("Items with different auxiliary flags should not be equal.",
                item1, diffAuxiliary);

        final var diffSuitableForHardware =
                new ImeSubtypeListItem(
                        "Ime", "Subtype", "Layout", imi1, 0 /* subtypeIndex */,
                        true /* showInImeSwitcherMenu */, false /* isAuxiliary */,
                        false /* suitableForHardware */);
        assertNotEquals("Items with different suitable for hardware flags should not be equal.",
                item1, diffSuitableForHardware);
    }

    /**
     * Moves the given item from its current position to the front of the given list.
     *