forked from mozilla-mobile/fenix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For mozilla-mobile#26026 - Refactor the FenixTabCounterMenu creation …
…from HomeFragment to TabCounterBuilder
- Loading branch information
1 parent
ce92a0a
commit c364c85
Showing
3 changed files
with
183 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
app/src/main/java/org/mozilla/fenix/home/TabCounterBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* 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.home | ||
|
||
import android.content.Context | ||
import androidx.core.content.ContextCompat | ||
import androidx.navigation.NavController | ||
import mozilla.components.ui.tabcounter.TabCounter | ||
import mozilla.components.ui.tabcounter.TabCounterMenu | ||
import mozilla.telemetry.glean.private.NoExtras | ||
import org.mozilla.fenix.GleanMetrics.StartOnHome | ||
import org.mozilla.fenix.R | ||
import org.mozilla.fenix.browser.browsingmode.BrowsingMode | ||
import org.mozilla.fenix.browser.browsingmode.BrowsingModeManager | ||
import org.mozilla.fenix.components.toolbar.FenixTabCounterMenu | ||
import org.mozilla.fenix.ext.nav | ||
|
||
/** | ||
* Helper class for building the [FenixTabCounterMenu]. | ||
* | ||
* @property context An Android [Context]. | ||
* @property browsingModeManager [BrowsingModeManager] used for fetching the current browsing mode. | ||
* @property navController [NavController] used for navigation. | ||
* @property tabCounter The [TabCounter] that will be setup with event handlers. | ||
*/ | ||
class TabCounterBuilder( | ||
private val context: Context, | ||
private val browsingModeManager: BrowsingModeManager, | ||
private val navController: NavController, | ||
private val tabCounter: TabCounter, | ||
) { | ||
|
||
/** | ||
* Builds the [FenixTabCounterMenu]. | ||
*/ | ||
fun build() { | ||
val tabCounterMenu = FenixTabCounterMenu( | ||
context = context, | ||
onItemTapped = ::onItemTapped, | ||
iconColor = if (browsingModeManager.mode == BrowsingMode.Private) { | ||
ContextCompat.getColor(context, R.color.fx_mobile_private_text_color_primary) | ||
} else { | ||
null | ||
} | ||
) | ||
|
||
tabCounterMenu.updateMenu( | ||
showOnly = when (browsingModeManager.mode) { | ||
BrowsingMode.Normal -> BrowsingMode.Private | ||
BrowsingMode.Private -> BrowsingMode.Normal | ||
} | ||
) | ||
|
||
tabCounter.setOnLongClickListener { | ||
tabCounterMenu.menuController.show(anchor = it) | ||
true | ||
} | ||
|
||
tabCounter.setOnClickListener { | ||
StartOnHome.openTabsTray.record(NoExtras()) | ||
|
||
navController.nav( | ||
R.id.homeFragment, | ||
HomeFragmentDirections.actionGlobalTabsTrayFragment() | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Callback invoked when a menu item is tapped on. | ||
*/ | ||
internal fun onItemTapped(item: TabCounterMenu.Item) { | ||
if (item is TabCounterMenu.Item.NewTab) { | ||
browsingModeManager.mode = BrowsingMode.Normal | ||
} else if (item is TabCounterMenu.Item.NewPrivateTab) { | ||
browsingModeManager.mode = BrowsingMode.Private | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
app/src/test/java/org/mozilla/fenix/home/TabCounterBuilderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* 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.home | ||
|
||
import androidx.navigation.NavController | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import mozilla.components.support.test.robolectric.testContext | ||
import mozilla.components.ui.tabcounter.TabCounter | ||
import mozilla.components.ui.tabcounter.TabCounterMenu | ||
import mozilla.telemetry.glean.testing.GleanTestRule | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Assert.assertNull | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mozilla.fenix.GleanMetrics.StartOnHome | ||
import org.mozilla.fenix.R | ||
import org.mozilla.fenix.browser.browsingmode.BrowsingMode | ||
import org.mozilla.fenix.browser.browsingmode.BrowsingModeManager | ||
import org.mozilla.fenix.browser.browsingmode.DefaultBrowsingModeManager | ||
import org.mozilla.fenix.ext.nav | ||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner | ||
import org.mozilla.fenix.utils.Settings | ||
|
||
@RunWith(FenixRobolectricTestRunner::class) | ||
class TabCounterBuilderTest { | ||
|
||
@get:Rule | ||
val gleanTestRule = GleanTestRule(testContext) | ||
|
||
private lateinit var navController: NavController | ||
private lateinit var browsingModeManager: BrowsingModeManager | ||
private lateinit var settings: Settings | ||
private lateinit var modeDidChange: (BrowsingMode) -> Unit | ||
private lateinit var tabCounterBuilder: TabCounterBuilder | ||
private lateinit var tabCounter: TabCounter | ||
|
||
@Before | ||
fun setup() { | ||
navController = mockk(relaxed = true) | ||
settings = mockk(relaxed = true) | ||
modeDidChange = mockk(relaxed = true) | ||
|
||
tabCounter = TabCounter(testContext) | ||
|
||
browsingModeManager = DefaultBrowsingModeManager( | ||
_mode = BrowsingMode.Normal, | ||
settings = settings, | ||
modeDidChange = modeDidChange, | ||
) | ||
|
||
tabCounterBuilder = TabCounterBuilder( | ||
context = testContext, | ||
browsingModeManager = browsingModeManager, | ||
navController = navController, | ||
tabCounter = tabCounter, | ||
) | ||
} | ||
|
||
@Test | ||
fun `WHEN tab counter is clicked THEN navigate to tabs tray and record metrics`() { | ||
tabCounterBuilder.build() | ||
|
||
assertNull(StartOnHome.openTabsTray.testGetValue()) | ||
|
||
tabCounter.performClick() | ||
|
||
assertNotNull(StartOnHome.openTabsTray.testGetValue()) | ||
|
||
verify { | ||
navController.nav( | ||
R.id.homeFragment, | ||
HomeFragmentDirections.actionGlobalTabsTrayFragment() | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun `WHEN New tab menu item is tapped THEN set browsing mode to normal`() { | ||
tabCounterBuilder.onItemTapped(TabCounterMenu.Item.NewTab) | ||
|
||
assertEquals(BrowsingMode.Normal, browsingModeManager.mode) | ||
} | ||
|
||
@Test | ||
fun `WHEN New private tab menu item is tapped THEN set browsing mode to private`() { | ||
tabCounterBuilder.onItemTapped(TabCounterMenu.Item.NewPrivateTab) | ||
|
||
assertEquals(BrowsingMode.Private, browsingModeManager.mode) | ||
} | ||
} |