1801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal/*
2801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal * Copyright (C) 2017 The Android Open Source Project
3801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal *
4801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal * Licensed under the Apache License, Version 2.0 (the "License");
5801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal * you may not use this file except in compliance with the License.
6801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal * You may obtain a copy of the License at
7801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal *
8801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal *      http://www.apache.org/licenses/LICENSE-2.0
9801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal *
10801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal * Unless required by applicable law or agreed to in writing, software
11801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal * distributed under the License is distributed on an "AS IS" BASIS,
12801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal * See the License for the specific language governing permissions and
14801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal * limitations under the License.
15801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal */
16801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
17ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikaspackage androidx.core.graphics.drawable;
18801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
19801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport static org.junit.Assert.assertEquals;
20f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyalimport static org.junit.Assert.assertNotNull;
21f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyalimport static org.junit.Assert.assertNotSame;
22f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyalimport static org.junit.Assert.assertNull;
23801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport static org.junit.Assert.assertTrue;
24801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
25801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport android.content.Context;
26801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport android.content.Intent;
27b041afb3216a387f27d899b78b60bbd0928409c3Jason Monkimport android.content.res.ColorStateList;
28801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport android.graphics.Bitmap;
29801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport android.graphics.Canvas;
30801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport android.graphics.Color;
31b041afb3216a387f27d899b78b60bbd0928409c3Jason Monkimport android.graphics.PorterDuff;
32801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport android.graphics.drawable.AdaptiveIconDrawable;
33801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport android.graphics.drawable.BitmapDrawable;
34801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport android.graphics.drawable.Drawable;
35b150c70c274cbfd5bd5580e45b7ad8af03a24f57Jason Monkimport android.graphics.drawable.Icon;
36b041afb3216a387f27d899b78b60bbd0928409c3Jason Monkimport android.net.Uri;
37801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport android.os.Build;
38b041afb3216a387f27d899b78b60bbd0928409c3Jason Monkimport android.os.Bundle;
39801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport android.support.test.InstrumentationRegistry;
40801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport android.support.test.filters.SdkSuppress;
41801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport android.support.test.filters.SmallTest;
42801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport android.support.test.runner.AndroidJUnit4;
439dede51868bbbe16aadcd65e04860bea8ea50e05Aurimas Liutikas
44ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.core.content.ContextCompat;
459dede51868bbbe16aadcd65e04860bea8ea50e05Aurimas Liutikasimport androidx.core.test.R;
46801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
47801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport org.junit.Test;
48801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport org.junit.runner.RunWith;
49801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
50801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport java.io.ByteArrayOutputStream;
51b041afb3216a387f27d899b78b60bbd0928409c3Jason Monkimport java.io.File;
52b041afb3216a387f27d899b78b60bbd0928409c3Jason Monkimport java.io.FileOutputStream;
53b041afb3216a387f27d899b78b60bbd0928409c3Jason Monkimport java.io.IOException;
54b041afb3216a387f27d899b78b60bbd0928409c3Jason Monkimport java.io.InputStream;
55b041afb3216a387f27d899b78b60bbd0928409c3Jason Monkimport java.io.OutputStream;
56801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalimport java.util.Arrays;
57801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
58801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal@RunWith(AndroidJUnit4.class)
59801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal@SmallTest
60801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyalpublic class IconCompatTest {
61801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
62b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    private Context mContext = InstrumentationRegistry.getContext();
63b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
64f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal    private static void verifyClippedCircle(Bitmap bitmap, int fillColor, int size) {
65801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        assertEquals(size, bitmap.getHeight());
66801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        assertEquals(bitmap.getWidth(), bitmap.getHeight());
67801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        assertEquals(fillColor, bitmap.getPixel(size / 2, size / 2));
68801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
69801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        assertEquals(Color.TRANSPARENT, bitmap.getPixel(0, 0));
70801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        assertEquals(Color.TRANSPARENT, bitmap.getPixel(0, size - 1));
71801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        assertEquals(Color.TRANSPARENT, bitmap.getPixel(size - 1, 0));
72f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal
73f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        // The badge is a full rectangle located at the bottom right corner. Check a single pixel
74f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        // in that region to verify that badging was properly applied.
75801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        assertEquals(Color.TRANSPARENT, bitmap.getPixel(size - 1, size - 1));
76801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    }
77801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
78f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal    public static void verifyBadgeBitmap(Intent intent, int bgColor, int badgeColor) {
79f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        Bitmap bitmap = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
80f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        int w = bitmap.getWidth();
81f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        int h = bitmap.getHeight();
82f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal
83f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        assertEquals(bgColor, bitmap.getPixel(2, 2));
84f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        assertEquals(bgColor, bitmap.getPixel(w - 2, 2));
85f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        assertEquals(bgColor, bitmap.getPixel(2, h - 2));
86f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        assertEquals(badgeColor, bitmap.getPixel(w - 2, h - 2));
87f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal    }
88f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal
89801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    @Test
90801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    public void testClipAdaptiveIcon() throws Throwable {
91801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Bitmap source = Bitmap.createBitmap(200, 150, Bitmap.Config.ARGB_8888);
92801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        source.eraseColor(Color.RED);
93201d7cec27eadf0bfe3ad0c95f1289f2d4a3290aSunny Goyal        Bitmap result = IconCompat.createLegacyIconFromAdaptiveIcon(source, false);
94801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        verifyClippedCircle(result, Color.RED, 100);
95801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    }
96801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
97801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    @Test
98801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    public void testCreateWithBitmap_legacy() {
99801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
100801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        bitmap.eraseColor(Color.RED);
101801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Intent intent = new Intent();
102b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        IconCompat.createWithBitmap(bitmap).addToShortcutIntent(intent, null, mContext);
103801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        assertEquals(bitmap, intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON));
104801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    }
105801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
106801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    @Test
107f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal    public void testAddBitmapToShortcutIntent_badged() {
108f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        Context context = InstrumentationRegistry.getContext();
109f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
110f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        bitmap.eraseColor(Color.RED);
111f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        Intent intent = new Intent();
112f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal
113f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        Drawable badge = ContextCompat.getDrawable(context, R.drawable.test_drawable_blue);
114b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        IconCompat.createWithBitmap(bitmap).addToShortcutIntent(intent, badge, mContext);
115f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        assertNotSame(bitmap, intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON));
116f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal
117f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        verifyBadgeBitmap(intent, Color.RED, ContextCompat.getColor(context, R.color.test_blue));
118f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal    }
119f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal
120f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal    @Test
121f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal    public void testAddResourceToShortcutIntent_badged() {
122f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        Context context = InstrumentationRegistry.getContext();
123f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        Intent intent = new Intent();
124f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal
125f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        // No badge
126f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        IconCompat.createWithResource(context, R.drawable.test_drawable_green)
127b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk                .addToShortcutIntent(intent, null, mContext);
128f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        assertNotNull(intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE));
129f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        assertNull(intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON));
130f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal
131f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        intent = new Intent();
132f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        Drawable badge = ContextCompat.getDrawable(context, R.drawable.test_drawable_red);
133f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        IconCompat.createWithResource(context, R.drawable.test_drawable_blue)
134b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk                .addToShortcutIntent(intent, badge, mContext);
135f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal
136f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        assertNull(intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE));
137f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal        verifyBadgeBitmap(intent, ContextCompat.getColor(context, R.color.test_blue),
138f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal                ContextCompat.getColor(context, R.color.test_red));
139f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal    }
140f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal
141f89a582fe62d49dd573203020c44028ee865ff7bSunny Goyal    @Test
142801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    @SdkSuppress(minSdkVersion = Build.VERSION_CODES.M)
143801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    public void testCreateWithBitmap() {
144801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
145801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        bitmap.eraseColor(Color.RED);
146801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        IconCompat compat = IconCompat.createWithBitmap(bitmap);
147801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Drawable d = compat.toIcon().loadDrawable(InstrumentationRegistry.getContext());
148801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        assertTrue(d instanceof BitmapDrawable);
149801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        assertEquals(bitmap, ((BitmapDrawable) d).getBitmap());
150801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    }
151801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
152801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    @Test
153801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    public void testCreateWithAdaptiveBitmap_legacy() {
154801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
155801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        bitmap.eraseColor(Color.GREEN);
156801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Intent intent = new Intent();
157b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        IconCompat.createWithAdaptiveBitmap(bitmap).addToShortcutIntent(intent, null, mContext);
158801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
159801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Bitmap clipped = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
160801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        verifyClippedCircle(clipped, Color.GREEN, clipped.getWidth());
161801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    }
162801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
163801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    @Test
164801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    @SdkSuppress(minSdkVersion = Build.VERSION_CODES.M)
165801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    public void testCreateWithAdaptiveBitmap() {
166801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
167801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        bitmap.eraseColor(Color.GREEN);
168801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        IconCompat compat = IconCompat.createWithAdaptiveBitmap(bitmap);
169801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Drawable d = compat.toIcon().loadDrawable(InstrumentationRegistry.getContext());
170d70bd6edd39e1a2d2404f21fbcbe8ad500a4a7daAurimas Liutikas        if (Build.VERSION.SDK_INT >= 26) {
171801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal            assertTrue(d instanceof AdaptiveIconDrawable);
172801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        } else {
173801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal            assertTrue(d instanceof BitmapDrawable);
174801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal            Bitmap clipped = ((BitmapDrawable) d).getBitmap();
175801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal            verifyClippedCircle(clipped, Color.GREEN, clipped.getWidth());
176801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        }
177801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    }
178801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
179801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    @Test
180801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    @SdkSuppress(minSdkVersion = Build.VERSION_CODES.M)
181801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    public void testCreateWithData() {
182801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
183801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        bitmap.eraseColor(Color.YELLOW);
184801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
185801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        ByteArrayOutputStream out = new ByteArrayOutputStream();
186801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
187801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        byte[] bytes = out.toByteArray();
188801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
189801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        byte[] resultCopy = Arrays.copyOf(bytes, bytes.length + 100);
190801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        // Shift all elements by 20
191801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        for (int i = bytes.length - 1; i >= 0; i--) {
192801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal            resultCopy[i + 20] = resultCopy[i];
193801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        }
194801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
195801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        IconCompat compat = IconCompat.createWithData(resultCopy, 20, bytes.length);
196801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Drawable d = compat.toIcon().loadDrawable(InstrumentationRegistry.getContext());
197801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        assertTrue(d instanceof BitmapDrawable);
198801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        assertTrue(bitmap.sameAs(((BitmapDrawable) d).getBitmap()));
199801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    }
200801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
201801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    @Test
202801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    @SdkSuppress(minSdkVersion = Build.VERSION_CODES.M)
203801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    public void testCreateWithResource() {
204801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Context context = InstrumentationRegistry.getContext();
205801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Drawable original = context.getDrawable(R.drawable.test_drawable_red);
206801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
207801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        IconCompat compat = IconCompat.createWithResource(context, R.drawable.test_drawable_red);
208801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Drawable d = compat.toIcon().loadDrawable(InstrumentationRegistry.getContext());
209801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
210801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        // Drawables are same classes
211801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        assertEquals(original.getClass(), d.getClass());
212801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
213801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Bitmap orgBitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
214801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        original.setBounds(0, 0, 200, 200);
215801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        original.draw(new Canvas(orgBitmap));
216801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
217801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        Bitmap compatBitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
218801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        d.setBounds(0, 0, 200, 200);
219801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        d.draw(new Canvas(compatBitmap));
220801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal
221801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        // Drawables behave the same
222801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal        assertTrue(orgBitmap.sameAs(compatBitmap));
223801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal    }
224b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
225b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    @Test
226b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    public void testBitmapIconCompat() {
227b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        verifyIconCompatValidity(
228b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk                IconCompat.createWithBitmap(Bitmap.createBitmap(16, 16, Bitmap.Config.ARGB_8888)));
229b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    }
230b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
231b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    @Test
232b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    public void testDataIconCompat() {
233b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        byte[] data = new byte[4];
234b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        data[0] = data[1] = data[2] = data[3] = (byte) 255;
235b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        verifyIconCompatValidity(IconCompat.createWithData(data, 0, 4));
236b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    }
237b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
238b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    @Test
239b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    public void testFileIconCompat() throws IOException {
240b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        File file = new File(mContext.getFilesDir(), "testimage.jpg");
241b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        try {
242b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            writeSampleImage(file);
243b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            assertTrue(file.exists());
244b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
245b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            verifyIconCompatValidity(IconCompat.createWithContentUri(Uri.fromFile(file)));
246b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
247b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            verifyIconCompatValidity(IconCompat.createWithContentUri(file.toURI().toString()));
248b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        } finally {
249b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            file.delete();
250b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        }
251b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    }
252b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
253b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    @Test
254b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    public void testResourceIconCompat() {
255b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        verifyIconCompatValidity(IconCompat.createWithResource(mContext, R.drawable.bmp_test));
256b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    }
257b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
258b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    @Test
259b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    public void testBitmapIconCompat_getType() {
260b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        IconCompat icon = IconCompat.createWithBitmap(Bitmap.createBitmap(16, 16,
261b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk                Bitmap.Config.ARGB_8888));
262b150c70c274cbfd5bd5580e45b7ad8af03a24f57Jason Monk        assertEquals(Icon.TYPE_BITMAP, icon.getType());
263b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    }
264b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
265b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    @Test
266b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    public void testDataIconCompat_getType() {
267b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        byte[] data = new byte[4];
268b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        data[0] = data[1] = data[2] = data[3] = (byte) 255;
269b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        IconCompat icon = IconCompat.createWithData(data, 0, 4);
270b150c70c274cbfd5bd5580e45b7ad8af03a24f57Jason Monk        assertEquals(Icon.TYPE_DATA, icon.getType());
271b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    }
272b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
273b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    @Test
274b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    public void testFileIconCompat_getType() throws IOException {
275b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        File file = new File(mContext.getFilesDir(), "testimage.jpg");
276b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        try {
277b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            writeSampleImage(file);
278b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            assertTrue(file.exists());
279b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            String filePath = file.toURI().getPath();
280b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
281b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            IconCompat icon = IconCompat.createWithContentUri(Uri.fromFile(file));
282b150c70c274cbfd5bd5580e45b7ad8af03a24f57Jason Monk            assertEquals(Icon.TYPE_URI, icon.getType());
283b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            assertEquals(filePath, icon.getUri().getPath());
284b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
285b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            icon = IconCompat.createWithContentUri(file.toURI().toString());
286b150c70c274cbfd5bd5580e45b7ad8af03a24f57Jason Monk            assertEquals(Icon.TYPE_URI, icon.getType());
287b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            assertEquals(filePath, icon.getUri().getPath());
288b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        } finally {
289b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            file.delete();
290b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        }
291b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    }
292b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
293b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    @Test
294b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    public void testResourceIconCompat_getType() {
295b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        IconCompat icon = IconCompat.createWithResource(mContext, R.drawable.bmp_test);
296b150c70c274cbfd5bd5580e45b7ad8af03a24f57Jason Monk        assertEquals(Icon.TYPE_RESOURCE, icon.getType());
297b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        assertEquals("androidx.core.test", icon.getResPackage());
298b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        assertEquals(R.drawable.bmp_test, icon.getResId());
299b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    }
300b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
301b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    private void writeSampleImage(File imagefile) throws IOException {
302b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        try (InputStream source = mContext.getResources().openRawResource(R.drawable.testimage);
303b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk             OutputStream target = new FileOutputStream(imagefile)) {
304b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            byte[] buffer = new byte[1024];
305b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            for (int len = source.read(buffer); len >= 0; len = source.read(buffer)) {
306b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk                target.write(buffer, 0, len);
307b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk            }
308b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        }
309b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    }
310b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
311b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    // Check if the created icon is valid and doesn't cause crashes for the public methods.
312b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    private void verifyIconCompatValidity(IconCompat icon) {
313b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        assertNotNull(icon);
314b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
315b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        // tint properties.
316b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        icon.setTint(Color.BLUE);
317b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        icon.setTintList(ColorStateList.valueOf(Color.RED));
318b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        icon.setTintMode(PorterDuff.Mode.XOR);
319b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
320b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        // Parcelable methods.
321b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        Bundle b = icon.toBundle();
322b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
323b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        assertNotNull(IconCompat.createFromBundle(b));
324b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk
325b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        // loading drawable synchronously.
326b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk        assertNotNull(icon.loadDrawable(mContext));
327b041afb3216a387f27d899b78b60bbd0928409c3Jason Monk    }
328801ca190c6df56bdf67ebd0a320ab5912640db10Sunny Goyal}
329