Home | History | Annotate | Download | only in drawable
      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.Config
     20 import android.graphics.Color
     21 import android.graphics.drawable.BitmapDrawable
     22 import android.graphics.drawable.ColorDrawable
     23 import android.support.test.InstrumentationRegistry
     24 import androidx.core.graphics.createBitmap
     25 import org.junit.Assert.assertEquals
     26 import org.junit.Test
     27 
     28 class DrawableTest {
     29     private val context = InstrumentationRegistry.getContext()
     30     private val resources = context.resources
     31 
     32     @Test fun bitmapDrawableNoSizeNoConfig() {
     33         val original = createBitmap(10, 10).apply {
     34             eraseColor(Color.RED)
     35         }
     36         val drawable = BitmapDrawable(resources, original)
     37 
     38         val bitmap = drawable.toBitmap()
     39         assertEquals(10, bitmap.width)
     40         assertEquals(10, bitmap.height)
     41         assertEquals(Config.ARGB_8888, bitmap.config)
     42         assertEquals(Color.RED, bitmap.getPixel(5, 5))
     43     }
     44 
     45     @Test fun bitmapDrawableNoSizeRetainedConfig() {
     46         val original = createBitmap(10, 10).apply {
     47             eraseColor(Color.RED)
     48         }
     49         val drawable = BitmapDrawable(resources, original)
     50 
     51         val bitmap = drawable.toBitmap(config = Config.ARGB_8888)
     52         assertEquals(10, bitmap.width)
     53         assertEquals(10, bitmap.height)
     54         assertEquals(Config.ARGB_8888, bitmap.config)
     55         assertEquals(Color.RED, bitmap.getPixel(5, 5))
     56     }
     57 
     58     @Test fun bitmapDrawableNoSizeDifferentConfig() {
     59         val original = createBitmap(10, 10).apply {
     60             eraseColor(Color.RED)
     61         }
     62         val drawable = BitmapDrawable(resources, original)
     63 
     64         val bitmap = drawable.toBitmap(config = Config.ARGB_8888)
     65         assertEquals(10, bitmap.width)
     66         assertEquals(10, bitmap.height)
     67         assertEquals(Config.ARGB_8888, bitmap.config)
     68         assertEquals(Color.RED, bitmap.getPixel(5, 5))
     69     }
     70 
     71     @Test fun bitmapDrawableDifferentSizeNoConfig() {
     72         val original = createBitmap(10, 10).apply {
     73             eraseColor(Color.RED)
     74         }
     75         val drawable = BitmapDrawable(resources, original)
     76 
     77         val bitmap = drawable.toBitmap(20, 20)
     78         assertEquals(20, bitmap.width)
     79         assertEquals(20, bitmap.height)
     80         assertEquals(Config.ARGB_8888, bitmap.config)
     81         assertEquals(Color.RED, bitmap.getPixel(10, 10))
     82     }
     83 
     84     @Test fun bitmapDrawableDifferentSizeDifferentConfig() {
     85         val original = createBitmap(10, 10).apply {
     86             eraseColor(Color.RED)
     87         }
     88         val drawable = BitmapDrawable(resources, original)
     89 
     90         val bitmap = drawable.toBitmap(20, 20, Config.ARGB_8888)
     91         assertEquals(20, bitmap.width)
     92         assertEquals(20, bitmap.height)
     93         assertEquals(Config.ARGB_8888, bitmap.config)
     94         assertEquals(Color.RED, bitmap.getPixel(10, 10))
     95     }
     96 
     97     @Test fun drawableNoConfig() {
     98         val drawable = object : ColorDrawable(Color.RED) {
     99             override fun getIntrinsicWidth() = 10
    100             override fun getIntrinsicHeight() = 10
    101         }
    102 
    103         val bitmap = drawable.toBitmap()
    104         assertEquals(10, bitmap.width)
    105         assertEquals(10, bitmap.height)
    106         assertEquals(Config.ARGB_8888, bitmap.config)
    107         assertEquals(Color.RED, bitmap.getPixel(5, 5))
    108     }
    109 
    110     @Test fun drawableConfig() {
    111         val drawable = object : ColorDrawable(Color.RED) {
    112             override fun getIntrinsicWidth() = 10
    113             override fun getIntrinsicHeight() = 10
    114         }
    115 
    116         val bitmap = drawable.toBitmap(config = Config.RGB_565)
    117         assertEquals(10, bitmap.width)
    118         assertEquals(10, bitmap.height)
    119         assertEquals(Config.RGB_565, bitmap.config)
    120         assertEquals(Color.RED, bitmap.getPixel(5, 5))
    121     }
    122 
    123     @Test fun drawableSize() {
    124         val drawable = object : ColorDrawable(Color.RED) {
    125             override fun getIntrinsicWidth() = 10
    126             override fun getIntrinsicHeight() = 10
    127         }
    128 
    129         val bitmap = drawable.toBitmap(20, 20)
    130         assertEquals(20, bitmap.width)
    131         assertEquals(20, bitmap.height)
    132         assertEquals(Config.ARGB_8888, bitmap.config)
    133         assertEquals(Color.RED, bitmap.getPixel(10, 10))
    134     }
    135 
    136     @Test fun oldBoundsRestored() {
    137         val drawable = object : ColorDrawable(Color.RED) {
    138             override fun getIntrinsicWidth() = 10
    139             override fun getIntrinsicHeight() = 10
    140         }
    141         drawable.setBounds(2, 2, 8, 8)
    142 
    143         val bitmap = drawable.toBitmap()
    144         assertEquals(10, bitmap.width)
    145         assertEquals(10, bitmap.height)
    146 
    147         assertEquals(2, drawable.bounds.left)
    148         assertEquals(2, drawable.bounds.top)
    149         assertEquals(8, drawable.bounds.right)
    150         assertEquals(8, drawable.bounds.bottom)
    151     }
    152 
    153     @Test fun updateBoundsTest() {
    154         val drawable = object : ColorDrawable(Color.RED) {
    155             override fun getIntrinsicWidth() = 10
    156             override fun getIntrinsicHeight() = 10
    157         }
    158         drawable.setBounds(0, 0, 10, 10)
    159 
    160         drawable.updateBounds(1, 2, 3, 4)
    161 
    162         assertEquals(1, drawable.bounds.left)
    163         assertEquals(2, drawable.bounds.top)
    164         assertEquals(3, drawable.bounds.right)
    165         assertEquals(4, drawable.bounds.bottom)
    166     }
    167 }
    168