• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.messaging.ui.conversationlist;
18 
19 import android.content.Context;
20 import android.test.suitebuilder.annotation.MediumTest;
21 import android.text.TextUtils;
22 import android.view.View;
23 import android.widget.TextView;
24 
25 import com.android.messaging.Factory;
26 import com.android.messaging.FakeFactory;
27 import com.android.messaging.R;
28 import com.android.messaging.datamodel.FakeCursor;
29 import com.android.messaging.datamodel.FakeDataModel;
30 import com.android.messaging.datamodel.data.ConversationListItemData;
31 import com.android.messaging.datamodel.data.TestDataFactory;
32 import com.android.messaging.ui.AsyncImageView;
33 import com.android.messaging.ui.UIIntentsImpl;
34 import com.android.messaging.ui.ViewTest;
35 import com.android.messaging.ui.conversationlist.ConversationListItemView;
36 import com.android.messaging.util.Dates;
37 
38 import org.mockito.ArgumentMatcher;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 
42 @MediumTest
43 public class ConversationListItemViewTest extends ViewTest<ConversationListItemView> {
44 
45     @Mock private ConversationListItemView.HostInterface mockHost;
46     private FakeCursor mCursor;
47 
48     @Override
setUp()49     protected void setUp() throws Exception {
50         super.setUp();
51         final Context context = getInstrumentation().getTargetContext();
52         FakeFactory.register(context)
53             .withDataModel(new FakeDataModel(context))
54             .withUIIntents(new UIIntentsImpl());
55         mCursor = TestDataFactory.getConversationListCursor();
56     }
57 
58 
verifyLaunchedConversationForId(final String id, final ConversationListItemView conversationView)59     protected void verifyLaunchedConversationForId(final String id,
60             final ConversationListItemView conversationView) {
61         // Must be a short click.
62         final ArgumentMatcher<ConversationListItemData> itemDataIdMatcher =
63                 arg -> TextUtils.equals(id, arg.getConversationId());
64         Mockito.verify(mockHost).onConversationClicked(
65                 Mockito.argThat(itemDataIdMatcher), Mockito.eq(false),
66                 Mockito.eq(conversationView));
67     }
68 
verifyContent( final ConversationListItemView view, final FakeCursor cursor, final int index)69     protected void verifyContent(
70             final ConversationListItemView view, final FakeCursor cursor, final int index) {
71         /* ConversationQueryColumns.NAME */
72         final String  conversationQueryColumnsName = "name";
73         final String name = (String) cursor.getAt(conversationQueryColumnsName, index);
74 
75         /* ConversationQueryColumns.SNIPPET_TEXT */
76         final String  conversationQueryColumnsSnippetText = "snippet_text";
77         final String snippet = (String) cursor.getAt(conversationQueryColumnsSnippetText, index);
78 
79         /* ConversationQueryColumns.SORT_TIMESTAMP */
80         final String  conversationQueryColumnsSortTimestamp = "sort_timestamp";
81         final String timestamp = Dates.getConversationTimeString(
82                 (Long) cursor.getAt(conversationQueryColumnsSortTimestamp, index)).toString();
83 
84         final boolean unread = !isRead(cursor, index);
85         verifyContent(view, name,  snippet, timestamp, unread);
86     }
87 
verifyContent( final ConversationListItemView view, final String conversationName, final String snippet, final String timestamp, final boolean unread)88     protected void verifyContent(
89             final ConversationListItemView view,
90             final String conversationName,
91             final String snippet,
92             final String timestamp,
93             final boolean unread) {
94         final TextView conversationNameView =
95                 (TextView) view.findViewById(R.id.conversation_name);
96         final TextView snippetTextView = (TextView) view.findViewById(R.id.conversation_snippet);
97         final TextView timestampTextView = (TextView) view.findViewById(
98                 R.id.conversation_timestamp);
99         final AsyncImageView imagePreviewView =
100                 (AsyncImageView) view.findViewById(R.id.conversation_image_preview);
101 
102         final Context context = Factory.get().getApplicationContext();
103         assertNotNull(conversationNameView);
104         assertEquals(conversationName, conversationNameView.getText());
105         assertNotNull(snippetTextView);
106         if (unread) {
107             assertEquals(ConversationListItemView.UNREAD_SNIPPET_LINE_COUNT,
108                     snippetTextView.getMaxLines());
109             assertEquals(context.getResources().getColor(R.color.conversation_list_item_unread),
110                     snippetTextView.getCurrentTextColor());
111             assertEquals(context.getResources().getColor(R.color.conversation_list_item_unread),
112                     conversationNameView.getCurrentTextColor());
113 
114         } else {
115             assertEquals(ConversationListItemView.NO_UNREAD_SNIPPET_LINE_COUNT,
116                     snippetTextView.getMaxLines());
117             assertEquals(context.getResources().getColor(R.color.conversation_list_item_read),
118                     snippetTextView.getCurrentTextColor());
119             assertEquals(context.getResources().getColor(R.color.conversation_list_item_read),
120                     conversationNameView.getCurrentTextColor());
121         }
122 
123         assertEquals(View.VISIBLE, imagePreviewView.getVisibility());
124         assertTrue(snippetTextView.getText().toString().contains(snippet));
125         assertEquals(timestamp, timestampTextView.getText());
126     }
127 
isRead(final FakeCursor cursor, final int index)128     protected boolean isRead(final FakeCursor cursor, final int index) {
129         return 1 == ((Integer) cursor.getAt("read", index)).intValue();
130     }
131 
testBindUnread()132     public void testBindUnread() {
133         final ConversationListItemView view = getView();
134         final int messageIndex = TestDataFactory.CONVERSATION_LIST_CURSOR_UNREAD_MESSAGE_INDEX;
135         mCursor.moveToPosition(messageIndex);
136         assertFalse(isRead(mCursor, messageIndex));
137         view.bind(mCursor, mockHost);
138         verifyContent(view, mCursor, messageIndex);
139     }
140 
testBindRead()141     public void testBindRead() {
142         final ConversationListItemView view = getView();
143 
144         final int messageIndex = TestDataFactory.CONVERSATION_LIST_CURSOR_READ_MESSAGE_INDEX;
145         mCursor.moveToPosition(messageIndex);
146         assertTrue(isRead(mCursor, messageIndex));
147         view.bind(mCursor, mockHost);
148         verifyContent(view, mCursor, messageIndex);
149     }
150 
testClickLaunchesConversation()151     public void testClickLaunchesConversation() {
152         final ConversationListItemView view = getView();
153         final View swipeableContainer = view.findViewById(R.id.swipeableContainer);
154         mCursor.moveToFirst();
155         view.bind(mCursor, mockHost);
156         swipeableContainer.performClick();
157         verifyLaunchedConversationForId(
158                 mCursor.getAt("_id" /* ConversationQueryColumns._ID */, 0).toString(), view);
159     }
160 
testBindTwice()161     public void testBindTwice() {
162         final ConversationListItemView view = getView();
163 
164         mCursor.moveToFirst();
165         view.bind(mCursor, mockHost);
166 
167         mCursor.moveToNext();
168         view.bind(mCursor, mockHost);
169         verifyContent(view, mCursor, mCursor.getPosition());
170     }
171 
172     @Override
getLayoutIdForView()173     protected int getLayoutIdForView() {
174         return R.layout.conversation_list_item_view;
175     }
176 }
177