1
0
Fork 0

Test viewholders in home.sessioncontrol (#12524)

master
Tiger Oakes 2020-07-14 10:31:50 -07:00 committed by GitHub
parent f424656575
commit e9fb1a61c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 322 additions and 4 deletions

View File

@ -9,17 +9,18 @@ import android.content.Context
import android.view.MotionEvent
import android.view.View
import android.widget.PopupWindow
import androidx.appcompat.content.res.AppCompatResources.getDrawable
import kotlinx.android.synthetic.main.top_site_item.*
import kotlinx.android.synthetic.main.top_site_item.view.*
import mozilla.components.browser.menu.BrowserMenuBuilder
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
import mozilla.components.feature.top.sites.TopSite
import org.mozilla.fenix.R
import org.mozilla.fenix.utils.view.ViewHolder
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.loadIntoView
import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor
import org.mozilla.fenix.settings.SupportUtils
import org.mozilla.fenix.utils.view.ViewHolder
class TopSiteItemViewHolder(
view: View,
@ -42,12 +43,12 @@ class TopSiteItemViewHolder(
interactor.onSelectTopSite(topSite.url, topSite.isDefault)
}
top_site_item.setOnLongClickListener() {
top_site_item.setOnLongClickListener {
val menu = topSiteMenu.menuBuilder.build(view.context).show(anchor = it.top_site_title)
it.setOnTouchListener @SuppressLint("ClickableViewAccessibility") { v, event ->
onTouchEvent(v, event, menu)
}
return@setOnLongClickListener true
true
}
}
@ -56,7 +57,7 @@ class TopSiteItemViewHolder(
top_site_title.text = topSite.title
when (topSite.url) {
SupportUtils.POCKET_TRENDING_URL -> {
favicon_image.setImageDrawable(itemView.context.getDrawable(R.drawable.ic_pocket))
favicon_image.setImageDrawable(getDrawable(itemView.context, R.drawable.ic_pocket))
}
else -> {
itemView.context.components.core.icons.loadIntoView(favicon_image, topSite.url)

View File

@ -0,0 +1,59 @@
/* 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
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.view.isVisible
import io.mockk.mockk
import io.mockk.verify
import kotlinx.android.synthetic.main.no_collections_message.view.*
import mozilla.components.support.test.robolectric.testContext
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.R
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.home.sessioncontrol.CollectionInteractor
@RunWith(FenixRobolectricTestRunner::class)
class NoCollectionsMessageViewHolderTest {
private lateinit var view: View
private lateinit var interactor: CollectionInteractor
@Before
fun setup() {
val appCompatContext = ContextThemeWrapper(testContext, R.style.NormalTheme)
view = LayoutInflater.from(appCompatContext)
.inflate(NoCollectionsMessageViewHolder.LAYOUT_ID, null)
interactor = mockk(relaxed = true)
}
@Test
fun `hide button when hasNormalTabsOpened is false`() {
NoCollectionsMessageViewHolder(view, interactor, hasNormalTabsOpened = false)
assertFalse(view.add_tabs_to_collections_button.isVisible)
}
@Test
fun `show button when hasNormalTabsOpened is true`() {
NoCollectionsMessageViewHolder(view, interactor, hasNormalTabsOpened = true)
assertTrue(view.add_tabs_to_collections_button.isVisible)
}
@Test
fun `call interactor on click`() {
NoCollectionsMessageViewHolder(view, interactor, hasNormalTabsOpened = true)
view.add_tabs_to_collections_button.performClick()
verify { interactor.onAddTabsToCollectionTapped() }
}
}

View File

@ -0,0 +1,46 @@
/* 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
import android.view.LayoutInflater
import android.view.View
import io.mockk.mockk
import kotlinx.android.synthetic.main.component_top_sites.view.*
import mozilla.components.feature.top.sites.TopSite
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.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor
@RunWith(FenixRobolectricTestRunner::class)
class TopSiteViewHolderTest {
private lateinit var view: View
private lateinit var interactor: TopSiteInteractor
@Before
fun setup() {
view = LayoutInflater.from(testContext)
.inflate(TopSiteViewHolder.LAYOUT_ID, null)
interactor = mockk()
}
@Test
fun `binds list of top sites`() {
TopSiteViewHolder(view, interactor).bind(listOf(
object : TopSite {
override val id = 1L
override val isDefault = true
override val title = "Pocket"
override val url = "https://getpocket.com"
}
))
assertEquals(1, view.top_sites_list.adapter!!.itemCount)
}
}

View File

@ -0,0 +1,56 @@
/* 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.content.res.Resources
import android.view.LayoutInflater
import android.view.View
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import io.mockk.verify
import kotlinx.android.synthetic.main.onboarding_private_browsing.view.*
import mozilla.components.support.ktx.android.content.res.resolveAttribute
import mozilla.components.support.test.robolectric.testContext
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.R
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 interactor: OnboardingInteractor
@Before
fun setup() {
mockkStatic("mozilla.components.support.ktx.android.content.res.ThemeKt")
view = LayoutInflater.from(testContext)
.inflate(OnboardingPrivateBrowsingViewHolder.LAYOUT_ID, null)
interactor = mockk(relaxed = true)
every {
any<Resources.Theme>().resolveAttribute(R.attr.onboardingSelected)
} returns R.color.onboarding_illustration_selected_normal_theme
}
@After
fun teardown() {
unmockkStatic("mozilla.components.support.ktx.android.content.res.ThemeKt")
}
@Test
fun `call interactor on click`() {
OnboardingPrivateBrowsingViewHolder(view, interactor)
view.open_settings_button.performClick()
verify { interactor.onOpenSettingsClicked() }
}
}

View File

@ -0,0 +1,56 @@
/* 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.content.res.Resources
import android.view.LayoutInflater
import android.view.View
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import io.mockk.verify
import kotlinx.android.synthetic.main.onboarding_privacy_notice.view.*
import mozilla.components.support.ktx.android.content.res.resolveAttribute
import mozilla.components.support.test.robolectric.testContext
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.R
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 interactor: OnboardingInteractor
@Before
fun setup() {
mockkStatic("mozilla.components.support.ktx.android.content.res.ThemeKt")
view = LayoutInflater.from(testContext)
.inflate(OnboardingPrivacyNoticeViewHolder.LAYOUT_ID, null)
interactor = mockk(relaxed = true)
every {
any<Resources.Theme>().resolveAttribute(R.attr.onboardingSelected)
} returns R.color.onboarding_illustration_selected_normal_theme
}
@After
fun teardown() {
unmockkStatic("mozilla.components.support.ktx.android.content.res.ThemeKt")
}
@Test
fun `call interactor on click`() {
OnboardingPrivacyNoticeViewHolder(view, interactor)
view.read_button.performClick()
verify { interactor.onReadPrivacyNoticeClicked() }
}
}

View File

@ -0,0 +1,54 @@
/* 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.content.res.Resources
import android.view.LayoutInflater
import android.view.View
import io.mockk.every
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import kotlinx.android.synthetic.main.onboarding_tracking_protection.view.*
import mozilla.components.support.ktx.android.content.res.resolveAttribute
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.helpers.FenixRobolectricTestRunner
@RunWith(FenixRobolectricTestRunner::class)
class OnboardingTrackingProtectionViewHolderTest {
private lateinit var view: View
@Before
fun setup() {
mockkStatic("mozilla.components.support.ktx.android.content.res.ThemeKt")
view = LayoutInflater.from(testContext)
.inflate(OnboardingTrackingProtectionViewHolder.LAYOUT_ID, null)
every {
any<Resources.Theme>().resolveAttribute(R.attr.onboardingSelected)
} returns R.color.onboarding_illustration_selected_normal_theme
}
@After
fun teardown() {
unmockkStatic("mozilla.components.support.ktx.android.content.res.ThemeKt")
}
@Test
fun `sets description text`() {
OnboardingTrackingProtectionViewHolder(view)
assertEquals(
"Privacy and security settings block trackers, malware, and companies that follow you.",
view.description_text.text
)
}
}

View File

@ -0,0 +1,46 @@
/* 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.topsites
import android.view.LayoutInflater
import android.view.View
import io.mockk.mockk
import io.mockk.verify
import kotlinx.android.synthetic.main.top_site_item.view.*
import mozilla.components.feature.top.sites.TopSite
import mozilla.components.support.test.robolectric.testContext
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor
@RunWith(FenixRobolectricTestRunner::class)
class TopSiteItemViewHolderTest {
private lateinit var view: View
private lateinit var interactor: TopSiteInteractor
private val pocket = object : TopSite {
override val id = 1L
override val isDefault = true
override val title = "Pocket"
override val url = "https://getpocket.com"
}
@Before
fun setup() {
view = LayoutInflater.from(testContext)
.inflate(TopSiteItemViewHolder.LAYOUT_ID, null)
interactor = mockk(relaxed = true)
}
@Test
fun `calls interactor on click`() {
TopSiteItemViewHolder(view, interactor).bind(pocket)
view.top_site_item.performClick()
verify { interactor.onSelectTopSite("https://getpocket.com", isDefault = true) }
}
}