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.mediapicker;
18 
19 import android.content.Context;
20 import android.view.View;
21 import android.view.ViewGroup;
22 import android.widget.ImageButton;
23 
24 import com.android.messaging.FakeFactory;
25 import com.android.messaging.R;
26 import com.android.messaging.datamodel.FakeDataModel;
27 import com.android.messaging.datamodel.binding.Binding;
28 import com.android.messaging.datamodel.binding.BindingBase;
29 import com.android.messaging.datamodel.data.DraftMessageData;
30 import com.android.messaging.datamodel.data.MediaPickerData;
31 import com.android.messaging.ui.FragmentTestCase;
32 
33 import org.mockito.Matchers;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 
37 public class MediaPickerTest extends FragmentTestCase<MediaPicker> {
38     @Mock protected MediaPickerData mMockMediaPickerData;
39     @Mock protected DraftMessageData mMockDraftMessageData;
40     protected FakeDataModel mFakeDataModel;
41 
MediaPickerTest()42     public MediaPickerTest() {
43         super(MediaPicker.class);
44     }
45 
46     @Override
getFragment()47     protected MediaPicker getFragment() {
48         if (mFragment == null) {
49             mFragment = new MediaPicker(getInstrumentation().getTargetContext());
50         }
51         return mFragment;
52     }
53 
54     @Override
setUp()55     protected void setUp() throws Exception {
56         super.setUp();
57         final Context context = getInstrumentation().getTargetContext();
58         mFakeDataModel = new FakeDataModel(context)
59                    .withMediaPickerData(mMockMediaPickerData);
60         FakeFactory.register(context)
61                    .withDataModel(mFakeDataModel);
62     }
63 
64     /**
65      * Helper method to initialize the MediaPicker and its data.
66      */
initFragment(final int supportedMediaTypes, final Integer[] expectedLoaderIds, final boolean filterTabBeforeAttach)67     private void initFragment(final int supportedMediaTypes, final Integer[] expectedLoaderIds,
68             final boolean filterTabBeforeAttach) {
69         Mockito.when(mMockMediaPickerData.isBound(Matchers.anyString()))
70             .thenReturn(true);
71         Mockito.when(mMockDraftMessageData.isBound(Matchers.anyString()))
72             .thenReturn(true);
73         final Binding<DraftMessageData> draftBinding = BindingBase.createBinding(this);
74         draftBinding.bind(mMockDraftMessageData);
75 
76         getActivity().runOnUiThread(new Runnable() {
77             @Override
78             public void run() {
79                 final MediaPicker fragment = getFragment();
80                 if (filterTabBeforeAttach) {
81                     fragment.setSupportedMediaTypes(supportedMediaTypes);
82                     getActivity().setFragment(fragment);
83                 } else {
84                     getActivity().setFragment(fragment);
85                     fragment.setSupportedMediaTypes(supportedMediaTypes);
86                 }
87                 fragment.setDraftMessageDataModel(draftBinding);
88                 Mockito.verify(mMockMediaPickerData,
89                         Mockito.atLeastOnce()).init(
90                         Matchers.eq(fragment.getLoaderManager()));
91                 fragment.open(MediaPicker.MEDIA_TYPE_ALL, false);
92             }
93         });
94         getInstrumentation().waitForIdleSync();
95     }
96 
testDefaultTabs()97     public void testDefaultTabs() {
98         Mockito.when(mMockMediaPickerData.getSelectedChooserIndex()).thenReturn(0);
99         initFragment(MediaPicker.MEDIA_TYPE_ALL, new Integer[] {
100                 MediaPickerData.GALLERY_MEDIA_LOADER },
101                 false);
102         final MediaPicker mediaPicker = getFragment();
103         final View view = mediaPicker.getView();
104         assertNotNull(view);
105         final ViewGroup tabStrip = (ViewGroup) view.findViewById(R.id.mediapicker_tabstrip);
106         assertEquals(tabStrip.getChildCount(), 3);
107         for (int i = 0; i < tabStrip.getChildCount(); i++) {
108             final ImageButton tabButton = (ImageButton) tabStrip.getChildAt(i);
109             assertEquals(View.VISIBLE, tabButton.getVisibility());
110             assertEquals(i == 0, tabButton.isSelected());
111         }
112     }
113 
testFilterTabsBeforeAttach()114     public void testFilterTabsBeforeAttach() {
115         Mockito.when(mMockMediaPickerData.getSelectedChooserIndex()).thenReturn(0);
116         initFragment(MediaPicker.MEDIA_TYPE_IMAGE, new Integer[] {
117                 MediaPickerData.GALLERY_MEDIA_LOADER },
118                 true);
119         final MediaPicker mediaPicker = getFragment();
120         final View view = mediaPicker.getView();
121         assertNotNull(view);
122         final ViewGroup tabStrip = (ViewGroup) view.findViewById(R.id.mediapicker_tabstrip);
123         assertEquals(tabStrip.getChildCount(), 3);
124         for (int i = 0; i < tabStrip.getChildCount(); i++) {
125             final ImageButton tabButton = (ImageButton) tabStrip.getChildAt(i);
126             assertEquals(i == 0, tabButton.isSelected());
127         }
128     }
129 }
130