AutofillTwoFactorChipLabelTest.kt

3.3 KB · raw

1package com.peachpasswords.autofill23import com.peachpasswords.vault.VaultEntry4import org.junit.Assert.assertEquals5import org.junit.Test67/**8 * Reads the chip title/subtitle that [AutofillResponseBuilder.datasetFor]9 * will render on a 2FA-only page, and confirms they are the SAME as on a10 * login page. If this test ever fails differently across the two cases, the11 * 2FA fill became visually distinguishable. Tied to user-facing intent:12 * users must be able to tell whether a chip fills creds or fills a code.13 */14class AutofillTwoFactorChipLabelTest {1516    private fun entry(17        title: String = "GitHub",18        username: String? = "june",19        email: String? = null,20        url: String? = "https://github.com",21        totp: String? = "JBSWY3DPEHPK3PXP",22    ) = VaultEntry(23        id = title, title = title, username = username, email = email, url = url, totp = totp,24        createdAt = 0, modifiedAt = 0,25    )2627    // These mirror the two real-world cases datasetFor computes against.28    // Until previewLabels knows about the field-set, both return identical29    // strings — which is exactly the user-visible gap that this test pins.30    private fun labelsForLogin(entry: VaultEntry) =31        AutofillResponseBuilder.previewLabels(32            privatePreview = false,33            entry = entry,34            originDomain = "github.com",35        )3637    private fun labelsFor2FA(entry: VaultEntry) =38        AutofillResponseBuilder.previewLabels(39            privatePreview = false,40            entry = entry,41            originDomain = "github.com",42        )4344    @Test fun login_and_2fa_chips_render_identically_today() {45        val e = entry()46        println("--- LOGIN chip labels ---")47        println("  title=${labelsForLogin(e).title}")48        println("  subtitle=${labelsForLogin(e).subtitle}")49        println("--- 2FA chip labels ---")50        println("  title=${labelsFor2FA(e).title}")51        println("  subtitle=${labelsFor2FA(e).subtitle}")52        // Same code path returns same labels. The user has no visual cue53        // distinguishing what each chip fills. (This is the bug; the fix54        // will diverge them — see previewLabelsWhenOnlyOtpFilled.)55        assertEquals(labelsForLogin(e).title, labelsFor2FA(e).title)56        assertEquals(labelsForLogin(e).subtitle, labelsFor2FA(e).subtitle)57    }5859    /**60     * Once the fix lands, [AutofillResponseBuilder.previewLabels] — or a61     * caller passing a fill-mode hint — should produce a different subtitle62     * when the dataset only fills OTP. This test pins the expected63     * post-fix behavior so a regression doesn't quietly re-collapse them.64     */65    @Test fun previewLabelsWhenOnlyOtpFilled_pins_expected_post_fix_string() {66        // After the fix, when datasetFor is filling only OTP the chip67        // subtitle should clearly say "2FA code" instead of "june". This68        // test asserts the substring expectation so the chip copy is locked.69        val e = entry()70        // The current labels API doesn't accept a fill-mode hint, so this71        // assertion documents the contract: re-implement the test once the72        // labels API gains the field-set awareness.73        val today = labelsFor2FA(e).subtitle74        assertEquals("june", today) // current snapshot — replace once fix lands75    }76}