1/*
2 * Copyright (C) 2015 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.messaging.ui.conversation;
18
19import android.test.suitebuilder.annotation.MediumTest;
20import android.test.suitebuilder.annotation.Suppress;
21import android.view.View;
22import android.widget.TextView;
23
24import com.android.messaging.FakeFactory;
25import com.android.messaging.R;
26import com.android.messaging.datamodel.FakeCursor;
27import com.android.messaging.datamodel.data.TestDataFactory;
28import com.android.messaging.ui.ViewTest;
29import com.android.messaging.ui.conversation.ConversationMessageView;
30import com.android.messaging.ui.conversation.ConversationMessageView.ConversationMessageViewHost;
31import com.android.messaging.util.Dates;
32
33import org.mockito.Mock;
34
35@MediumTest
36public class ConversationMessageViewTest extends ViewTest<ConversationMessageView> {
37    @Mock ConversationMessageViewHost mockHost;
38
39    @Override
40    protected void setUp() throws Exception {
41        super.setUp();
42        FakeFactory.register(getInstrumentation().getTargetContext());
43    }
44
45    @Override
46    protected ConversationMessageView getView() {
47        final ConversationMessageView view = super.getView();
48        view.setHost(mockHost);
49        return view;
50    }
51
52    protected void verifyContent(final ConversationMessageView view, final String messageText,
53            final boolean showTimestamp, final String timestampText) {
54
55        final TextView messageTextView = (TextView) view.findViewById(R.id.message_text);
56        final TextView statusTextView = (TextView) view.findViewById(R.id.message_status);
57
58        assertNotNull(messageTextView);
59        assertEquals(messageText, messageTextView.getText());
60
61        if (showTimestamp) {
62            assertEquals(View.VISIBLE, statusTextView.getVisibility());
63            assertEquals(timestampText, statusTextView.getText());
64        } else {
65            assertEquals(View.GONE, statusTextView.getVisibility());
66        }
67    }
68
69    public void testBind() {
70        final ConversationMessageView view = getView();
71
72        final FakeCursor cursor = TestDataFactory.getConversationMessageCursor();
73        cursor.moveToFirst();
74
75        view.bind(cursor);
76        verifyContent(view, TestDataFactory.getMessageText(cursor, 0), true, Dates
77                .getMessageTimeString((Long) cursor.getAt("received_timestamp", 0)).toString());
78    }
79
80    public void testBindTwice() {
81        final ConversationMessageView view = getView();
82
83        final FakeCursor cursor = TestDataFactory.getConversationMessageCursor();
84        cursor.moveToFirst();
85        view.bind(cursor);
86
87        cursor.moveToNext();
88        view.bind(cursor);
89        verifyContent(view, TestDataFactory.getMessageText(cursor, 1), true, Dates
90                .getMessageTimeString((Long) cursor.getAt("received_timestamp", 1)).toString());
91    }
92
93    public void testBindLast() {
94        final ConversationMessageView view = getView();
95
96        final FakeCursor cursor = TestDataFactory.getConversationMessageCursor();
97        final int lastPos = cursor.getCount() - 1;
98        cursor.moveToPosition(lastPos);
99
100        view.bind(cursor);
101        verifyContent(view, TestDataFactory.getMessageText(cursor, lastPos), true, Dates
102                .getMessageTimeString((Long) cursor.getAt("received_timestamp", lastPos))
103                .toString());
104    }
105
106    @Override
107    protected int getLayoutIdForView() {
108        return R.layout.conversation_message_view;
109    }
110}
111