summaryrefslogtreecommitdiff
path: root/library/test/robotest/src/com/android/setupwizardlib/GlifLayoutTest.java
blob: a36ce76e4fb370edf9785f74ad4bf1b2a5d5094a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.setupwizardlib;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.robolectric.RuntimeEnvironment.application;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.IdRes;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import com.android.setupwizardlib.template.ColoredHeaderMixin;
import com.android.setupwizardlib.template.HeaderMixin;
import com.android.setupwizardlib.template.IconMixin;
import com.android.setupwizardlib.template.ProgressBarMixin;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

@RunWith(SuwLibRobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK })
public class GlifLayoutTest {

    private Context mContext;

    @Before
    public void setUp() throws Exception {
        mContext = new ContextThemeWrapper(application, R.style.SuwThemeGlif_Light);
    }

    @Test
    public void testDefaultTemplate() {
        GlifLayout layout = new GlifLayout(mContext);
        assertDefaultTemplateInflated(layout);
    }

    @Test
    public void testSetHeaderText() {
        GlifLayout layout = new GlifLayout(mContext);
        TextView title = (TextView) layout.findViewById(R.id.suw_layout_title);
        layout.setHeaderText("Abracadabra");
        assertEquals("Header text should be \"Abracadabra\"", "Abracadabra", title.getText());
    }

    @Test
    public void testAddView() {
        @IdRes int testViewId = 123456;
        GlifLayout layout = new GlifLayout(mContext);
        TextView tv = new TextView(mContext);
        tv.setId(testViewId);
        layout.addView(tv);
        assertDefaultTemplateInflated(layout);
        View view = layout.findViewById(testViewId);
        assertSame("The view added should be the same text view", tv, view);
    }

    @Test
    public void testGetScrollView() {
        GlifLayout layout = new GlifLayout(mContext);
        assertNotNull("Get scroll view should not be null with default template",
                layout.getScrollView());
    }

    @Test
    public void testSetPrimaryColor() {
        GlifLayout layout = new GlifLayout(mContext);
        layout.setProgressBarShown(true);
        layout.setPrimaryColor(ColorStateList.valueOf(Color.RED));
        assertEquals("Primary color should be red",
                ColorStateList.valueOf(Color.RED), layout.getPrimaryColor());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ProgressBar progressBar = (ProgressBar) layout.findViewById(R.id.suw_layout_progress);
            assertEquals("Progress bar should be tinted red",
                    ColorStateList.valueOf(Color.RED), progressBar.getIndeterminateTintList());
            assertEquals("Determinate progress bar should also be tinted red",
                    ColorStateList.valueOf(Color.RED), progressBar.getProgressBackgroundTintList());
        }
    }

    @Config(qualifiers = "sw600dp")
    @Test
    public void testSetPrimaryColorTablet() {
        GlifLayout layout = new GlifLayout(mContext);
        layout.setProgressBarShown(true);
        layout.setPrimaryColor(ColorStateList.valueOf(Color.RED));
        assertEquals("Primary color should be red",
                ColorStateList.valueOf(Color.RED), layout.getPrimaryColor());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ProgressBar progressBar = (ProgressBar) layout.findViewById(R.id.suw_layout_progress);
            assertEquals("Progress bar should be tinted red",
                    ColorStateList.valueOf(Color.RED), progressBar.getIndeterminateTintList());
            assertEquals("Determinate progress bar should also be tinted red",
                    ColorStateList.valueOf(Color.RED), progressBar.getProgressBackgroundTintList());
        }

        final View patternBg = layout.findManagedViewById(R.id.suw_pattern_bg);
        final GlifPatternDrawable background = (GlifPatternDrawable) patternBg.getBackground();
        assertEquals(Color.RED, background.getColor());
    }

    @Test
    public void testNonGlifTheme() {
        mContext = new ContextThemeWrapper(application, android.R.style.Theme);
        new GlifLayout(mContext);
        // Inflating with a non-GLIF theme should not crash
    }

    @Test
    public void testPeekProgressBarNull() {
        GlifLayout layout = new GlifLayout(mContext);
        assertNull("PeekProgressBar should return null initially", layout.peekProgressBar());
    }

    @Test
    public void testPeekProgressBar() {
        GlifLayout layout = new GlifLayout(mContext);
        layout.setProgressBarShown(true);
        assertNotNull("Peek progress bar should return the bar after setProgressBarShown(true)",
                layout.peekProgressBar());
    }

    @Test
    public void testMixins() {
        GlifLayout layout = new GlifLayout(mContext);
        final HeaderMixin header = layout.getMixin(HeaderMixin.class);
        assertTrue("Header should be instance of ColoredHeaderMixin. "
                + "Found " + header.getClass() + " instead.", header instanceof ColoredHeaderMixin);

        assertNotNull("GlifLayout should have icon mixin", layout.getMixin(IconMixin.class));
        assertNotNull("GlifLayout should have progress bar mixin",
                layout.getMixin(ProgressBarMixin.class));
    }

    private void assertDefaultTemplateInflated(GlifLayout layout) {
        View title = layout.findViewById(R.id.suw_layout_title);
        assertNotNull("@id/suw_layout_title should not be null", title);

        View icon = layout.findViewById(R.id.suw_layout_icon);
        assertNotNull("@id/suw_layout_icon should not be null", icon);

        View scrollView = layout.findViewById(R.id.suw_scroll_view);
        assertTrue("@id/suw_scroll_view should be a ScrollView", scrollView instanceof ScrollView);
    }
}