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.util; 18 19 import com.android.contacts.util.HtmlUtils.StreamItemQuoteSpan; 20 21 import android.graphics.drawable.ColorDrawable; 22 import android.test.AndroidTestCase; 23 import android.test.suitebuilder.annotation.SmallTest; 24 import android.text.SpannableStringBuilder; 25 import android.text.Spanned; 26 import android.text.SpannedString; 27 import android.text.style.ImageSpan; 28 import android.text.style.QuoteSpan; 29 30 /** 31 * Tests for {@link HtmlUtils}. 32 * 33 * adb shell am instrument -w -e class com.android.contacts.util.HtmlUtilsTest \ 34 com.android.contacts.tests/android.test.InstrumentationTestRunner 35 */ 36 @SmallTest 37 public class HtmlUtilsTest extends AndroidTestCase { 38 /** 39 * Test for {@link HtmlUtils#postprocess} specifically about trimming newlines. 40 */ 41 public void testPostProcess_trimNewLines() { 42 checkTrimNewLines("", ""); 43 checkTrimNewLines("", "\n"); 44 checkTrimNewLines("", "\n\n"); 45 checkTrimNewLines("a", "a"); 46 checkTrimNewLines("abc", "abc"); 47 checkTrimNewLines("abc", "abc\n"); 48 checkTrimNewLines("abc", "abc\n\n\n"); 49 checkTrimNewLines("ab\nc", "ab\nc\n"); 50 51 assertNull(HtmlUtils.postprocess(getContext(), null)); 52 } 53 54 private final void checkTrimNewLines(String expectedString, CharSequence text) { 55 // Test with both SpannedString and SpannableStringBuilder. 56 assertEquals(expectedString, 57 HtmlUtils.postprocess(getContext(), new SpannedString(text)).toString()); 58 59 assertEquals(expectedString, 60 HtmlUtils.postprocess(getContext(), new SpannableStringBuilder(text)).toString()); 61 } 62 63 public void testPostProcess_with_newlines() { 64 final SpannableStringBuilder builder = new SpannableStringBuilder("01234\n\n"); 65 66 setSpans(builder); 67 68 // First test with a SpannableStringBuilder, as opposed to SpannedString 69 checkPostProcess(HtmlUtils.postprocess(getContext(), builder)); 70 71 // Then pass a SpannedString, which is immutable, but the method should still work. 72 checkPostProcess(HtmlUtils.postprocess(getContext(), new SpannedString(builder))); 73 } 74 75 /** 76 * Same as {@link #testPostProcess_with_newlines}, but text has no newlines. 77 * (The internal code path is slightly different.) 78 */ 79 public void testPostProcess_no_newlines() { 80 final SpannableStringBuilder builder = new SpannableStringBuilder("01234"); 81 82 setSpans(builder); 83 84 // First test with a SpannableStringBuilder, as opposed to SpannedString 85 checkPostProcess(HtmlUtils.postprocess(getContext(), builder)); 86 87 // Then pass a SpannedString, which is immutable, but the method should still work. 88 checkPostProcess(HtmlUtils.postprocess(getContext(), new SpannedString(builder))); 89 } 90 91 private void setSpans(SpannableStringBuilder builder) { 92 builder.setSpan(new ImageSpan(new ColorDrawable(), ImageSpan.ALIGN_BOTTOM), 0, 2, 0); 93 builder.setSpan(new QuoteSpan(), 2, 4, 0); 94 builder.setSpan(new CustomSpan(), 4, builder.length(), 0); 95 } 96 97 private void checkPostProcess(Spanned ret) { 98 // Newlines should be trimmed. 99 assertEquals("01234", ret.toString()); 100 101 // First, check the image span. 102 // - Vertical alignment should be changed to ALIGN_BASELINE 103 // - Drawable shouldn't be changed. 104 ImageSpan[] imageSpans = ret.getSpans(0, ret.length(), ImageSpan.class); 105 assertEquals(1, imageSpans.length); 106 assertEquals(ImageSpan.ALIGN_BASELINE, imageSpans[0].getVerticalAlignment()); 107 assertEquals(ColorDrawable.class, imageSpans[0].getDrawable().getClass()); 108 109 // QuoteSpans should be replaced with StreamItemQuoteSpans. 110 QuoteSpan[] quoteSpans = ret.getSpans(0, ret.length(), QuoteSpan.class); 111 assertEquals(1, quoteSpans.length); 112 assertEquals(StreamItemQuoteSpan.class, quoteSpans[0].getClass()); 113 114 // Other spans should be preserved. 115 CustomSpan[] customSpans = ret.getSpans(0, ret.length(), CustomSpan.class); 116 assertEquals(1, customSpans.length); 117 } 118 119 /** Custom span class used in {@link #testPostProcess} */ 120 private static class CustomSpan { 121 } 122 } 123