Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
For #9659 - New forceCollapse api in BrowserToolbarBehavior
Browse files Browse the repository at this point in the history
Similar to the existing forceExpand this new method will collapse the toolbar
(be it placed at the bottom or at the top).

There were also some tests from a previous patch that needed some touchups.
  • Loading branch information
Mugurell authored and mergify[bot] committed Feb 15, 2021
1 parent 4a649aa commit 6037caf
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ class BrowserToolbarBehavior(
yTranslator.expandWithAnimation(toolbar)
}

/**
* Used to collapse the [BrowserToolbar]
*/
fun forceCollapse(toolbar: BrowserToolbar) {
yTranslator.collapseWithAnimation(toolbar)
}

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal fun positionSnackbar(child: View, snackbarLayout: Snackbar.SnackbarLayout) {
val params = snackbarLayout.layoutParams as CoordinatorLayout.LayoutParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import mozilla.components.browser.toolbar.BrowserToolbar
*/
class BrowserToolbarYTranslator(toolbarPosition: ToolbarPosition) {
@VisibleForTesting
internal val strategy = getTranslationStrategy(toolbarPosition)
internal var strategy = getTranslationStrategy(toolbarPosition)

/**
* Snap the [BrowserToolbar] to be collapsed or expanded, depending on whatever state is closer
Expand All @@ -41,6 +41,13 @@ class BrowserToolbarYTranslator(toolbarPosition: ToolbarPosition) {
strategy.expandWithAnimation(toolbar)
}

/**
* Translate the [BrowserToolbar] to be hidden from view over a short amount of time.
*/
internal fun collapseWithAnimation(toolbar: BrowserToolbar) {
strategy.collapseWithAnimation(toolbar)
}

/**
* Force expanding the [BrowserToolbar] depending on the [distance] value that should be translated
* cancelling any other translation already in progress.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ class BrowserToolbarBehaviorTest {
assertTrue(behavior.shouldSnapAfterScroll)
verify(yTranslator).cancelInProgressTranslation()
verify(yTranslator, never()).expandWithAnimation(any())
verify(yTranslator, never()).collapseWithAnimation(any())

behavior.stopNestedScroll(0, child)

Expand Down Expand Up @@ -239,6 +240,7 @@ class BrowserToolbarBehaviorTest {
assertTrue(behavior.shouldSnapAfterScroll)
verify(yTranslator).cancelInProgressTranslation()
verify(yTranslator, never()).expandWithAnimation(any())
verify(yTranslator, never()).collapseWithAnimation(any())

behavior.stopNestedScroll(0, child)

Expand Down Expand Up @@ -381,66 +383,27 @@ class BrowserToolbarBehaviorTest {
}

@Test
fun `Behavior will snap toolbar first finishing translation animations if they are in progress`() {
// val behavior = BrowserToolbarBehavior(testContext, null, ToolbarPosition.BOTTOM)
// val yTranslator: BrowserToolbarYTranslator = mock()
// behavior.yTranslator = yTranslatorr
// val child = mock<BrowserToolbar>()
// behavior.browserToolbar = child
// doReturn(100).`when`(child).height
// doReturn(40f).`when`(child).translationY
//
// behavior.snapToolbarVertically()
//
// verify(animator).end()
}
fun `forceExpand should delegate the translator`() {
val behavior = spy(BrowserToolbarBehavior(testContext, null, ToolbarPosition.BOTTOM))
val yTranslator: BrowserToolbarYTranslator = mock()
behavior.yTranslator = yTranslator
val toolbar: BrowserToolbar = mock()

@Test
fun `Behavior can snap toolbar if it is translated to the bottom half`() {
// val behavior = BrowserToolbarBehavior(testContext, null, ToolbarPosition.BOTTOM)
// val animator: ValueAnimator = mock()
// behavior.snapAnimator = animator
// doReturn(false).`when`(animator).isStarted
// val child = mock<BrowserToolbar>()
// behavior.browserToolbar = child
// doReturn(100).`when`(child).height
// doReturn(40f).`when`(child).translationY
//
// behavior.snapToolbarVertically()
//
// verify(child).translationY = 0f
}
behavior.forceExpand(toolbar)

@Test
fun `Behavior can snap toolbar if it is translated to the top half`() {
// val behavior = BrowserToolbarBehavior(testContext, null, ToolbarPosition.BOTTOM)
// val animator: ValueAnimator = mock()
// behavior.snapAnimator = animator
// doReturn(false).`when`(animator).isStarted
// val child = mock<BrowserToolbar>()
// behavior.browserToolbar = child
// doReturn(100).`when`(child).height
// doReturn(60f).`when`(child).translationY
//
// behavior.snapToolbarVertically()
//
// verify(child).translationY = 100f
verify(yTranslator).expandWithAnimation(toolbar)
}

@Test
fun `Behavior will snap toolbar to top if it is translated to exactly half`() {
// val behavior = BrowserToolbarBehavior(testContext, null, ToolbarPosition.BOTTOM)
// val animator: ValueAnimator = mock()
// behavior.snapAnimator = animator
// doReturn(false).`when`(animator).isStarted
// val child = mock<BrowserToolbar>()
// doReturn(100).`when`(child).height
// doReturn(50f).`when`(child).translationY
// behavior.browserToolbar = child
//
// behavior.snapToolbarVertically()
//
// verify(child).translationY = 100f
fun `forceCollapse should delegate the translator`() {
val behavior = spy(BrowserToolbarBehavior(testContext, null, ToolbarPosition.BOTTOM))
val yTranslator: BrowserToolbarYTranslator = mock()
behavior.yTranslator = yTranslator
val toolbar: BrowserToolbar = mock()

behavior.forceCollapse(toolbar)

verify(yTranslator).collapseWithAnimation(toolbar)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import mozilla.components.support.test.mock
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.spy
import org.mockito.Mockito.verify

@RunWith(AndroidJUnit4::class)
Expand All @@ -31,60 +30,84 @@ class BrowserToolbarYTranslatorTest {

@Test
fun `yTranslator should delegate it's strategy for snapWithAnimation`() {
val yTranslator = spy(BrowserToolbarYTranslator(ToolbarPosition.BOTTOM))
val yTranslator = BrowserToolbarYTranslator(ToolbarPosition.BOTTOM)
val strategy: BrowserToolbarYTranslationStrategy = mock()
yTranslator.strategy = strategy
val toolbar: BrowserToolbar = mock()

yTranslator.snapWithAnimation(toolbar)

verify(yTranslator).snapWithAnimation(toolbar)
verify(strategy).snapWithAnimation(toolbar)
}

@Test
fun `yTranslator should delegate it's strategy for expandWithAnimation`() {
val yTranslator = spy(BrowserToolbarYTranslator(ToolbarPosition.BOTTOM))
val yTranslator = BrowserToolbarYTranslator(ToolbarPosition.BOTTOM)
val strategy: BrowserToolbarYTranslationStrategy = mock()
yTranslator.strategy = strategy
val toolbar: BrowserToolbar = mock()

yTranslator.expandWithAnimation(toolbar)

verify(yTranslator).expandWithAnimation(toolbar)
verify(strategy).expandWithAnimation(toolbar)
}

@Test
fun `yTranslator should delegate it's strategy for collapseWithAnimation`() {
val yTranslator = BrowserToolbarYTranslator(ToolbarPosition.BOTTOM)
val strategy: BrowserToolbarYTranslationStrategy = mock()
yTranslator.strategy = strategy
val toolbar: BrowserToolbar = mock()

yTranslator.collapseWithAnimation(toolbar)

verify(strategy).collapseWithAnimation(toolbar)
}

@Test
fun `yTranslator should delegate it's strategy for forceExpandIfNotAlready`() {
val yTranslator = spy(BrowserToolbarYTranslator(ToolbarPosition.BOTTOM))
val yTranslator = BrowserToolbarYTranslator(ToolbarPosition.BOTTOM)
val strategy: BrowserToolbarYTranslationStrategy = mock()
yTranslator.strategy = strategy
val toolbar: BrowserToolbar = mock()

yTranslator.forceExpandIfNotAlready(toolbar, 14f)

verify(yTranslator).forceExpandIfNotAlready(toolbar, 14f)
verify(strategy).forceExpandWithAnimation(toolbar, 14f)
}

@Test
fun `yTranslator should delegate it's strategy for translate`() {
val yTranslator = spy(BrowserToolbarYTranslator(ToolbarPosition.BOTTOM))
val yTranslator = BrowserToolbarYTranslator(ToolbarPosition.BOTTOM)
val strategy: BrowserToolbarYTranslationStrategy = mock()
yTranslator.strategy = strategy
val toolbar: BrowserToolbar = mock()

yTranslator.translate(toolbar, 23f)

verify(yTranslator).translate(toolbar, 23f)
verify(strategy).translate(toolbar, 23f)
}

@Test
fun `yTranslator should delegate it's strategy for cancelInProgressTranslation`() {
val yTranslator = spy(BrowserToolbarYTranslator(ToolbarPosition.BOTTOM))
val yTranslator = BrowserToolbarYTranslator(ToolbarPosition.BOTTOM)
val strategy: BrowserToolbarYTranslationStrategy = mock()
yTranslator.strategy = strategy

yTranslator.cancelInProgressTranslation()

verify(yTranslator).cancelInProgressTranslation()
verify(strategy).cancelInProgressTranslation()
}

@Test
fun `yTranslator should delegate it's strategy for snapImmediately`() {
val yTranslator = spy(BrowserToolbarYTranslator(ToolbarPosition.BOTTOM))
val yTranslator = BrowserToolbarYTranslator(ToolbarPosition.BOTTOM)
val strategy: BrowserToolbarYTranslationStrategy = mock()
yTranslator.strategy = strategy
val toolbar: BrowserToolbar = mock()

yTranslator.snapImmediately(toolbar)

verify(yTranslator).snapImmediately(toolbar)
verify(strategy).snapImmediately(toolbar)
}
}
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ permalink: /changelog/
* **All components**
* ⚠️Increased `targetSdkVersion` to 30 (Android R)

* **browser-toolbar**
* 🌟 Added `BrowserToolbarBehavior#forceCollapse` to easily collapse the top/bottom toolbar.

* **browser-toolbar**
* ⚠️ **This is a breaking change**: `BrowserToolbarBottomBehavior` is renamed to `BrowserToolbarBehavior` as it is now a common behavior for toolbars be them placed at the bottom or at the top of the screen.

Expand Down

0 comments on commit 6037caf

Please sign in to comment.