Skip to content

Commit

Permalink
For mozilla-mobile#17917: Use View binding in Onboarding Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codrut.topliceanu authored and pkirakosyan committed Sep 15, 2021
1 parent 9d8f7b2 commit 624cb42
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
package org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding

import android.view.LayoutInflater
import android.view.View
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.android.synthetic.main.onboarding_automatic_signin.view.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runBlockingTest
Expand All @@ -28,21 +26,21 @@ 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

@ExperimentalCoroutinesApi
@RunWith(FenixRobolectricTestRunner::class)
class OnboardingAutomaticSignInViewHolderTest {

private lateinit var view: View
private lateinit var backgroundServices: BackgroundServices
private lateinit var snackbar: FenixSnackbar
private lateinit var binding: OnboardingAutomaticSigninBinding

@Before
fun setup() {
view = LayoutInflater.from(testContext)
.inflate(OnboardingAutomaticSignInViewHolder.LAYOUT_ID, null)
binding = OnboardingAutomaticSigninBinding.inflate(LayoutInflater.from(testContext))
snackbar = mockk(relaxed = true)
mockkObject(FenixSnackbar.Companion)

Expand All @@ -57,17 +55,17 @@ class OnboardingAutomaticSignInViewHolderTest {

@Test
fun `bind updates header text`() {
val holder = OnboardingAutomaticSignInViewHolder(view)
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?",
view.header_text.text
binding.headerText.text
)
assertTrue(view.fxa_sign_in_button.isEnabled)
assertTrue(binding.fxaSignInButton.isEnabled)
}

@Test
Expand All @@ -79,12 +77,12 @@ class OnboardingAutomaticSignInViewHolderTest {
backgroundServices.accountManager.migrateFromAccount(account)
} returns MigrationResult.Success

val holder = OnboardingAutomaticSignInViewHolder(view, scope = this)
val holder = OnboardingAutomaticSignInViewHolder(binding.root, scope = this)
holder.bind(account)
holder.onClick(view.fxa_sign_in_button)
holder.onClick(binding.fxaSignInButton)

assertEquals("Signing in…", view.fxa_sign_in_button.text)
assertFalse(view.fxa_sign_in_button.isEnabled)
assertEquals("Signing in…", binding.fxaSignInButton.text)
assertFalse(binding.fxaSignInButton.isEnabled)
}

@Test
Expand All @@ -96,12 +94,12 @@ class OnboardingAutomaticSignInViewHolderTest {
backgroundServices.accountManager.migrateFromAccount(account)
} returns MigrationResult.WillRetry

val holder = OnboardingAutomaticSignInViewHolder(view, scope = this)
val holder = OnboardingAutomaticSignInViewHolder(binding.root, scope = this)
holder.bind(account)
holder.onClick(view.fxa_sign_in_button)
holder.onClick(binding.fxaSignInButton)

assertEquals("Signing in…", view.fxa_sign_in_button.text)
assertFalse(view.fxa_sign_in_button.isEnabled)
assertEquals("Signing in…", binding.fxaSignInButton.text)
assertFalse(binding.fxaSignInButton.isEnabled)
}

@Test
Expand All @@ -113,12 +111,12 @@ class OnboardingAutomaticSignInViewHolderTest {
backgroundServices.accountManager.migrateFromAccount(account)
} returns MigrationResult.Failure

val holder = OnboardingAutomaticSignInViewHolder(view, scope = this)
val holder = OnboardingAutomaticSignInViewHolder(binding.root, scope = this)
holder.bind(account)
holder.onClick(view.fxa_sign_in_button)
holder.onClick(binding.fxaSignInButton)

