1a6b4c791665c452b818cf545155565f379f74651Chiao Cheng/* 2a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * Copyright (C) 2011 The Android Open Source Project 3a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * 4a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License"); 5a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * you may not use this file except in compliance with the License. 6a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * You may obtain a copy of the License at 7a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * 8a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * http://www.apache.org/licenses/LICENSE-2.0 9a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * 10a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * Unless required by applicable law or agreed to in writing, software 11a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS, 12a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * See the License for the specific language governing permissions and 14a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * limitations under the License. 15a6b4c791665c452b818cf545155565f379f74651Chiao Cheng */ 16a6b4c791665c452b818cf545155565f379f74651Chiao Cheng 1769c182afb0e6d82a341a28b4317aa703af768906Gary Maipackage com.android.contacts.format; 18a6b4c791665c452b818cf545155565f379f74651Chiao Cheng 19a6b4c791665c452b818cf545155565f379f74651Chiao Chengimport android.text.Html; 20ccba9501355b4888cc0bb07be93d0a50845e6c6aChristine Chenimport android.text.SpannableString; 21a6b4c791665c452b818cf545155565f379f74651Chiao Chengimport android.text.Spanned; 22a6b4c791665c452b818cf545155565f379f74651Chiao Chengimport android.text.TextUtils; 23426e5d474d81fc018c01814f6087cb17f5fe8510Yorke Leeimport android.text.style.StyleSpan; 24a6b4c791665c452b818cf545155565f379f74651Chiao Chengimport android.widget.TextView; 25a6b4c791665c452b818cf545155565f379f74651Chiao Cheng 26a6b4c791665c452b818cf545155565f379f74651Chiao Chengimport junit.framework.Assert; 27a6b4c791665c452b818cf545155565f379f74651Chiao Cheng 28a6b4c791665c452b818cf545155565f379f74651Chiao Cheng/** 29a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * Utility class to check the value of spanned text in text views. 30a6b4c791665c452b818cf545155565f379f74651Chiao Cheng */ 31a6b4c791665c452b818cf545155565f379f74651Chiao Chengpublic class SpannedTestUtils { 32a6b4c791665c452b818cf545155565f379f74651Chiao Cheng /** 33a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * Checks that the text contained in the text view matches the given HTML text. 34a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * 35a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * @param expectedHtmlText the expected text to be in the text view 36a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * @param textView the text view from which to get the text 37a6b4c791665c452b818cf545155565f379f74651Chiao Cheng */ 38a6b4c791665c452b818cf545155565f379f74651Chiao Cheng public static void checkHtmlText(String expectedHtmlText, TextView textView) { 39a6b4c791665c452b818cf545155565f379f74651Chiao Cheng String actualHtmlText = Html.toHtml((Spanned) textView.getText()); 40a6b4c791665c452b818cf545155565f379f74651Chiao Cheng if (TextUtils.isEmpty(expectedHtmlText)) { 41a6b4c791665c452b818cf545155565f379f74651Chiao Cheng // If the text is empty, it does not add the <p></p> bits to it. 42a6b4c791665c452b818cf545155565f379f74651Chiao Cheng Assert.assertEquals("", actualHtmlText); 43a6b4c791665c452b818cf545155565f379f74651Chiao Cheng } else { 44a6b4c791665c452b818cf545155565f379f74651Chiao Cheng Assert.assertEquals("<p dir=ltr>" + expectedHtmlText + "</p>\n", actualHtmlText); 45a6b4c791665c452b818cf545155565f379f74651Chiao Cheng } 46a6b4c791665c452b818cf545155565f379f74651Chiao Cheng } 47a6b4c791665c452b818cf545155565f379f74651Chiao Cheng 48a6b4c791665c452b818cf545155565f379f74651Chiao Cheng 49a6b4c791665c452b818cf545155565f379f74651Chiao Cheng /** 50a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * Assert span exists in the correct location. 51a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * 52a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * @param seq The spannable string to check. 53a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * @param start The starting index. 54a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * @param end The ending index. 55a6b4c791665c452b818cf545155565f379f74651Chiao Cheng */ 56a6b4c791665c452b818cf545155565f379f74651Chiao Cheng public static void assertPrefixSpan(CharSequence seq, int start, int end) { 57a6b4c791665c452b818cf545155565f379f74651Chiao Cheng Assert.assertTrue(seq instanceof Spanned); 58a6b4c791665c452b818cf545155565f379f74651Chiao Cheng Spanned spannable = (Spanned) seq; 59a6b4c791665c452b818cf545155565f379f74651Chiao Cheng 60a6b4c791665c452b818cf545155565f379f74651Chiao Cheng if (start > 0) { 61a6b4c791665c452b818cf545155565f379f74651Chiao Cheng Assert.assertEquals(0, getNumForegroundColorSpansBetween(spannable, 0, start - 1)); 62a6b4c791665c452b818cf545155565f379f74651Chiao Cheng } 63a6b4c791665c452b818cf545155565f379f74651Chiao Cheng Assert.assertEquals(1, getNumForegroundColorSpansBetween(spannable, start, end)); 64a6b4c791665c452b818cf545155565f379f74651Chiao Cheng Assert.assertEquals(0, getNumForegroundColorSpansBetween(spannable, end + 1, 65a6b4c791665c452b818cf545155565f379f74651Chiao Cheng spannable.length() - 1)); 66a6b4c791665c452b818cf545155565f379f74651Chiao Cheng } 67a6b4c791665c452b818cf545155565f379f74651Chiao Cheng 68a6b4c791665c452b818cf545155565f379f74651Chiao Cheng private static int getNumForegroundColorSpansBetween(Spanned value, int start, int end) { 69426e5d474d81fc018c01814f6087cb17f5fe8510Yorke Lee return value.getSpans(start, end, StyleSpan.class).length; 70a6b4c791665c452b818cf545155565f379f74651Chiao Cheng } 71a6b4c791665c452b818cf545155565f379f74651Chiao Cheng 72a6b4c791665c452b818cf545155565f379f74651Chiao Cheng /** 73a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * Asserts that the given character sequence is not a Spanned object and text is correct. 74a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * 75a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * @param seq The sequence to check. 76a6b4c791665c452b818cf545155565f379f74651Chiao Cheng * @param expected The expected text. 77a6b4c791665c452b818cf545155565f379f74651Chiao Cheng */ 78a6b4c791665c452b818cf545155565f379f74651Chiao Cheng public static void assertNotSpanned(CharSequence seq, String expected) { 79a6b4c791665c452b818cf545155565f379f74651Chiao Cheng Assert.assertFalse(seq instanceof Spanned); 80a6b4c791665c452b818cf545155565f379f74651Chiao Cheng Assert.assertEquals(expected, seq); 81a6b4c791665c452b818cf545155565f379f74651Chiao Cheng } 823efbe59d5252b691af58bebdc27af7ae36bf72d6Christine Chen 83ccba9501355b4888cc0bb07be93d0a50845e6c6aChristine Chen public static int getNextTransition(SpannableString seq, int start) { 84426e5d474d81fc018c01814f6087cb17f5fe8510Yorke Lee return seq.nextSpanTransition(start, seq.length(), StyleSpan.class); 853efbe59d5252b691af58bebdc27af7ae36bf72d6Christine Chen } 86a6b4c791665c452b818cf545155565f379f74651Chiao Cheng} 87