|
6 | 6 | import static org.junit.Assert.assertFalse;
|
7 | 7 | import static org.junit.Assert.assertNotNull;
|
8 | 8 | import static org.junit.Assert.assertTrue;
|
| 9 | +import static org.mockito.ArgumentMatchers.any; |
9 | 10 | import static org.mockito.ArgumentMatchers.eq;
|
10 | 11 | import static org.mockito.ArgumentMatchers.isA;
|
11 | 12 | import static org.mockito.ArgumentMatchers.isNull;
|
|
16 | 17 | import static org.mockito.Mockito.when;
|
17 | 18 |
|
18 | 19 | import android.app.Application;
|
| 20 | +import android.content.res.ColorStateList; |
19 | 21 | import android.graphics.Bitmap;
|
20 | 22 | import android.graphics.Canvas;
|
21 | 23 | import android.graphics.Color;
|
|
40 | 42 | import org.junit.Rule;
|
41 | 43 | import org.junit.Test;
|
42 | 44 | import org.junit.runner.RunWith;
|
43 |
| -import org.mockito.ArgumentCaptor; |
44 | 45 | import org.mockito.Mock;
|
45 | 46 | import org.mockito.MockitoAnnotations;
|
46 | 47 | import org.robolectric.RobolectricTestRunner;
|
@@ -568,12 +569,53 @@ public void testSetAlphaSetsAlphaOnPaint() {
|
568 | 569 | public void testSetColorFilterSetsColorFilterOnPaint() {
|
569 | 570 | ColorFilter colorFilter = new PorterDuffColorFilter(Color.RED, Mode.ADD);
|
570 | 571 | drawable.setColorFilter(colorFilter);
|
| 572 | + verify(paint).setColorFilter(eq(colorFilter)); |
| 573 | + } |
| 574 | + |
| 575 | + @Config(sdk = Build.VERSION_CODES.LOLLIPOP) |
| 576 | + @Test |
| 577 | + public void testDrawSetsTintListColorFilterOnPaint() { |
| 578 | + ColorStateList tint = |
| 579 | + new ColorStateList( |
| 580 | + new int[][] {new int[] {android.R.attr.state_pressed}, new int[0]}, |
| 581 | + new int[] {Color.RED, Color.GREEN}); |
| 582 | + drawable.setTintList(tint); |
| 583 | + drawable.setTintMode(Mode.ADD); |
| 584 | + when(paint.getColorFilter()).thenReturn(null); |
| 585 | + drawable.draw(new Canvas()); |
| 586 | + |
| 587 | + // draw() temporary sets tint filter then restore. |
| 588 | + verify(paint).setColorFilter(eq(new PorterDuffColorFilter(Color.GREEN, Mode.ADD))); |
| 589 | + verify(paint).setColorFilter(null); |
| 590 | + |
| 591 | + assertThat(drawable.setState(new int[] {android.R.attr.state_pressed})).isTrue(); |
| 592 | + drawable.draw(new Canvas()); |
| 593 | + |
| 594 | + // Pressed state. draw() temporary sets a red color filter. |
| 595 | + verify(paint).setColorFilter(eq(new PorterDuffColorFilter(Color.RED, Mode.ADD))); |
| 596 | + verify(paint, times(2)).setColorFilter(null); |
| 597 | + } |
| 598 | + |
| 599 | + @Config(sdk = Build.VERSION_CODES.LOLLIPOP) |
| 600 | + @Test |
| 601 | + public void testDrawUsesColorFilterInsteadOfTintList() { |
| 602 | + ColorStateList tint = |
| 603 | + new ColorStateList( |
| 604 | + new int[][] {new int[] {android.R.attr.state_pressed}, new int[0]}, |
| 605 | + new int[] {Color.RED, Color.GREEN}); |
| 606 | + drawable.setTintList(tint); |
| 607 | + drawable.setTintMode(Mode.ADD); |
| 608 | + ColorFilter colorFilter = new PorterDuffColorFilter(Color.BLUE, Mode.ADD); |
| 609 | + drawable.setColorFilter(colorFilter); |
| 610 | + verify(paint).setColorFilter(eq(colorFilter)); |
| 611 | + when(paint.getColorFilter()).thenReturn(colorFilter); |
| 612 | + |
| 613 | + drawable.draw(new Canvas()); |
| 614 | + drawable.onStateChange(new int[] {android.R.attr.state_pressed}); |
| 615 | + drawable.draw(new Canvas()); |
571 | 616 |
|
572 |
| - // Use ArgumentCaptor instead of eq() due to b/73121412 where ShadowPorterDuffColorFilter.equals |
573 |
| - // uses a method that can't be found (PorterDuffColorFilter.getColor). |
574 |
| - ArgumentCaptor<ColorFilter> captor = ArgumentCaptor.forClass(ColorFilter.class); |
575 |
| - verify(paint).setColorFilter(captor.capture()); |
576 |
| - assertThat(captor.getValue()).isSameInstanceAs(colorFilter); |
| 617 | + // ColorFilter disables tint list, so draw() should not invoke setColorFilter() any more. |
| 618 | + verify(paint).setColorFilter(any()); |
577 | 619 | }
|
578 | 620 |
|
579 | 621 | @Test
|
|
0 commit comments