From 756e54a037b4541c52bae190a8b38506da98acc6 Mon Sep 17 00:00:00 2001 From: Gabriel Luong Date: Mon, 14 Jun 2021 13:39:35 -0400 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/19988 - Part 2: Load tab icon from BrowserIcons cache if needed for the recent tab Co-authored-by: Jonathan Almeida --- .../recenttabs/view/RecentTabViewHolder.kt | 14 ++++++++++++-- .../recenttabs/view/RecentTabViewHolderTest.kt | 18 ++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/home/recenttabs/view/RecentTabViewHolder.kt b/app/src/main/java/org/mozilla/fenix/home/recenttabs/view/RecentTabViewHolder.kt index 27644e128f..5ac12cd58f 100644 --- a/app/src/main/java/org/mozilla/fenix/home/recenttabs/view/RecentTabViewHolder.kt +++ b/app/src/main/java/org/mozilla/fenix/home/recenttabs/view/RecentTabViewHolder.kt @@ -6,8 +6,11 @@ package org.mozilla.fenix.home.recenttabs.view import android.view.View import kotlinx.android.synthetic.main.recent_tabs_list_row.* +import mozilla.components.browser.icons.BrowserIcons import mozilla.components.browser.state.state.TabSessionState import org.mozilla.fenix.R +import org.mozilla.fenix.ext.components +import org.mozilla.fenix.ext.loadIntoView import org.mozilla.fenix.home.recenttabs.interactor.RecentTabInteractor import org.mozilla.fenix.utils.view.ViewHolder @@ -15,15 +18,22 @@ import org.mozilla.fenix.utils.view.ViewHolder * View holder for a recent tab item. * * @param interactor [RecentTabInteractor] which will have delegated to all user interactions. + * @param icons */ class RecentTabViewHolder( view: View, - private val interactor: RecentTabInteractor + private val interactor: RecentTabInteractor, + private val icons: BrowserIcons = view.context.components.core.icons ) : ViewHolder(view) { fun bindTab(tab: TabSessionState) { recent_tab_title.text = tab.content.title - recent_tab_icon.setImageBitmap(tab.content.icon) + + if (tab.content.icon != null) { + recent_tab_icon.setImageBitmap(tab.content.icon) + } else { + icons.loadIntoView(recent_tab_icon, tab.content.url) + } itemView.setOnClickListener { interactor.onRecentTabClicked(tab.id) diff --git a/app/src/test/java/org/mozilla/fenix/home/recenttabs/view/RecentTabViewHolderTest.kt b/app/src/test/java/org/mozilla/fenix/home/recenttabs/view/RecentTabViewHolderTest.kt index 0cea1c3605..589d164637 100644 --- a/app/src/test/java/org/mozilla/fenix/home/recenttabs/view/RecentTabViewHolderTest.kt +++ b/app/src/test/java/org/mozilla/fenix/home/recenttabs/view/RecentTabViewHolderTest.kt @@ -6,9 +6,13 @@ package org.mozilla.fenix.home.recenttabs.view import android.view.LayoutInflater import android.view.View +import io.mockk.every import io.mockk.mockk import io.mockk.verify +import kotlinx.android.synthetic.main.recent_tabs_list_row.* import kotlinx.android.synthetic.main.recent_tabs_list_row.view.* +import mozilla.components.browser.icons.BrowserIcons +import mozilla.components.browser.icons.IconRequest import mozilla.components.browser.state.state.createTab import mozilla.components.support.test.robolectric.testContext import org.junit.Assert.assertEquals @@ -23,6 +27,7 @@ class RecentTabViewHolderTest { private lateinit var view: View private lateinit var interactor: SessionControlInteractor + private lateinit var icons: BrowserIcons private val tab = createTab( url = "https://mozilla.org", @@ -33,18 +38,23 @@ class RecentTabViewHolderTest { fun setup() { view = LayoutInflater.from(testContext).inflate(RecentTabViewHolder.LAYOUT_ID, null) interactor = mockk(relaxed = true) + icons = mockk(relaxed = true) + + every { icons.loadIntoView(view.recent_tab_icon, any()) } returns mockk() } @Test - fun `GIVEN a new recent tab on bind THEN set the title text`() { - RecentTabViewHolder(view, interactor).bindTab(tab) + fun `GIVEN a new recent tab on bind THEN set the title text and load the tab icon`() { + RecentTabViewHolder(view, interactor, icons).bindTab(tab) assertEquals(tab.content.title, view.recent_tab_title.text) + + verify { icons.loadIntoView(view.recent_tab_icon, IconRequest(tab.content.url)) } } @Test - fun `WHEN a recent tab item is clicked THEN interactor iis called`() { - RecentTabViewHolder(view, interactor).bindTab(tab) + fun `WHEN a recent tab item is clicked THEN interactor is called`() { + RecentTabViewHolder(view, interactor, icons).bindTab(tab) view.performClick()