1 /* 2 * Copyright (C) 2011 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 17 package com.android.contacts.common.format; 18 19 import android.test.suitebuilder.annotation.SmallTest; 20 import android.text.Html; 21 import android.text.SpannableString; 22 import android.text.Spanned; 23 import android.text.TextUtils; 24 import android.text.style.StyleSpan; 25 import android.widget.TextView; 26 27 import junit.framework.Assert; 28 29 /** 30 * Utility class to check the value of spanned text in text views. 31 */ 32 @SmallTest 33 public class SpannedTestUtils { 34 /** 35 * Checks that the text contained in the text view matches the given HTML text. 36 * 37 * @param expectedHtmlText the expected text to be in the text view 38 * @param textView the text view from which to get the text 39 */ checkHtmlText(String expectedHtmlText, TextView textView)40 public static void checkHtmlText(String expectedHtmlText, TextView textView) { 41 String actualHtmlText = Html.toHtml((Spanned) textView.getText()); 42 if (TextUtils.isEmpty(expectedHtmlText)) { 43 // If the text is empty, it does not add the <p></p> bits to it. 44 Assert.assertEquals("", actualHtmlText); 45 } else { 46 Assert.assertEquals("<p dir=ltr>" + expectedHtmlText + "</p>\n", actualHtmlText); 47 } 48 } 49 50 51 /** 52 * Assert span exists in the correct location. 53 * 54 * @param seq The spannable string to check. 55 * @param start The starting index. 56 * @param end The ending index. 57 */ assertPrefixSpan(CharSequence seq, int start, int end)58 public static void assertPrefixSpan(CharSequence seq, int start, int end) { 59 Assert.assertTrue(seq instanceof Spanned); 60 Spanned spannable = (Spanned) seq; 61 62 if (start > 0) { 63 Assert.assertEquals(0, getNumForegroundColorSpansBetween(spannable, 0, start - 1)); 64 } 65 Assert.assertEquals(1, getNumForegroundColorSpansBetween(spannable, start, end)); 66 Assert.assertEquals(0, getNumForegroundColorSpansBetween(spannable, end + 1, 67 spannable.length() - 1)); 68 } 69 getNumForegroundColorSpansBetween(Spanned value, int start, int end)70 private static int getNumForegroundColorSpansBetween(Spanned value, int start, int end) { 71 return value.getSpans(start, end, StyleSpan.class).length; 72 } 73 74 /** 75 * Asserts that the given character sequence is not a Spanned object and text is correct. 76 * 77 * @param seq The sequence to check. 78 * @param expected The expected text. 79 */ assertNotSpanned(CharSequence seq, String expected)80 public static void assertNotSpanned(CharSequence seq, String expected) { 81 Assert.assertFalse(seq instanceof Spanned); 82 Assert.assertEquals(expected, seq); 83 } 84 getNextTransition(SpannableString seq, int start)85 public static int getNextTransition(SpannableString seq, int start) { 86 return seq.nextSpanTransition(start, seq.length(), StyleSpan.class); 87 } 88 } 89