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 org.junit.Assert.assertEquals;
20import static org.mockito.Matchers.eq;
21import static org.mockito.Mockito.doReturn;
22import static org.mockito.Mockito.spy;
23
24import android.content.Context;
25import android.content.res.ColorStateList;
26import android.content.res.XmlResourceParser;
27import android.graphics.Color;
28import android.support.test.InstrumentationRegistry;
29import android.support.test.filters.SmallTest;
30import android.support.test.runner.AndroidJUnit4;
31import android.util.Xml;
32import android.widget.TextView;
33
34import com.android.setupwizardlib.TemplateLayout;
35import com.android.setupwizardlib.test.R;
36
37import org.junit.Before;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40import org.xmlpull.v1.XmlPullParserException;
41
42import java.io.IOException;
43
44@RunWith(AndroidJUnit4.class)
45@SmallTest
46public class ColoredHeaderMixinTest {
47
48    private Context mContext;
49    private TemplateLayout mTemplateLayout;
50    private TextView mHeaderTextView;
51
52    @Before
53    public void setUp() {
54        mContext = InstrumentationRegistry.getTargetContext();
55        mTemplateLayout = spy(new TemplateLayout(mContext, R.layout.test_template,
56                R.id.suw_layout_content));
57
58        mHeaderTextView = new TextView(mContext);
59        doReturn(mHeaderTextView).when(mTemplateLayout)
60                .findManagedViewById(eq(R.id.suw_layout_title));
61    }
62
63    @Test
64    public void testSetColor() {
65        ColoredHeaderMixin mixin = new ColoredHeaderMixin(mTemplateLayout, null, 0);
66        mixin.setColor(ColorStateList.valueOf(Color.MAGENTA));
67
68        assertEquals(ColorStateList.valueOf(Color.MAGENTA), mHeaderTextView.getTextColors());
69    }
70
71    @Test
72    public void testGetColor() {
73        ColoredHeaderMixin mixin = new ColoredHeaderMixin(mTemplateLayout, null, 0);
74        mHeaderTextView.setTextColor(ColorStateList.valueOf(Color.GREEN));
75
76        assertEquals(ColorStateList.valueOf(Color.GREEN), mixin.getColor());
77    }
78
79    @SuppressWarnings("ResourceType")  // Needed to create attribute set from layout XML.
80    @Test
81    public void testSetColorFromXml() throws IOException, XmlPullParserException {
82        final XmlResourceParser parser =
83                mContext.getResources().getXml(R.layout.test_mixin_attributes);
84        while (!TemplateLayout.class.getName().equals(parser.getName())) {
85            parser.next();
86        }
87        new ColoredHeaderMixin(mTemplateLayout, Xml.asAttributeSet(parser), 0);
88
89        assertEquals(ColorStateList.valueOf(Color.RED), mHeaderTextView.getTextColors());
90    }
91}
92