SetupWizardLayoutTest.java revision 7514f1cee29b3feb4822ce16945c1c312057d24f
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.assertNull;
23import static org.junit.Assert.assertSame;
24import static org.junit.Assert.assertTrue;
25import static org.junit.Assert.fail;
26
27import android.content.Context;
28import android.graphics.Color;
29import android.graphics.drawable.ColorDrawable;
30import android.os.Parcelable;
31import android.support.annotation.IdRes;
32import android.support.test.InstrumentationRegistry;
33import android.support.test.filters.SmallTest;
34import android.support.test.runner.AndroidJUnit4;
35import android.util.SparseArray;
36import android.view.AbsSavedState;
37import android.view.ContextThemeWrapper;
38import android.view.InflateException;
39import android.view.LayoutInflater;
40import android.view.View;
41import android.widget.ProgressBar;
42import android.widget.TextView;
43
44import com.android.setupwizardlib.SetupWizardLayout;
45import com.android.setupwizardlib.template.HeaderMixin;
46import com.android.setupwizardlib.template.NavigationBarMixin;
47import com.android.setupwizardlib.template.ProgressBarMixin;
48import com.android.setupwizardlib.view.NavigationBar;
49
50import org.junit.Before;
51import org.junit.Test;
52import org.junit.runner.RunWith;
53
54@RunWith(AndroidJUnit4.class)
55@SmallTest
56public class SetupWizardLayoutTest {
57
58    @IdRes
59    private static final int ID1234 = 1234;
60
61    private Context mContext;
62
63    @Before
64    public void setUp() throws Exception {
65        mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(),
66                R.style.SuwThemeMaterial_Light);
67    }
68
69    @Test
70    public void testDefaultTemplate() {
71        SetupWizardLayout layout = new SetupWizardLayout(mContext);
72        assertDefaultTemplateInflated(layout);
73    }
74
75    @Test
76    public void testSetHeaderText() {
77        SetupWizardLayout layout = new SetupWizardLayout(mContext);
78        TextView title = (TextView) layout.findViewById(R.id.suw_layout_title);
79        layout.setHeaderText("Abracadabra");
80        assertEquals("Header text should be \"Abracadabra\"", "Abracadabra", title.getText());
81    }
82
83    @Test
84    public void testAddView() {
85        SetupWizardLayout layout = new SetupWizardLayout(mContext);
86        TextView tv = new TextView(mContext);
87        tv.setId(R.id.test_view_id);
88        layout.addView(tv);
89        assertDefaultTemplateInflated(layout);
90        View view = layout.findViewById(R.id.test_view_id);
91        assertSame("The view added should be the same text view", tv, view);
92    }
93
94    @Test
95    public void testInflateFromXml() {
96        LayoutInflater inflater = LayoutInflater.from(mContext);
97        SetupWizardLayout layout = (SetupWizardLayout) inflater.inflate(R.layout.test_layout, null);
98        assertDefaultTemplateInflated(layout);
99        View content = layout.findViewById(R.id.test_content);
100        assertTrue("@id/test_content should be a TextView", content instanceof TextView);
101    }
102
103    @Test
104    public void testCustomTemplate() {
105        SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template);
106        View templateView = layout.findViewById(R.id.test_template_view);
107        assertNotNull("@id/test_template_view should exist in template", templateView);
108
109        TextView tv = new TextView(mContext);
110        tv.setId(R.id.test_view_id);
111        layout.addView(tv);
112
113        templateView = layout.findViewById(R.id.test_template_view);
114        assertNotNull("@id/test_template_view should exist in template", templateView);
115        View contentView = layout.findViewById(R.id.test_view_id);
116        assertSame("The view added should be the same text view", tv, contentView);
117
118        // The following methods should be no-ops because the custom template doesn't contain the
119        // corresponding optional views. Just check that they don't throw exceptions.
120        layout.setHeaderText("Abracadabra");
121        layout.setIllustration(new ColorDrawable(Color.MAGENTA));
122        layout.setLayoutBackground(new ColorDrawable(Color.RED));
123    }
124
125    @Test
126    public void testGetNavigationBar() {
127        final SetupWizardLayout layout = new SetupWizardLayout(mContext);
128        final NavigationBar navigationBar = layout.getNavigationBar();
129        assertEquals("Navigation bar should have ID = @id/suw_layout_navigation_bar",
130                R.id.suw_layout_navigation_bar, navigationBar.getId());
131    }
132
133    @Test
134    public void testGetNavigationBarNull() {
135        // test_template does not have navigation bar so getNavigationBar() should return null.
136        final SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template);
137        final NavigationBar navigationBar = layout.getNavigationBar();
138        assertNull("getNavigationBar() in test_template should return null", navigationBar);
139    }
140
141    @Test
142    public void testShowProgressBar() {
143        final SetupWizardLayout layout = new SetupWizardLayout(mContext);
144        layout.showProgressBar();
145        assertTrue("Progress bar should be shown", layout.isProgressBarShown());
146        final View progressBar = layout.findViewById(R.id.suw_layout_progress);
147        assertTrue("Progress bar view should be shown",
148                progressBar instanceof ProgressBar && progressBar.getVisibility() == View.VISIBLE);
149    }
150
151    @Test
152    public void testHideProgressBar() {
153        final SetupWizardLayout layout = new SetupWizardLayout(mContext);
154        layout.showProgressBar();
155        assertTrue("Progress bar should be shown", layout.isProgressBarShown());
156        layout.hideProgressBar();
157        assertFalse("Progress bar should be hidden", layout.isProgressBarShown());
158        final View progressBar = layout.findViewById(R.id.suw_layout_progress);
159        assertTrue("Progress bar view should exist",
160                progressBar == null || progressBar.getVisibility() != View.VISIBLE);
161    }
162
163    @Test
164    public void testShowProgressBarNotExist() {
165        // test_template does not have progress bar, so showNavigationBar() should do nothing.
166        final SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template);
167        layout.showProgressBar();
168        assertFalse("Progress bar should not be shown", layout.isProgressBarShown());
169    }
170
171    @Test
172    public void testWrongTheme() {
173        // Test the error message when using the wrong theme
174        mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(),
175                android.R.style.Theme);
176        try {
177            new SetupWizardLayout(mContext);
178            fail("Should have thrown InflateException");
179        } catch (InflateException e) {
180            assertEquals("Exception message should mention correct theme to use",
181                    "Unable to inflate layout. Are you using @style/SuwThemeMaterial "
182                            + "(or its descendant) as your theme?", e.getMessage());
183        }
184    }
185
186    @Test
187    public void testOnRestoreFromInstanceState() {
188        final SetupWizardLayout layout = new SetupWizardLayout(mContext);
189        layout.setId(ID1234);
190
191        SparseArray<Parcelable> container = new SparseArray<>();
192        layout.saveHierarchyState(container);
193
194        final SetupWizardLayout layout2 = new SetupWizardLayout(mContext);
195        layout2.setId(ID1234);
196        layout2.restoreHierarchyState(container);
197
198        assertFalse("Progress bar should not be shown", layout2.isProgressBarShown());
199    }
200
201    @Test
202    public void testOnRestoreFromInstanceStateProgressBarShown() {
203        final SetupWizardLayout layout = new SetupWizardLayout(mContext);
204        layout.setId(ID1234);
205
206        layout.setProgressBarShown(true);
207
208        SparseArray<Parcelable> container = new SparseArray<>();
209        layout.saveHierarchyState(container);
210
211        final SetupWizardLayout layout2 = new SetupWizardLayout(mContext);
212        layout2.setId(ID1234);
213        layout2.restoreHierarchyState(container);
214
215        assertTrue("Progress bar should be shown", layout2.isProgressBarShown());
216    }
217
218    @Test
219    public void testOnRestoreFromIncompatibleInstanceState() {
220        final SetupWizardLayout layout = new SetupWizardLayout(mContext);
221        layout.setId(ID1234);
222
223        SparseArray<Parcelable> container = new SparseArray<>();
224        container.put(1234, AbsSavedState.EMPTY_STATE);
225        layout.restoreHierarchyState(container);
226
227        // SetupWizardLayout shouldn't crash with incompatible Parcelable
228
229        assertFalse("Progress bar should not be shown", layout.isProgressBarShown());
230    }
231
232    @Test
233    public void testGetMixins() {
234        final SetupWizardLayout layout = new SetupWizardLayout(mContext);
235        assertNotNull("SetupWizardLayout should have header mixin",
236                layout.getMixin(HeaderMixin.class));
237        assertNotNull("SetupWizardLayout should have progress bar mixin",
238                layout.getMixin(ProgressBarMixin.class));
239        assertNotNull("SetupWizardLayout should have navigation bar mixin",
240                layout.getMixin(NavigationBarMixin.class));
241    }
242
243    private void assertDefaultTemplateInflated(SetupWizardLayout layout) {
244        View decorView = layout.findViewById(R.id.suw_layout_decor);
245        View navbar = layout.findViewById(R.id.suw_layout_navigation_bar);
246        View title = layout.findViewById(R.id.suw_layout_title);
247        assertNotNull("@id/suw_layout_decor_view should not be null", decorView);
248        assertTrue("@id/suw_layout_navigation_bar should be an instance of NavigationBar",
249                navbar instanceof NavigationBar);
250        assertNotNull("@id/suw_layout_title should not be null", title);
251    }
252}
253