From 0b8829ad55a0a1e074e6bf70fcd8712e6ced334f Mon Sep 17 00:00:00 2001 From: Ward Abbass Date: Sun, 17 Jan 2021 14:35:46 +0200 Subject: [PATCH] add missing default margin in component title bar (#6903) There was a missing margin at the start of the custom title, added into considerations to fix backwards compatibility with the default spaces --- .../views/stack/topbar/titlebar/TitleBar.java | 3 ++- .../views/stack/topbar/titlebar/TitleBarTest.java | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/android/app/src/main/java/com/reactnativenavigation/views/stack/topbar/titlebar/TitleBar.java b/lib/android/app/src/main/java/com/reactnativenavigation/views/stack/topbar/titlebar/TitleBar.java index a512aef7dc3..262a627a191 100644 --- a/lib/android/app/src/main/java/com/reactnativenavigation/views/stack/topbar/titlebar/TitleBar.java +++ b/lib/android/app/src/main/java/com/reactnativenavigation/views/stack/topbar/titlebar/TitleBar.java @@ -170,7 +170,8 @@ private void alignComponent() { component.setX((getWidth() - component.getWidth() - direction * getStart()) / 2f); } else if (lp.gravity == Gravity.START) { boolean isRTL = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; - component.setX(isRTL ? getWidth() - component.getWidth() - getStart() : 0); + int defaultLeftMarginPx = UiUtils.dpToPx(getContext(), DEFAULT_LEFT_MARGIN); + component.setX(isRTL ? getWidth() - component.getWidth() - defaultLeftMarginPx : defaultLeftMarginPx); } }); } diff --git a/lib/android/app/src/test/java/com/reactnativenavigation/views/stack/topbar/titlebar/TitleBarTest.java b/lib/android/app/src/test/java/com/reactnativenavigation/views/stack/topbar/titlebar/TitleBarTest.java index 69fb891fee8..bd57f792c4a 100644 --- a/lib/android/app/src/test/java/com/reactnativenavigation/views/stack/topbar/titlebar/TitleBarTest.java +++ b/lib/android/app/src/test/java/com/reactnativenavigation/views/stack/topbar/titlebar/TitleBarTest.java @@ -11,6 +11,7 @@ import com.reactnativenavigation.BaseTest; import com.reactnativenavigation.TestUtils; +import com.reactnativenavigation.utils.UiUtils; import org.junit.Test; @@ -19,6 +20,7 @@ import static com.reactnativenavigation.utils.Assertions.assertNotNull; import static com.reactnativenavigation.utils.ViewUtils.findChildByClass; +import static com.reactnativenavigation.views.stack.topbar.titlebar.TitleBar.DEFAULT_LEFT_MARGIN; import static org.assertj.core.api.Java6Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.eq; @@ -72,7 +74,7 @@ public void setComponent_doesNothingIfComponentIsAlreadyAdded() { } @Test - public void setComponent_alignedStartAfterMeasure() { + public void setComponent_alignedStartAfterMeasureWithDefaultMargin() { uut.layout(0, 0, UUT_WIDTH, UUT_HEIGHT); View component = new View(activity); @@ -80,11 +82,12 @@ public void setComponent_alignedStartAfterMeasure() { component.setLayoutParams(new Toolbar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.START)); uut.setComponent(component); uut.onLayout(true, 0, 0, 0, 0); - assertThat(component.getX()).isEqualTo(0); + int defaultLeftMarginPx = UiUtils.dpToPx(activity, DEFAULT_LEFT_MARGIN); + assertThat(component.getX()).isEqualTo(defaultLeftMarginPx); } @Test - public void setComponent_alignedStartRTLAfterMeasure() { + public void setComponent_alignedStartRTLAfterMeasureWithDefaultMargin() { ViewGroup parent = new FrameLayout(activity); parent.layout(0, 0, UUT_WIDTH, UUT_HEIGHT); when(uut.getParent()).thenReturn(parent); @@ -95,7 +98,8 @@ public void setComponent_alignedStartRTLAfterMeasure() { component.setLayoutParams(new Toolbar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.START)); uut.setComponent(component); uut.onLayout(true, 0, 0, 0, 0); - assertThat(component.getX()).isEqualTo(UUT_WIDTH - COMPONENT_WIDTH); + int defaultLeftMarginPx = UiUtils.dpToPx(activity, DEFAULT_LEFT_MARGIN); + assertThat(component.getX()).isEqualTo(UUT_WIDTH - COMPONENT_WIDTH - defaultLeftMarginPx); } @Test