No issue: Move menu to separate integrations class
parent
dd3e95b4cf
commit
2e3086ac40
@ -0,0 +1,73 @@
|
|||||||
|
/* 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.content.Context
|
||||||
|
import android.view.View
|
||||||
|
import androidx.annotation.VisibleForTesting
|
||||||
|
import androidx.cardview.widget.CardView
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import com.google.android.material.tabs.TabLayout
|
||||||
|
import mozilla.components.browser.menu.BrowserMenu
|
||||||
|
import mozilla.components.browser.menu.BrowserMenuBuilder
|
||||||
|
import mozilla.components.browser.state.store.BrowserStore
|
||||||
|
import org.mozilla.fenix.R
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A wrapper class that building the tabs tray menu that handles item clicks.
|
||||||
|
*/
|
||||||
|
class MenuIntegration(
|
||||||
|
private val context: Context,
|
||||||
|
private val browserStore: BrowserStore,
|
||||||
|
private val tabsTrayStore: TabsTrayStore,
|
||||||
|
private val tabLayout: TabLayout,
|
||||||
|
private val navigationInteractor: NavigationInteractor
|
||||||
|
) {
|
||||||
|
private val tabsTrayItemMenu by lazy {
|
||||||
|
TabsTrayMenu(
|
||||||
|
context = context,
|
||||||
|
browserStore = browserStore,
|
||||||
|
tabLayout = tabLayout,
|
||||||
|
onItemTapped = ::handleMenuClicked
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private val isPrivateMode: Boolean
|
||||||
|
get() = tabsTrayStore.state.selectedPage == Page.PrivateTabs
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds the internal menu items list. See [BrowserMenuBuilder.build].
|
||||||
|
*/
|
||||||
|
fun build() = tabsTrayItemMenu.menuBuilder.build(context)
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
internal fun handleMenuClicked(item: TabsTrayMenu.Item) = when (item) {
|
||||||
|
is TabsTrayMenu.Item.ShareAllTabs ->
|
||||||
|
navigationInteractor.onShareTabsOfTypeClicked(isPrivateMode)
|
||||||
|
is TabsTrayMenu.Item.OpenTabSettings ->
|
||||||
|
navigationInteractor.onTabSettingsClicked()
|
||||||
|
is TabsTrayMenu.Item.CloseAllTabs ->
|
||||||
|
navigationInteractor.onCloseAllTabsClicked(isPrivateMode)
|
||||||
|
is TabsTrayMenu.Item.OpenRecentlyClosed ->
|
||||||
|
navigationInteractor.onOpenRecentlyClosedClicked()
|
||||||
|
is TabsTrayMenu.Item.SelectTabs -> {
|
||||||
|
/* TODO implement when mulitiselect call is available */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invokes [BrowserMenu.show] and applies the default theme color background.
|
||||||
|
*/
|
||||||
|
fun BrowserMenu.showWithTheme(view: View) {
|
||||||
|
show(view).also { popupMenu ->
|
||||||
|
(popupMenu.contentView as? CardView)?.setCardBackgroundColor(
|
||||||
|
ContextCompat.getColor(
|
||||||
|
view.context,
|
||||||
|
R.color.foundation_normal_theme
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
/* 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 io.mockk.mockk
|
||||||
|
import io.mockk.verify
|
||||||
|
import mozilla.components.support.test.middleware.CaptureActionsMiddleware
|
||||||
|
import org.junit.Assert.assertNotNull
|
||||||
|
import org.junit.Ignore
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class MenuIntegrationTest {
|
||||||
|
|
||||||
|
private val captureMiddleware = CaptureActionsMiddleware<TabsTrayState, TabsTrayAction>()
|
||||||
|
private val tabsTrayStore = TabsTrayStore(middlewares = listOf(captureMiddleware))
|
||||||
|
private val interactor = mockk<NavigationInteractor>(relaxed = true)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `WHEN the share all menu item is clicked THEN invoke the action`() {
|
||||||
|
val menu = MenuIntegration(mockk(), mockk(), tabsTrayStore, mockk(), interactor)
|
||||||
|
|
||||||
|
menu.handleMenuClicked(TabsTrayMenu.Item.ShareAllTabs)
|
||||||
|
|
||||||
|
verify { interactor.onShareTabsOfTypeClicked(false) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `WHEN the open settings menu item is clicked THEN invoke the action`() {
|
||||||
|
val menu = MenuIntegration(mockk(), mockk(), tabsTrayStore, mockk(), interactor)
|
||||||
|
|
||||||
|
menu.handleMenuClicked(TabsTrayMenu.Item.OpenTabSettings)
|
||||||
|
|
||||||
|
verify { interactor.onTabSettingsClicked() }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `WHEN the close all menu item is clicked THEN invoke the action`() {
|
||||||
|
val menu = MenuIntegration(mockk(), mockk(), tabsTrayStore, mockk(), interactor)
|
||||||
|
|
||||||
|
menu.handleMenuClicked(TabsTrayMenu.Item.CloseAllTabs)
|
||||||
|
|
||||||
|
verify { interactor.onCloseAllTabsClicked(false) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `WHEN the recently menu item is clicked THEN invoke the action`() {
|
||||||
|
val menu = MenuIntegration(mockk(), mockk(), tabsTrayStore, mockk(), interactor)
|
||||||
|
|
||||||
|
menu.handleMenuClicked(TabsTrayMenu.Item.OpenRecentlyClosed)
|
||||||
|
|
||||||
|
verify { interactor.onOpenRecentlyClosedClicked() }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Ignore("Enable after we connect this menu item to the store")
|
||||||
|
@Test
|
||||||
|
fun `WHEN the select menu item is clicked THEN invoke the action`() {
|
||||||
|
val menu = MenuIntegration(mockk(), mockk(), tabsTrayStore, mockk(), interactor)
|
||||||
|
|
||||||
|
menu.handleMenuClicked(TabsTrayMenu.Item.ShareAllTabs)
|
||||||
|
|
||||||
|
assertNotNull(captureMiddleware.findLastAction(TabsTrayAction.EnterSelectMode::class))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue