From 0e067a0e53d8d22821bb1d362f069a0c086c09a1 Mon Sep 17 00:00:00 2001 From: mcarare Date: Fri, 14 May 2021 14:56:28 +0300 Subject: [PATCH] [components] Closes https://github.com/mozilla-mobile/android-components/issues/10263: Add Credit Card native name to type mapping. Also added unit tests. --- .../mozilla/components/service/fxa/sync/Types.kt | 1 + .../components/service/fxa/sync/TypesTest.kt | 80 ++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 mobile/android/android-components/components/service/firefox-accounts/src/test/java/mozilla/components/service/fxa/sync/TypesTest.kt diff --git a/mobile/android/android-components/components/service/firefox-accounts/src/main/java/mozilla/components/service/fxa/sync/Types.kt b/mobile/android/android-components/components/service/firefox-accounts/src/main/java/mozilla/components/service/fxa/sync/Types.kt index c27eb629e082..82dd26c51d42 100644 --- a/mobile/android/android-components/components/service/firefox-accounts/src/main/java/mozilla/components/service/fxa/sync/Types.kt +++ b/mobile/android/android-components/components/service/firefox-accounts/src/main/java/mozilla/components/service/fxa/sync/Types.kt @@ -32,6 +32,7 @@ internal fun String.toSyncEngine(): SyncEngine { "bookmarks" -> SyncEngine.Bookmarks "passwords" -> SyncEngine.Passwords "tabs" -> SyncEngine.Tabs + "creditcards" -> SyncEngine.CreditCards // This handles a case of engines that we don't yet model in SyncEngine. else -> SyncEngine.Other(this) } diff --git a/mobile/android/android-components/components/service/firefox-accounts/src/test/java/mozilla/components/service/fxa/sync/TypesTest.kt b/mobile/android/android-components/components/service/firefox-accounts/src/test/java/mozilla/components/service/fxa/sync/TypesTest.kt new file mode 100644 index 000000000000..235164e06c41 --- /dev/null +++ b/mobile/android/android-components/components/service/firefox-accounts/src/test/java/mozilla/components/service/fxa/sync/TypesTest.kt @@ -0,0 +1,80 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package mozilla.components.service.fxa.sync + +import mozilla.components.service.fxa.SyncEngine +import org.junit.Assert.assertEquals +import org.junit.Rule +import org.junit.Test +import org.junit.rules.ExpectedException + +class TypesTest { + + @Test + fun `raw strings are correctly mapped to SyncEngine types`() { + assertEquals(SyncEngine.Tabs, "tabs".toSyncEngine()) + assertEquals(SyncEngine.History, "history".toSyncEngine()) + assertEquals(SyncEngine.Bookmarks, "bookmarks".toSyncEngine()) + assertEquals(SyncEngine.Passwords, "passwords".toSyncEngine()) + assertEquals(SyncEngine.CreditCards, "creditcards".toSyncEngine()) + assertEquals(SyncEngine.Other("other"), "other".toSyncEngine()) + } + + @Test + fun `a list of raw strings are correctly mapped to a set of SyncEngine engines`() { + assertEquals( + setOf(SyncEngine.History), + listOf("history").toSyncEngines() + ) + + assertEquals( + setOf(SyncEngine.Bookmarks, SyncEngine.History), + listOf("history", "bookmarks").toSyncEngines() + ) + + assertEquals( + setOf(SyncEngine.History, SyncEngine.CreditCards), + listOf("history", "creditcards").toSyncEngines() + ) + + assertEquals( + setOf(SyncEngine.Other("other"), SyncEngine.CreditCards), + listOf("other", "creditcards").toSyncEngines() + ) + + assertEquals( + setOf(SyncEngine.Bookmarks, SyncEngine.History), + listOf("history", "bookmarks", "bookmarks", "history").toSyncEngines() + ) + } + + @Test + fun `raw strings are correctly mapped to SyncReason types`() { + assertEquals(SyncReason.Startup, "startup".toSyncReason()) + assertEquals(SyncReason.FirstSync, "first_sync".toSyncReason()) + assertEquals(SyncReason.Scheduled, "scheduled".toSyncReason()) + assertEquals(SyncReason.User, "user".toSyncReason()) + assertEquals(SyncReason.EngineChange, "engine_change".toSyncReason()) + } + + @Test + fun `SyncReason types are correctly mapped to strings`() { + assertEquals("startup", SyncReason.Startup.asString()) + assertEquals("first_sync", SyncReason.FirstSync.asString()) + assertEquals("scheduled", SyncReason.Scheduled.asString()) + assertEquals("user", SyncReason.User.asString()) + assertEquals("engine_change", SyncReason.EngineChange.asString()) + } + + @Rule @JvmField + val exceptionRule: ExpectedException = ExpectedException.none() + + @Test + fun `invalid sync reason raw strings throw IllegalStateException when mapped`() { + exceptionRule.expect(IllegalStateException::class.java) + exceptionRule.expectMessage("Invalid SyncReason: some_reason") + "some_reason".toSyncReason() + } +} -- 2.11.4.GIT