|
| 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 |
| 18 | + |
| 19 | +import android.graphics.Bitmap |
| 20 | +import android.graphics.ImageDecoder |
| 21 | +import org.junit.Assert.assertEquals |
| 22 | +import org.junit.BeforeClass |
| 23 | +import org.junit.Test |
| 24 | +import java.io.ByteArrayOutputStream |
| 25 | +import java.nio.ByteBuffer |
| 26 | + |
| 27 | +class ImageDecoderTest { |
| 28 | + |
| 29 | + @Test fun decodeBitmap() { |
| 30 | + val src = ImageDecoder.createSource(buffer) |
| 31 | + val decodedBitmap = src.decodeBitmap { _, _ -> setTargetSize(10, 10) } |
| 32 | + assertEquals(10, decodedBitmap.width) |
| 33 | + assertEquals(10, decodedBitmap.height) |
| 34 | + } |
| 35 | + |
| 36 | + @Test fun decodeDrawable() { |
| 37 | + val src = ImageDecoder.createSource(buffer) |
| 38 | + val decodedDrawable = src.decodeDrawable { _, _ -> setTargetSize(10, 10) } |
| 39 | + assertEquals(10, decodedDrawable.intrinsicWidth) |
| 40 | + assertEquals(10, decodedDrawable.intrinsicHeight) |
| 41 | + } |
| 42 | + |
| 43 | + companion object { |
| 44 | + private lateinit var buffer: ByteBuffer |
| 45 | + |
| 46 | + @BeforeClass |
| 47 | + @JvmStatic |
| 48 | + fun beforeClass() { |
| 49 | + val stream = ByteArrayOutputStream().apply { |
| 50 | + Bitmap |
| 51 | + .createBitmap(1, 1, Bitmap.Config.ARGB_8888) |
| 52 | + .compress(Bitmap.CompressFormat.JPEG, 100, this) |
| 53 | + } |
| 54 | + |
| 55 | + buffer = ByteBuffer.wrap(stream.toByteArray()) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments