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.contact;
18
19import android.content.Context;
20import android.database.Cursor;
21import android.support.v4.view.ViewPager;
22import android.test.suitebuilder.annotation.LargeTest;
23import android.view.View;
24import android.widget.ListView;
25
26import com.android.messaging.FakeFactory;
27import com.android.messaging.R;
28import com.android.messaging.datamodel.FakeDataModel;
29import com.android.messaging.datamodel.action.ActionTestHelpers;
30import com.android.messaging.datamodel.action.ActionTestHelpers.StubActionService;
31import com.android.messaging.datamodel.action.ActionTestHelpers.StubActionService.StubActionServiceCallLog;
32import com.android.messaging.datamodel.action.GetOrCreateConversationAction;
33import com.android.messaging.datamodel.data.ContactPickerData;
34import com.android.messaging.datamodel.data.ParticipantData;
35import com.android.messaging.datamodel.data.TestDataFactory;
36import com.android.messaging.ui.CustomHeaderViewPagerAdapter;
37import com.android.messaging.ui.FragmentTestCase;
38import com.android.messaging.ui.UIIntents;
39import com.android.messaging.ui.contact.ContactPickerFragment.ContactPickerFragmentHost;
40
41import org.mockito.Matchers;
42import org.mockito.Mock;
43import org.mockito.Mockito;
44
45import java.util.ArrayList;
46import java.util.List;
47
48
49/**
50 * Unit tests for {@link ContactPickerFragment}.
51 */
52@LargeTest
53public class ContactPickerFragmentTest
54    extends FragmentTestCase<ContactPickerFragment> {
55
56    @Mock protected ContactPickerData mMockContactPickerData;
57    @Mock protected UIIntents mMockUIIntents;
58    @Mock protected ContactPickerFragmentHost mockHost;
59    protected FakeDataModel mFakeDataModel;
60    private ActionTestHelpers.StubActionService mService;
61
62    public ContactPickerFragmentTest() {
63        super(ContactPickerFragment.class);
64    }
65
66    @Override
67    protected void setUp() throws Exception {
68        super.setUp();
69
70        final Context context = getInstrumentation().getTargetContext();
71        mService = new StubActionService();
72        mFakeDataModel = new FakeDataModel(context)
73            .withContactPickerData(mMockContactPickerData)
74            .withActionService(mService);
75        FakeFactory.register(context)
76                .withDataModel(mFakeDataModel)
77                .withUIIntents(mMockUIIntents);
78    }
79
80    /**
81     * Helper method to initialize the ContactPickerFragment and its data.
82     */
83    private ContactPickerFragmentTest initFragment(final int initialMode) {
84        Mockito.when(mMockContactPickerData.isBound(Matchers.anyString()))
85            .thenReturn(true);
86
87        getActivity().runOnUiThread(new Runnable() {
88            @Override
89            public void run() {
90                final ContactPickerFragment fragment = getFragment();
91                fragment.setHost(mockHost);
92                fragment.setContactPickingMode(initialMode, false);
93
94                getActivity().setFragment(fragment);
95                Mockito.verify(mMockContactPickerData).init(fragment.getLoaderManager(),
96                        fragment.mBinding);
97            }
98        });
99        getInstrumentation().waitForIdleSync();
100        return this;
101    }
102
103    /**
104     * Bind the datamodel with all contacts cursor to populate the all contacts list in the
105     * fragment.
106     */
107    private ContactPickerFragmentTest loadWithAllContactsCursor(final Cursor cursor) {
108        Mockito.when(mMockContactPickerData.isBound(Matchers.anyString()))
109            .thenReturn(true);
110
111        getActivity().runOnUiThread(new Runnable() {
112            @Override
113            public void run() {
114                getFragment().onAllContactsCursorUpdated(cursor);
115            }
116        });
117        getInstrumentation().waitForIdleSync();
118        return this;
119    }
120
121    /**
122     * Bind the datamodel with frequent contacts cursor to populate the contacts list in the
123     * fragment.
124     */
125    private ContactPickerFragmentTest loadWithFrequentContactsCursor(final Cursor cursor) {
126        Mockito.when(mMockContactPickerData.isBound(Matchers.anyString()))
127            .thenReturn(true);
128        getActivity().runOnUiThread(new Runnable() {
129            @Override
130            public void run() {
131                getFragment().onFrequentContactsCursorUpdated(cursor);
132            }
133        });
134        getInstrumentation().waitForIdleSync();
135        return this;
136    }
137
138    /**
139     * Test the initial state of the fragment before loading data.
140     */
141    public void testInitialState() {
142        initFragment(ContactPickerFragment.MODE_PICK_INITIAL_CONTACT);
143
144        // Make sure that the frequent contacts view is shown by default.
145        final ViewPager pager = (ViewPager) getFragment().getView().findViewById(R.id.pager);
146        final View currentPagedView = pager.getChildAt(pager.getCurrentItem());
147        final View frequentContactsView = ((CustomHeaderViewPagerAdapter) pager.getAdapter())
148                .getViewHolder(0).getView(null);
149        assertEquals(frequentContactsView, currentPagedView);
150    }
151
152    /**
153     * Verifies that list view gets correctly populated given a cursor.
154     */
155    public void testLoadAllContactsList() {
156        final Cursor cursor = TestDataFactory.getAllContactListCursor();
157        initFragment(ContactPickerFragment.MODE_PICK_INITIAL_CONTACT)
158                .loadWithAllContactsCursor(cursor);
159        final ListView listView = (ListView) getFragment().getView()
160                .findViewById(R.id.all_contacts_list);
161        assertEquals(cursor.getCount(), listView.getCount());
162    }
163
164    /**
165     * Verifies that list view gets correctly populated given a cursor.
166     */
167    public void testLoadFrequentContactsList() {
168        final Cursor cursor = TestDataFactory.getFrequentContactListCursor();
169        initFragment(ContactPickerFragment.MODE_PICK_INITIAL_CONTACT)
170                .loadWithFrequentContactsCursor(cursor);
171        final ListView listView = (ListView) getFragment().getView()
172                .findViewById(R.id.frequent_contacts_list);
173        assertEquals(cursor.getCount(), listView.getCount());
174    }
175
176    public void testPickInitialContact() {
177        final Cursor cursor = TestDataFactory.getFrequentContactListCursor();
178        initFragment(ContactPickerFragment.MODE_PICK_INITIAL_CONTACT)
179                .loadWithFrequentContactsCursor(cursor);
180        final ListView listView = (ListView) getFragment().getView()
181                .findViewById(R.id.frequent_contacts_list);
182        // Click on the first contact to add it.
183        final ContactListItemView cliv = (ContactListItemView) listView.getChildAt(0);
184        clickButton(cliv);
185        final ContactRecipientAutoCompleteView chipsView = (ContactRecipientAutoCompleteView)
186                getFragment().getView()
187                .findViewById(R.id.recipient_text_view);
188        // Verify the contact is added to the chips view.
189        final List<ParticipantData> participants =
190                chipsView.getRecipientParticipantDataForConversationCreation();
191        assertEquals(1, participants.size());
192        assertEquals(cliv.mData.getDestination(), participants.get(0).getSendDestination());
193        assertTrue(mService.getCalls().get(0).action instanceof GetOrCreateConversationAction);
194    }
195
196    public void testLeaveChipsMode() {
197        final Cursor cursor = TestDataFactory.getFrequentContactListCursor();
198        initFragment(ContactPickerFragment.MODE_CHIPS_ONLY)
199                .loadWithFrequentContactsCursor(cursor);
200        // Click on the add more participants button
201        // TODO: Figure out a way to click on the add more participants button now that
202        // it's part of the menu.
203        // final ImageButton AddMoreParticipantsButton = (ImageButton) getFragment().getView()
204        //         .findViewById(R.id.add_more_participants_button);
205        // clickButton(AddMoreParticipantsButton);
206        // Mockito.verify(mockHost).onInitiateAddMoreParticipants();
207    }
208
209    public void testPickMoreContacts() {
210        final Cursor cursor = TestDataFactory.getFrequentContactListCursor();
211        initFragment(ContactPickerFragment.MODE_PICK_MORE_CONTACTS)
212                .loadWithFrequentContactsCursor(cursor);
213        final ListView listView = (ListView) getFragment().getView()
214                .findViewById(R.id.frequent_contacts_list);
215        // Click on the first contact to add it.
216        final ContactListItemView cliv = (ContactListItemView) listView.getChildAt(0);
217        clickButton(cliv);
218        // Verify that we don't attempt to create a conversation right away.
219        assertEquals(0, mService.getCalls().size());
220    }
221}
222