2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-17 15:26:23 +00:00

[fenix] Bug 1812580 - Wait for the layout cycle to complete before changing anchors

This comes to solve the issue seen in the error logs by adding the anchor
change done for the snackbar to the operation queue on thread Main thread.
Should allow to avoid chaning the snackbar's anchor in between onMeasure and
onLayout.
This commit is contained in:
Mugurell 2023-01-31 13:37:47 +02:00 committed by mergify[bot]
parent 18e026ba28
commit 5ca40120f0
2 changed files with 31 additions and 24 deletions

View File

@ -60,18 +60,20 @@ class FenixSnackbarBehavior<V : View>(
currentAnchorId = dependency?.id ?: View.NO_ID
val params = snackbar.layoutParams as CoordinatorLayout.LayoutParams
snackbar.post {
if (dependency == null || (dependency.id == R.id.toolbar && toolbarPosition == ToolbarPosition.TOP)) {
// Position the snackbar at the bottom of the screen.
params.anchorId = View.NO_ID
params.anchorGravity = Gravity.NO_GRAVITY
params.gravity = Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL
snackbar.layoutParams = params
} else {
// Position the snackbar just above the anchor.
params.anchorId = dependency.id
params.anchorGravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
params.gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
}
snackbar.layoutParams = params
}
}
}
}

View File

@ -9,6 +9,8 @@ import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import io.mockk.every
import io.mockk.mockk
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
import org.junit.Before
@ -20,14 +22,20 @@ import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
@RunWith(FenixRobolectricTestRunner::class)
class FenixSnackbarBehaviorTest {
private val snackbarParams = CoordinatorLayout.LayoutParams(0, 0)
private val snackbarContainer = FrameLayout(testContext)
private val snackbarContainer = mockk<FrameLayout>(relaxed = true)
private var snackbarLayoutParams = CoordinatorLayout.LayoutParams(0, 0)
private val dependency = View(testContext)
private val parent = CoordinatorLayout(testContext)
@Before
fun setup() {
snackbarContainer.layoutParams = snackbarParams
every { snackbarContainer.layoutParams } returns snackbarLayoutParams
every { snackbarContainer.post(any()) } answers {
// Immediately run the given Runnable argument
val action: Runnable = firstArg()
action.run()
true
}
parent.addView(dependency)
}
@ -235,17 +243,14 @@ class FenixSnackbarBehaviorTest {
}
private fun assertSnackbarPlacementAboveAnchor(anchor: View = dependency) {
assertEquals(anchor.id, snackbarContainer.params.anchorId)
assertEquals(Gravity.TOP or Gravity.CENTER_HORIZONTAL, snackbarContainer.params.anchorGravity)
assertEquals(Gravity.TOP or Gravity.CENTER_HORIZONTAL, snackbarContainer.params.gravity)
assertEquals(anchor.id, snackbarLayoutParams.anchorId)
assertEquals(Gravity.TOP or Gravity.CENTER_HORIZONTAL, snackbarLayoutParams.anchorGravity)
assertEquals(Gravity.TOP or Gravity.CENTER_HORIZONTAL, snackbarLayoutParams.gravity)
}
private fun assertSnackbarIsPlacedAtTheBottomOfTheScreen() {
assertEquals(View.NO_ID, snackbarContainer.params.anchorId)
assertEquals(Gravity.NO_GRAVITY, snackbarContainer.params.anchorGravity)
assertEquals(Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL, snackbarContainer.params.gravity)
assertEquals(View.NO_ID, snackbarLayoutParams.anchorId)
assertEquals(Gravity.NO_GRAVITY, snackbarLayoutParams.anchorGravity)
assertEquals(Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL, snackbarLayoutParams.gravity)
}
private val FrameLayout.params
get() = layoutParams as CoordinatorLayout.LayoutParams
}