Skip to content

Commit 9a68c5a

Browse files
committed
Bug 1854131 - Compare availableHeightToTop and availableHeightToBottom when positioning menu. r=android-reviewers,twhite
When the popup doesn't fit up or down, because both availableHeightToTop and availableHeightToBottom are both smaller than containerHeight choose the highest one to choose menu direction. Differential Revision: https://phabricator.services.mozilla.com/D205207
1 parent af4b1f3 commit 9a68c5a

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

mobile/android/android-components/components/browser/menu/src/main/java/mozilla/components/browser/menu/BrowserMenuPositioning.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ internal fun inferMenuPositioningData(
7474
val (availableHeightToTop, availableHeightToBottom) = getMaxAvailableHeightToTopAndBottom(anchor)
7575
val containerHeight = containerView.measuredHeight
7676

77-
val fitsUp = availableHeightToTop >= containerHeight
78-
val fitsDown = availableHeightToBottom >= containerHeight
77+
val fitsUp = availableHeightToTop >= containerHeight || availableHeightToTop > availableHeightToBottom
78+
val fitsDown = availableHeightToBottom >= containerHeight || availableHeightToBottom > availableHeightToTop
7979

8080
return inferMenuPosition(
8181
anchor,

mobile/android/android-components/components/browser/menu/src/test/java/mozilla/components/browser/menu/BrowserMenuPositioningTest.kt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import android.view.ViewGroup
99
import androidx.test.ext.junit.runners.AndroidJUnit4
1010
import mozilla.components.support.test.mock
1111
import mozilla.components.support.test.robolectric.testContext
12+
import mozilla.components.support.test.whenever
1213
import org.junit.Assert
1314
import org.junit.Test
1415
import org.junit.runner.RunWith
1516
import org.mockito.Mockito
17+
import org.mockito.Mockito.spy
1618
import org.robolectric.Shadows
1719
import org.robolectric.shadows.ShadowDisplay
1820

@@ -41,6 +43,61 @@ class BrowserMenuPositioningTest {
4143
Assert.assertEquals(expected, result)
4244
}
4345

46+
@Test
47+
fun `GIVEN inferMenuPositioningData WHEN availableHeightToBottom is bigger than availableHeightToTop THEN it returns a new MenuPositioningData populated with all data needed to show a PopupWindow that fits down`() {
48+
val view: ViewGroup = mock()
49+
Mockito.doReturn(70).`when`(view).measuredHeight
50+
val anchor = View(testContext)
51+
anchor.layoutParams = ViewGroup.LayoutParams(20, 40)
52+
53+
setScreenHeight(50)
54+
55+
val result = inferMenuPositioningData(view, anchor, MenuPositioningData())
56+
57+
val expected = MenuPositioningData(
58+
BrowserMenuPlacement.AnchoredToTop.Dropdown(anchor), // orientation DOWN and fitsDown
59+
askedOrientation = BrowserMenu.Orientation.DOWN, // default
60+
fitsUp = false, // availableHeightToTop(0) is smaller than containerHeight(70) and smaller than availableHeightToBottom(50)
61+
fitsDown = true, // availableHeightToBottom(50) is smaller than containerHeight(70) and bigger than availableHeightToTop(0)
62+
availableHeightToTop = 0,
63+
availableHeightToBottom = 50, // mocked by us above
64+
containerViewHeight = 70, // mocked by us above
65+
)
66+
Assert.assertEquals(expected, result)
67+
}
68+
69+
@Test
70+
fun `GIVEN inferMenuPositioningData WHEN availableHeightToTop is bigger than availableHeightToBottom THEN it returns a new MenuPositioningData populated with all data needed to show a PopupWindow that fits up`() {
71+
val view: ViewGroup = mock()
72+
Mockito.doReturn(70).`when`(view).measuredHeight
73+
val anchor = spy(View(testContext))
74+
anchor.layoutParams = ViewGroup.LayoutParams(20, 40)
75+
76+
whenever(anchor.getLocationOnScreen(IntArray(2))).thenAnswer { invocation ->
77+
val args = invocation.arguments
78+
val location = args[0] as IntArray
79+
location[0] = 0
80+
location[1] = 60
81+
location
82+
}
83+
84+
setScreenHeight(100)
85+
86+
val result = inferMenuPositioningData(view, anchor, MenuPositioningData())
87+
88+
val expected = MenuPositioningData(
89+
BrowserMenuPlacement.AnchoredToBottom.Dropdown(anchor), // orientation UP and fitsUp
90+
askedOrientation = BrowserMenu.Orientation.DOWN, // default
91+
fitsUp = true, // availableHeightToTop(60) is smaller than containerHeight(70) and bigger than availableHeightToBottom(40)
92+
fitsDown = false, // availableHeightToBottom(40) is smaller than containerHeight(70) and smaller than availableHeightToTop(60)
93+
availableHeightToTop = 60, // mocked by us above
94+
availableHeightToBottom = 40,
95+
containerViewHeight = 70, // mocked by us above
96+
)
97+
98+
Assert.assertEquals(expected, result)
99+
}
100+
44101
@Test
45102
fun `GIVEN inferMenuPosition WHEN called with an anchor and the current menu data THEN it returns a new MenuPositioningData with data about positioning the menu`() {
46103
val view: View = mock()

0 commit comments

Comments
 (0)