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.conversationlist; 18 19import android.content.Context; 20import android.database.Cursor; 21import android.support.v7.widget.RecyclerView; 22import android.test.suitebuilder.annotation.LargeTest; 23import android.view.View; 24import android.widget.ImageView; 25import android.widget.ListView; 26 27import com.android.messaging.FakeFactory; 28import com.android.messaging.R; 29import com.android.messaging.datamodel.FakeDataModel; 30import com.android.messaging.datamodel.data.ConversationListData; 31import com.android.messaging.datamodel.data.TestDataFactory; 32import com.android.messaging.ui.FragmentTestCase; 33import com.android.messaging.ui.UIIntents; 34import com.android.messaging.ui.conversationlist.ConversationListFragment; 35import com.android.messaging.ui.conversationlist.ConversationListFragment.ConversationListFragmentHost; 36 37import org.mockito.Matchers; 38import org.mockito.Mock; 39import org.mockito.Mockito; 40 41 42/** 43 * Unit tests for {@link ConversationListFragment}. 44 */ 45@LargeTest 46public class ConversationListFragmentTest 47 extends FragmentTestCase<ConversationListFragment> { 48 49 @Mock protected ConversationListData mMockConversationListData; 50 @Mock protected ConversationListFragmentHost mMockConversationHostListHost; 51 @Mock protected UIIntents mMockUIIntents; 52 protected FakeDataModel mFakeDataModel; 53 54 public ConversationListFragmentTest() { 55 super(ConversationListFragment.class); 56 } 57 58 @Override 59 protected void setUp() throws Exception { 60 super.setUp(); 61 62 final Context context = getInstrumentation().getTargetContext(); 63 mFakeDataModel = new FakeDataModel(context) 64 .withConversationListData(mMockConversationListData); 65 FakeFactory.register(context) 66 .withDataModel(mFakeDataModel) 67 .withUIIntents(mMockUIIntents); 68 } 69 70 /** 71 * Helper that will do the 'binding' of ConversationListFragmentTest with ConversationListData 72 * and leave fragment in 'ready' state. 73 * @param cursor 74 */ 75 private void loadWith(final Cursor cursor) { 76 Mockito.when(mMockConversationListData.isBound(Matchers.anyString())) 77 .thenReturn(true); 78 79 final ConversationListFragment fragment = getFragment(); 80 getActivity().runOnUiThread(new Runnable() { 81 @Override 82 public void run() { 83 fragment.setHost(mMockConversationHostListHost); 84 getActivity().setFragment(fragment); 85 Mockito.verify(mMockConversationListData).init(fragment.getLoaderManager(), 86 fragment.mListBinding); 87 fragment.onConversationListCursorUpdated(mMockConversationListData, cursor); 88 } 89 }); 90 getInstrumentation().waitForIdleSync(); 91 } 92 93 /** 94 * Verifies that list view gets correctly populated given a cursor. 95 */ 96 public void testLoadListView() { 97 final Cursor cursor = TestDataFactory.getConversationListCursor(); 98 loadWith(cursor); 99 final RecyclerView listView = 100 (RecyclerView) getFragment().getView().findViewById(android.R.id.list); 101 //assertEquals(cursor.getCount(), listView.getCount()); 102 assertEquals(cursor.getCount(), listView.getChildCount()); 103 } 104 105 /** 106 * Verifies that 'empty list' promo is rendered with an empty cursor. 107 */ 108 public void testEmptyView() { 109 loadWith(TestDataFactory.getEmptyConversationListCursor()); 110 final RecyclerView listView = 111 (RecyclerView) getFragment().getView().findViewById(android.R.id.list); 112 final View emptyMessageView = 113 getFragment().getView().findViewById(R.id.no_conversations_view); 114 assertEquals(View.VISIBLE, emptyMessageView.getVisibility()); 115 assertEquals(0, listView.getChildCount()); 116 } 117 118 /** 119 * Verifies that the button to start a new conversation works. 120 */ 121 public void testStartNewConversation() { 122 final Cursor cursor = TestDataFactory.getConversationListCursor(); 123 loadWith(cursor); 124 final ImageView startNewConversationButton = (ImageView) 125 getFragment().getView().findViewById(R.id.start_new_conversation_button); 126 127 clickButton(startNewConversationButton); 128 Mockito.verify(mMockConversationHostListHost).onCreateConversationClick(); 129 } 130} 131