Home | History | Annotate | Download | only in attachmentchooser
      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.attachmentchooser;
     18 
     19 import android.app.Fragment;
     20 import android.test.suitebuilder.annotation.LargeTest;
     21 import android.widget.CheckBox;
     22 
     23 import com.android.messaging.FakeFactory;
     24 import com.android.messaging.R;
     25 import com.android.messaging.datamodel.DataModel;
     26 import com.android.messaging.datamodel.data.DraftMessageData;
     27 import com.android.messaging.datamodel.data.MessageData;
     28 import com.android.messaging.datamodel.data.MessagePartData;
     29 import com.android.messaging.datamodel.data.TestDataFactory;
     30 import com.android.messaging.ui.FragmentTestCase;
     31 import com.android.messaging.ui.TestActivity;
     32 import com.android.messaging.ui.TestActivity.FragmentEventListener;
     33 import com.android.messaging.ui.attachmentchooser.AttachmentChooserFragment;
     34 import com.android.messaging.ui.attachmentchooser.AttachmentGridItemView;
     35 import com.android.messaging.ui.attachmentchooser.AttachmentGridView;
     36 import com.android.messaging.ui.attachmentchooser.AttachmentChooserFragment.AttachmentChooserFragmentHost;
     37 import com.android.messaging.ui.conversationlist.ConversationListFragment;
     38 
     39 import org.mockito.ArgumentMatcher;
     40 import org.mockito.Matchers;
     41 import org.mockito.Mock;
     42 import org.mockito.Mockito;
     43 
     44 import java.util.Arrays;
     45 import java.util.HashSet;
     46 import java.util.List;
     47 import java.util.Set;
     48 
     49 
     50 /**
     51  * Unit tests for {@link ConversationListFragment}.
     52  */
     53 @LargeTest
     54 public class AttachmentChooserFragmentTest extends FragmentTestCase<AttachmentChooserFragment> {
     55 
     56     @Mock protected DataModel mockDataModel;
     57     @Mock protected DraftMessageData mockDraftMessageData;
     58     @Mock protected AttachmentChooserFragmentHost mockHost;
     59 
     60     private static final String CONVERSATION_ID = "cid";
     61 
     62     /** A custom argument matcher that checks whether the set argument passed in is a set
     63      * with identical attachment data as the given set.
     64      */
     65     private class IsSetOfGivenAttachments implements ArgumentMatcher<Set<MessagePartData>> {
     66         private final Set<MessagePartData> mGivenParts;
     67         public IsSetOfGivenAttachments(final Set<MessagePartData> givenParts) {
     68             mGivenParts = givenParts;
     69         }
     70 
     71         @Override
     72         public boolean matches(final Set<MessagePartData> actualSet) {
     73             if (actualSet.size() != mGivenParts.size()) {
     74                 return false;
     75             }
     76             return mGivenParts.containsAll(actualSet) && actualSet.containsAll(mGivenParts);
     77         }
     78      }
     79 
     80     public AttachmentChooserFragmentTest() {
     81         super(AttachmentChooserFragment.class);
     82     }
     83 
     84     @Override
     85     protected void setUp() throws Exception {
     86         super.setUp();
     87         FakeFactory.register(this.getInstrumentation().getTargetContext())
     88             .withDataModel(mockDataModel);
     89     }
     90 
     91     private void loadWith(final List<MessagePartData> attachments) {
     92         Mockito.when(mockDraftMessageData.isBound(Matchers.anyString()))
     93             .thenReturn(true);
     94         Mockito.doReturn(mockDraftMessageData)
     95             .when(mockDataModel)
     96             .createDraftMessageData(Mockito.anyString());
     97         Mockito.doReturn(attachments)
     98             .when(mockDraftMessageData)
     99             .getReadOnlyAttachments();
    100         Mockito.when(mockDataModel.createDraftMessageData(
    101                 Matchers.anyString()))
    102             .thenReturn(mockDraftMessageData);
    103 
    104         // Create fragment synchronously to avoid need for volatile, synchronization etc.
    105         final AttachmentChooserFragment fragment = getFragment();
    106         // Binding to model happens when attaching fragment to activity, so hook into test
    107         // activity to do so.
    108         getActivity().setFragmentEventListener(new FragmentEventListener() {
    109             @Override
    110             public void onAttachFragment(final Fragment attachedFragment) {
    111                 if (fragment == attachedFragment) {
    112                     fragment.setConversationId(CONVERSATION_ID);
    113                 }
    114             }
    115         });
    116 
    117         getActivity().runOnUiThread(new Runnable() {
    118             @Override
    119             public void run() {
    120                 fragment.setHost(mockHost);
    121                 getActivity().setFragment(fragment);
    122                 Mockito.verify(mockDataModel).createDraftMessageData(
    123                         Mockito.matches(CONVERSATION_ID));
    124                 Mockito.verify(mockDraftMessageData).loadFromStorage(
    125                         Matchers.eq(fragment.mBinding), Matchers.eq((MessageData) null),
    126                         Matchers.eq(false));
    127             }
    128         });
    129         // Now load the cursor
    130         getActivity().runOnUiThread(new Runnable() {
    131             @Override
    132             public void run() {
    133                 fragment.onDraftChanged(mockDraftMessageData, DraftMessageData.ALL_CHANGED);
    134             }
    135         });
    136         getInstrumentation().waitForIdleSync();
    137     }
    138 
    139     public void testUnselect() {
    140         final List<MessagePartData> attachments = TestDataFactory.getTestDraftAttachments();
    141         loadWith(attachments);
    142         final AttachmentGridView attachmentGridView = (AttachmentGridView)
    143                 getFragment().getView().findViewById(R.id.grid);
    144         assertEquals("bad view count", attachments.size(),
    145                 attachmentGridView.getAdapter().getCount());
    146 
    147         final AttachmentGridItemView itemView = (AttachmentGridItemView)
    148                 attachmentGridView.getChildAt(0);
    149         assertEquals(attachmentGridView, itemView.testGetHostInterface());
    150         final CheckBox checkBox = (CheckBox) itemView.findViewById(R.id.checkbox);
    151         assertEquals(true, checkBox.isChecked());
    152         assertEquals(true, attachmentGridView.isItemSelected(itemView.mAttachmentData));
    153         clickButton(checkBox);
    154         assertEquals(false, checkBox.isChecked());
    155         assertEquals(false, attachmentGridView.isItemSelected(itemView.mAttachmentData));
    156 
    157         final AttachmentGridItemView itemView2 = (AttachmentGridItemView)
    158                 attachmentGridView.getChildAt(1);
    159         final CheckBox checkBox2 = (CheckBox) itemView2.findViewById(R.id.checkbox);
    160         clickButton(checkBox2);
    161 
    162         getFragment().confirmSelection();
    163         final MessagePartData[] attachmentsToRemove = new MessagePartData[] {
    164                 itemView.mAttachmentData, itemView2.mAttachmentData };
    165         Mockito.verify(mockDraftMessageData).removeExistingAttachments(Matchers.argThat(
    166                 new IsSetOfGivenAttachments(new HashSet<>(Arrays.asList(attachmentsToRemove)))));
    167         Mockito.verify(mockDraftMessageData).saveToStorage(Matchers.eq(getFragment().mBinding));
    168         Mockito.verify(mockHost).onConfirmSelection();
    169     }
    170 }
    171