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