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 android.content.Context;
20import android.content.res.ColorStateList;
21import android.graphics.Color;
22import android.os.Build;
23import android.test.InstrumentationTestCase;
24import android.test.suitebuilder.annotation.SmallTest;
25import android.view.ContextThemeWrapper;
26import android.view.InflateException;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.widget.ProgressBar;
30import android.widget.ScrollView;
31import android.widget.TextView;
32
33import com.android.setupwizardlib.GlifLayout;
34
35public class GlifLayoutTest extends InstrumentationTestCase {
36
37    private Context mContext;
38
39    @Override
40    protected void setUp() throws Exception {
41        super.setUp();
42        mContext = new ContextThemeWrapper(getInstrumentation().getContext(),
43                R.style.SuwThemeGlif_Light);
44    }
45
46    @SmallTest
47    public void testDefaultTemplate() {
48        GlifLayout layout = new GlifLayout(mContext);
49        assertDefaultTemplateInflated(layout);
50    }
51
52    @SmallTest
53    public void testSetHeaderText() {
54        GlifLayout layout = new GlifLayout(mContext);
55        TextView title = (TextView) layout.findViewById(R.id.suw_layout_title);
56        layout.setHeaderText("Abracadabra");
57        assertEquals("Header text should be \"Abracadabra\"", "Abracadabra", title.getText());
58    }
59
60    @SmallTest
61    public void testAddView() {
62        GlifLayout layout = new GlifLayout(mContext);
63        TextView tv = new TextView(mContext);
64        tv.setId(R.id.test_view_id);
65        layout.addView(tv);
66        assertDefaultTemplateInflated(layout);
67        View view = layout.findViewById(R.id.test_view_id);
68        assertSame("The view added should be the same text view", tv, view);
69    }
70
71    @SmallTest
72    public void testInflateFromXml() {
73        LayoutInflater inflater = LayoutInflater.from(mContext);
74        GlifLayout layout = (GlifLayout) inflater.inflate(R.layout.test_glif_layout, null);
75        assertDefaultTemplateInflated(layout);
76        View content = layout.findViewById(R.id.test_content);
77        assertTrue("@id/test_content should be a TextView", content instanceof TextView);
78    }
79
80    @SmallTest
81    public void testGetScrollView() {
82        GlifLayout layout = new GlifLayout(mContext);
83        assertNotNull("Get scroll view should not be null with default template",
84                layout.getScrollView());
85    }
86
87    @SmallTest
88    public void testSetPrimaryColor() {
89        GlifLayout layout = new GlifLayout(mContext);
90        layout.setProgressBarShown(true);
91        layout.setPrimaryColor(ColorStateList.valueOf(Color.RED));
92        assertEquals("Primary color should be red",
93                ColorStateList.valueOf(Color.RED), layout.getPrimaryColor());
94
95        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
96            ProgressBar progressBar = (ProgressBar) layout.findViewById(R.id.suw_layout_progress);
97            assertEquals("Progress bar should be tinted red",
98                    ColorStateList.valueOf(Color.RED), progressBar.getIndeterminateTintList());
99        }
100    }
101
102    @SmallTest
103    public void testWrongTheme() {
104        // Test the error message when using the wrong theme
105        mContext = new ContextThemeWrapper(getInstrumentation().getContext(),
106                android.R.style.Theme);
107        try {
108            new GlifLayout(mContext);
109            fail("Should have thrown InflateException");
110        } catch (InflateException e) {
111            assertEquals("Exception message should mention correct theme to use",
112                    "Unable to inflate layout. Are you using @style/SuwThemeGlif "
113                            + "(or its descendant) as your theme?", e.getMessage());
114        }
115    }
116
117    private void assertDefaultTemplateInflated(GlifLayout layout) {
118        View title = layout.findViewById(R.id.suw_layout_title);
119        assertNotNull("@id/suw_layout_title should not be null", title);
120
121        View icon = layout.findViewById(R.id.suw_layout_icon);
122        assertNotNull("@id/suw_layout_icon should not be null", icon);
123
124        View scrollView = layout.findViewById(R.id.suw_scroll_view);
125        assertTrue("@id/suw_scroll_view should be a ScrollView", scrollView instanceof ScrollView);
126    }
127}
128