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 17package android.text.style.cts; 18 19import com.android.cts.stub.R; 20 21 22import android.content.Context; 23import android.graphics.Bitmap; 24import android.graphics.drawable.BitmapDrawable; 25import android.graphics.drawable.Drawable; 26import android.net.Uri; 27import android.test.AndroidTestCase; 28import android.text.style.DynamicDrawableSpan; 29import android.text.style.ImageSpan; 30import android.widget.cts.WidgetTestUtils; 31 32public class ImageSpanTest extends AndroidTestCase { 33 public void testConstructor() { 34 int width = 80; 35 int height = 120; 36 int[] color = new int[width * height]; 37 Bitmap b = Bitmap.createBitmap(color, width, height, Bitmap.Config.RGB_565); 38 39 new ImageSpan(b); 40 new ImageSpan(b, DynamicDrawableSpan.ALIGN_BOTTOM); 41 new ImageSpan(b, DynamicDrawableSpan.ALIGN_BASELINE); 42 43 Drawable d = mContext.getResources().getDrawable(R.drawable.pass); 44 new ImageSpan(d); 45 new ImageSpan(d, DynamicDrawableSpan.ALIGN_BOTTOM); 46 new ImageSpan(d, DynamicDrawableSpan.ALIGN_BASELINE); 47 48 new ImageSpan(d, "cts test."); 49 new ImageSpan(d, "cts test.", DynamicDrawableSpan.ALIGN_BOTTOM); 50 new ImageSpan(d, "cts test.", DynamicDrawableSpan.ALIGN_BASELINE); 51 52 new ImageSpan(mContext, Uri.parse("content://user/a/b")); 53 new ImageSpan(mContext, Uri.parse("content://user/a/b"), 54 DynamicDrawableSpan.ALIGN_BOTTOM); 55 new ImageSpan(mContext, Uri.parse("content://user/a/b"), 56 DynamicDrawableSpan.ALIGN_BASELINE); 57 58 new ImageSpan(mContext, R.drawable.pass); 59 new ImageSpan(mContext, R.drawable.pass, DynamicDrawableSpan.ALIGN_BOTTOM); 60 new ImageSpan(mContext, R.drawable.pass, DynamicDrawableSpan.ALIGN_BASELINE); 61 62 new ImageSpan((Bitmap) null); 63 new ImageSpan((Drawable) null); 64 new ImageSpan((Drawable) null, (String) null); 65 new ImageSpan((Context) null, -1); 66 new ImageSpan((Bitmap) null, -1); 67 new ImageSpan((Drawable) null, -1); 68 new ImageSpan((Drawable) null, (String) null, -1); 69 new ImageSpan((Context) null, -1, -1); 70 } 71 72 public void testGetSource() { 73 Drawable d = mContext.getResources().getDrawable(R.drawable.pass); 74 75 ImageSpan imageSpan = new ImageSpan(d); 76 assertNull(imageSpan.getSource()); 77 78 String source = "cts test."; 79 imageSpan = new ImageSpan(d, source); 80 assertEquals(source, imageSpan.getSource()); 81 82 source = "content://user/a/b"; 83 imageSpan = new ImageSpan(mContext, Uri.parse(source)); 84 assertEquals(source, imageSpan.getSource()); 85 } 86 87 public void testGetDrawable() { 88 Drawable drawable = mContext.getResources().getDrawable(R.drawable.pass); 89 90 ImageSpan imageSpan = new ImageSpan(drawable); 91 assertSame(drawable, imageSpan.getDrawable()); 92 93 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; 94 imageSpan = new ImageSpan(mContext, R.drawable.pass); 95 BitmapDrawable resultDrawable = (BitmapDrawable) imageSpan.getDrawable(); 96 WidgetTestUtils.assertEquals(bitmapDrawable.getBitmap(), resultDrawable.getBitmap()); 97 98 imageSpan = new ImageSpan(mContext, Uri.parse("unknown uri.")); 99 assertNull(imageSpan.getDrawable()); 100 101 imageSpan = new ImageSpan((Context) null, -1); 102 assertNull(imageSpan.getDrawable()); 103 } 104} 105