1/*
2 * Copyright (C) 2017 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.setupwizardlib.template;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import static org.junit.Assert.assertEquals;
22import static org.junit.Assert.assertSame;
23import static org.mockito.Matchers.eq;
24import static org.mockito.Mockito.doReturn;
25import static org.mockito.Mockito.spy;
26
27import android.content.Context;
28import android.content.res.XmlResourceParser;
29import android.graphics.Color;
30import android.graphics.drawable.BitmapDrawable;
31import android.graphics.drawable.ColorDrawable;
32import android.graphics.drawable.Drawable;
33import android.support.test.InstrumentationRegistry;
34import android.support.test.filters.SmallTest;
35import android.support.test.runner.AndroidJUnit4;
36import android.util.Xml;
37import android.view.View;
38import android.widget.ImageView;
39
40import com.android.setupwizardlib.TemplateLayout;
41import com.android.setupwizardlib.test.R;
42
43import org.junit.Before;
44import org.junit.Test;
45import org.junit.runner.RunWith;
46import org.xmlpull.v1.XmlPullParserException;
47
48import java.io.IOException;
49
50@RunWith(AndroidJUnit4.class)
51@SmallTest
52public class IconMixinTest {
53
54    private Context mContext;
55    private TemplateLayout mTemplateLayout;
56    private ImageView mIconView;
57
58    @Before
59    public void setUp() {
60        mContext = InstrumentationRegistry.getContext();
61        mTemplateLayout = spy(new TemplateLayout(mContext, R.layout.test_template,
62                R.id.suw_layout_content));
63
64        mIconView = new ImageView(mContext);
65        doReturn(mIconView).when(mTemplateLayout).findManagedViewById(eq(R.id.suw_layout_icon));
66    }
67
68    @Test
69    public void testGetIconView() {
70        IconMixin mixin = new IconMixin(mTemplateLayout, null, 0);
71        assertSame(mIconView, mixin.getView());
72    }
73
74    @Test
75    public void testSetIcon() {
76        final ColorDrawable drawable = new ColorDrawable(Color.CYAN);
77        IconMixin mixin = new IconMixin(mTemplateLayout, null, 0);
78        mixin.setIcon(drawable);
79
80        assertSame(drawable, mIconView.getDrawable());
81        assertEquals(View.VISIBLE, mIconView.getVisibility());
82    }
83
84    @Test
85    public void setIcon_resourceId_shouldSetIcon() {
86        int icon = android.R.drawable.ic_menu_add;
87        IconMixin mixin = new IconMixin(mTemplateLayout, null, 0);
88        mixin.setIcon(icon);
89
90        Drawable drawable = mIconView.getDrawable();
91        assertThat(drawable).isInstanceOf(BitmapDrawable.class);
92        assertEquals(View.VISIBLE, mIconView.getVisibility());
93    }
94
95    @Test
96    public void setIcon_shouldSetVisibilityToGone_whenIconIsNull() {
97        IconMixin mixin = new IconMixin(mTemplateLayout, null, 0);
98        mixin.setIcon(null);
99
100        assertEquals(View.GONE, mIconView.getVisibility());
101    }
102
103    @Test
104    public void testGetIcon() {
105        final ColorDrawable drawable = new ColorDrawable(Color.BLUE);
106        mIconView.setImageDrawable(drawable);
107
108        IconMixin mixin = new IconMixin(mTemplateLayout, null, 0);
109        assertSame(drawable, mixin.getIcon());
110    }
111
112    @SuppressWarnings("ResourceType")  // Needed to create attribute set from layout XML.
113    @Test
114    public void testSetIconFromXml() throws IOException, XmlPullParserException {
115        final XmlResourceParser parser =
116                mContext.getResources().getXml(R.layout.test_mixin_attributes);
117        while (!TemplateLayout.class.getName().equals(parser.getName())) {
118            parser.next();
119        }
120        new IconMixin(mTemplateLayout, Xml.asAttributeSet(parser), 0);
121
122        // Check that the bitmaps themselves are equal because BitmapDrawable does not implement
123        // equals()
124        final BitmapDrawable expected = (BitmapDrawable) mContext.getResources()
125                .getDrawable(android.R.drawable.ic_menu_add);
126        final BitmapDrawable actual = (BitmapDrawable) mIconView.getDrawable();
127        assertEquals(expected.getBitmap(), actual.getBitmap());
128        assertEquals(View.VISIBLE, mIconView.getVisibility());
129    }
130
131    @Test
132    public void setContentDescription_shouldSetContentDescriptionOnIconView() {
133        IconMixin mixin = new IconMixin(mTemplateLayout, null, 0);
134        mixin.setContentDescription("hello world");
135        assertThat(mIconView.getContentDescription()).isEqualTo("hello world");
136    }
137
138    @Test
139    public void getContentDescription_shouldReturnContentDescriptionFromView() {
140        IconMixin mixin = new IconMixin(mTemplateLayout, null, 0);
141        mIconView.setContentDescription("aloha");
142        assertThat(mixin.getContentDescription()).isEqualTo("aloha");
143    }
144}
145