AutofillNavRepositoryTest.kt

2.3 KB · raw

1package com.peachpasswords.autofill23import org.junit.Assert.assertEquals4import org.junit.Assert.assertNull5import org.junit.Test67/**8 * AutofillNavRepository carries the one-shot "open the new-entry editor" request9 * from the autofill picker through MainActivity to PeachAppNavHost. The NavHost10 * consumes the request as part of its LaunchedEffect, so the repository must11 * surface the request exactly once before clearing — re-firing the LockScreen12 * post-unlock branch after the consume is what made the new-entry redirect13 * land on the vault list instead of the editor.14 */15class AutofillNavRepositoryTest {1617    @Test fun request_makes_payload_visible_to_collectors() {18        val repo = AutofillNavRepository()19        assertNull(repo.newEntryRequest.value)20        repo.requestNewEntry(url = "https://github.com/login", username = "june", email = null)21        val req = repo.newEntryRequest.value22        assertEquals("https://github.com/login", req?.url)23        assertEquals("june", req?.username)24        assertNull(req?.email)25    }2627    @Test fun blank_optional_fields_are_dropped_from_the_payload() {28        // Hosts that don't surface a typed username/email pass empty strings;29        // these must not pollute the new-entry editor's prefill.30        val repo = AutofillNavRepository()31        repo.requestNewEntry(url = "", username = "", email = "")32        val req = repo.newEntryRequest.value33        assertEquals(null, req?.url)34        assertEquals(null, req?.username)35        assertEquals(null, req?.email)36    }3738    @Test fun consume_clears_the_request_so_lock_screen_does_not_re_fire_on_post_unlock() {39        // Regression: the v2.14.5 guard in LockScreen was keyed on `autofillPending`,40        // so when the NavHost's LaunchedEffect consumed the request the key41        // flipped and LockScreen re-fired `onUnlocked(...)`, dropping the user42        // on the vault list. The repository must surface exactly one non-null43        // value across the request/consume cycle, never two.44        val repo = AutofillNavRepository()45        repo.requestNewEntry(url = "https://example.com", username = null, email = null)46        assertEquals("https://example.com", repo.newEntryRequest.value?.url)47        repo.consumeNewEntryRequest()48        assertNull(repo.newEntryRequest.value)49    }50}