summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/messaging/ui/mediapicker/MediaPickerTest.java
blob: eaf933870ab14aa03f2425db2c3793cc03bb3986 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.messaging.ui.mediapicker;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.android.messaging.FakeFactory;
import com.android.messaging.R;
import com.android.messaging.datamodel.FakeDataModel;
import com.android.messaging.datamodel.binding.Binding;
import com.android.messaging.datamodel.binding.BindingBase;
import com.android.messaging.datamodel.data.DraftMessageData;
import com.android.messaging.datamodel.data.MediaPickerData;
import com.android.messaging.ui.FragmentTestCase;

import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;

public class MediaPickerTest extends FragmentTestCase<MediaPicker> {
    @Mock protected MediaPickerData mMockMediaPickerData;
    @Mock protected DraftMessageData mMockDraftMessageData;
    protected FakeDataModel mFakeDataModel;

    public MediaPickerTest() {
        super(MediaPicker.class);
    }

    @Override
    protected MediaPicker getFragment() {
        if (mFragment == null) {
            mFragment = new MediaPicker(getInstrumentation().getTargetContext());
        }
        return mFragment;
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        final Context context = getInstrumentation().getTargetContext();
        mFakeDataModel = new FakeDataModel(context)
                   .withMediaPickerData(mMockMediaPickerData);
        FakeFactory.register(context)
                   .withDataModel(mFakeDataModel);
    }

    /**
     * Helper method to initialize the MediaPicker and its data.
     */
    private void initFragment(final int supportedMediaTypes, final Integer[] expectedLoaderIds,
            final boolean filterTabBeforeAttach) {
        Mockito.when(mMockMediaPickerData.isBound(Matchers.anyString()))
            .thenReturn(true);
        Mockito.when(mMockDraftMessageData.isBound(Matchers.anyString()))
            .thenReturn(true);
        final Binding<DraftMessageData> draftBinding = BindingBase.createBinding(this);
        draftBinding.bind(mMockDraftMessageData);

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final MediaPicker fragment = getFragment();
                if (filterTabBeforeAttach) {
                    fragment.setSupportedMediaTypes(supportedMediaTypes);
                    getActivity().setFragment(fragment);
                } else {
                    getActivity().setFragment(fragment);
                    fragment.setSupportedMediaTypes(supportedMediaTypes);
                }
                fragment.setDraftMessageDataModel(draftBinding);
                Mockito.verify(mMockMediaPickerData,
                        Mockito.atLeastOnce()).init(
                        Matchers.eq(fragment.getLoaderManager()));
                fragment.open(MediaPicker.MEDIA_TYPE_ALL, false);
            }
        });
        getInstrumentation().waitForIdleSync();
    }

    public void testDefaultTabs() {
        Mockito.when(mMockMediaPickerData.getSelectedChooserIndex()).thenReturn(0);
        initFragment(MediaPicker.MEDIA_TYPE_ALL, new Integer[] {
                MediaPickerData.GALLERY_MEDIA_LOADER },
                false);
        final MediaPicker mediaPicker = getFragment();
        final View view = mediaPicker.getView();
        assertNotNull(view);
        final ViewGroup tabStrip = (ViewGroup) view.findViewById(R.id.mediapicker_tabstrip);
        assertEquals(tabStrip.getChildCount(), 3);
        for (int i = 0; i < tabStrip.getChildCount(); i++) {
            final ImageButton tabButton = (ImageButton) tabStrip.getChildAt(i);
            assertEquals(View.VISIBLE, tabButton.getVisibility());
            assertEquals(i == 0, tabButton.isSelected());
        }
    }

    public void testFilterTabsBeforeAttach() {
        Mockito.when(mMockMediaPickerData.getSelectedChooserIndex()).thenReturn(0);
        initFragment(MediaPicker.MEDIA_TYPE_IMAGE, new Integer[] {
                MediaPickerData.GALLERY_MEDIA_LOADER },
                true);
        final MediaPicker mediaPicker = getFragment();
        final View view = mediaPicker.getView();
        assertNotNull(view);
        final ViewGroup tabStrip = (ViewGroup) view.findViewById(R.id.mediapicker_tabstrip);
        assertEquals(tabStrip.getChildCount(), 3);
        for (int i = 0; i < tabStrip.getChildCount(); i++) {
            final ImageButton tabButton = (ImageButton) tabStrip.getChildAt(i);
            assertEquals(i == 0, tabButton.isSelected());
        }
    }
}