1 /* 2 * Copyright (C) 2008 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 package android.graphics.cts; 17 18 import android.app.Instrumentation; 19 import android.content.res.AssetManager; 20 import android.content.res.Resources; 21 import android.graphics.Bitmap; 22 import android.graphics.BitmapFactory; 23 import android.support.test.filters.SmallTest; 24 import android.support.test.runner.AndroidJUnit4; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 28 import java.io.IOException; 29 import java.io.InputStream; 30 31 import static android.support.test.InstrumentationRegistry.getInstrumentation; 32 import static org.junit.Assert.assertEquals; 33 import static org.junit.Assert.assertFalse; 34 import static org.junit.Assert.assertNull; 35 import static org.junit.Assert.assertTrue; 36 37 @SmallTest 38 @RunWith(AndroidJUnit4.class) 39 public class BitmapFactory_OptionsTest { 40 @Test 41 public void testOptions() { 42 new BitmapFactory.Options(); 43 } 44 45 @Test 46 public void testRequestCancelDecode() { 47 BitmapFactory.Options option = new BitmapFactory.Options(); 48 49 assertFalse(option.mCancel); 50 option.requestCancelDecode(); 51 assertTrue(option.mCancel); 52 } 53 54 @Test 55 public void testExtractMetaData() { 56 Bitmap b; 57 58 BitmapFactory.Options options = new BitmapFactory.Options(); 59 options.inJustDecodeBounds = true; 60 61 Instrumentation instrumentation = getInstrumentation(); 62 Resources resources = instrumentation.getTargetContext().getResources(); 63 64 // Config from source file, RGBA_F16 65 AssetManager assets = resources.getAssets(); 66 try (InputStream in = assets.open("prophoto-rgba16f.png")) { 67 b = BitmapFactory.decodeStream(in, null, options); 68 } catch (IOException e) { 69 throw new RuntimeException("Test failed: ", e); 70 } 71 assertNull(b); 72 assertEquals(64, options.outWidth); 73 assertEquals(64, options.outHeight); 74 assertEquals("image/png", options.outMimeType); 75 assertEquals(Bitmap.Config.RGBA_F16, options.outConfig); 76 77 // Config from source file, ARGB_8888 78 b = BitmapFactory.decodeResource(resources, R.drawable.alpha, options); 79 assertNull(b); 80 assertEquals(Bitmap.Config.ARGB_8888, options.outConfig); 81 82 // Force config to 565 83 options.inPreferredConfig = Bitmap.Config.RGB_565; 84 b = BitmapFactory.decodeResource(resources, R.drawable.icon_green, options); 85 assertNull(b); 86 assertEquals(Bitmap.Config.RGB_565, options.outConfig); 87 88 options = new BitmapFactory.Options(); 89 options.inJustDecodeBounds = true; 90 91 // Unscaled, indexed bitmap 92 b = BitmapFactory.decodeResource(resources, R.drawable.bitmap_indexed, options); 93 assertNull(b); 94 assertEquals("image/gif", options.outMimeType); 95 96 // Scaled, indexed bitmap 97 options.inScaled = true; 98 options.inDensity = 160; 99 options.inScreenDensity = 480; 100 options.inTargetDensity = 320; 101 102 b = BitmapFactory.decodeResource(resources, R.drawable.bitmap_indexed, options); 103 assertNull(b); 104 assertEquals(Bitmap.Config.ARGB_8888, options.outConfig); 105 } 106 } 107