15a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda/*
25a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda * Copyright (C) 2011 The Android Open Source Project
35a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda *
45a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda * Licensed under the Apache License, Version 2.0 (the "License");
55a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda * you may not use this file except in compliance with the License.
65a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda * You may obtain a copy of the License at
75a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda *
85a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda *      http://www.apache.org/licenses/LICENSE-2.0
95a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda *
105a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda * Unless required by applicable law or agreed to in writing, software
115a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda * distributed under the License is distributed on an "AS IS" BASIS,
125a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda * See the License for the specific language governing permissions and
145a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda * limitations under the License.
155a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda */
165a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
175a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerdapackage com.android.contacts.detail;
185a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
195a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerdaimport com.android.contacts.R;
205a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerdaimport com.android.contacts.util.StreamItemEntry;
21a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerdaimport com.android.contacts.util.StreamItemEntryBuilder;
225a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
235a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerdaimport android.content.Context;
245a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerdaimport android.test.AndroidTestCase;
255a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerdaimport android.test.suitebuilder.annotation.SmallTest;
265a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerdaimport android.text.Html;
275a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerdaimport android.text.Spanned;
285a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerdaimport android.view.LayoutInflater;
295a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerdaimport android.view.View;
305a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerdaimport android.widget.TextView;
315a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
325a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda/**
335a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda * Unit tests for {@link ContactDetailDisplayUtils}.
345a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda */
355a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda@SmallTest
365a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerdapublic class ContactDetailDisplayUtilsTest extends AndroidTestCase {
375a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    private static final String TEST_STREAM_ITEM_TEXT = "text";
385a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
395a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    private LayoutInflater mLayoutInflater;
405a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
415a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    @Override
425a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    protected void setUp() throws Exception {
435a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        super.setUp();
445a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        mLayoutInflater =
455a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda                (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
465a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    }
475a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
485a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    @Override
495a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    protected void tearDown() throws Exception {
505a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        super.tearDown();
515a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    }
525a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
535a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    public void testAddStreamItemText_IncludesComments() {
545a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        StreamItemEntry streamItem = getTestBuilder().setComment("1 comment").build();
555a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        View streamItemView = addStreamItemText(streamItem);
565a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        assertHasText(streamItemView, R.id.stream_item_comments, "1 comment");
575a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    }
585a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
595a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    public void testAddStreamItemText_IncludesHtmlComments() {
605a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        StreamItemEntry streamItem = getTestBuilder().setComment("1 <b>comment</b>").build();
615a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        View streamItemView = addStreamItemText(streamItem);
625a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        assertHasHtmlText(streamItemView, R.id.stream_item_comments, "1 <b>comment<b>");
635a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    }
645a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
655a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    public void testAddStreamItemText_NoComments() {
665a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        StreamItemEntry streamItem = getTestBuilder().setComment(null).build();
675a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        View streamItemView = addStreamItemText(streamItem);
685a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        assertGone(streamItemView, R.id.stream_item_comments);
695a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    }
705a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
715a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    /** Checks that the given id corresponds to a visible text view with the expected text. */
725a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    private void assertHasText(View parent, int textViewId, String expectedText) {
735a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        TextView textView = (TextView) parent.findViewById(textViewId);
745a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        assertNotNull(textView);
755a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        assertEquals(View.VISIBLE, textView.getVisibility());
765a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        assertEquals(expectedText, textView.getText().toString());
775a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    }
785a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
795a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    /** Checks that the given id corresponds to a visible text view with the expected HTML. */
805a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    private void assertHasHtmlText(View parent, int textViewId, String expectedHtml) {
815a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        TextView textView = (TextView) parent.findViewById(textViewId);
825a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        assertNotNull(textView);
835a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        assertEquals(View.VISIBLE, textView.getVisibility());
845a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        assertSpannableEquals(Html.fromHtml(expectedHtml), textView.getText());
855a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    }
865a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
875a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    /**
885a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda     * Asserts that a char sequence is actually a {@link Spanned} matching the one expected.
895a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda     */
905a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    private void assertSpannableEquals(Spanned expected, CharSequence actualCharSequence) {
915a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        assertEquals(expected.toString(), actualCharSequence.toString());
92965da8446f4bf73217611b43059bd92f311db089Flavio Lerda        assertTrue("char sequence should be an instance of Spanned",
93965da8446f4bf73217611b43059bd92f311db089Flavio Lerda                actualCharSequence instanceof Spanned);
945a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        Spanned actual = (Spanned) actualCharSequence;
955a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        assertEquals(Html.toHtml(expected), Html.toHtml(actual));
965a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    }
975a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
985a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    /** Checks that the given id corresponds to a gone view. */
995a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    private void assertGone(View parent, int textId) {
1005a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        View view = parent.findViewById(textId);
1015a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        assertNotNull(view);
1025a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        assertEquals(View.GONE, view.getVisibility());
1035a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    }
1045a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
105965da8446f4bf73217611b43059bd92f311db089Flavio Lerda    /**
106965da8446f4bf73217611b43059bd92f311db089Flavio Lerda     * Calls {@link ContactDetailDisplayUtils#addStreamItemText(LayoutInflater, Context,
107fd43cddee690e84f56163cd1c0899c3200b624e7Flavio Lerda     * StreamItemEntry, View)} with the default parameters and the given stream item.
108965da8446f4bf73217611b43059bd92f311db089Flavio Lerda     */
109965da8446f4bf73217611b43059bd92f311db089Flavio Lerda    private View addStreamItemText(StreamItemEntry streamItem) {
110fd43cddee690e84f56163cd1c0899c3200b624e7Flavio Lerda        return ContactDetailDisplayUtils.addStreamItemText(getContext(), streamItem,
111be7a9d511eed5a549226b2e1bc2ebd6f65018c4cKatherine Kuan                mLayoutInflater.inflate(R.layout.stream_item_row_text, null));
1125a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    }
1135a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda
1145a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    private StreamItemEntryBuilder getTestBuilder() {
1155a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda        return new StreamItemEntryBuilder().setText(TEST_STREAM_ITEM_TEXT);
1165a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda    }
1175a77a5563706fe1b143d976d1b47abaeb873e138Flavio Lerda}
118