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 package com.android.messaging.ui.mediapicker;
17 
18 import android.content.Context;
19 import android.provider.MediaStore.Images.Media;
20 import android.view.View;
21 import android.widget.CheckBox;
22 
23 import com.android.messaging.FakeFactory;
24 import com.android.messaging.R;
25 import com.android.messaging.datamodel.FakeCursor;
26 import com.android.messaging.datamodel.FakeDataModel;
27 import com.android.messaging.datamodel.data.GalleryGridItemData;
28 import com.android.messaging.datamodel.data.TestDataFactory;
29 import com.android.messaging.ui.AsyncImageView;
30 import com.android.messaging.ui.ViewTest;
31 import com.android.messaging.util.UriUtil;
32 
33 import org.mockito.Matchers;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 
37 public class GalleryGridItemViewTest extends ViewTest<GalleryGridItemView> {
38 
39     @Mock GalleryGridItemView.HostInterface mockHost;
40 
41     @Override
setUp()42     protected void setUp() throws Exception {
43         super.setUp();
44         final Context context = getInstrumentation().getTargetContext();
45         FakeFactory.register(context)
46             .withDataModel(new FakeDataModel(context));
47     }
48 
verifyClickedItem(final View view, final GalleryGridItemData data)49     protected void verifyClickedItem(final View view, final GalleryGridItemData data) {
50         Mockito.verify(mockHost).onItemClicked(view, data, false /* longClick */);
51     }
52 
verifyContent( final GalleryGridItemView view, final String imageUrl, final boolean showCheckbox, final boolean isSelected)53     protected void verifyContent(
54             final GalleryGridItemView view,
55             final String imageUrl,
56             final boolean showCheckbox,
57             final boolean isSelected) {
58         final AsyncImageView imageView = (AsyncImageView) view.findViewById(R.id.thumbnail);
59         final CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
60 
61         assertNotNull(imageView);
62         assertTrue(imageView.mImageRequestBinding.isBound());
63         assertTrue(imageView.mImageRequestBinding.getData().getKey().startsWith(imageUrl));
64         assertNotNull(checkBox);
65         if (showCheckbox) {
66             assertEquals(View.VISIBLE, checkBox.getVisibility());
67             assertEquals(isSelected, checkBox.isChecked());
68         } else {
69             assertNotSame(View.VISIBLE, checkBox.getVisibility());
70         }
71     }
72 
testBind()73     public void testBind() {
74         Mockito.when(mockHost.isMultiSelectEnabled()).thenReturn(false);
75         Mockito.when(mockHost.isItemSelected(Matchers.<GalleryGridItemData>any()))
76                 .thenReturn(false);
77         final GalleryGridItemView view = getView();
78         final FakeCursor cursor = TestDataFactory.getGalleryGridCursor();
79         cursor.moveToFirst();
80         final String path = (String) cursor.getAt(Media.DATA, 0);
81         view.bind(cursor, mockHost);
82         verifyContent(view, UriUtil.getUriForResourceFile(path).toString(),
83                 false, false);
84     }
85 
testBindMultiSelectUnSelected()86     public void testBindMultiSelectUnSelected() {
87         Mockito.when(mockHost.isMultiSelectEnabled()).thenReturn(true);
88         Mockito.when(mockHost.isItemSelected(Matchers.<GalleryGridItemData>any()))
89                 .thenReturn(false);
90         final GalleryGridItemView view = getView();
91         final FakeCursor cursor = TestDataFactory.getGalleryGridCursor();
92         cursor.moveToFirst();
93         final String path = (String) cursor.getAt(Media.DATA, 0);
94         view.bind(cursor, mockHost);
95         verifyContent(view, UriUtil.getUriForResourceFile(path).toString(),
96                 true, false);
97     }
98 
testBindMultiSelectSelected()99     public void testBindMultiSelectSelected() {
100         Mockito.when(mockHost.isMultiSelectEnabled()).thenReturn(true);
101         Mockito.when(mockHost.isItemSelected(Matchers.<GalleryGridItemData>any()))
102                 .thenReturn(true);
103         final GalleryGridItemView view = getView();
104         final FakeCursor cursor = TestDataFactory.getGalleryGridCursor();
105         cursor.moveToFirst();
106         final String path = (String) cursor.getAt(Media.DATA, 0);
107         view.bind(cursor, mockHost);
108         verifyContent(view, UriUtil.getUriForResourceFile(path).toString(),
109                 true, true);
110     }
111 
testClick()112     public void testClick() {
113         Mockito.when(mockHost.isMultiSelectEnabled()).thenReturn(false);
114         Mockito.when(mockHost.isItemSelected(Matchers.<GalleryGridItemData>any()))
115                 .thenReturn(false);
116         final GalleryGridItemView view = getView();
117         final FakeCursor cursor = TestDataFactory.getGalleryGridCursor();
118         cursor.moveToFirst();
119         view.bind(cursor, mockHost);
120         view.performClick();
121         verifyClickedItem(view, view.mData);
122     }
123 
testBindTwice()124     public void testBindTwice() {
125         Mockito.when(mockHost.isMultiSelectEnabled()).thenReturn(true);
126         Mockito.when(mockHost.isItemSelected(Matchers.<GalleryGridItemData>any()))
127                 .thenReturn(false);
128         final GalleryGridItemView view = getView();
129         final FakeCursor cursor = TestDataFactory.getGalleryGridCursor();
130 
131         cursor.moveToFirst();
132         view.bind(cursor, mockHost);
133 
134         cursor.moveToNext();
135         final String path = (String) cursor.getAt(Media.DATA, 1);
136         view.bind(cursor, mockHost);
137         verifyContent(view, UriUtil.getUriForResourceFile(path).toString(),
138                 true, false);
139     }
140 
141     @Override
getLayoutIdForView()142     protected int getLayoutIdForView() {
143         return R.layout.gallery_grid_item_view;
144     }
145 }
146