1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/*
2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project
3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License");
5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License.
6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at
7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *      http://www.apache.org/licenses/LICENSE-2.0
9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software
11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS,
12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and
14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License.
15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.ui.conversationlist;
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Context;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.database.Cursor;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.support.v7.widget.RecyclerView;
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.test.suitebuilder.annotation.LargeTest;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.View;
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.widget.ImageView;
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.widget.ListView;
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.FakeFactory;
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.R;
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.FakeDataModel;
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.ConversationListData;
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.TestDataFactory;
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.FragmentTestCase;
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.UIIntents;
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.conversationlist.ConversationListFragment;
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.conversationlist.ConversationListFragment.ConversationListFragmentHost;
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport org.mockito.Matchers;
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport org.mockito.Mock;
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport org.mockito.Mockito;
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/**
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unit tests for {@link ConversationListFragment}.
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd@LargeTest
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class ConversationListFragmentTest
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    extends FragmentTestCase<ConversationListFragment> {
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Mock protected ConversationListData mMockConversationListData;
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Mock protected ConversationListFragmentHost mMockConversationHostListHost;
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Mock protected UIIntents mMockUIIntents;
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    protected FakeDataModel mFakeDataModel;
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public ConversationListFragmentTest() {
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        super(ConversationListFragment.class);
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    protected void setUp() throws Exception {
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        super.setUp();
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Context context = getInstrumentation().getTargetContext();
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mFakeDataModel = new FakeDataModel(context)
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            .withConversationListData(mMockConversationListData);
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        FakeFactory.register(context)
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                .withDataModel(mFakeDataModel)
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                .withUIIntents(mMockUIIntents);
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Helper that will do the 'binding' of ConversationListFragmentTest with ConversationListData
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * and leave fragment in 'ready' state.
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param cursor
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private void loadWith(final Cursor cursor) {
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        Mockito.when(mMockConversationListData.isBound(Matchers.anyString()))
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            .thenReturn(true);
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final ConversationListFragment fragment = getFragment();
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        getActivity().runOnUiThread(new Runnable() {
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            @Override
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            public void run() {
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                fragment.setHost(mMockConversationHostListHost);
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                getActivity().setFragment(fragment);
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                Mockito.verify(mMockConversationListData).init(fragment.getLoaderManager(),
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        fragment.mListBinding);
87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                fragment.onConversationListCursorUpdated(mMockConversationListData, cursor);
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        });
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        getInstrumentation().waitForIdleSync();
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Verifies that list view gets correctly populated given a cursor.
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void testLoadListView() {
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Cursor cursor = TestDataFactory.getConversationListCursor();
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        loadWith(cursor);
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final RecyclerView listView =
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                (RecyclerView) getFragment().getView().findViewById(android.R.id.list);
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        //assertEquals(cursor.getCount(), listView.getCount());
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        assertEquals(cursor.getCount(), listView.getChildCount());
103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Verifies that 'empty list' promo is rendered with an empty cursor.
107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void testEmptyView() {
109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        loadWith(TestDataFactory.getEmptyConversationListCursor());
110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final RecyclerView listView =
111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                (RecyclerView) getFragment().getView().findViewById(android.R.id.list);
112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final View emptyMessageView =
113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                getFragment().getView().findViewById(R.id.no_conversations_view);
114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        assertEquals(View.VISIBLE, emptyMessageView.getVisibility());
115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        assertEquals(0, listView.getChildCount());
116d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
117d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
118d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
119d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Verifies that the button to start a new conversation works.
120d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
121d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void testStartNewConversation() {
122d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Cursor cursor = TestDataFactory.getConversationListCursor();
123d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        loadWith(cursor);
124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final ImageView startNewConversationButton = (ImageView)
125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                getFragment().getView().findViewById(R.id.start_new_conversation_button);
126d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
127d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        clickButton(startNewConversationButton);
128d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        Mockito.verify(mMockConversationHostListHost).onCreateConversationClick();
129d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
130d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
131