SetupWizardLayoutTest.java revision 7e5f0f0ea3b3075258cac9d26f90fd97c1a71dca
1170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam/*
2170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam * Copyright (C) 2015 The Android Open Source Project
3170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam *
4170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam * Licensed under the Apache License, Version 2.0 (the "License");
5170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam * you may not use this file except in compliance with the License.
6170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam * You may obtain a copy of the License at
7170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam *
8170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam *      http://www.apache.org/licenses/LICENSE-2.0
9170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam *
10170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam * Unless required by applicable law or agreed to in writing, software
11170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam * distributed under the License is distributed on an "AS IS" BASIS,
12170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam * See the License for the specific language governing permissions and
14170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam * limitations under the License.
15170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam */
16170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam
17170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lampackage com.android.setupwizardlib.test;
18170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam
19170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.annotation.TargetApi;
2024ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lamimport android.content.Context;
21170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.graphics.Color;
22170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.graphics.drawable.ColorDrawable;
23170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.os.Build.VERSION_CODES;
24170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.test.InstrumentationTestCase;
25170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.test.suitebuilder.annotation.SmallTest;
2624ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lamimport android.view.ContextThemeWrapper;
27170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.view.LayoutInflater;
28170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.view.View;
29170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.widget.TextView;
30170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam
31170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport com.android.setupwizardlib.SetupWizardLayout;
32170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport com.android.setupwizardlib.view.NavigationBar;
33170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam
34170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lampublic class SetupWizardLayoutTests extends InstrumentationTestCase {
35170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam
3624ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam    private Context mContext;
3724ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam
3824ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam    @Override
3924ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam    protected void setUp() throws Exception {
4024ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam        super.setUp();
4124ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam        mContext = new ContextThemeWrapper(getInstrumentation().getContext(),
4224ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam                R.style.SuwThemeMaterial_Light);
4324ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam    }
4424ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam
45170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    @SmallTest
46170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    public void testDefaultTemplate() {
4724ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam        SetupWizardLayout layout = new SetupWizardLayout(mContext);
48170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        assertDefaultTemplateInflated(layout);
49170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    }
50170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam
51170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    @SmallTest
52170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    public void testSetHeaderText() {
5324ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam        SetupWizardLayout layout = new SetupWizardLayout(mContext);
54170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        TextView title = (TextView) layout.findViewById(R.id.suw_layout_title);
55170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        layout.setHeaderText("Abracadabra");
56170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        assertEquals("Header text should be \"Abracadabra\"", "Abracadabra", title.getText());
57170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    }
58170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam
59170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    @TargetApi(VERSION_CODES.JELLY_BEAN_MR1)
60170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    @SmallTest
61170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    public void testAddView() {
6224ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam        SetupWizardLayout layout = new SetupWizardLayout(mContext);
6324ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam        TextView tv = new TextView(mContext);
64170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        int id = View.generateViewId();
65170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        tv.setId(id);
66170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        layout.addView(tv);
67170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        assertDefaultTemplateInflated(layout);
68170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        View view = layout.findViewById(id);
69170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        assertSame("The view added should be the same text view", tv, view);
70170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    }
71170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam
72170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    @SmallTest
73170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    public void testInflateFromXml() {
7424ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam        LayoutInflater inflater = LayoutInflater.from(mContext);
75170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        SetupWizardLayout layout = (SetupWizardLayout) inflater.inflate(R.layout.test_layout, null);
76170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        assertDefaultTemplateInflated(layout);
77170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        View content = layout.findViewById(R.id.test_content);
78170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        assertTrue("@id/test_content should be a TextView", content instanceof TextView);
79170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    }
80170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam
81170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    @TargetApi(VERSION_CODES.JELLY_BEAN_MR1)
82170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    @SmallTest
83170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    public void testCustomTemplate() {
8424ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam        SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template);
85170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        View templateView = layout.findViewById(R.id.test_template_view);
86170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        assertNotNull("@id/test_template_view should exist in template", templateView);
87170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam
8824ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam        TextView tv = new TextView(mContext);
89170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        int id = View.generateViewId();
90170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        tv.setId(id);
91170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        layout.addView(tv);
92170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam
93170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        templateView = layout.findViewById(R.id.test_template_view);
94170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        assertNotNull("@id/test_template_view should exist in template", templateView);
95170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        View contentView = layout.findViewById(id);
96170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        assertSame("The view added should be the same text view", tv, contentView);
97170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam
98170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        // The following methods should be no-ops because the custom template doesn't contain the
99170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        // corresponding optional views. Just check that they don't throw exceptions.
100170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        layout.setHeaderText("Abracadabra");
101170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        layout.setIllustration(new ColorDrawable(Color.MAGENTA));
102170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        layout.setLayoutBackground(new ColorDrawable(Color.RED));
103170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    }
104170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam
1057e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam    @SmallTest
1067e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam    public void testGetNavigationBar() {
1077e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam        final SetupWizardLayout layout = new SetupWizardLayout(mContext);
1087e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam        final NavigationBar navigationBar = layout.getNavigationBar();
1097e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam        assertEquals("Navigation bar should have ID = @id/suw_layout_navigation_bar",
1107e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam                R.id.suw_layout_navigation_bar, navigationBar.getId());
1117e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam    }
1127e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam
1137e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam    @SmallTest
1147e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam    public void testGetNavigationBarNull() {
1157e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam        // test_template does not have navigation bar so getNavigationBar() should return null.
1167e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam        final SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template);
1177e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam        final NavigationBar navigationBar = layout.getNavigationBar();
1187e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam        assertNull("getNavigationBar() in test_template should return null", navigationBar);
1197e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam    }
1207e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam
121170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    private void assertDefaultTemplateInflated(SetupWizardLayout layout) {
122170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        View decorView = layout.findViewById(R.id.suw_layout_decor);
123170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        View navbar = layout.findViewById(R.id.suw_layout_navigation_bar);
124170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        View title = layout.findViewById(R.id.suw_layout_title);
125170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        assertNotNull("@id/suw_layout_decor_view should not be null", decorView);
126170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        assertTrue("@id/suw_layout_navigation_bar should be an instance of NavigationBar",
127170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam                navbar instanceof NavigationBar);
128170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam        assertNotNull("@id/suw_layout_title should not be null", title);
129170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam    }
130170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam}
131