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