parent
a8291bf641
commit
e93a8b7d34
@ -0,0 +1,100 @@
|
||||
/* 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.components.toolbar
|
||||
|
||||
import android.content.Context
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import mozilla.components.browser.menu2.BrowserMenuController
|
||||
import mozilla.components.concept.menu.MenuController
|
||||
import mozilla.components.concept.menu.candidate.DividerMenuCandidate
|
||||
import mozilla.components.concept.menu.candidate.DrawableMenuIcon
|
||||
import mozilla.components.concept.menu.candidate.MenuCandidate
|
||||
import mozilla.components.concept.menu.candidate.TextMenuCandidate
|
||||
import mozilla.components.concept.menu.candidate.TextStyle
|
||||
import mozilla.components.support.ktx.android.content.getColorFromAttr
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
|
||||
import org.mozilla.fenix.components.metrics.Event
|
||||
import org.mozilla.fenix.components.metrics.MetricController
|
||||
|
||||
class TabCounterMenu(
|
||||
context: Context,
|
||||
private val metrics: MetricController,
|
||||
private val onItemTapped: (Item) -> Unit
|
||||
) {
|
||||
|
||||
sealed class Item {
|
||||
object CloseTab : Item()
|
||||
data class NewTab(val mode: BrowsingMode) : Item()
|
||||
}
|
||||
|
||||
val menuController: MenuController by lazy { BrowserMenuController() }
|
||||
|
||||
private val newTabItem: TextMenuCandidate
|
||||
private val newPrivateTabItem: TextMenuCandidate
|
||||
private val closeTabItem: TextMenuCandidate
|
||||
|
||||
init {
|
||||
val primaryTextColor = context.getColorFromAttr(R.attr.primaryText)
|
||||
val textStyle = TextStyle(color = primaryTextColor)
|
||||
|
||||
newTabItem = TextMenuCandidate(
|
||||
text = context.getString(R.string.browser_menu_new_tab),
|
||||
start = DrawableMenuIcon(
|
||||
context,
|
||||
R.drawable.ic_new,
|
||||
tint = primaryTextColor
|
||||
),
|
||||
textStyle = textStyle
|
||||
) {
|
||||
metrics.track(Event.TabCounterMenuItemTapped(Event.TabCounterMenuItemTapped.Item.NEW_TAB))
|
||||
onItemTapped(Item.NewTab(BrowsingMode.Normal))
|
||||
}
|
||||
|
||||
newPrivateTabItem = TextMenuCandidate(
|
||||
text = context.getString(R.string.home_screen_shortcut_open_new_private_tab_2),
|
||||
start = DrawableMenuIcon(
|
||||
context,
|
||||
R.drawable.ic_private_browsing,
|
||||
tint = primaryTextColor
|
||||
),
|
||||
textStyle = textStyle
|
||||
) {
|
||||
metrics.track(Event.TabCounterMenuItemTapped(Event.TabCounterMenuItemTapped.Item.NEW_PRIVATE_TAB))
|
||||
onItemTapped(Item.NewTab(BrowsingMode.Private))
|
||||
}
|
||||
|
||||
closeTabItem = TextMenuCandidate(
|
||||
text = context.getString(R.string.close_tab),
|
||||
start = DrawableMenuIcon(
|
||||
context,
|
||||
R.drawable.ic_close,
|
||||
tint = primaryTextColor
|
||||
),
|
||||
textStyle = textStyle
|
||||
) {
|
||||
metrics.track(Event.TabCounterMenuItemTapped(Event.TabCounterMenuItemTapped.Item.CLOSE_TAB))
|
||||
onItemTapped(Item.CloseTab)
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
internal fun menuItems(showOnly: BrowsingMode?): List<MenuCandidate> {
|
||||
return when (showOnly) {
|
||||
BrowsingMode.Normal -> listOf(newTabItem)
|
||||
BrowsingMode.Private -> listOf(newPrivateTabItem)
|
||||
null -> listOf(
|
||||
newTabItem,
|
||||
newPrivateTabItem,
|
||||
DividerMenuCandidate(),
|
||||
closeTabItem
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun updateMenu(showOnly: BrowsingMode? = null) {
|
||||
menuController.submitList(menuItems(showOnly))
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
/* 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.components.toolbar
|
||||
|
||||
sealed class TabCounterMenuItem {
|
||||
object CloseTab : TabCounterMenuItem()
|
||||
class NewTab(val isPrivate: Boolean) : TabCounterMenuItem()
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
/* 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.components.toolbar
|
||||
|
||||
import android.content.Context
|
||||
import androidx.appcompat.view.ContextThemeWrapper
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verifyAll
|
||||
import mozilla.components.concept.menu.candidate.DividerMenuCandidate
|
||||
import mozilla.components.concept.menu.candidate.DrawableMenuIcon
|
||||
import mozilla.components.concept.menu.candidate.TextMenuCandidate
|
||||
import mozilla.components.support.test.robolectric.testContext
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
|
||||
import org.mozilla.fenix.components.metrics.Event
|
||||
import org.mozilla.fenix.components.metrics.MetricController
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
|
||||
@RunWith(FenixRobolectricTestRunner::class)
|
||||
class TabCounterMenuTest {
|
||||
|
||||
private lateinit var context: Context
|
||||
private lateinit var metrics: MetricController
|
||||
private lateinit var onItemTapped: (TabCounterMenu.Item) -> Unit
|
||||
private lateinit var menu: TabCounterMenu
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
context = ContextThemeWrapper(testContext, R.style.NormalTheme)
|
||||
metrics = mockk(relaxed = true)
|
||||
onItemTapped = mockk(relaxed = true)
|
||||
menu = TabCounterMenu(context, metrics, onItemTapped)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `all items use primary text color styling`() {
|
||||
val items = menu.menuItems(showOnly = null)
|
||||
assertEquals(4, items.size)
|
||||
|
||||
val textItems = items.mapNotNull { it as? TextMenuCandidate }
|
||||
assertEquals(3, textItems.size)
|
||||
|
||||
val primaryTextColor = context.getColor(R.color.primary_text_normal_theme)
|
||||
for (item in textItems) {
|
||||
assertEquals(primaryTextColor, item.textStyle.color)
|
||||
assertEquals(primaryTextColor, (item.start as DrawableMenuIcon).tint)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `return only the new tab item`() {
|
||||
val items = menu.menuItems(showOnly = BrowsingMode.Normal)
|
||||
assertEquals(1, items.size)
|
||||
|
||||
val item = items[0] as TextMenuCandidate
|
||||
assertEquals("New tab", item.text)
|
||||
item.onClick()
|
||||
|
||||
verifyAll {
|
||||
metrics.track(Event.TabCounterMenuItemTapped(Event.TabCounterMenuItemTapped.Item.NEW_TAB))
|
||||
onItemTapped(TabCounterMenu.Item.NewTab(BrowsingMode.Normal))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `return only the new private tab item`() {
|
||||
val items = menu.menuItems(showOnly = BrowsingMode.Private)
|
||||
assertEquals(1, items.size)
|
||||
|
||||
val item = items[0] as TextMenuCandidate
|
||||
assertEquals("New private tab", item.text)
|
||||
item.onClick()
|
||||
|
||||
verifyAll {
|
||||
metrics.track(Event.TabCounterMenuItemTapped(Event.TabCounterMenuItemTapped.Item.NEW_PRIVATE_TAB))
|
||||
onItemTapped(TabCounterMenu.Item.NewTab(BrowsingMode.Private))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `return two new tab items and a close button`() {
|
||||
val (newTab, newPrivateTab, divider, closeTab) = menu.menuItems(showOnly = null)
|
||||
|
||||
assertEquals("New tab", (newTab as TextMenuCandidate).text)
|
||||
assertEquals("New private tab", (newPrivateTab as TextMenuCandidate).text)
|
||||
assertEquals("Close tab", (closeTab as TextMenuCandidate).text)
|
||||
assertEquals(DividerMenuCandidate(), divider)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue