1192e49e3860a2e65963fca68d397c7ca2030f66dScott Su/*
2192e49e3860a2e65963fca68d397c7ca2030f66dScott Su * Copyright (C) 2008 The Android Open Source Project
3192e49e3860a2e65963fca68d397c7ca2030f66dScott Su *
4192e49e3860a2e65963fca68d397c7ca2030f66dScott Su * Licensed under the Apache License, Version 2.0 (the "License");
5192e49e3860a2e65963fca68d397c7ca2030f66dScott Su * you may not use this file except in compliance with the License.
6192e49e3860a2e65963fca68d397c7ca2030f66dScott Su * You may obtain a copy of the License at
7192e49e3860a2e65963fca68d397c7ca2030f66dScott Su *
8192e49e3860a2e65963fca68d397c7ca2030f66dScott Su *      http://www.apache.org/licenses/LICENSE-2.0
9192e49e3860a2e65963fca68d397c7ca2030f66dScott Su *
10192e49e3860a2e65963fca68d397c7ca2030f66dScott Su * Unless required by applicable law or agreed to in writing, software
11192e49e3860a2e65963fca68d397c7ca2030f66dScott Su * distributed under the License is distributed on an "AS IS" BASIS,
12192e49e3860a2e65963fca68d397c7ca2030f66dScott Su * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13192e49e3860a2e65963fca68d397c7ca2030f66dScott Su * See the License for the specific language governing permissions and
14192e49e3860a2e65963fca68d397c7ca2030f66dScott Su * limitations under the License.
15192e49e3860a2e65963fca68d397c7ca2030f66dScott Su */
16192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
17192e49e3860a2e65963fca68d397c7ca2030f66dScott Supackage android.text.style.cts;
18192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
19192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
20192e49e3860a2e65963fca68d397c7ca2030f66dScott Suimport android.graphics.Color;
21192e49e3860a2e65963fca68d397c7ca2030f66dScott Suimport android.os.Parcel;
22192e49e3860a2e65963fca68d397c7ca2030f66dScott Suimport android.text.TextPaint;
23192e49e3860a2e65963fca68d397c7ca2030f66dScott Suimport android.text.style.BackgroundColorSpan;
24192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
25192e49e3860a2e65963fca68d397c7ca2030f66dScott Suimport junit.framework.TestCase;
26192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
27192e49e3860a2e65963fca68d397c7ca2030f66dScott Supublic class BackgroundColorSpanTest extends TestCase {
28192e49e3860a2e65963fca68d397c7ca2030f66dScott Su    public void testConstructor() {
29192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        BackgroundColorSpan b = new BackgroundColorSpan(Color.GREEN);
30192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
31192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        final Parcel p = Parcel.obtain();
3230a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot        try {
3330a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            b.writeToParcel(p, 0);
3430a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            p.setDataPosition(0);
3530a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            new BackgroundColorSpan(p);
3630a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot        } finally {
3730a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            p.recycle();
3830a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot        }
39192e49e3860a2e65963fca68d397c7ca2030f66dScott Su    }
40192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
41192e49e3860a2e65963fca68d397c7ca2030f66dScott Su    public void testUpdateDrawState() {
42192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.BLACK);
43192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
44192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        TextPaint tp = new TextPaint();
45192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        backgroundColorSpan.updateDrawState(tp);
46192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        assertEquals(Color.BLACK, tp.bgColor);
47192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
48192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        backgroundColorSpan = new BackgroundColorSpan(Color.BLUE);
49192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        backgroundColorSpan.updateDrawState(tp);
50192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        assertEquals(Color.BLUE, tp.bgColor);
51192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
52192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        try {
53192e49e3860a2e65963fca68d397c7ca2030f66dScott Su            backgroundColorSpan.updateDrawState(null);
54192e49e3860a2e65963fca68d397c7ca2030f66dScott Su            fail("did not throw NullPointerException when TextPaint is null.");
55192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        } catch (NullPointerException e) {
56192e49e3860a2e65963fca68d397c7ca2030f66dScott Su            // expected, test success.
57192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        }
58192e49e3860a2e65963fca68d397c7ca2030f66dScott Su    }
59192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
60192e49e3860a2e65963fca68d397c7ca2030f66dScott Su    public void testGetBackgroundColor() {
61192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.CYAN);
62192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        assertEquals(Color.CYAN, backgroundColorSpan.getBackgroundColor());
63192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
64192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        backgroundColorSpan = new BackgroundColorSpan(Color.GRAY);
65192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        assertEquals(Color.GRAY, backgroundColorSpan.getBackgroundColor());
66192e49e3860a2e65963fca68d397c7ca2030f66dScott Su    }
67192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
68192e49e3860a2e65963fca68d397c7ca2030f66dScott Su    public void testDescribeContents() {
69192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.RED);
70192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        backgroundColorSpan.describeContents();
71192e49e3860a2e65963fca68d397c7ca2030f66dScott Su    }
72192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
73192e49e3860a2e65963fca68d397c7ca2030f66dScott Su    public void testGetSpanTypeId() {
74192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.RED);
75192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        backgroundColorSpan.getSpanTypeId();
76192e49e3860a2e65963fca68d397c7ca2030f66dScott Su    }
77192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
78192e49e3860a2e65963fca68d397c7ca2030f66dScott Su    public void testWriteToParcel() {
79192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        Parcel p = Parcel.obtain();
8030a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot        try {
8130a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.RED);
8230a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            backgroundColorSpan.writeToParcel(p, 0);
8330a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            p.setDataPosition(0);
8430a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            BackgroundColorSpan b = new BackgroundColorSpan(p);
8530a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            assertEquals(Color.RED, b.getBackgroundColor());
8630a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot        } finally {
8730a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            p.recycle();
8830a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot        }
89192e49e3860a2e65963fca68d397c7ca2030f66dScott Su
90192e49e3860a2e65963fca68d397c7ca2030f66dScott Su        p = Parcel.obtain();
9130a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot        try {
9230a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.MAGENTA);
9330a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            backgroundColorSpan.writeToParcel(p, 0);
9430a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            p.setDataPosition(0);
9530a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            BackgroundColorSpan b = new BackgroundColorSpan(p);
9630a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            assertEquals(Color.MAGENTA, b.getBackgroundColor());
9730a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot        } finally {
9830a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot            p.recycle();
9930a5c5d2b1caa999829a7924ff24ab33efed5c5bBrett Chabot        }
100192e49e3860a2e65963fca68d397c7ca2030f66dScott Su    }
101192e49e3860a2e65963fca68d397c7ca2030f66dScott Su}
102