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