Skip to content

Commit f259def

Browse files
cketchamleticiarossi
authored andcommitted
Force the path to be calculated for a copied MaterialShapeDrawable
PiperOrigin-RevId: 249833111
1 parent b8f9f5e commit f259def

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,10 @@ public MaterialShapeDrawableState(MaterialShapeDrawableState orig) {
13101310

13111311
@Override
13121312
public Drawable newDrawable() {
1313-
return new MaterialShapeDrawable(this);
1313+
MaterialShapeDrawable msd = new MaterialShapeDrawable(this);
1314+
// Force the calculation of the path for the new drawable.
1315+
msd.pathDirty = true;
1316+
return msd;
13141317
}
13151318

13161319
@Override
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (C) 2019 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+
17+
package com.google.android.material.shape;
18+
19+
import static org.junit.Assert.assertFalse;
20+
import static org.mockito.Matchers.any;
21+
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.verify;
23+
24+
import android.content.res.ColorStateList;
25+
import android.graphics.Canvas;
26+
import android.graphics.Paint;
27+
import android.graphics.Path;
28+
import android.graphics.drawable.Drawable;
29+
import androidx.test.filters.SmallTest;
30+
import androidx.test.runner.AndroidJUnit4;
31+
import org.junit.Before;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.mockito.ArgumentCaptor;
35+
36+
@SmallTest
37+
@RunWith(AndroidJUnit4.class)
38+
public class MaterialShapeDrawableDrawTest {
39+
40+
private static final int SHAPE_SIZE = 300;
41+
42+
private final ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel();
43+
private final MaterialShapeDrawable materialShapeDrawable =
44+
new MaterialShapeDrawable(shapeAppearanceModel);
45+
46+
@Before
47+
public void setMaterialShapeDrawableBounds() {
48+
materialShapeDrawable.setBounds(0, 0, SHAPE_SIZE, SHAPE_SIZE);
49+
}
50+
51+
@Test
52+
public void createNewDrawable_notRoundRect_pathIsCalculated() {
53+
shapeAppearanceModel.setTopLeftCorner(CornerFamily.CUT, 10);
54+
Drawable copy = copyMaterialShapeDrawable();
55+
56+
Canvas canvasMock = mock(Canvas.class);
57+
copy.draw(canvasMock);
58+
ArgumentCaptor<Path> pathArgument = ArgumentCaptor.forClass(Path.class);
59+
verify(canvasMock).drawPath(pathArgument.capture(), any(Paint.class));
60+
61+
assertFalse("Trying to draw but path is empty", pathArgument.getValue().isEmpty());
62+
}
63+
64+
@Test
65+
public void newDrawableFromContstantState() {
66+
Drawable copy = copyMaterialShapeDrawable();
67+
68+
copy.draw(new Canvas());
69+
}
70+
71+
@Test
72+
public void createNewDrawable_withStroke() {
73+
materialShapeDrawable.setStroke(10, 0);
74+
Drawable copy = copyMaterialShapeDrawable();
75+
76+
copy.draw(new Canvas());
77+
}
78+
79+
@Test
80+
public void createNewDrawable_withFill() {
81+
materialShapeDrawable.setFillColor(ColorStateList.valueOf(0));
82+
Drawable copy = copyMaterialShapeDrawable();
83+
84+
copy.draw(new Canvas());
85+
}
86+
87+
@Test
88+
public void canDrawNewDrawableFromConstantState_withFill() {
89+
materialShapeDrawable.setFillColor(ColorStateList.valueOf(0));
90+
Drawable copy = copyMaterialShapeDrawable();
91+
92+
copy.draw(new Canvas());
93+
}
94+
95+
private Drawable copyMaterialShapeDrawable() {
96+
// Do a draw first to simulate copying from a drawable that has been used.
97+
materialShapeDrawable.draw(new Canvas());
98+
return materialShapeDrawable.getConstantState().newDrawable();
99+
}
100+
}

0 commit comments

Comments
 (0)