diff --git a/app/src/main/java/org/mozilla/fenix/tabstray/TabsTrayViewHolder.kt b/app/src/main/java/org/mozilla/fenix/tabstray/TabsTrayViewHolder.kt index 36dd0aae9b..a37a194fe2 100644 --- a/app/src/main/java/org/mozilla/fenix/tabstray/TabsTrayViewHolder.kt +++ b/app/src/main/java/org/mozilla/fenix/tabstray/TabsTrayViewHolder.kt @@ -87,8 +87,10 @@ abstract class TabsTrayViewHolder( updateSelectedTabIndicator(isSelected) updateMediaState(tab) - selectionHolder?.let { - setSelectionInteractor(tab, it, browserTrayInteractor) + if (selectionHolder != null) { + setSelectionInteractor(tab, selectionHolder, browserTrayInteractor) + } else { + itemView.setOnClickListener { browserTrayInteractor.open(tab) } } if (tab.thumbnail != null) { diff --git a/app/src/test/java/org/mozilla/fenix/tabstray/TabsTrayViewHolderTest.kt b/app/src/test/java/org/mozilla/fenix/tabstray/TabsTrayViewHolderTest.kt new file mode 100644 index 0000000000..4c9468af76 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/tabstray/TabsTrayViewHolderTest.kt @@ -0,0 +1,102 @@ +/* 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.tabstray + +import android.view.LayoutInflater +import android.view.View +import io.mockk.mockk +import io.mockk.verify +import mozilla.components.browser.state.store.BrowserStore +import mozilla.components.concept.base.images.ImageLoader +import mozilla.components.concept.tabstray.Tab +import mozilla.components.support.test.robolectric.testContext +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.mozilla.fenix.R +import org.mozilla.fenix.components.metrics.MetricController +import org.mozilla.fenix.helpers.FenixRobolectricTestRunner +import org.mozilla.fenix.selection.SelectionHolder +import org.mozilla.fenix.tabstray.browser.BrowserTrayInteractor +import org.mozilla.fenix.tabstray.browser.createTab + +@RunWith(FenixRobolectricTestRunner::class) +class TabsTrayViewHolderTest { + val store = TabsTrayStore() + val browserStore = BrowserStore() + val interactor = mockk(relaxed = true) + + @Test + fun `WHEN itemView is clicked THEN interactor invokes open`() { + val view = LayoutInflater.from(testContext).inflate(R.layout.tab_tray_item, null) + val holder = TestTabTrayViewHolder( + view, + mockk(relaxed = true), + store, + null, + browserStore, + mockk(relaxed = true), + interactor + ) + + holder.bind(createTab(), false, mockk(), mockk()) + + holder.itemView.performClick() + + verify { interactor.open(any()) } + } + + @Test + fun `WHEN itemView is clicked with a selection holder THEN the select holder is invoked`() { + val view = LayoutInflater.from(testContext).inflate(R.layout.tab_tray_item, null) + val selectionHolder = TestSelectionHolder(emptySet()) + val holder = TestTabTrayViewHolder( + view, + mockk(relaxed = true), + store, + selectionHolder, + browserStore, + mockk(relaxed = true), + interactor + ) + + holder.bind(createTab(), false, mockk(), mockk()) + + holder.itemView.performClick() + + verify { interactor.open(any()) } + assertTrue(selectionHolder.invoked) + } + + @Suppress("LongParameterList") + class TestTabTrayViewHolder( + itemView: View, + imageLoader: ImageLoader, + trayStore: TabsTrayStore, + selectionHolder: SelectionHolder?, + store: BrowserStore, + metrics: MetricController, + override val browserTrayInteractor: BrowserTrayInteractor + ) : TabsTrayViewHolder(itemView, imageLoader, trayStore, selectionHolder, store, metrics) { + override val thumbnailSize: Int + get() = 30 + + override fun updateSelectedTabIndicator(showAsSelected: Boolean) { + // do nothing + } + } + + class TestSelectionHolder( + private val testItems: Set + ) : SelectionHolder { + override val selectedItems: Set + get() { + invoked = true + return testItems + } + + var invoked = false + } +}