Skip to content

Commit

Permalink
For mozilla-mobile#10668 - Round the translation value to the nearest…
Browse files Browse the repository at this point in the history
… integer
  • Loading branch information
Mugurell authored and Grisha Kruglov committed Sep 11, 2021
1 parent a453a40 commit ef6487d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
Expand Up @@ -8,9 +8,11 @@ import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.annotation.VisibleForTesting
import androidx.coordinatorlayout.widget.CoordinatorLayout
import mozilla.components.concept.engine.EngineView
import mozilla.components.support.ktx.android.view.findViewInHierarchy
import kotlin.math.roundToInt

/**
* A [CoordinatorLayout.Behavior] implementation that allows the [EngineView] to automatically
Expand All @@ -33,15 +35,17 @@ class EngineViewBrowserToolbarBehavior(
toolbarPosition: ToolbarPosition
) : CoordinatorLayout.Behavior<View>(context, attrs) {

private val engineView = engineViewParent.findViewInHierarchy { it is EngineView } as EngineView?
private var toolbarChangedAction: (Float) -> Unit?
@VisibleForTesting
internal val engineView = engineViewParent.findViewInHierarchy { it is EngineView } as EngineView?
@VisibleForTesting
internal var toolbarChangedAction: (Float) -> Unit?
private val bottomToolbarChangedAction = { newToolbarTranslationY: Float ->
engineView?.setVerticalClipping(-newToolbarTranslationY.toInt())
engineView?.setVerticalClipping(-newToolbarTranslationY.roundToInt())
}
private val topToolbarChangedAction = { newToolbarTranslationY: Float ->
// the top toolbar is translated upwards when collapsing-> all values received are 0 or negative
engineView?.let {
it.setVerticalClipping(newToolbarTranslationY.toInt())
it.setVerticalClipping(newToolbarTranslationY.roundToInt())
// Need to add the toolbarHeight to effectively place the engineView below the toolbar.
engineViewParent.translationY = newToolbarTranslationY + toolbarHeight
}
Expand Down
Expand Up @@ -81,6 +81,78 @@ class EngineViewBrowserToolbarBehaviorTest {

assertTrue(behavior.layoutDependsOn(mock(), mock(), BrowserToolbar(testContext)))
}

@Test
fun `GIVEN a bottom toolbar WHEN translation has below a half decimal THEN set vertical clipping with the floor value`() {
val engineView: FakeEngineView = mock()
val behavior = EngineViewBrowserToolbarBehavior(
mock(), null, engineView, 100, ToolbarPosition.BOTTOM
)

behavior.toolbarChangedAction(40.4f)

verify(engineView).setVerticalClipping(-40)
}

@Test
fun `GIVEN a bottom toolbar WHEN translation has exactly half of a decimal THEN set vertical clipping with the ceiling value`() {
val engineView: FakeEngineView = mock()
val behavior = EngineViewBrowserToolbarBehavior(
mock(), null, engineView, 100, ToolbarPosition.BOTTOM
)

behavior.toolbarChangedAction(40.5f)

verify(engineView).setVerticalClipping(-41)
}

@Test
fun `GIVEN a bottom toolbar WHEN translation has more than a half decimal THEN set vertical clipping with the ceiling value`() {
val engineView: FakeEngineView = mock()
val behavior = EngineViewBrowserToolbarBehavior(
mock(), null, engineView, 100, ToolbarPosition.BOTTOM
)

behavior.toolbarChangedAction(40.6f)

verify(engineView).setVerticalClipping(-41)
}

@Test
fun `GIVEN a top toolbar WHEN translation has below a half decimal THEN set vertical clipping with the floor value`() {
val engineView: FakeEngineView = mock()
val behavior = EngineViewBrowserToolbarBehavior(
mock(), null, engineView, 100, ToolbarPosition.TOP
)

behavior.toolbarChangedAction(40.4f)

verify(engineView).setVerticalClipping(40)
}

@Test
fun `GIVEN a top toolbar WHEN translation has exactly half of a decimal THEN set vertical clipping with the ceiling value`() {
val engineView: FakeEngineView = mock()
val behavior = EngineViewBrowserToolbarBehavior(
mock(), null, engineView, 100, ToolbarPosition.TOP
)

behavior.toolbarChangedAction(40.5f)

verify(engineView).setVerticalClipping(41)
}

@Test
fun `GIVEN a top toolbar WHEN translation has more than a half decimal THEN set vertical clipping with the ceiling value`() {
val engineView: FakeEngineView = mock()
val behavior = EngineViewBrowserToolbarBehavior(
mock(), null, engineView, 100, ToolbarPosition.TOP
)

behavior.toolbarChangedAction(40.6f)

verify(engineView).setVerticalClipping(41)
}
}

class FakeEngineView(context: Context) : TextView(context), EngineView {
Expand Down

0 comments on commit ef6487d

Please sign in to comment.