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;
18 
19 
20 import android.content.Context;
21 import android.net.Uri;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.TextView;
25 
26 import androidx.test.filters.MediumTest;
27 
28 import com.android.messaging.FakeFactory;
29 import com.android.messaging.datamodel.data.MessagePartData;
30 
31 import java.util.Arrays;
32 import java.util.Collections;
33 
34 @MediumTest
35 public class MultiAttachmentLayoutTest extends ViewTest<MultiAttachmentLayout> {
36     @Override
setUp()37     protected void setUp() throws Exception {
38         super.setUp();
39         final Context context = getInstrumentation().getTargetContext();
40         FakeFactory.register(context);
41     }
42 
43     @Override
getView()44     protected MultiAttachmentLayout getView() {
45         if (mView == null) {
46             // View creation deferred (typically until test time) so that factory/appcontext is
47             // ready.
48             mView = new MultiAttachmentLayout(getActivity(), null);
49             mView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
50         }
51         return mView;
52     }
53 
verifyContent( final MultiAttachmentLayout view, final int imageCount, final int plusCount)54     protected void verifyContent(
55             final MultiAttachmentLayout view,
56             final int imageCount,
57             final int plusCount) {
58         final int count = view.getChildCount();
59         int actualImageCount = 0;
60         final boolean needPlusText = plusCount > 0;
61         boolean hasPlusText = false;
62         for (int i = 0; i < count; i++) {
63             final View child = view.getChildAt(i);
64             if (child instanceof AsyncImageView) {
65                 actualImageCount++;
66             } else if (child instanceof TextView) {
67                 assertTrue(plusCount > 0);
68                 assertTrue(((TextView) child).getText().toString().contains("" + plusCount));
69                 hasPlusText = true;
70             } else {
71                 // Nothing other than image and overflow text view should appear in this layout.
72                 fail("unexpected view in layout. view = " + child);
73             }
74         }
75         assertEquals(imageCount, actualImageCount);
76         assertEquals(needPlusText, hasPlusText);
77     }
78 
testBindTwoAttachments()79     public void testBindTwoAttachments() {
80         final MultiAttachmentLayout view = getView();
81         final MessagePartData testAttachment1 = MessagePartData.createMediaMessagePart(
82                 "image/jpeg", Uri.parse("content://uri1"), 100, 100);
83         final MessagePartData testAttachment2 = MessagePartData.createMediaMessagePart(
84                 "image/jpeg", Uri.parse("content://uri2"), 100, 100);
85 
86         view.bindAttachments(createAttachmentList(testAttachment1, testAttachment2),
87                 null /* transitionRect */, 2);
88         verifyContent(view, 2, 0);
89     }
90 
testBindFiveAttachments()91     public void testBindFiveAttachments() {
92         final MultiAttachmentLayout view = getView();
93         final MessagePartData testAttachment1 = MessagePartData.createMediaMessagePart(
94                 "image/jpeg", Uri.parse("content://uri1"), 100, 100);
95         final MessagePartData testAttachment2 = MessagePartData.createMediaMessagePart(
96                 "image/jpeg", Uri.parse("content://uri2"), 100, 100);
97         final MessagePartData testAttachment3 = MessagePartData.createMediaMessagePart(
98                 "image/jpeg", Uri.parse("content://uri3"), 100, 100);
99         final MessagePartData testAttachment4 = MessagePartData.createMediaMessagePart(
100                 "image/jpeg", Uri.parse("content://uri4"), 100, 100);
101         final MessagePartData testAttachment5 = MessagePartData.createMediaMessagePart(
102                 "image/jpeg", Uri.parse("content://uri5"), 100, 100);
103 
104         view.bindAttachments(createAttachmentList(testAttachment1, testAttachment2, testAttachment3,
105                 testAttachment4, testAttachment5), null /* transitionRect */, 5);
106         verifyContent(view, 4, 1);
107     }
108 
testBindTwice()109     public void testBindTwice() {
110         // Put the above two tests together so we can simulate binding twice.
111         testBindTwoAttachments();
112         testBindFiveAttachments();
113     }
114 
createAttachmentList(final MessagePartData... attachments)115     private Iterable<MessagePartData> createAttachmentList(final MessagePartData... attachments) {
116         return Collections.unmodifiableList(Arrays.asList(attachments));
117     }
118 
119     @Override
getLayoutIdForView()120     protected int getLayoutIdForView() {
121         return 0;   // We construct the view with getView().
122     }
123 }