2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-11 13:11:01 +00:00

Use shared list widget in tab history (#13884)

This commit is contained in:
Tiger Oakes 2020-08-27 18:27:57 -07:00 committed by GitHub
parent 0102e03d75
commit 50ac4fe17d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 48 deletions

View File

@ -8,6 +8,7 @@ import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.ListAdapter
import mozilla.components.ui.widgets.WidgetSiteItemView
import org.mozilla.fenix.R import org.mozilla.fenix.R
data class TabHistoryItem( data class TabHistoryItem(
@ -23,7 +24,7 @@ class TabHistoryAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TabHistoryViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TabHistoryViewHolder {
val view = LayoutInflater.from(parent.context) val view = LayoutInflater.from(parent.context)
.inflate(R.layout.tab_history_list_item, parent, false) .inflate(R.layout.site_list_item, parent, false) as WidgetSiteItemView
return TabHistoryViewHolder(view, interactor) return TabHistoryViewHolder(view, interactor)
} }

View File

@ -4,40 +4,38 @@
package org.mozilla.fenix.tabhistory package org.mozilla.fenix.tabhistory
import android.view.View import androidx.recyclerview.widget.RecyclerView
import androidx.core.view.isVisible import mozilla.components.browser.icons.BrowserIcons
import kotlinx.android.synthetic.main.tab_history_list_item.*
import mozilla.components.support.ktx.android.content.getColorFromAttr import mozilla.components.support.ktx.android.content.getColorFromAttr
import mozilla.components.ui.widgets.WidgetSiteItemView
import org.mozilla.fenix.R import org.mozilla.fenix.R
import org.mozilla.fenix.library.LibrarySiteItemView import org.mozilla.fenix.ext.components
import org.mozilla.fenix.utils.view.ViewHolder import org.mozilla.fenix.ext.loadIntoView
class TabHistoryViewHolder( class TabHistoryViewHolder(
view: View, private val view: WidgetSiteItemView,
private val interactor: TabHistoryViewInteractor private val interactor: TabHistoryViewInteractor,
) : ViewHolder(view) { private val icons: BrowserIcons = view.context.components.core.icons
) : RecyclerView.ViewHolder(view) {
private lateinit var item: TabHistoryItem private lateinit var item: TabHistoryItem
init { init {
history_layout.setOnClickListener { interactor.goToHistoryItem(item) } view.setOnClickListener { interactor.goToHistoryItem(item) }
} }
fun bind(item: TabHistoryItem) { fun bind(item: TabHistoryItem) {
this.item = item this.item = item
history_layout.displayAs(LibrarySiteItemView.ItemType.SITE) view.setText(label = item.title, caption = item.url)
history_layout.overflowView.isVisible = false icons.loadIntoView(view.iconView, item.url)
history_layout.titleView.text = item.title
history_layout.urlView.text = item.url
history_layout.loadFavicon(item.url)
if (item.isSelected) { if (item.isSelected) {
history_layout.setBackgroundColor( view.setBackgroundColor(
history_layout.context.getColorFromAttr(R.attr.tabHistoryItemSelectedBackground) view.context.getColorFromAttr(R.attr.tabHistoryItemSelectedBackground)
) )
} else { } else {
history_layout.background = null view.background = null
} }
} }
} }

View File

@ -5,15 +5,13 @@
package org.mozilla.fenix.tabhistory package org.mozilla.fenix.tabhistory
import android.content.Context import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.widget.FrameLayout import android.widget.FrameLayout
import androidx.appcompat.view.ContextThemeWrapper import androidx.appcompat.view.ContextThemeWrapper
import io.mockk.MockKAnnotations import io.mockk.MockKAnnotations
import io.mockk.impl.annotations.MockK import io.mockk.impl.annotations.MockK
import kotlinx.android.synthetic.main.history_list_item.* import io.mockk.spyk
import mozilla.components.support.ktx.android.content.getColorFromAttr import io.mockk.verify
import mozilla.components.support.test.robolectric.testContext import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.Before import org.junit.Before
@ -56,18 +54,13 @@ class TabHistoryAdapterTest {
fun `creates and binds view holder`() { fun `creates and binds view holder`() {
adapter.submitList(listOf(selectedItem, unselectedItem)) adapter.submitList(listOf(selectedItem, unselectedItem))
val holder = adapter.createViewHolder(parent, 0) val holder = spyk(adapter.createViewHolder(parent, 0))
adapter.bindViewHolder(holder, 0) adapter.bindViewHolder(holder, 0)
assertEquals("Mozilla", holder.history_layout.titleView.text) verify { holder.bind(selectedItem) }
assertEquals(
context.getColorFromAttr(R.attr.tabHistoryItemSelectedBackground),
(holder.history_layout.background as ColorDrawable).color
)
adapter.bindViewHolder(holder, 1) adapter.bindViewHolder(holder, 1)
assertEquals("Firefox", holder.history_layout.titleView.text) verify { holder.bind(unselectedItem) }
assertEquals(null, holder.history_layout.background)
} }
@Test @Test

View File

@ -10,28 +10,42 @@ import io.mockk.just
import io.mockk.mockk import io.mockk.mockk
import io.mockk.slot import io.mockk.slot
import io.mockk.verify import io.mockk.verify
import mozilla.components.browser.icons.BrowserIcons
import mozilla.components.browser.icons.IconRequest
import mozilla.components.ui.widgets.WidgetSiteItemView
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import org.mozilla.fenix.R
import org.mozilla.fenix.library.LibrarySiteItemView
class TabHistoryViewHolderTest { class TabHistoryViewHolderTest {
@MockK private lateinit var view: View @MockK(relaxed = true) private lateinit var view: WidgetSiteItemView
@MockK private lateinit var interactor: TabHistoryViewInteractor @MockK private lateinit var interactor: TabHistoryViewInteractor
@MockK(relaxed = true) private lateinit var siteItemView: LibrarySiteItemView @MockK private lateinit var icons: BrowserIcons
private lateinit var holder: TabHistoryViewHolder private lateinit var holder: TabHistoryViewHolder
private lateinit var onClick: CapturingSlot<View.OnClickListener> private lateinit var onClick: CapturingSlot<View.OnClickListener>
private val selectedItem = TabHistoryItem(
title = "Mozilla",
url = "https://mozilla.org",
index = 0,
isSelected = true
)
private val unselectedItem = TabHistoryItem(
title = "Firefox",
url = "https://firefox.com",
index = 1,
isSelected = false
)
@Before @Before
fun setup() { fun setup() {
MockKAnnotations.init(this) MockKAnnotations.init(this)
onClick = slot() onClick = slot()
every { siteItemView.setOnClickListener(capture(onClick)) } just Runs every { view.setOnClickListener(capture(onClick)) } just Runs
every { view.findViewById<LibrarySiteItemView>(R.id.history_layout) } returns siteItemView every { icons.loadIntoView(view.iconView, any()) } returns mockk()
holder = TabHistoryViewHolder(view, interactor) holder = TabHistoryViewHolder(view, interactor, icons)
} }
@Test @Test
@ -46,17 +60,18 @@ class TabHistoryViewHolderTest {
@Test @Test
fun `binds title and url`() { fun `binds title and url`() {
val item = TabHistoryItem( holder.bind(unselectedItem)
title = "Firefox",
url = "https://firefox.com",
index = 1,
isSelected = false
)
holder.bind(item)
verify { siteItemView.displayAs(LibrarySiteItemView.ItemType.SITE) } verify { view.setText(label = "Firefox", caption = "https://firefox.com") }
verify { siteItemView.titleView.text = "Firefox" } verify { icons.loadIntoView(view.iconView, IconRequest("https://firefox.com")) }
verify { siteItemView.urlView.text = "https://firefox.com" } }
verify { siteItemView.loadFavicon("https://firefox.com") }
@Test
fun `binds background`() {
holder.bind(selectedItem)
verify { view.setBackgroundColor(any()) }
holder.bind(unselectedItem)
verify { view.background = null }
} }
} }