1 /*
2  * Copyright (C) 2018 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 androidx.core.graphics.drawable
18 
19 import android.graphics.Bitmap
20 import android.graphics.Bitmap.Config.ARGB_8888
21 import android.graphics.Canvas
22 import android.graphics.Color
23 import android.graphics.drawable.Icon
24 import androidx.core.graphics.createBitmap
25 import androidx.core.net.toUri
26 import androidx.test.core.app.ApplicationProvider
27 import androidx.test.filters.SdkSuppress
28 import androidx.test.filters.SmallTest
29 import java.io.File
30 import org.junit.Assert.assertEquals
31 import org.junit.Test
32 
33 @SdkSuppress(minSdkVersion = 26)
34 @SmallTest
35 class IconTest {
36     private val context = ApplicationProvider.getApplicationContext() as android.content.Context
37 
38     @Test
fromBitmapAdaptivenull39     fun fromBitmapAdaptive() {
40         val density = context.resources.displayMetrics.density
41 
42         val edge = (108.0f * density + 0.5f).toInt()
43         val bitmap = Bitmap.createBitmap(edge, edge, ARGB_8888).apply { eraseColor(Color.RED) }
44         val icon = bitmap.toAdaptiveIcon()
45 
46         val rendered = icon.toIntrinsicBitmap()
47         val masked = (72.0f * density + 0.5f).toInt()
48         assertEquals(masked, rendered.width)
49         assertEquals(masked, rendered.height)
50         // Grab a pixel from the middle to ensure we are not being masked.
51         assertEquals(Color.RED, rendered.getPixel(masked / 2, masked / 2))
52     }
53 
54     @Test
fromBitmapnull55     fun fromBitmap() {
56         val bitmap = createBitmap(1, 1).apply { eraseColor(Color.RED) }
57         val icon = bitmap.toIcon()
58 
59         val rendered = icon.toIntrinsicBitmap()
60         assertEquals(1, rendered.width)
61         assertEquals(1, rendered.height)
62         assertEquals(Color.RED, rendered.getPixel(0, 0))
63     }
64 
65     @Test
fromUrinull66     fun fromUri() {
67         // Icon can't read from file:///android_asset/red.png so copy to a real file.
68         val cacheFile = File(context.cacheDir, "red.png")
69         context.assets.open("red.png").use { cacheFile.writeBytes(it.readBytes()) }
70 
71         val uri = cacheFile.toUri()
72         val icon = uri.toIcon()
73 
74         val rendered = icon.toIntrinsicBitmap()
75         assertEquals(1, rendered.width)
76         assertEquals(1, rendered.height)
77         assertEquals(Color.RED, rendered.getPixel(0, 0))
78     }
79 
80     @Test
fromByteArraynull81     fun fromByteArray() {
82         val bytes = context.assets.open("red.png").use { it.readBytes() }
83         val icon = bytes.toIcon()
84 
85         val rendered = icon.toIntrinsicBitmap()
86         assertEquals(1, rendered.width)
87         assertEquals(1, rendered.height)
88         assertEquals(Color.RED, rendered.getPixel(0, 0))
89     }
90 
toIntrinsicBitmapnull91     private fun Icon.toIntrinsicBitmap(): Bitmap {
92         val drawable = loadDrawable(context)!!
93         val bitmap = createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight)
94         drawable.setBounds(0, 0, drawable.intrinsicHeight, drawable.intrinsicHeight)
95         drawable.draw(Canvas(bitmap))
96         return bitmap
97     }
98 }
99