GlifLayoutTest.java revision 921c65f4b9dd01d331e8a80e3d165966c915d4fe
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.setupwizardlib.test;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertNotNull;
22import static org.junit.Assert.assertTrue;
23
24import android.content.Context;
25import android.content.res.ColorStateList;
26import android.graphics.Color;
27import android.os.Build.VERSION;
28import android.os.Build.VERSION_CODES;
29import android.support.test.InstrumentationRegistry;
30import android.support.test.filters.SmallTest;
31import android.support.test.runner.AndroidJUnit4;
32import android.view.ContextThemeWrapper;
33import android.view.Gravity;
34import android.view.LayoutInflater;
35import android.view.View;
36import android.widget.LinearLayout;
37import android.widget.ScrollView;
38import android.widget.TextView;
39
40import com.android.setupwizardlib.GlifLayout;
41
42import org.junit.Before;
43import org.junit.Test;
44import org.junit.runner.RunWith;
45
46@RunWith(AndroidJUnit4.class)
47@SmallTest
48public class GlifLayoutTest {
49
50    private Context mContext;
51
52    @Before
53    public void setUp() throws Exception {
54        mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(),
55                R.style.SuwThemeGlif_Light);
56    }
57
58    @Test
59    public void testInflateFromXml() {
60        LayoutInflater inflater = LayoutInflater.from(mContext);
61        GlifLayout layout = (GlifLayout) inflater.inflate(R.layout.test_glif_layout, null);
62        assertDefaultTemplateInflated(layout);
63        View content = layout.findViewById(R.id.test_content);
64        assertTrue("@id/test_content should be a TextView", content instanceof TextView);
65    }
66
67    @Test
68    public void testPrimaryColorFromXml() {
69        LayoutInflater inflater = LayoutInflater.from(mContext);
70        GlifLayout layout =
71                (GlifLayout) inflater.inflate(R.layout.test_glif_layout_primary_color, null);
72        assertDefaultTemplateInflated(layout);
73
74        assertEquals(ColorStateList.valueOf(Color.RED), layout.getPrimaryColor());
75    }
76
77    @Test
78    public void testSetProgressBarShownInvalid() {
79        GlifLayout layout = new GlifLayout(mContext, R.layout.test_template);
80        layout.setProgressBarShown(true);
81        // This is a no-op because there is no progress bar stub
82    }
83
84    @Test
85    public void testGlifTheme() {
86        mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(),
87                R.style.SuwThemeGlif_Light);
88        final GlifLayout glifLayout = new GlifLayout(mContext);
89
90        if (VERSION.SDK_INT >= VERSION_CODES.M) {
91            // Scroll indicators are only available on versions >= M
92            assertEquals(View.SCROLL_INDICATOR_BOTTOM,
93                    glifLayout.getScrollView().getScrollIndicators());
94        }
95    }
96
97    @Test
98    public void testGlifPixelTheme() {
99        mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(),
100                R.style.SuwThemeGlifPixel_Light);
101        final GlifLayout glifLayout = new GlifLayout(mContext);
102        final TextView titleView = (TextView) glifLayout.findManagedViewById(R.id.suw_layout_title);
103        if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
104            assertEquals(View.TEXT_ALIGNMENT_GRAVITY, titleView.getTextAlignment());
105        }
106        assertEquals("Title text should be center aligned on GLIF Pixel theme",
107                Gravity.CENTER_HORIZONTAL, titleView.getGravity() & Gravity.CENTER_HORIZONTAL);
108
109        if (VERSION.SDK_INT >= VERSION_CODES.N) {
110            // LinearLayout.getGravity is only available on versions >= N
111            final View iconView = glifLayout.findManagedViewById(R.id.suw_layout_icon);
112            final LinearLayout parent = (LinearLayout) iconView.getParent();
113            assertEquals("Icon should be center aligned on GLIF Pixel theme",
114                    Gravity.CENTER_HORIZONTAL, parent.getGravity() & Gravity.CENTER_HORIZONTAL);
115        }
116
117        assertEquals("Status bar color should be white in GLIF Pixel theme",
118                "fffafafa",
119                Integer.toHexString(glifLayout.getBackgroundBaseColor().getDefaultColor()));
120        assertFalse("GLIF Pixel theme shuold not have patterned background",
121                glifLayout.isBackgroundPatterned());
122
123        if (VERSION.SDK_INT >= VERSION_CODES.M) {
124            // Scroll indicators are only available on versions >= M
125            assertEquals(View.SCROLL_INDICATOR_TOP | View.SCROLL_INDICATOR_BOTTOM,
126                    glifLayout.getScrollView().getScrollIndicators());
127        }
128    }
129
130    private void assertDefaultTemplateInflated(GlifLayout layout) {
131        View title = layout.findViewById(R.id.suw_layout_title);
132        assertNotNull("@id/suw_layout_title should not be null", title);
133
134        View icon = layout.findViewById(R.id.suw_layout_icon);
135        assertNotNull("@id/suw_layout_icon should not be null", icon);
136
137        View scrollView = layout.findViewById(R.id.suw_scroll_view);
138        assertTrue("@id/suw_scroll_view should be a ScrollView", scrollView instanceof ScrollView);
139    }
140}
141