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 17package com.android.contacts.detail; 18 19import com.android.contacts.R; 20import com.android.contacts.util.StreamItemEntry; 21import com.android.contacts.util.StreamItemEntryBuilder; 22 23import android.content.Context; 24import android.test.AndroidTestCase; 25import android.test.suitebuilder.annotation.SmallTest; 26import android.text.Html; 27import android.text.Spanned; 28import android.view.LayoutInflater; 29import android.view.View; 30import android.widget.TextView; 31 32/** 33 * Unit tests for {@link ContactDetailDisplayUtils}. 34 */ 35@SmallTest 36public class ContactDetailDisplayUtilsTest extends AndroidTestCase { 37 private static final String TEST_STREAM_ITEM_TEXT = "text"; 38 39 private LayoutInflater mLayoutInflater; 40 41 @Override 42 protected void setUp() throws Exception { 43 super.setUp(); 44 mLayoutInflater = 45 (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 46 } 47 48 @Override 49 protected void tearDown() throws Exception { 50 super.tearDown(); 51 } 52 53 public void testAddStreamItemText_IncludesComments() { 54 StreamItemEntry streamItem = getTestBuilder().setComment("1 comment").build(getContext()); 55 View streamItemView = addStreamItemText(streamItem); 56 assertHasText(streamItemView, R.id.stream_item_comments, "1 comment"); 57 } 58 59 public void testAddStreamItemText_IncludesHtmlComments() { 60 StreamItemEntry streamItem = getTestBuilder().setComment("1 <b>comment</b>") 61 .build(getContext()); 62 View streamItemView = addStreamItemText(streamItem); 63 assertHasHtmlText(streamItemView, R.id.stream_item_comments, "1 <b>comment<b>"); 64 } 65 66 public void testAddStreamItemText_NoComments() { 67 StreamItemEntry streamItem = getTestBuilder().setComment(null).build(getContext()); 68 View streamItemView = addStreamItemText(streamItem); 69 assertGone(streamItemView, R.id.stream_item_comments); 70 } 71 72 /** Checks that the given id corresponds to a visible text view with the expected text. */ 73 private void assertHasText(View parent, int textViewId, String expectedText) { 74 TextView textView = (TextView) parent.findViewById(textViewId); 75 assertNotNull(textView); 76 assertEquals(View.VISIBLE, textView.getVisibility()); 77 assertEquals(expectedText, textView.getText().toString()); 78 } 79 80 /** Checks that the given id corresponds to a visible text view with the expected HTML. */ 81 private void assertHasHtmlText(View parent, int textViewId, String expectedHtml) { 82 TextView textView = (TextView) parent.findViewById(textViewId); 83 assertNotNull(textView); 84 assertEquals(View.VISIBLE, textView.getVisibility()); 85 assertSpannableEquals(Html.fromHtml(expectedHtml), textView.getText()); 86 } 87 88 /** 89 * Asserts that a char sequence is actually a {@link Spanned} matching the one expected. 90 */ 91 private void assertSpannableEquals(Spanned expected, CharSequence actualCharSequence) { 92 assertEquals(expected.toString(), actualCharSequence.toString()); 93 assertTrue("char sequence should be an instance of Spanned", 94 actualCharSequence instanceof Spanned); 95 Spanned actual = (Spanned) actualCharSequence; 96 assertEquals(Html.toHtml(expected), Html.toHtml(actual)); 97 } 98 99 /** Checks that the given id corresponds to a gone view. */ 100 private void assertGone(View parent, int textId) { 101 View view = parent.findViewById(textId); 102 assertNotNull(view); 103 assertEquals(View.GONE, view.getVisibility()); 104 } 105 106 /** 107 * Calls {@link ContactDetailDisplayUtils#addStreamItemText(LayoutInflater, Context, 108 * StreamItemEntry, View)} with the default parameters and the given stream item. 109 */ 110 private View addStreamItemText(StreamItemEntry streamItem) { 111 return ContactDetailDisplayUtils.addStreamItemText(getContext(), streamItem, 112 mLayoutInflater.inflate(R.layout.stream_item_container, null)); 113 } 114 115 private StreamItemEntryBuilder getTestBuilder() { 116 return new StreamItemEntryBuilder().setText(TEST_STREAM_ITEM_TEXT); 117 } 118} 119