• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2014, Google Inc.
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 com.android.mail.utils;
17 
18 import android.test.AndroidTestCase;
19 import android.test.suitebuilder.annotation.SmallTest;
20 import android.text.SpannableString;
21 import android.text.Spanned;
22 import android.text.style.TextAppearanceSpan;
23 
24 /**
25  * Tests for {@link Utils}.
26  */
27 @SmallTest
28 public class UtilsTest extends AndroidTestCase {
29 
testInsertStringWithStyle()30     public void testInsertStringWithStyle() {
31         final String entire = "Hello World!";
32         final String sub = "World";
33         final int appearance = android.R.style.TextAppearance_Holo_Small;
34         final Spanned actual = Utils.insertStringWithStyle(
35                 getContext(), entire, sub, appearance);
36         final SpannableString expected = new SpannableString(entire);
37         expected.setSpan(new TextAppearanceSpan(getContext(), appearance), 6, 11, 0);
38 
39         assertSpannedEquals(expected, actual);
40     }
41 
testInsertStringWithStyle_substringNotInEntire()42     public void testInsertStringWithStyle_substringNotInEntire() {
43         final String entire = "Hello World!";
44         final String sub = "foo";
45         final int appearance = android.R.style.TextAppearance_Holo_Small;
46         final Spanned actual = Utils.insertStringWithStyle(
47                 getContext(), entire, sub, appearance);
48         final SpannableString expected = new SpannableString(entire);
49 
50         assertSpannedEquals(expected, actual);
51     }
52 
assertSpannedEquals(Spanned expected, Spanned actual)53     public static void assertSpannedEquals(Spanned expected, Spanned actual) {
54         assertEquals(expected.length(), actual.length());
55         assertEquals(expected.toString(), actual.toString());
56         if (expected.length() > 0) {
57             TextAppearanceSpan[] expectedSpans =
58                     expected.getSpans(0, expected.length(), TextAppearanceSpan.class);
59             TextAppearanceSpan[] actualSpans =
60                     actual.getSpans(0, actual.length(), TextAppearanceSpan.class);
61             assertEquals(expectedSpans.length, actualSpans.length);
62             for (int i = 0 ; i < expectedSpans.length ; i++) {
63                 assertTextAppearanceSpanEquals(expectedSpans[i], actualSpans[i]);
64             }
65         }
66     }
67 
assertTextAppearanceSpanEquals( TextAppearanceSpan expected, TextAppearanceSpan actual)68     public static void assertTextAppearanceSpanEquals(
69             TextAppearanceSpan expected, TextAppearanceSpan actual) {
70         assertEquals(expected.describeContents(), actual.describeContents());
71         assertEquals(expected.getTextStyle(), actual.getTextStyle());
72         assertEquals(expected.getTextColor(), actual.getTextColor());
73         assertEquals(expected.getLinkTextColor(), actual.getLinkTextColor());
74         assertEquals(expected.getFamily(), actual.getFamily());
75         assertEquals(expected.getTextSize(), actual.getTextSize());
76     }
77 }
78