diff --git a/lib/java/com/google/android/material/shape/StateListCornerSize.java b/lib/java/com/google/android/material/shape/StateListCornerSize.java index 4aa61b04d16..2cf1da5dd1e 100644 --- a/lib/java/com/google/android/material/shape/StateListCornerSize.java +++ b/lib/java/com/google/android/material/shape/StateListCornerSize.java @@ -77,12 +77,11 @@ public static StateListCornerSize create( @NonNull CornerSize defaultCornerSize) { int resourceId = attributes.getResourceId(index, 0); if (resourceId == ID_NULL) { - return create(defaultCornerSize); + return create(ShapeAppearanceModel.getCornerSize(attributes, index, defaultCornerSize)); } String typeName = context.getResources().getResourceTypeName(resourceId); if (!typeName.equals("xml")) { - return StateListCornerSize.create( - ShapeAppearanceModel.getCornerSize(attributes, index, defaultCornerSize)); + return create(ShapeAppearanceModel.getCornerSize(attributes, index, defaultCornerSize)); } try (XmlResourceParser parser = context.getResources().getXml(resourceId)) { StateListCornerSize stateListCornerSize = new StateListCornerSize(); diff --git a/lib/javatests/com/google/android/material/shape/StateListCornerSizeTest.java b/lib/javatests/com/google/android/material/shape/StateListCornerSizeTest.java new file mode 100644 index 00000000000..1615058a6a7 --- /dev/null +++ b/lib/javatests/com/google/android/material/shape/StateListCornerSizeTest.java @@ -0,0 +1,120 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.material.shape; + +import com.google.android.material.test.R; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import android.content.Context; +import android.content.res.TypedArray; +import android.util.AttributeSet; +import androidx.test.core.app.ApplicationProvider; +import java.util.HashMap; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.android.AttributeSetBuilder; +import org.robolectric.annotation.internal.DoNotInstrument; + +@RunWith(RobolectricTestRunner.class) +@DoNotInstrument +public class StateListCornerSizeTest { + private static final double FLOAT_TOLERANCE = 0.00001; + private static final Context context = ApplicationProvider.getApplicationContext(); + + private Map attributeMap; + + @Before + public void clearAttributeMap() { + attributeMap = new HashMap<>(); + } + + @Test + public void testCreateStateListWithStateList() { + attributeMap.put(R.attr.testCornerSizeAttr, "@xml/state_list_corner_size"); + AttributeSet attributeSet = setupAttributeSetForTest(); + TypedArray attrs = + context.obtainStyledAttributes(attributeSet, new int[] {R.attr.testCornerSizeAttr}); + StateListCornerSize stateListCornerSize = + StateListCornerSize.create( + context, attrs, R.styleable.ShapeTest_testCornerSizeAttr, new AbsoluteCornerSize(0)); + + CornerSize pressedCornerSize = + stateListCornerSize.getCornerSizeForState(new int[] {android.R.attr.state_pressed}); + CornerSize defaultCornerSize = stateListCornerSize.getDefaultCornerSize(); + + assertTrue(pressedCornerSize instanceof AbsoluteCornerSize); + assertEquals(((AbsoluteCornerSize) pressedCornerSize).getCornerSize(), 2, FLOAT_TOLERANCE); + assertTrue(defaultCornerSize instanceof RelativeCornerSize); + assertEquals( + ((RelativeCornerSize) defaultCornerSize).getRelativePercent(), 0.5, FLOAT_TOLERANCE); + } + + @Test + public void testCreateStateListWithDimensionValue() { + attributeMap.put(R.attr.testCornerSizeAttr, "2dp"); + AttributeSet attributeSet = setupAttributeSetForTest(); + TypedArray attrs = + context.obtainStyledAttributes(attributeSet, new int[] {R.attr.testCornerSizeAttr}); + StateListCornerSize stateListCornerSize = + StateListCornerSize.create( + context, attrs, R.styleable.ShapeTest_testCornerSizeAttr, new AbsoluteCornerSize(0)); + + CornerSize pressedCornerSize = + stateListCornerSize.getCornerSizeForState(new int[] {android.R.attr.state_pressed}); + CornerSize defaultCornerSize = stateListCornerSize.getDefaultCornerSize(); + + assertTrue(pressedCornerSize instanceof AbsoluteCornerSize); + assertEquals(((AbsoluteCornerSize) pressedCornerSize).getCornerSize(), 2, FLOAT_TOLERANCE); + assertTrue(defaultCornerSize instanceof AbsoluteCornerSize); + assertEquals(((AbsoluteCornerSize) defaultCornerSize).getCornerSize(), 2, FLOAT_TOLERANCE); + } + + @Test + public void testCreateStateListWithFractionValue() { + attributeMap.put(R.attr.testCornerSizeAttr, "50%"); + AttributeSet attributeSet = setupAttributeSetForTest(); + TypedArray attrs = + context.obtainStyledAttributes(attributeSet, new int[] {R.attr.testCornerSizeAttr}); + StateListCornerSize stateListCornerSize = + StateListCornerSize.create( + context, attrs, R.styleable.ShapeTest_testCornerSizeAttr, new AbsoluteCornerSize(0)); + + CornerSize pressedCornerSize = + stateListCornerSize.getCornerSizeForState(new int[] {android.R.attr.state_pressed}); + CornerSize defaultCornerSize = stateListCornerSize.getDefaultCornerSize(); + + assertTrue(pressedCornerSize instanceof RelativeCornerSize); + assertEquals( + ((RelativeCornerSize) pressedCornerSize).getRelativePercent(), 0.5, FLOAT_TOLERANCE); + assertTrue(defaultCornerSize instanceof RelativeCornerSize); + assertEquals( + ((RelativeCornerSize) defaultCornerSize).getRelativePercent(), 0.5, FLOAT_TOLERANCE); + } + + private AttributeSet setupAttributeSetForTest() { + AttributeSetBuilder attributeSetBuilder = Robolectric.buildAttributeSet(); + for (Map.Entry entry : attributeMap.entrySet()) { + attributeSetBuilder.addAttribute(entry.getKey(), entry.getValue()); + } + return attributeSetBuilder.build(); + } +} diff --git a/lib/javatests/com/google/android/material/shape/res/values/attrs.xml b/lib/javatests/com/google/android/material/shape/res/values/attrs.xml new file mode 100644 index 00000000000..97155c2ced2 --- /dev/null +++ b/lib/javatests/com/google/android/material/shape/res/values/attrs.xml @@ -0,0 +1,21 @@ + + + + + + + diff --git a/lib/javatests/com/google/android/material/shape/res/xml/state_list_corner_size.xml b/lib/javatests/com/google/android/material/shape/res/xml/state_list_corner_size.xml new file mode 100644 index 00000000000..b00af45f918 --- /dev/null +++ b/lib/javatests/com/google/android/material/shape/res/xml/state_list_corner_size.xml @@ -0,0 +1,23 @@ + + + + + +