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