SetupWizardLayoutTest.java revision fd95620a24f1938a24036e8fb4150710e8a2ae90
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.test.InstrumentationTestCase;
23import android.test.suitebuilder.annotation.SmallTest;
24import android.view.ContextThemeWrapper;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.widget.ProgressBar;
28import android.widget.TextView;
29
30import com.android.setupwizardlib.SetupWizardLayout;
31import com.android.setupwizardlib.view.NavigationBar;
32
33public class SetupWizardLayoutTest extends InstrumentationTestCase {
34
35    private Context mContext;
36
37    @Override
38    protected void setUp() throws Exception {
39        super.setUp();
40        mContext = new ContextThemeWrapper(getInstrumentation().getContext(),
41                R.style.SuwThemeMaterial_Light);
42    }
43
44    @SmallTest
45    public void testDefaultTemplate() {
46        SetupWizardLayout layout = new SetupWizardLayout(mContext);
47        assertDefaultTemplateInflated(layout);
48    }
49
50    @SmallTest
51    public void testSetHeaderText() {
52        SetupWizardLayout layout = new SetupWizardLayout(mContext);
53        TextView title = (TextView) layout.findViewById(R.id.suw_layout_title);
54        layout.setHeaderText("Abracadabra");
55        assertEquals("Header text should be \"Abracadabra\"", "Abracadabra", title.getText());
56    }
57
58    @SmallTest
59    public void testAddView() {
60        SetupWizardLayout layout = new SetupWizardLayout(mContext);
61        TextView tv = new TextView(mContext);
62        tv.setId(R.id.test_view_id);
63        layout.addView(tv);
64        assertDefaultTemplateInflated(layout);
65        View view = layout.findViewById(R.id.test_view_id);
66        assertSame("The view added should be the same text view", tv, view);
67    }
68
69    @SmallTest
70    public void testInflateFromXml() {
71        LayoutInflater inflater = LayoutInflater.from(mContext);
72        SetupWizardLayout layout = (SetupWizardLayout) inflater.inflate(R.layout.test_layout, null);
73        assertDefaultTemplateInflated(layout);
74        View content = layout.findViewById(R.id.test_content);
75        assertTrue("@id/test_content should be a TextView", content instanceof TextView);
76    }
77
78    @SmallTest
79    public void testCustomTemplate() {
80        SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template);
81        View templateView = layout.findViewById(R.id.test_template_view);
82        assertNotNull("@id/test_template_view should exist in template", templateView);
83
84        TextView tv = new TextView(mContext);
85        tv.setId(R.id.test_view_id);
86        layout.addView(tv);
87
88        templateView = layout.findViewById(R.id.test_template_view);
89        assertNotNull("@id/test_template_view should exist in template", templateView);
90        View contentView = layout.findViewById(R.id.test_view_id);
91        assertSame("The view added should be the same text view", tv, contentView);
92
93        // The following methods should be no-ops because the custom template doesn't contain the
94        // corresponding optional views. Just check that they don't throw exceptions.
95        layout.setHeaderText("Abracadabra");
96        layout.setIllustration(new ColorDrawable(Color.MAGENTA));
97        layout.setLayoutBackground(new ColorDrawable(Color.RED));
98    }
99
100    @SmallTest
101    public void testGetNavigationBar() {
102        final SetupWizardLayout layout = new SetupWizardLayout(mContext);
103        final NavigationBar navigationBar = layout.getNavigationBar();
104        assertEquals("Navigation bar should have ID = @id/suw_layout_navigation_bar",
105                R.id.suw_layout_navigation_bar, navigationBar.getId());
106    }
107
108    @SmallTest
109    public void testGetNavigationBarNull() {
110        // test_template does not have navigation bar so getNavigationBar() should return null.
111        final SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template);
112        final NavigationBar navigationBar = layout.getNavigationBar();
113        assertNull("getNavigationBar() in test_template should return null", navigationBar);
114    }
115
116    @SmallTest
117    public void testShowProgressBar() {
118        final SetupWizardLayout layout = new SetupWizardLayout(mContext);
119        layout.showProgressBar();
120        assertTrue("Progress bar should be shown", layout.isProgressBarShown());
121        final View progressBar = layout.findViewById(R.id.suw_layout_progress);
122        assertTrue("Progress bar view should be shown",
123                progressBar instanceof ProgressBar && progressBar.getVisibility() == View.VISIBLE);
124    }
125
126    @SmallTest
127    public void testHideProgressBar() {
128        final SetupWizardLayout layout = new SetupWizardLayout(mContext);
129        layout.showProgressBar();
130        assertTrue("Progress bar should be shown", layout.isProgressBarShown());
131        layout.hideProgressBar();
132        assertFalse("Progress bar should be hidden", layout.isProgressBarShown());
133        final View progressBar = layout.findViewById(R.id.suw_layout_progress);
134        assertTrue("Progress bar view should exist",
135                progressBar == null || progressBar.getVisibility() != View.VISIBLE);
136    }
137
138    @SmallTest
139    public void testShowProgressBarNotExist() {
140        // test_template does not have progress bar, so showNavigationBar() should do nothing.
141        final SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template);
142        layout.showProgressBar();
143        assertFalse("Progress bar should not be shown", layout.isProgressBarShown());
144    }
145
146    private void assertDefaultTemplateInflated(SetupWizardLayout layout) {
147        View decorView = layout.findViewById(R.id.suw_layout_decor);
148        View navbar = layout.findViewById(R.id.suw_layout_navigation_bar);
149        View title = layout.findViewById(R.id.suw_layout_title);
150        assertNotNull("@id/suw_layout_decor_view should not be null", decorView);
151        assertTrue("@id/suw_layout_navigation_bar should be an instance of NavigationBar",
152                navbar instanceof NavigationBar);
153        assertNotNull("@id/suw_layout_title should not be null", title);
154    }
155}
156