Commit 21816c76 authored by MozLando's avatar MozLando
Browse files

Merge #7111



7111: For #7110: Dismiss menu popup window when anchor is detached from window. r=Amejia481,psymoon a=mcarare
Co-authored-by: default avatarmcarare <mihai.carare.dev@gmail.com>
parents 521d6332 3e60f4e8
......@@ -30,7 +30,7 @@ import mozilla.components.support.ktx.android.view.onNextGlobalLayout
*/
open class BrowserMenu internal constructor(
internal val adapter: BrowserMenuAdapter
) {
) : View.OnAttachStateChangeListener {
protected var currentPopup: PopupWindow? = null
private var menuList: RecyclerView? = null
internal var currAnchor: View? = null
......@@ -86,6 +86,7 @@ open class BrowserMenu internal constructor(
}
displayPopup(view, anchor, orientation).also {
anchor.addOnAttachStateChangeListener(this@BrowserMenu)
currAnchor = anchor
}
}.also {
......@@ -149,6 +150,15 @@ open class BrowserMenu internal constructor(
UP,
DOWN
}
override fun onViewDetachedFromWindow(v: View?) {
currentPopup?.dismiss()
currAnchor?.removeOnAttachStateChangeListener(this)
}
override fun onViewAttachedToWindow(v: View?) {
// no-op
}
}
@VisibleForTesting
......
......@@ -357,6 +357,23 @@ class BrowserMenuTest {
verify(popupWindow).showAtLocation(anchor, Gravity.START or Gravity.TOP, 0, 0)
}
@Test
fun `popup is dismissed when anchor is detached`() {
val items = listOf(
SimpleBrowserMenuItem("Mock") {},
SimpleBrowserMenuItem("Menu") {})
val adapter = BrowserMenuAdapter(testContext, items)
val menu = BrowserMenu(adapter)
val anchor = Button(testContext)
val popupWindow = menu.show(anchor)
assertTrue(popupWindow.isShowing)
menu.onViewDetachedFromWindow(anchor)
assertFalse(popupWindow.isShowing)
}
private fun createMockViewWith(y: Int): View {
val view = spy(View(testContext))
doAnswer { invocation ->
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment