ResourcesCompatTest.java revision 754cb29c50f09a83251dd4bb633ba445b2411adb
1/*
2 * Copyright (C) 2015 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 */
16package android.support.v4.content.res;
17
18import static org.junit.Assert.assertEquals;
19
20import android.content.res.ColorStateList;
21import android.content.res.Resources;
22import android.graphics.drawable.Drawable;
23import android.os.Build;
24import android.support.compat.test.R;
25import android.support.test.InstrumentationRegistry;
26import android.support.test.filters.SmallTest;
27import android.support.v4.testutils.TestUtils;
28import android.util.DisplayMetrics;
29
30import org.junit.Before;
31import org.junit.Test;
32
33@SmallTest
34public class ResourcesCompatTest {
35    private Resources mResources;
36
37    @Before
38    public void setup() {
39        mResources = InstrumentationRegistry.getContext().getResources();
40    }
41
42    @Test
43    public void testGetColor() throws Throwable {
44        assertEquals("Unthemed color load",
45                ResourcesCompat.getColor(mResources, R.color.text_color, null),
46                0xFFFF8090);
47
48        if (Build.VERSION.SDK_INT >= 23) {
49            // The following tests are only expected to pass on v23+ devices. The result of
50            // calling theme-aware getColor() in pre-v23 is undefined.
51            final Resources.Theme yellowTheme = mResources.newTheme();
52            yellowTheme.applyStyle(R.style.YellowTheme, true);
53            assertEquals("Themed yellow color load", 0xFFF0B000,
54                    ResourcesCompat.getColor(mResources, R.color.simple_themed_selector,
55                            yellowTheme));
56
57            final Resources.Theme lilacTheme = mResources.newTheme();
58            lilacTheme.applyStyle(R.style.LilacTheme, true);
59            assertEquals("Themed lilac color load", 0xFFF080F0,
60                    ResourcesCompat.getColor(mResources, R.color.simple_themed_selector,
61                            lilacTheme));
62        }
63    }
64
65    @Test
66    public void testGetColorStateList() throws Throwable {
67        final ColorStateList unthemedColorStateList =
68                ResourcesCompat.getColorStateList(mResources, R.color.complex_unthemed_selector,
69                        null);
70        assertEquals("Unthemed color state list load: default", 0xFF70A0C0,
71                unthemedColorStateList.getDefaultColor());
72        assertEquals("Unthemed color state list load: focused", 0xFF70B0F0,
73                unthemedColorStateList.getColorForState(
74                        new int[]{android.R.attr.state_focused}, 0));
75        assertEquals("Unthemed color state list load: pressed", 0xFF6080B0,
76                unthemedColorStateList.getColorForState(
77                        new int[]{android.R.attr.state_pressed}, 0));
78
79        if (Build.VERSION.SDK_INT >= 23) {
80            // The following tests are only expected to pass on v23+ devices. The result of
81            // calling theme-aware getColorStateList() in pre-v23 is undefined.
82            final Resources.Theme yellowTheme = mResources.newTheme();
83            yellowTheme.applyStyle(R.style.YellowTheme, true);
84            final ColorStateList themedYellowColorStateList =
85                    ResourcesCompat.getColorStateList(mResources, R.color.complex_themed_selector,
86                            yellowTheme);
87            assertEquals("Themed yellow color state list load: default", 0xFFF0B000,
88                    themedYellowColorStateList.getDefaultColor());
89            assertEquals("Themed yellow color state list load: focused", 0xFFF0A020,
90                    themedYellowColorStateList.getColorForState(
91                            new int[]{android.R.attr.state_focused}, 0));
92            assertEquals("Themed yellow color state list load: pressed", 0xFFE0A040,
93                    themedYellowColorStateList.getColorForState(
94                            new int[]{android.R.attr.state_pressed}, 0));
95
96            final Resources.Theme lilacTheme = mResources.newTheme();
97            lilacTheme.applyStyle(R.style.LilacTheme, true);
98            final ColorStateList themedLilacColorStateList =
99                    ResourcesCompat.getColorStateList(mResources, R.color.complex_themed_selector,
100                            lilacTheme);
101            assertEquals("Themed lilac color state list load: default", 0xFFF080F0,
102                    themedLilacColorStateList.getDefaultColor());
103            assertEquals("Themed lilac color state list load: focused", 0xFFF070D0,
104                    themedLilacColorStateList.getColorForState(
105                            new int[]{android.R.attr.state_focused}, 0));
106            assertEquals("Themed lilac color state list load: pressed", 0xFFE070A0,
107                    themedLilacColorStateList.getColorForState(
108                            new int[]{android.R.attr.state_pressed}, 0));
109        }
110    }
111
112    @Test
113    public void testGetDrawable() throws Throwable {
114        final Drawable unthemedDrawable =
115                ResourcesCompat.getDrawable(mResources, R.drawable.test_drawable_red, null);
116        TestUtils.assertAllPixelsOfColor("Unthemed drawable load",
117                unthemedDrawable, mResources.getColor(R.color.test_red));
118
119        if (Build.VERSION.SDK_INT >= 23) {
120            // The following tests are only expected to pass on v23+ devices. The result of
121            // calling theme-aware getDrawable() in pre-v23 is undefined.
122            final Resources.Theme yellowTheme = mResources.newTheme();
123            yellowTheme.applyStyle(R.style.YellowTheme, true);
124            final Drawable themedYellowDrawable =
125                    ResourcesCompat.getDrawable(mResources, R.drawable.themed_drawable,
126                            yellowTheme);
127            TestUtils.assertAllPixelsOfColor("Themed yellow drawable load",
128                    themedYellowDrawable, 0xFFF0B000);
129
130            final Resources.Theme lilacTheme = mResources.newTheme();
131            lilacTheme.applyStyle(R.style.LilacTheme, true);
132            final Drawable themedLilacDrawable =
133                    ResourcesCompat.getDrawable(mResources, R.drawable.themed_drawable, lilacTheme);
134            TestUtils.assertAllPixelsOfColor("Themed lilac drawable load",
135                    themedLilacDrawable, 0xFFF080F0);
136        }
137    }
138
139    @Test
140    public void testGetDrawableForDensityUnthemed() throws Throwable {
141        // Density-aware drawable loading for now only works on raw bitmap drawables.
142
143        // Different variants of density_aware_drawable are set up in the following way:
144        //    mdpi - 12x12 px which is 12x12 dip
145        //    hdpi - 21x21 px which is 14x14 dip
146        //   xhdpi - 32x32 px which is 16x16 dip
147        //  xxhdpi - 54x54 px which is 18x18 dip
148        // The tests below (on v15+ devices) are checking that an unthemed density-aware
149        // loading of raw bitmap drawables returns a drawable with matching intrinsic
150        // dimensions.
151
152        final Drawable unthemedDrawableForMediumDensity =
153                ResourcesCompat.getDrawableForDensity(mResources, R.drawable.density_aware_drawable,
154                        DisplayMetrics.DENSITY_MEDIUM, null);
155        // For pre-v15 devices we should get a drawable that corresponds to the density of the
156        // current device. For v15+ devices we should get a drawable that corresponds to the
157        // density requested in the API call.
158        final int expectedSizeForMediumDensity = (Build.VERSION.SDK_INT < 15) ?
159                mResources.getDimensionPixelSize(R.dimen.density_aware_size) : 12;
160        assertEquals("Unthemed density-aware drawable load: medium width",
161                expectedSizeForMediumDensity, unthemedDrawableForMediumDensity.getIntrinsicWidth());
162        assertEquals("Unthemed density-aware drawable load: medium height",
163                expectedSizeForMediumDensity,
164                unthemedDrawableForMediumDensity.getIntrinsicHeight());
165
166        final Drawable unthemedDrawableForHighDensity =
167                ResourcesCompat.getDrawableForDensity(mResources, R.drawable.density_aware_drawable,
168                        DisplayMetrics.DENSITY_HIGH, null);
169        // For pre-v15 devices we should get a drawable that corresponds to the density of the
170        // current device. For v15+ devices we should get a drawable that corresponds to the
171        // density requested in the API call.
172        final int expectedSizeForHighDensity = (Build.VERSION.SDK_INT < 15) ?
173                mResources.getDimensionPixelSize(R.dimen.density_aware_size) : 21;
174        assertEquals("Unthemed density-aware drawable load: high width",
175                expectedSizeForHighDensity, unthemedDrawableForHighDensity.getIntrinsicWidth());
176        assertEquals("Unthemed density-aware drawable load: high height",
177                expectedSizeForHighDensity, unthemedDrawableForHighDensity.getIntrinsicHeight());
178
179        final Drawable unthemedDrawableForXHighDensity =
180                ResourcesCompat.getDrawableForDensity(mResources, R.drawable.density_aware_drawable,
181                        DisplayMetrics.DENSITY_XHIGH, null);
182        // For pre-v15 devices we should get a drawable that corresponds to the density of the
183        // current device. For v15+ devices we should get a drawable that corresponds to the
184        // density requested in the API call.
185        final int expectedSizeForXHighDensity = (Build.VERSION.SDK_INT < 15) ?
186                mResources.getDimensionPixelSize(R.dimen.density_aware_size) : 32;
187        assertEquals("Unthemed density-aware drawable load: xhigh width",
188                expectedSizeForXHighDensity, unthemedDrawableForXHighDensity.getIntrinsicWidth());
189        assertEquals("Unthemed density-aware drawable load: xhigh height",
190                expectedSizeForXHighDensity, unthemedDrawableForXHighDensity.getIntrinsicHeight());
191
192        final Drawable unthemedDrawableForXXHighDensity =
193                ResourcesCompat.getDrawableForDensity(mResources, R.drawable.density_aware_drawable,
194                        DisplayMetrics.DENSITY_XXHIGH, null);
195        // For pre-v15 devices we should get a drawable that corresponds to the density of the
196        // current device. For v15+ devices we should get a drawable that corresponds to the
197        // density requested in the API call.
198        final int expectedSizeForXXHighDensity = (Build.VERSION.SDK_INT < 15) ?
199                mResources.getDimensionPixelSize(R.dimen.density_aware_size) : 54;
200        assertEquals("Unthemed density-aware drawable load: xxhigh width",
201                expectedSizeForXXHighDensity, unthemedDrawableForXXHighDensity.getIntrinsicWidth());
202        assertEquals("Unthemed density-aware drawable load: xxhigh height",
203                expectedSizeForXXHighDensity,
204                unthemedDrawableForXXHighDensity.getIntrinsicHeight());
205    }
206
207    @Test
208    public void testGetDrawableForDensityThemed() throws Throwable {
209        if (Build.VERSION.SDK_INT < 21) {
210            // The following tests are only expected to pass on v21+ devices. The result of
211            // calling theme-aware getDrawableForDensity() in pre-v21 is undefined.
212            return;
213        }
214
215        // Density- and theme-aware drawable loading for now only works partially. This test
216        // checks only for theming of a tinted bitmap XML drawable, but not correct scaling.
217
218        // Set up the two test themes, yellow and lilac.
219        final Resources.Theme yellowTheme = mResources.newTheme();
220        yellowTheme.applyStyle(R.style.YellowTheme, true);
221
222        final Resources.Theme lilacTheme = mResources.newTheme();
223        lilacTheme.applyStyle(R.style.LilacTheme, true);
224
225        Drawable themedYellowDrawableForMediumDensity =
226                ResourcesCompat.getDrawableForDensity(mResources, R.drawable.themed_bitmap,
227                        DisplayMetrics.DENSITY_MEDIUM, yellowTheme);
228        // We should get a drawable that corresponds to the theme requested in the API call.
229        TestUtils.assertAllPixelsOfColor("Themed yellow density-aware drawable load : medium color",
230                themedYellowDrawableForMediumDensity, 0xFFF0B000);
231
232        Drawable themedLilacDrawableForMediumDensity =
233                ResourcesCompat.getDrawableForDensity(mResources, R.drawable.themed_bitmap,
234                        DisplayMetrics.DENSITY_MEDIUM, lilacTheme);
235        // We should get a drawable that corresponds to the theme requested in the API call.
236        TestUtils.assertAllPixelsOfColor("Themed lilac density-aware drawable load : medium color",
237                themedLilacDrawableForMediumDensity, 0xFFF080F0);
238
239        Drawable themedYellowDrawableForHighDensity =
240                ResourcesCompat.getDrawableForDensity(mResources, R.drawable.themed_bitmap,
241                        DisplayMetrics.DENSITY_HIGH, yellowTheme);
242        // We should get a drawable that corresponds to the theme requested in the API call.
243        TestUtils.assertAllPixelsOfColor("Themed yellow density-aware drawable load : high color",
244                themedYellowDrawableForHighDensity, 0xFFF0B000);
245
246        Drawable themedLilacDrawableForHighDensity =
247                ResourcesCompat.getDrawableForDensity(mResources, R.drawable.themed_bitmap,
248                        DisplayMetrics.DENSITY_HIGH, lilacTheme);
249        // We should get a drawable that corresponds to the theme requested in the API call.
250        TestUtils.assertAllPixelsOfColor("Themed lilac density-aware drawable load : high color",
251                themedLilacDrawableForHighDensity, 0xFFF080F0);
252
253        Drawable themedYellowDrawableForXHighDensity =
254                ResourcesCompat.getDrawableForDensity(mResources, R.drawable.themed_bitmap,
255                        DisplayMetrics.DENSITY_XHIGH, yellowTheme);
256        // We should get a drawable that corresponds to the theme requested in the API call.
257        TestUtils.assertAllPixelsOfColor("Themed yellow density-aware drawable load : xhigh color",
258                themedYellowDrawableForXHighDensity, 0xFFF0B000);
259
260        Drawable themedLilacDrawableForXHighDensity =
261                ResourcesCompat.getDrawableForDensity(mResources, R.drawable.themed_bitmap,
262                        DisplayMetrics.DENSITY_XHIGH, lilacTheme);
263        // We should get a drawable that corresponds to the theme requested in the API call.
264        TestUtils.assertAllPixelsOfColor("Themed lilac density-aware drawable load : xhigh color",
265                themedLilacDrawableForXHighDensity, 0xFFF080F0);
266
267        Drawable themedYellowDrawableForXXHighDensity =
268                ResourcesCompat.getDrawableForDensity(mResources, R.drawable.themed_bitmap,
269                        DisplayMetrics.DENSITY_XXHIGH, yellowTheme);
270        // We should get a drawable that corresponds to the theme requested in the API call.
271        TestUtils.assertAllPixelsOfColor("Themed yellow density-aware drawable load : xxhigh color",
272                themedYellowDrawableForXXHighDensity, 0xFFF0B000);
273
274        Drawable themedLilacDrawableForXXHighDensity =
275                ResourcesCompat.getDrawableForDensity(mResources, R.drawable.themed_bitmap,
276                        DisplayMetrics.DENSITY_XXHIGH, lilacTheme);
277        // We should get a drawable that corresponds to the theme requested in the API call.
278        TestUtils.assertAllPixelsOfColor("Themed lilac density-aware drawable load : xxhigh color",
279                themedLilacDrawableForXXHighDensity, 0xFFF080F0);
280    }
281}
282