From 9d2f864f0ac6c75b6601fedcf3a2a6c9c1648184 Mon Sep 17 00:00:00 2001 From: leticiars Date: Fri, 29 Oct 2021 15:05:54 +0000 Subject: [PATCH] [TextInputLayout] Fixed getBoxCornerRadiusBottomEnd and getBoxCornerRadiusBottomStart returning wrong values. PiperOrigin-RevId: 406360531 --- .../material/textfield/TextInputLayout.java | 10 ++-- .../testutils/TextInputLayoutActions.java | 16 +++--- .../textfield/TextInputLayoutTest.java | 53 ++++++++++++++++++- 3 files changed, 65 insertions(+), 14 deletions(-) diff --git a/lib/java/com/google/android/material/textfield/TextInputLayout.java b/lib/java/com/google/android/material/textfield/TextInputLayout.java index 4d755dd7153..91562694c29 100644 --- a/lib/java/com/google/android/material/textfield/TextInputLayout.java +++ b/lib/java/com/google/android/material/textfield/TextInputLayout.java @@ -1295,14 +1295,14 @@ public void setBoxCornerRadii( if (boxBackground == null || boxBackground.getTopLeftCornerResolvedSize() != boxCornerRadiusTopStart || boxBackground.getTopRightCornerResolvedSize() != boxCornerRadiusTopEnd - || boxBackground.getBottomRightCornerResolvedSize() != boxCornerRadiusBottomEnd - || boxBackground.getBottomLeftCornerResolvedSize() != boxCornerRadiusBottomStart) { + || boxBackground.getBottomLeftCornerResolvedSize() != boxCornerRadiusBottomStart + || boxBackground.getBottomRightCornerResolvedSize() != boxCornerRadiusBottomEnd) { shapeAppearanceModel = shapeAppearanceModel.toBuilder() .setTopLeftCornerSize(boxCornerRadiusTopStart) .setTopRightCornerSize(boxCornerRadiusTopEnd) - .setBottomRightCornerSize(boxCornerRadiusBottomEnd) .setBottomLeftCornerSize(boxCornerRadiusBottomStart) + .setBottomRightCornerSize(boxCornerRadiusBottomEnd) .build(); applyBoxAttributes(); } @@ -1335,7 +1335,7 @@ public float getBoxCornerRadiusTopEnd() { * @see #setBoxCornerRadii(float, float, float, float) */ public float getBoxCornerRadiusBottomEnd() { - return boxBackground.getBottomLeftCornerResolvedSize(); + return boxBackground.getBottomRightCornerResolvedSize(); } /** @@ -1345,7 +1345,7 @@ public float getBoxCornerRadiusBottomEnd() { * @see #setBoxCornerRadii(float, float, float, float) */ public float getBoxCornerRadiusBottomStart() { - return boxBackground.getBottomRightCornerResolvedSize(); + return boxBackground.getBottomLeftCornerResolvedSize(); } /** diff --git a/tests/javatests/com/google/android/material/testutils/TextInputLayoutActions.java b/tests/javatests/com/google/android/material/testutils/TextInputLayoutActions.java index 3fd33b5255b..7968c0a2272 100644 --- a/tests/javatests/com/google/android/material/testutils/TextInputLayoutActions.java +++ b/tests/javatests/com/google/android/material/testutils/TextInputLayoutActions.java @@ -641,8 +641,8 @@ public void perform(UiController uiController, View view) { public static ViewAction setBoxCornerRadii( final float topLeftCornerRadius, final float topRightCornerRadius, - final float bottomRightCornerRadius, - final float bottomLeftCornerRadius) { + final float bottomLeftCornerRadius, + final float bottomRightCornerRadius) { return new ViewAction() { @Override public Matcher getConstraints() { @@ -660,8 +660,8 @@ public void perform(UiController uiController, View view) { layout.setBoxCornerRadii( topLeftCornerRadius, topRightCornerRadius, - bottomRightCornerRadius, - bottomLeftCornerRadius); + bottomLeftCornerRadius, + bottomRightCornerRadius); } }; } @@ -669,8 +669,8 @@ public void perform(UiController uiController, View view) { public static ViewAction setBoxCornerRadii( @DimenRes final int topLeftCornerRadiusId, @DimenRes final int topRightCornerRadiusId, - @DimenRes final int bottomRightCornerRadiusId, - @DimenRes final int bottomLeftCornerRadiusId) { + @DimenRes final int bottomLeftCornerRadiusId, + @DimenRes final int bottomRightCornerRadiusId) { return new ViewAction() { @Override public Matcher getConstraints() { @@ -688,8 +688,8 @@ public void perform(UiController uiController, View view) { layout.setBoxCornerRadiiResources( topLeftCornerRadiusId, topRightCornerRadiusId, - bottomRightCornerRadiusId, - bottomLeftCornerRadiusId); + bottomLeftCornerRadiusId, + bottomRightCornerRadiusId); } }; } diff --git a/tests/javatests/com/google/android/material/textfield/TextInputLayoutTest.java b/tests/javatests/com/google/android/material/textfield/TextInputLayoutTest.java index 052b6b1e5ed..8669eace7fa 100644 --- a/tests/javatests/com/google/android/material/textfield/TextInputLayoutTest.java +++ b/tests/javatests/com/google/android/material/textfield/TextInputLayoutTest.java @@ -48,6 +48,7 @@ import static com.google.android.material.testutils.TextInputLayoutActions.setHintTextAppearance; import static com.google.android.material.testutils.TextInputLayoutActions.setPlaceholderText; import static com.google.android.material.testutils.TextInputLayoutActions.setTypeface; +import static com.google.common.truth.Truth.assertThat; import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -687,6 +688,29 @@ public void testFilledBoxBackgroundChangesColor() { onView(withId(R.id.textinput_box_filled)).check(isBoxBackgroundColor(yellow)); } + @Test + public void testSetBoxCornerRadii() { + float boxCornerRadiusTopStart = 0.5f; + float boxCornerRadiusTopEnd = 1f; + float boxCornerRadiusBottomStart = 1.5f; + float boxCornerRadiusBottomEnd = 2f; + + // Set the outline box's corner radii. + onView(withId(R.id.textinput_box_outline)) + .perform( + setBoxCornerRadii( + boxCornerRadiusTopStart, + boxCornerRadiusTopEnd, + boxCornerRadiusBottomStart, + boxCornerRadiusBottomEnd)); + + // Assert values match each respective corner. + onView(withId(R.id.textinput_box_outline)).check(isBoxCornerRadiusTopStart(boxCornerRadiusTopStart)); + onView(withId(R.id.textinput_box_outline)).check(isBoxCornerRadiusTopEnd(boxCornerRadiusTopEnd)); + onView(withId(R.id.textinput_box_outline)).check(isBoxCornerRadiusBottomStart(boxCornerRadiusBottomStart)); + onView(withId(R.id.textinput_box_outline)).check(isBoxCornerRadiusBottomEnd(boxCornerRadiusBottomEnd)); + } + @Test public void testBoxTopEndCornerRadiusChangesWithFloatValue() { float cornerRadiusSmall = @@ -801,14 +825,41 @@ private static ViewAssertion isBoxBackgroundColor(@ColorInt final int boxBackgro }; } + private static ViewAssertion isBoxCornerRadiusTopStart(final float boxCornerRadiusTopStart) { + return (view, noViewFoundException) -> { + assertThat(view).isInstanceOf(TextInputLayout.class); + assertEquals( + boxCornerRadiusTopStart, ((TextInputLayout) view).getBoxCornerRadiusTopStart(), 0.01); + }; + } + private static ViewAssertion isBoxCornerRadiusTopEnd(final float boxCornerRadiusTopEnd) { return (view, noViewFoundException) -> { - assertTrue(view instanceof TextInputLayout); + assertThat(view).isInstanceOf(TextInputLayout.class); assertEquals( boxCornerRadiusTopEnd, ((TextInputLayout) view).getBoxCornerRadiusTopEnd(), 0.01); }; } + private static ViewAssertion isBoxCornerRadiusBottomEnd(final float boxCornerRadiusBottomEnd) { + return (view, noViewFoundException) -> { + assertThat(view).isInstanceOf(TextInputLayout.class); + assertEquals( + boxCornerRadiusBottomEnd, ((TextInputLayout) view).getBoxCornerRadiusBottomEnd(), 0.01); + }; + } + + private static ViewAssertion isBoxCornerRadiusBottomStart( + final float boxCornerRadiusBottomStart) { + return (view, noViewFoundException) -> { + assertThat(view).isInstanceOf(TextInputLayout.class); + assertEquals( + boxCornerRadiusBottomStart, + ((TextInputLayout) view).getBoxCornerRadiusBottomStart(), + 0.01); + }; + } + private static ViewAssertion isCutoutOpen(final boolean open) { return (view, noViewFoundException) -> { assertTrue(view instanceof TextInputLayout);