AutofillPreviewLabelsTest.kt

4.3 KB · raw

1package com.peachpasswords.autofill23import com.peachpasswords.autofill.AutofillResponseBuilder.FillMode4import com.peachpasswords.vault.VaultEntry5import org.junit.Assert.assertEquals6import org.junit.Test78class AutofillPreviewLabelsTest {9    private fun entry(10        title: String = "GitHub",11        username: String? = "june",12        email: String? = null,13        url: String? = "https://github.com",14    ) = VaultEntry(15        id = title, title = title, username = username, email = email, url = url,16        createdAt = 0, modifiedAt = 0,17    )1819    @Test fun normal_mode_shows_entry_title_and_identity() {20        val labels = AutofillResponseBuilder.previewLabels(false, entry(), "github.com")21        assertEquals("GitHub", labels.title)22        assertEquals("june", labels.subtitle)23        assertEquals(FillMode.LOGIN, labels.fillMode)24    }2526    @Test fun normal_mode_falls_back_email_then_domain() {27        val emailOnly = entry(username = null, email = "[email protected]")28        assertEquals("[email protected]", AutofillResponseBuilder.previewLabels(false, emailOnly, null).subtitle)29        val bare = entry(username = null, email = null)30        assertEquals("github.com", AutofillResponseBuilder.previewLabels(false, bare, null).subtitle)31    }3233    @Test fun private_mode_hides_title_and_identity() {34        val labels = AutofillResponseBuilder.previewLabels(true, entry(), "github.com")35        assertEquals("Peach", labels.title)36        assertEquals("github.com", labels.subtitle)37    }3839    @Test fun private_mode_without_origin_has_blank_subtitle() {40        val labels = AutofillResponseBuilder.previewLabels(true, entry(), null)41        assertEquals("Peach", labels.title)42        assertEquals("", labels.subtitle)43    }4445    // --- OTP_FILL fillMode: the chip surfaces the actual TOTP code ---4647    @Test fun otpFill_mode_shows_chunked_code_as_title_and_entry_as_subtitle() {48        val labels = AutofillResponseBuilder.previewLabels(49            privatePreview = false,50            entry = entry(),51            originDomain = "github.com",52            fillMode = FillMode.OTP_FILL,53            otpCode = "482901",54        )55        assertEquals("482 901", labels.title)56        assertEquals("2FA", labels.subtitle)57        assertEquals("GitHub", labels.iconSource)58        assertEquals(FillMode.OTP_FILL, labels.fillMode)59    }6061    @Test fun otpFill_mode_passes_through_long_codes_unchunked() {62        val labels = AutofillResponseBuilder.previewLabels(63            privatePreview = false,64            entry = entry(),65            originDomain = null,66            fillMode = FillMode.OTP_FILL,67            otpCode = "JBSWY3DPEHPK3PXP",68        )69        // 8-char Steam-style code — chunk only fires on <=6 digits, this passes through.70        assertEquals("JBSWY3DPEHPK3PXP", labels.title)71        assertEquals("2FA", labels.subtitle)72    }7374    @Test fun otpFill_mode_never_leaks_digits_in_private_preview() {75        val labels = AutofillResponseBuilder.previewLabels(76            privatePreview = true,77            entry = entry(),78            originDomain = "github.com",79            fillMode = FillMode.OTP_FILL,80            otpCode = "482901",81        )82        // Privacy always wins over fillMode — chip says "Peach" not the digits.83        assertEquals("Peach", labels.title)84        assertEquals("github.com", labels.subtitle)85    }8687    @Test(expected = IllegalArgumentException::class)88    fun otpFill_mode_requires_otpCode() {89        AutofillResponseBuilder.previewLabels(90            privatePreview = false,91            entry = entry(),92            originDomain = "github.com",93            fillMode = FillMode.OTP_FILL,94            otpCode = null,95        )96    }9798    @Test fun chunkOtpForDisplay_groups_only_6digit_codes_with_space() {99        // 6-digit only: grouped into 3+3.100        assertEquals("482 901", AutofillResponseBuilder.chunkOtpForDisplay("482901"))101        // 5-digit and 7-digit: pass through (no sane chunking).102        assertEquals("12345", AutofillResponseBuilder.chunkOtpForDisplay("12345"))103        assertEquals("1234567", AutofillResponseBuilder.chunkOtpForDisplay("1234567"))104        // Alphanumeric (Steam-style base32): pass through unchanged — never strip letters.105        assertEquals("JBSWY3DPEHPK3PXP", AutofillResponseBuilder.chunkOtpForDisplay("JBSWY3DPEHPK3PXP"))106    }107}