assertEquals("Yes, sign me in", view.fxa_sign_in_button.text)
assertTrue(view.fxa_sign_in_button.isEnabled)
assertEquals("Yes, sign me in", binding.fxaSignInButton.text)
assertTrue(binding.fxaSignInButton.isEnabled)
verify { snackbar.setText("Failed to sign-in") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,33 @@
package org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding

import android.view.LayoutInflater
import android.view.View
import io.mockk.mockk
import io.mockk.verify
import kotlinx.android.synthetic.main.onboarding_finish.view.*
import mozilla.components.support.test.robolectric.testContext
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.databinding.OnboardingFinishBinding
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.home.sessioncontrol.OnboardingInteractor

@RunWith(FenixRobolectricTestRunner::class)
class OnboardingFinishViewHolderTest {

private lateinit var view: View
private lateinit var binding: OnboardingFinishBinding
private lateinit var interactor: OnboardingInteractor

@Before
fun setup() {
view = LayoutInflater.from(testContext)
.inflate(OnboardingFinishViewHolder.LAYOUT_ID, null)
binding = OnboardingFinishBinding.inflate(LayoutInflater.from(testContext))
interactor = mockk(relaxed = true)
}

@Test
fun `call interactor on click`() {
OnboardingFinishViewHolder(view, interactor)
OnboardingFinishViewHolder(binding.root, interactor)

view.finish_button.performClick()
binding.finishButton.performClick()
verify { interactor.onStartBrowsingClicked() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,32 @@
package org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding

import android.view.LayoutInflater
import android.view.View
import kotlinx.android.synthetic.main.onboarding_header.view.*
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.R
import org.mozilla.fenix.databinding.OnboardingHeaderBinding
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner

@RunWith(FenixRobolectricTestRunner::class)
class OnboardingHeaderViewHolderTest {

private lateinit var view: View
private lateinit var binding: OnboardingHeaderBinding

@Before
fun setup() {
view = LayoutInflater.from(testContext)
.inflate(OnboardingHeaderViewHolder.LAYOUT_ID, null)
binding = OnboardingHeaderBinding.inflate(LayoutInflater.from(testContext))
}

@Test
fun `bind header text`() {
val appName = testContext.getString(R.string.app_name)
val welcomeMessage = testContext.getString(R.string.onboarding_header, appName)

OnboardingHeaderViewHolder(view)
OnboardingHeaderViewHolder(binding.root)

assertEquals(welcomeMessage, view.header_text.text)
assertEquals(welcomeMessage, binding.headerText.text)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.NavController
import androidx.navigation.Navigation
Expand All @@ -14,34 +13,33 @@ import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import io.mockk.verify
import kotlinx.android.synthetic.main.onboarding_manual_signin.view.*
import mozilla.components.support.test.robolectric.testContext
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.R
import org.mozilla.fenix.databinding.OnboardingManualSigninBinding
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.home.HomeFragmentDirections

@RunWith(FenixRobolectricTestRunner::class)
class OnboardingManualSignInViewHolderTest {

private lateinit var view: View
private lateinit var binding: OnboardingManualSigninBinding
private lateinit var navController: NavController
private lateinit var itemView: ViewGroup

@Before
fun setup() {
view = LayoutInflater.from(testContext)
.inflate(OnboardingManualSignInViewHolder.LAYOUT_ID, null)
binding = OnboardingManualSigninBinding.inflate(LayoutInflater.from(testContext))
navController = mockk(relaxed = true)
itemView = mockk(relaxed = true)

mockkStatic(Navigation::class)
every { itemView.context } returns testContext
every { Navigation.findNavController(view) } returns navController
every { Navigation.findNavController(binding.root) } returns navController
}

@After
Expand All @@ -51,18 +49,18 @@ class OnboardingManualSignInViewHolderTest {

@Test
fun `bind header text`() {
OnboardingManualSignInViewHolder(view).bind()
OnboardingManualSignInViewHolder(binding.root).bind()
val string = testContext.getString(R.string.onboarding_account_sign_in_header_1)
assertEquals(
string,
view.header_text.text
binding.headerText.text
)
}

@Test
fun `navigate on click`() {
OnboardingManualSignInViewHolder(view)
view.fxa_sign_in_button.performClick()
OnboardingManualSignInViewHolder(binding.root)
binding.fxaSignInButton.performClick()

verify { navController.navigate(HomeFragmentDirections.actionGlobalTurnOnSync()) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,36 @@
package org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding

import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.view.ContextThemeWrapper
import io.mockk.mockk
import io.mockk.verify
import kotlinx.android.synthetic.main.onboarding_private_browsing.view.*
import mozilla.components.support.test.robolectric.testContext
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.R
import org.mozilla.fenix.databinding.OnboardingPrivateBrowsingBinding
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.home.sessioncontrol.OnboardingInteractor

@RunWith(FenixRobolectricTestRunner::class)
class OnboardingPrivacyBrowsingViewHolderTest {

private lateinit var view: View
private lateinit var binding: OnboardingPrivateBrowsingBinding
private lateinit var interactor: OnboardingInteractor

@Before
fun setup() {
val context = ContextThemeWrapper(testContext, R.style.NormalTheme)
view = LayoutInflater.from(context)
.inflate(OnboardingPrivateBrowsingViewHolder.LAYOUT_ID, null)
binding = OnboardingPrivateBrowsingBinding.inflate(LayoutInflater.from(context))
interactor = mockk(relaxed = true)
}

@Test
fun `call interactor on click`() {
OnboardingPrivateBrowsingViewHolder(view, interactor)
OnboardingPrivateBrowsingViewHolder(binding.root, interactor)

view.open_settings_button.performClick()
binding.openSettingsButton.performClick()
verify { interactor.onOpenSettingsClicked() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,36 @@
package org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding

import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.view.ContextThemeWrapper
import io.mockk.mockk
import io.mockk.verify
import kotlinx.android.synthetic.main.onboarding_privacy_notice.view.*
import mozilla.components.support.test.robolectric.testContext
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.R
import org.mozilla.fenix.databinding.OnboardingPrivacyNoticeBinding
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.home.sessioncontrol.OnboardingInteractor

@RunWith(FenixRobolectricTestRunner::class)
class OnboardingPrivacyNoticeViewHolderTest {

private lateinit var view: View
private lateinit var binding: OnboardingPrivacyNoticeBinding
private lateinit var interactor: OnboardingInteractor

@Before
fun setup() {
val context = ContextThemeWrapper(testContext, R.style.NormalTheme)
view = LayoutInflater.from(context)
.inflate(OnboardingPrivacyNoticeViewHolder.LAYOUT_ID, null)
binding = OnboardingPrivacyNoticeBinding.inflate(LayoutInflater.from(context))
interactor = mockk(relaxed = true)
}

@Test
fun `call interactor on click`() {
OnboardingPrivacyNoticeViewHolder(view, interactor)
OnboardingPrivacyNoticeViewHolder(binding.root, interactor)

view.read_button.performClick()
binding.readButton.performClick()
verify { interactor.onReadPrivacyNoticeClicked() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,32 @@
package org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding

import android.view.LayoutInflater
import android.view.View
import kotlinx.android.synthetic.main.onboarding_section_header.view.*
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.databinding.OnboardingSectionHeaderBinding
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner

@RunWith(FenixRobolectricTestRunner::class)
class OnboardingSectionHeaderViewHolderTest {

private lateinit var view: View
private lateinit var binding: OnboardingSectionHeaderBinding

@Before
fun setup() {
view = LayoutInflater.from(testContext)
.inflate(OnboardingSectionHeaderViewHolder.LAYOUT_ID, null)
binding = OnboardingSectionHeaderBinding.inflate(LayoutInflater.from(testContext))
}

@Test
fun `bind text`() {
val holder = OnboardingSectionHeaderViewHolder(view)
val holder = OnboardingSectionHeaderViewHolder(binding.root)
holder.bind { "Hello world" }

assertEquals(
"Hello world",
view.section_header_text.text
binding.sectionHeaderText.text
)
}
}
Loading

0 comments on commit 624cb42

Please sign in to comment.