For #19886 integrate view binding.
parent
fd8e6a9c27
commit
512475df9e
@ -0,0 +1,87 @@
|
||||
/* 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.settings.quicksettings
|
||||
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import androidx.core.content.ContextCompat.getColor
|
||||
import androidx.core.view.isVisible
|
||||
import mozilla.components.browser.icons.BrowserIcons
|
||||
import mozilla.components.support.ktx.android.content.getDrawableWithTint
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.databinding.ConnectionDetailsWebsiteInfoBinding
|
||||
import org.mozilla.fenix.ext.loadIntoView
|
||||
|
||||
/**
|
||||
* MVI View that knows to display a whether the current website connection details.
|
||||
*
|
||||
* Currently it does not support any user interaction.
|
||||
*
|
||||
* @param container [ViewGroup] in which this View will inflate itself.
|
||||
* @param icons Icons component for loading, caching and processing website icons.
|
||||
* @param interactor [WebSiteInfoInteractor] which will have delegated to all user interactions.
|
||||
*/
|
||||
class ConnectionDetailsView(
|
||||
container: ViewGroup,
|
||||
private val icons: BrowserIcons,
|
||||
val interactor: WebSiteInfoInteractor,
|
||||
) {
|
||||
val binding = ConnectionDetailsWebsiteInfoBinding.inflate(
|
||||
LayoutInflater.from(container.context), container, true
|
||||
)
|
||||
|
||||
/**
|
||||
* Allows changing what this View displays.
|
||||
*
|
||||
* @param state [WebsiteInfoState] to be rendered.
|
||||
*/
|
||||
fun update(state: WebsiteInfoState) {
|
||||
icons.loadIntoView(binding.faviconImage, state.websiteUrl)
|
||||
bindUrl(state.websiteUrl)
|
||||
bindSecurityInfo(state.websiteSecurityUiValues)
|
||||
bindCertificateName(state.certificateName)
|
||||
bindTitle(state.websiteTitle)
|
||||
bindBackButtonListener()
|
||||
}
|
||||
|
||||
private fun bindUrl(websiteUrl: String) {
|
||||
binding.url.text = websiteUrl
|
||||
}
|
||||
|
||||
private fun bindSecurityInfo(uiValues: WebsiteSecurityUiValues) {
|
||||
val tint = getColor(provideContext(), uiValues.iconTintRes)
|
||||
binding.securityInfo.setText(uiValues.securityInfoRes)
|
||||
binding.securityInfoIcon.setImageDrawable(
|
||||
provideContext().getDrawableWithTint(uiValues.iconRes, tint)
|
||||
)
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
internal fun provideContext(): Context = binding.root.context
|
||||
|
||||
@VisibleForTesting
|
||||
internal fun bindBackButtonListener() {
|
||||
binding.detailsBack.isVisible = true
|
||||
binding.detailsBack.setOnClickListener {
|
||||
interactor.onBackPressed()
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
internal fun bindTitle(websiteTitle: String) {
|
||||
binding.title.text = websiteTitle
|
||||
binding.titleContainer.isVisible = websiteTitle.isNotEmpty()
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
internal fun bindCertificateName(cert: String) {
|
||||
val certificateLabel =
|
||||
provideContext().getString(R.string.certificate_info_verified_by, cert)
|
||||
binding.certificateInfo.text = certificateLabel
|
||||
binding.certificateInfo.isVisible = cert.isNotEmpty()
|
||||
}
|
||||
}
|
@ -0,0 +1,104 @@
|
||||
/* 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.settings.quicksettings
|
||||
|
||||
import android.widget.FrameLayout
|
||||
import androidx.core.view.isVisible
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.spyk
|
||||
import io.mockk.verify
|
||||
import mozilla.components.browser.icons.BrowserIcons
|
||||
import mozilla.components.browser.icons.IconRequest
|
||||
import mozilla.components.support.test.robolectric.testContext
|
||||
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.databinding.ConnectionDetailsWebsiteInfoBinding
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
|
||||
@RunWith(FenixRobolectricTestRunner::class)
|
||||
class ConnectionDetailsViewTest {
|
||||
|
||||
private lateinit var view: ConnectionDetailsView
|
||||
private lateinit var icons: BrowserIcons
|
||||
private lateinit var binding: ConnectionDetailsWebsiteInfoBinding
|
||||
private lateinit var interactor: WebSiteInfoInteractor
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
icons = mockk(relaxed = true)
|
||||
interactor = mockk(relaxed = true)
|
||||
view = spyk(ConnectionDetailsView(FrameLayout(testContext), icons, interactor))
|
||||
binding = view.binding
|
||||
every { icons.loadIntoView(any(), any()) } returns mockk()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN updating THEN bind url and title`() {
|
||||
val websiteUrl = "https://mozilla.org"
|
||||
|
||||
view.update(
|
||||
WebsiteInfoState(
|
||||
websiteUrl = websiteUrl,
|
||||
websiteTitle = "Mozilla",
|
||||
websiteSecurityUiValues = WebsiteSecurityUiValues.SECURE,
|
||||
certificateName = ""
|
||||
)
|
||||
)
|
||||
|
||||
verify { icons.loadIntoView(binding.faviconImage, IconRequest(websiteUrl)) }
|
||||
|
||||
assertEquals("https://mozilla.org", binding.url.text)
|
||||
assertEquals("Secure Connection", binding.securityInfo.text)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN updating THEN bind certificate`() {
|
||||
view.update(
|
||||
WebsiteInfoState(
|
||||
websiteUrl = "https://mozilla.org",
|
||||
websiteTitle = "Mozilla",
|
||||
websiteSecurityUiValues = WebsiteSecurityUiValues.INSECURE,
|
||||
certificateName = "Certificate"
|
||||
)
|
||||
)
|
||||
|
||||
assertEquals("Insecure Connection", binding.securityInfo.text)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN updating THEN bind the certificate, title and back button listener`() {
|
||||
view.update(
|
||||
WebsiteInfoState(
|
||||
websiteUrl = "https://mozilla.org",
|
||||
websiteTitle = "Mozilla",
|
||||
websiteSecurityUiValues = WebsiteSecurityUiValues.INSECURE,
|
||||
certificateName = "Certificate"
|
||||
)
|
||||
)
|
||||
|
||||
verify {
|
||||
view.bindCertificateName("Certificate")
|
||||
view.bindTitle("Mozilla")
|
||||
view.bindBackButtonListener()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN title is empty THEN the title should be gone`() {
|
||||
|
||||
view.bindTitle("")
|
||||
|
||||
assertFalse(binding.titleContainer.isVisible)
|
||||
|
||||
view.bindTitle("Title")
|
||||
|
||||
assertTrue(binding.titleContainer.isVisible)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue