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 19 20import android.graphics.Color; 21import android.os.Parcel; 22import android.text.TextPaint; 23import android.text.style.BackgroundColorSpan; 24 25import junit.framework.TestCase; 26 27public class BackgroundColorSpanTest extends TestCase { 28 public void testConstructor() { 29 BackgroundColorSpan b = new BackgroundColorSpan(Color.GREEN); 30 31 final Parcel p = Parcel.obtain(); 32 try { 33 b.writeToParcel(p, 0); 34 p.setDataPosition(0); 35 new BackgroundColorSpan(p); 36 } finally { 37 p.recycle(); 38 } 39 } 40 41 public void testUpdateDrawState() { 42 BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.BLACK); 43 44 TextPaint tp = new TextPaint(); 45 backgroundColorSpan.updateDrawState(tp); 46 assertEquals(Color.BLACK, tp.bgColor); 47 48 backgroundColorSpan = new BackgroundColorSpan(Color.BLUE); 49 backgroundColorSpan.updateDrawState(tp); 50 assertEquals(Color.BLUE, tp.bgColor); 51 52 try { 53 backgroundColorSpan.updateDrawState(null); 54 fail("did not throw NullPointerException when TextPaint is null."); 55 } catch (NullPointerException e) { 56 // expected, test success. 57 } 58 } 59 60 public void testGetBackgroundColor() { 61 BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.CYAN); 62 assertEquals(Color.CYAN, backgroundColorSpan.getBackgroundColor()); 63 64 backgroundColorSpan = new BackgroundColorSpan(Color.GRAY); 65 assertEquals(Color.GRAY, backgroundColorSpan.getBackgroundColor()); 66 } 67 68 public void testDescribeContents() { 69 BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.RED); 70 backgroundColorSpan.describeContents(); 71 } 72 73 public void testGetSpanTypeId() { 74 BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.RED); 75 backgroundColorSpan.getSpanTypeId(); 76 } 77 78 public void testWriteToParcel() { 79 Parcel p = Parcel.obtain(); 80 try { 81 BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.RED); 82 backgroundColorSpan.writeToParcel(p, 0); 83 p.setDataPosition(0); 84 BackgroundColorSpan b = new BackgroundColorSpan(p); 85 assertEquals(Color.RED, b.getBackgroundColor()); 86 } finally { 87 p.recycle(); 88 } 89 90 p = Parcel.obtain(); 91 try { 92 BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.MAGENTA); 93 backgroundColorSpan.writeToParcel(p, 0); 94 p.setDataPosition(0); 95 BackgroundColorSpan b = new BackgroundColorSpan(p); 96 assertEquals(Color.MAGENTA, b.getBackgroundColor()); 97 } finally { 98 p.recycle(); 99 } 100 } 101} 102