Skip to content

Commit 629e24f

Browse files
pekingmedrchen
authored andcommitted
[Shape] Fixed a bug in loading state list corner size.
PiperOrigin-RevId: 658879368
1 parent 75ff33f commit 629e24f

File tree

4 files changed

+166
-3
lines changed

4 files changed

+166
-3
lines changed

lib/java/com/google/android/material/shape/StateListCornerSize.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,11 @@ public static StateListCornerSize create(
7777
@NonNull CornerSize defaultCornerSize) {
7878
int resourceId = attributes.getResourceId(index, 0);
7979
if (resourceId == ID_NULL) {
80-
return create(defaultCornerSize);
80+
return create(ShapeAppearanceModel.getCornerSize(attributes, index, defaultCornerSize));
8181
}
8282
String typeName = context.getResources().getResourceTypeName(resourceId);
8383
if (!typeName.equals("xml")) {
84-
return StateListCornerSize.create(
85-
ShapeAppearanceModel.getCornerSize(attributes, index, defaultCornerSize));
84+
return create(ShapeAppearanceModel.getCornerSize(attributes, index, defaultCornerSize));
8685
}
8786
try (XmlResourceParser parser = context.getResources().getXml(resourceId)) {
8887
StateListCornerSize stateListCornerSize = new StateListCornerSize();
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.android.material.shape;
17+
18+
import com.google.android.material.test.R;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import android.content.Context;
24+
import android.content.res.TypedArray;
25+
import android.util.AttributeSet;
26+
import androidx.test.core.app.ApplicationProvider;
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.robolectric.Robolectric;
33+
import org.robolectric.RobolectricTestRunner;
34+
import org.robolectric.android.AttributeSetBuilder;
35+
import org.robolectric.annotation.internal.DoNotInstrument;
36+
37+
@RunWith(RobolectricTestRunner.class)
38+
@DoNotInstrument
39+
public class StateListCornerSizeTest {
40+
private static final double FLOAT_TOLERANCE = 0.00001;
41+
private static final Context context = ApplicationProvider.getApplicationContext();
42+
43+
private Map<Integer, String> attributeMap;
44+
45+
@Before
46+
public void clearAttributeMap() {
47+
attributeMap = new HashMap<>();
48+
}
49+
50+
@Test
51+
public void testCreateStateListWithStateList() {
52+
attributeMap.put(R.attr.testCornerSizeAttr, "@xml/state_list_corner_size");
53+
AttributeSet attributeSet = setupAttributeSetForTest();
54+
TypedArray attrs =
55+
context.obtainStyledAttributes(attributeSet, new int[] {R.attr.testCornerSizeAttr});
56+
StateListCornerSize stateListCornerSize =
57+
StateListCornerSize.create(
58+
context, attrs, R.styleable.ShapeTest_testCornerSizeAttr, new AbsoluteCornerSize(0));
59+
60+
CornerSize pressedCornerSize =
61+
stateListCornerSize.getCornerSizeForState(new int[] {android.R.attr.state_pressed});
62+
CornerSize defaultCornerSize = stateListCornerSize.getDefaultCornerSize();
63+
64+
assertTrue(pressedCornerSize instanceof AbsoluteCornerSize);
65+
assertEquals(((AbsoluteCornerSize) pressedCornerSize).getCornerSize(), 2, FLOAT_TOLERANCE);
66+
assertTrue(defaultCornerSize instanceof RelativeCornerSize);
67+
assertEquals(
68+
((RelativeCornerSize) defaultCornerSize).getRelativePercent(), 0.5, FLOAT_TOLERANCE);
69+
}
70+
71+
@Test
72+
public void testCreateStateListWithDimensionValue() {
73+
attributeMap.put(R.attr.testCornerSizeAttr, "2dp");
74+
AttributeSet attributeSet = setupAttributeSetForTest();
75+
TypedArray attrs =
76+
context.obtainStyledAttributes(attributeSet, new int[] {R.attr.testCornerSizeAttr});
77+
StateListCornerSize stateListCornerSize =
78+
StateListCornerSize.create(
79+
context, attrs, R.styleable.ShapeTest_testCornerSizeAttr, new AbsoluteCornerSize(0));
80+
81+
CornerSize pressedCornerSize =
82+
stateListCornerSize.getCornerSizeForState(new int[] {android.R.attr.state_pressed});
83+
CornerSize defaultCornerSize = stateListCornerSize.getDefaultCornerSize();
84+
85+
assertTrue(pressedCornerSize instanceof AbsoluteCornerSize);
86+
assertEquals(((AbsoluteCornerSize) pressedCornerSize).getCornerSize(), 2, FLOAT_TOLERANCE);
87+
assertTrue(defaultCornerSize instanceof AbsoluteCornerSize);
88+
assertEquals(((AbsoluteCornerSize) defaultCornerSize).getCornerSize(), 2, FLOAT_TOLERANCE);
89+
}
90+
91+
@Test
92+
public void testCreateStateListWithFractionValue() {
93+
attributeMap.put(R.attr.testCornerSizeAttr, "50%");
94+
AttributeSet attributeSet = setupAttributeSetForTest();
95+
TypedArray attrs =
96+
context.obtainStyledAttributes(attributeSet, new int[] {R.attr.testCornerSizeAttr});
97+
StateListCornerSize stateListCornerSize =
98+
StateListCornerSize.create(
99+
context, attrs, R.styleable.ShapeTest_testCornerSizeAttr, new AbsoluteCornerSize(0));
100+
101+
CornerSize pressedCornerSize =
102+
stateListCornerSize.getCornerSizeForState(new int[] {android.R.attr.state_pressed});
103+
CornerSize defaultCornerSize = stateListCornerSize.getDefaultCornerSize();
104+
105+
assertTrue(pressedCornerSize instanceof RelativeCornerSize);
106+
assertEquals(
107+
((RelativeCornerSize) pressedCornerSize).getRelativePercent(), 0.5, FLOAT_TOLERANCE);
108+
assertTrue(defaultCornerSize instanceof RelativeCornerSize);
109+
assertEquals(
110+
((RelativeCornerSize) defaultCornerSize).getRelativePercent(), 0.5, FLOAT_TOLERANCE);
111+
}
112+
113+
private AttributeSet setupAttributeSetForTest() {
114+
AttributeSetBuilder attributeSetBuilder = Robolectric.buildAttributeSet();
115+
for (Map.Entry<Integer, String> entry : attributeMap.entrySet()) {
116+
attributeSetBuilder.addAttribute(entry.getKey(), entry.getValue());
117+
}
118+
return attributeSetBuilder.build();
119+
}
120+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright 2024 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<resources>
18+
<declare-styleable name="ShapeTest">
19+
<attr name="testCornerSizeAttr" format="dimension|fraction|reference"/>
20+
</declare-styleable>
21+
</resources>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright 2024 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<selector xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:app="http://schemas.android.com/apk/res-auto">
19+
<item
20+
android:state_pressed="true"
21+
app:cornerSize="2dp" />
22+
<item app:cornerSize="50%" />
23+
</selector>

0 commit comments

Comments
 (0)