1package com.peachpasswords.ui.vault23import com.peachpasswords.vault.VaultEntry4import org.junit.Assert.assertEquals5import org.junit.Assert.assertFalse6import org.junit.Assert.assertTrue7import org.junit.Test89class EntryEditScreenTest {1011 1213 @Test fun title_bar_label_is_type_specific_for_new_entries() {14 assertEquals("new entry", editTitleBarLabel(isNew = true, type = "login"))15 assertEquals("new note", editTitleBarLabel(isNew = true, type = "note"))16 assertEquals("new card", editTitleBarLabel(isNew = true, type = "card"))17 }1819 @Test fun title_bar_label_is_edit_for_existing_entries() {20 assertEquals("edit", editTitleBarLabel(isNew = false, type = "login"))21 assertEquals("edit", editTitleBarLabel(isNew = false, type = "note"))22 }2324 @Test fun primary_action_label_matches_browser_add_view() {25 assertEquals("create", entryEditPrimaryActionLabel(isNew = true, saving = false))26 assertEquals("save", entryEditPrimaryActionLabel(isNew = false, saving = false))27 assertEquals("saving...", entryEditPrimaryActionLabel(isNew = true, saving = true))28 }2930 @Test fun save_is_disabled_until_browser_minimum_fields_are_present() {31 assertFalse(canSaveEditForm(EditForm(type = "login", title = "")))32 assertTrue(canSaveEditForm(EditForm(type = "login", title = "GitHub")))33 assertFalse(canSaveEditForm(EditForm(type = "card", title = "Visa", cardholderName = "", cardNumber = "4242")))34 assertFalse(canSaveEditForm(EditForm(type = "card", title = "Visa", cardholderName = "June", cardNumber = "")))35 assertTrue(canSaveEditForm(EditForm(type = "card", title = "Visa", cardholderName = "June", cardNumber = "4242")))36 }3738 3940 @Test fun new_note_routes_to_notes_mode() {41 assertTrue(shouldOpenNotesMode(isNew = true, type = "note"))42 43 assertFalse(shouldOpenNotesMode(isNew = false, type = "note"))44 assertFalse(shouldOpenNotesMode(isNew = true, type = "login"))45 assertFalse(shouldOpenNotesMode(isNew = true, type = "card"))46 }4748 4950 @Test fun new_entry_ghost_watermark_appears_on_title_type() {51 assertEquals(0f, editGhostWatermarkTarget(""), 0f)52 assertEquals(0f, editGhostWatermarkTarget(" "), 0f)53 assertEquals(0.03f, editGhostWatermarkTarget("GitHub"), 0f)54 }5556 5758 @Test fun new_login_hides_optional_totp_and_notes_until_requested() {59 assertFalse(shouldShowLoginTotp("login", revealed = false, totp = ""))60 assertFalse(shouldShowLoginNotes("login", revealed = false, notes = ""))61 assertTrue(shouldShowAddTotpLink("login", revealed = false, totp = ""))62 assertTrue(shouldShowAddNotesLink("login", revealed = false, notes = ""))63 }6465 @Test fun optional_login_fields_show_when_revealed_or_populated() {66 assertTrue(shouldShowLoginTotp("login", revealed = true, totp = ""))67 assertTrue(shouldShowLoginTotp("login", revealed = false, totp = "JBSWY3DPEHPK3PXP"))68 assertTrue(shouldShowLoginNotes("login", revealed = true, notes = ""))69 assertTrue(shouldShowLoginNotes("login", revealed = false, notes = "remember this"))70 assertFalse(shouldShowAddTotpLink("login", revealed = true, totp = ""))71 assertFalse(shouldShowAddNotesLink("login", revealed = false, notes = "remember this"))72 }7374 @Test fun identity_suggestions_split_username_and_email_like_browser() {75 val entries = listOf(76 identityEntry("a", username = "june", email = "[email protected]"),77 identityEntry("b", username = "june", email = null),78 identityEntry("c", username = "[email protected]", email = "[email protected]"),79 identityEntry("d", username = "[email protected]", email = null),80 )8182 assertEquals(listOf("june"), identitySuggestionsFor(entries, IdentityField.Username))83 assertEquals(84 listOf("[email protected]", "[email protected]", "[email protected]"),85 identitySuggestionsFor(entries, IdentityField.Email),86 )87 }8889 @Test fun generator_defaults_and_toggle_label_match_browser_add_view() {90 assertEquals(64, BROWSER_ADD_GENERATOR_LENGTH)91 assertEquals("generate", generatorToggleLabel(open = false))92 assertEquals("hide generator", generatorToggleLabel(open = true))93 }9495 9697 @Test fun new_card_number_auto_formats() {98 assertEquals("4242 4242 4242 4242", CardFieldUtils.formatPan("4242424242424242"))99 100 assertEquals("3782 822463 10005", CardFieldUtils.formatPan("378282246310005"))101 }102103 @Test fun card_network_detection_matches_browser_add_view_badges() {104 assertEquals("visa", CardFieldUtils.detectNetwork("4242424242424242"))105 assertEquals("mastercard", CardFieldUtils.detectNetwork("5555555555554444"))106 assertEquals("mastercard", CardFieldUtils.detectNetwork("2221000000000009"))107 assertEquals("amex", CardFieldUtils.detectNetwork("378282246310005"))108 assertEquals("discover", CardFieldUtils.detectNetwork("6011111111111117"))109 assertEquals("discover", CardFieldUtils.detectNetwork("6500000000000002"))110 assertEquals("unknown", CardFieldUtils.detectNetwork("6000000000000000"))111 }112113 @Test fun new_card_expiry_auto_formats_mm_yy() {114 assertEquals("12", CardFieldUtils.formatExpiry("12"))115 assertEquals("12/28", CardFieldUtils.formatExpiry("1228"))116 assertEquals("12/28", CardFieldUtils.formatExpiry("122899")) 117 }118119 @Test fun new_card_expiry_rejects_invalid_month() {120 121 assertFalse(CardFieldUtils.isExpiryValid("13/28", currentYear2 = 26, currentMonth = 6))122 assertFalse(CardFieldUtils.isExpiryValid("00/28", currentYear2 = 26, currentMonth = 6))123 assertTrue(CardFieldUtils.isExpiryValid("12/28", currentYear2 = 26, currentMonth = 6))124 125 assertFalse(CardFieldUtils.isExpiryValid("12/24", currentYear2 = 26, currentMonth = 6))126 }127128 private fun identityEntry(id: String, username: String?, email: String?) = VaultEntry(129 id = id,130 title = id,131 type = "login",132 username = username,133 email = email,134 createdAt = 0,135 modifiedAt = 0,136 )137}