DrawableCompatTest.java revision 3ac77bf186f87ecad4bf0063b2f6c4384efbd56a
1/*
2 * Copyright (C) 2016 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
17package android.support.v4.graphics;
18
19import android.content.res.ColorStateList;
20import android.graphics.Color;
21import android.graphics.PorterDuff;
22import android.graphics.Rect;
23import android.graphics.drawable.Drawable;
24import android.graphics.drawable.GradientDrawable;
25import android.support.test.runner.AndroidJUnit4;
26import android.os.Build;
27import android.support.v4.graphics.drawable.DrawableCompat;
28import android.test.AndroidTestCase;
29import android.test.suitebuilder.annotation.SmallTest;
30import org.junit.Test;
31import org.junit.runner.RunWith;
32
33import static org.junit.Assert.assertEquals;
34import static org.junit.Assert.assertNotNull;
35import static org.junit.Assert.assertNotSame;
36import static org.junit.Assert.assertSame;
37import static org.mockito.Mockito.mock;
38import static org.mockito.Mockito.times;
39import static org.mockito.Mockito.verify;
40
41@RunWith(AndroidJUnit4.class)
42@SmallTest
43public class DrawableCompatTest {
44    @Test
45    public void testDrawableWrap() {
46        final Drawable original = new GradientDrawable();
47        final Drawable wrappedDrawable = DrawableCompat.wrap(original);
48
49        if (Build.VERSION.SDK_INT < 23) {
50            assertNotSame(original, wrappedDrawable);
51        } else {
52            assertSame(original, wrappedDrawable);
53        }
54    }
55
56    @Test
57    public void testDrawableUnwrap() {
58        final Drawable original = new GradientDrawable();
59        final Drawable wrappedDrawable = DrawableCompat.wrap(original);
60        assertSame(original, DrawableCompat.unwrap(wrappedDrawable));
61    }
62
63    @Test
64    public void testDrawableChangeBoundsCopy() {
65        final Rect bounds = new Rect(0, 0, 10, 10);
66
67        final Drawable drawable = new GradientDrawable();
68
69        final Drawable wrapper = DrawableCompat.wrap(drawable);
70        wrapper.setBounds(bounds);
71
72        // Assert that the bounds were given to the original drawable
73        assertEquals(bounds, drawable.getBounds());
74    }
75
76    @Test
77    public void testDrawableWrapOnlyWrapsOnce() {
78        final Drawable wrappedDrawable = DrawableCompat.wrap(new GradientDrawable());
79        assertSame(wrappedDrawable, DrawableCompat.wrap(wrappedDrawable));
80    }
81
82    @Test
83    public void testWrapMutatedDrawableHasConstantState() {
84        // First create a Drawable, and mutated it so that it has a constant state
85        Drawable drawable = new GradientDrawable();
86        drawable = drawable.mutate();
87        assertNotNull(drawable.getConstantState());
88
89        // Now wrap and assert that the wrapper also returns a constant state
90        final Drawable wrapper = DrawableCompat.wrap(drawable);
91        assertNotNull(wrapper.getConstantState());
92    }
93
94    @Test
95    public void testWrappedDrawableHasCallbackSet() {
96        // First create a Drawable
97        final Drawable drawable = new GradientDrawable();
98
99        // Now wrap it and set a mock as the wrapper's callback
100        final Drawable wrapper = DrawableCompat.wrap(drawable);
101        final Drawable.Callback mockCallback = mock(Drawable.Callback.class);
102        wrapper.setCallback(mockCallback);
103
104        // Now make the wrapped drawable invalidate itself
105        drawable.invalidateSelf();
106
107        // ...and verify that the wrapper calls to be invalidated
108        verify(mockCallback, times(1)).invalidateDrawable(wrapper);
109    }
110
111    @Test
112    public void testDoesNotWrapTintAwareDrawable() {
113        final TestTintAwareDrawable tintAwareDrawable = new TestTintAwareDrawable();
114        final Drawable wrapped = DrawableCompat.wrap(tintAwareDrawable);
115        // Assert that the tint aware drawable was not wrapped
116        assertSame(tintAwareDrawable, wrapped);
117    }
118
119    @Test
120    public void testTintAwareDrawableGetsTintCallsDirectly() {
121        final TestTintAwareDrawable d = mock(TestTintAwareDrawable.class);
122
123        final ColorStateList tint = ColorStateList.valueOf(Color.BLACK);
124        final PorterDuff.Mode tintMode = PorterDuff.Mode.DST;
125
126        // Now set the tint list and mode using DrawableCompat
127        DrawableCompat.setTintList(d, tint);
128        DrawableCompat.setTintMode(d, tintMode);
129
130        // Verify that the calls were directly on to the TintAwareDrawable
131        verify(d).setTintList(tint);
132        verify(d).setTintMode(tintMode);
133    }
134
135}