|
| 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 | +} |
0 commit comments