[fenix] For https://github.com/mozilla-mobile/fenix/issues/22595 - Remove unused OnboardingAutomaticSignInViewHolder
parent
7faa6eb5f5
commit
c7aea80bb2
@ -1,83 +0,0 @@
|
||||
/* 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 org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding
|
||||
|
||||
import android.view.View
|
||||
import android.widget.Button
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import androidx.appcompat.content.res.AppCompatResources.getDrawable
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.MainScope
|
||||
import kotlinx.coroutines.launch
|
||||
import mozilla.components.service.fxa.manager.MigrationResult
|
||||
import mozilla.components.service.fxa.sharing.ShareableAccount
|
||||
import mozilla.components.support.ktx.android.view.putCompoundDrawablesRelativeWithIntrinsicBounds
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.components.FenixSnackbar
|
||||
import org.mozilla.fenix.components.metrics.Event
|
||||
import org.mozilla.fenix.databinding.OnboardingAutomaticSigninBinding
|
||||
import org.mozilla.fenix.ext.components
|
||||
|
||||
class OnboardingAutomaticSignInViewHolder(
|
||||
view: View,
|
||||
private val scope: CoroutineScope = MainScope()
|
||||
) : RecyclerView.ViewHolder(view) {
|
||||
|
||||
private lateinit var shareableAccount: ShareableAccount
|
||||
private val binding = OnboardingAutomaticSigninBinding.bind(view)
|
||||
private val headerText = binding.headerText
|
||||
|
||||
init {
|
||||
binding.fxaSignInButton.setOnClickListener {
|
||||
scope.launch {
|
||||
onClick(binding.fxaSignInButton)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun bind(account: ShareableAccount) {
|
||||
shareableAccount = account
|
||||
headerText.text = itemView.context.getString(
|
||||
R.string.onboarding_firefox_account_auto_signin_header_3, account.email
|
||||
)
|
||||
val icon = getDrawable(itemView.context, R.drawable.ic_onboarding_avatar_anonymous)
|
||||
headerText.putCompoundDrawablesRelativeWithIntrinsicBounds(start = icon)
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
internal suspend fun onClick(button: Button) {
|
||||
val context = button.context
|
||||
context.components.analytics.metrics.track(Event.OnboardingAutoSignIn)
|
||||
|
||||
button.text = context.getString(R.string.onboarding_firefox_account_signing_in)
|
||||
button.isEnabled = false
|
||||
|
||||
val accountManager = context.components.backgroundServices.accountManager
|
||||
when (accountManager.migrateFromAccount(shareableAccount)) {
|
||||
MigrationResult.WillRetry,
|
||||
MigrationResult.Success -> {
|
||||
// We consider both of these as a 'success'.
|
||||
}
|
||||
MigrationResult.Failure -> {
|
||||
// Failed to sign-in (e.g. bad credentials). Allow to try again.
|
||||
button.text = context.getString(R.string.onboarding_firefox_account_auto_signin_confirm)
|
||||
button.isEnabled = true
|
||||
FenixSnackbar.make(
|
||||
view = button,
|
||||
duration = Snackbar.LENGTH_SHORT,
|
||||
isDisplayedWithBrowserToolbar = false
|
||||
).setText(
|
||||
context.getString(R.string.onboarding_firefox_account_automatic_signin_failed)
|
||||
).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val LAYOUT_ID = R.layout.onboarding_automatic_signin
|
||||
}
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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/. -->
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M17.6655,14.2092C16.1843,12.4998 14.1333,14.8 11.9997,14.8C9.8675,14.8 7.8151,12.4998 6.3339,14.2092C5.9139,14.6936 5.8803,15.3992 6.2401,15.9284C7.4959,17.7764 9.5959,19 11.9997,19C14.4035,19 16.5035,17.7764 17.7593,15.9284C18.1205,15.3992 18.0855,14.6936 17.6655,14.2092M16.25,9.25C16.25,6.9026 14.3474,5 12,5C9.6526,5 7.75,6.9026 7.75,9.25C7.75,11.5974 9.6526,13.5 12,13.5C14.3474,13.5 16.25,11.5974 16.25,9.25Z" />
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M12,22C6.4772,22 2,17.5228 2,12C2,6.4772 6.4772,2 12,2C17.5228,2 22,6.4772 22,12C21.9939,17.5203 17.5203,21.9939 12,22L12,22ZM12,4C7.5817,4 4,7.5817 4,12C4,16.4183 7.5817,20 12,20C16.4183,20 20,16.4183 20,12C19.995,7.5838 16.4162,4.005 12,4Z" />
|
||||
</vector>
|
@ -1,42 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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/. -->
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/onboarding_card"
|
||||
style="@style/OnboardingCardDark"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginHorizontal="@dimen/home_item_horizontal_margin"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/header_text"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="14dp"
|
||||
android:drawablePadding="12dp"
|
||||
android:textAppearance="@style/Header16TextStyle"
|
||||
android:textColor="@color/onboarding_card_primary_text_dark"
|
||||
tools:text="@string/onboarding_firefox_account_auto_signin_header_3" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/fxa_sign_in_button"
|
||||
style="@style/NeutralButton"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:background="@drawable/button_background"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:gravity="center"
|
||||
android:padding="10dp"
|
||||
android:text="@string/onboarding_firefox_account_auto_signin_confirm"
|
||||
android:textAllCaps="false"
|
||||
android:textColor="@color/onboarding_card_button_text_dark"
|
||||
android:textSize="14sp"
|
||||
android:textStyle="bold"
|
||||
app:backgroundTint="@color/onboarding_card_button_background_dark" />
|
||||
</LinearLayout>
|
@ -1,120 +0,0 @@
|
||||
/* 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 org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import io.mockk.every
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkObject
|
||||
import io.mockk.verify
|
||||
import io.mockk.unmockkObject
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.test.runBlockingTest
|
||||
import mozilla.components.service.fxa.manager.MigrationResult
|
||||
import mozilla.components.service.fxa.sharing.ShareableAccount
|
||||
import mozilla.components.support.test.robolectric.testContext
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.components.BackgroundServices
|
||||
import org.mozilla.fenix.components.FenixSnackbar
|
||||
import org.mozilla.fenix.databinding.OnboardingAutomaticSigninBinding
|
||||
import org.mozilla.fenix.ext.components
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
|
||||
@RunWith(FenixRobolectricTestRunner::class)
|
||||
class OnboardingAutomaticSignInViewHolderTest {
|
||||
|
||||
private lateinit var backgroundServices: BackgroundServices
|
||||
private lateinit var snackbar: FenixSnackbar
|
||||
private lateinit var binding: OnboardingAutomaticSigninBinding
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
binding = OnboardingAutomaticSigninBinding.inflate(LayoutInflater.from(testContext))
|
||||
snackbar = mockk(relaxed = true)
|
||||
mockkObject(FenixSnackbar.Companion)
|
||||
|
||||
backgroundServices = testContext.components.backgroundServices
|
||||
every { FenixSnackbar.make(any(), any(), any(), any()) } returns snackbar
|
||||
}
|
||||
|
||||
@After
|
||||
fun teardown() {
|
||||
unmockkObject(FenixSnackbar.Companion)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `bind updates header text`() {
|
||||
val holder = OnboardingAutomaticSignInViewHolder(binding.root)
|
||||
holder.bind(
|
||||
mockk {
|
||||
every { email } returns "email@example.com"
|
||||
}
|
||||
)
|
||||
assertEquals(
|
||||
"You are signed in as email@example.com on another Firefox browser on this device. Would you like to sign in with this account?",
|
||||
binding.headerText.text
|
||||
)
|
||||
assertTrue(binding.fxaSignInButton.isEnabled)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `sign in on click - MigrationResult Success`() = runBlocking {
|
||||
val account = mockk<ShareableAccount> {
|
||||
every { email } returns "email@example.com"
|
||||
}
|
||||
coEvery {
|
||||
backgroundServices.accountManager.migrateFromAccount(account)
|
||||
} returns MigrationResult.Success
|
||||
|
||||
val holder = OnboardingAutomaticSignInViewHolder(binding.root, scope = this)
|
||||
holder.bind(account)
|
||||
holder.onClick(binding.fxaSignInButton)
|
||||
|
||||
assertEquals("Signing in…", binding.fxaSignInButton.text)
|
||||
assertFalse(binding.fxaSignInButton.isEnabled)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `sign in on click - MigrationResult WillRetry treated the same as Success`() = runBlocking {
|
||||
val account = mockk<ShareableAccount> {
|
||||
every { email } returns "email@example.com"
|
||||
}
|
||||
coEvery {
|
||||
backgroundServices.accountManager.migrateFromAccount(account)
|
||||
} returns MigrationResult.WillRetry
|
||||
|
||||
val holder = OnboardingAutomaticSignInViewHolder(binding.root, scope = this)
|
||||
holder.bind(account)
|
||||
holder.onClick(binding.fxaSignInButton)
|
||||
|
||||
assertEquals("Signing in…", binding.fxaSignInButton.text)
|
||||
assertFalse(binding.fxaSignInButton.isEnabled)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `show error if sign in fails`() = runBlockingTest {
|
||||
val account = mockk<ShareableAccount> {
|
||||
every { email } returns "email@example.com"
|
||||
}
|
||||
coEvery {
|
||||
backgroundServices.accountManager.migrateFromAccount(account)
|
||||
} returns MigrationResult.Failure
|
||||
|
||||
val holder = OnboardingAutomaticSignInViewHolder(binding.root, scope = this)
|
||||
holder.bind(account)
|
||||
holder.onClick(binding.fxaSignInButton)
|
||||
|
||||
assertEquals("Yes, sign me in", binding.fxaSignInButton.text)
|
||||
assertTrue(binding.fxaSignInButton.isEnabled)
|
||||
verify { snackbar.setText("Failed to sign-in") }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue