1fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam/*
2fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam * Copyright (C) 2015 The Android Open Source Project
3fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam *
4fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam * Licensed under the Apache License, Version 2.0 (the "License");
5fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam * you may not use this file except in compliance with the License.
6fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam * You may obtain a copy of the License at
7fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam *
8fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam *      http://www.apache.org/licenses/LICENSE-2.0
9fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam *
10fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam * Unless required by applicable law or agreed to in writing, software
11fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam * distributed under the License is distributed on an "AS IS" BASIS,
12fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam * See the License for the specific language governing permissions and
14fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam * limitations under the License.
15fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam */
16fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam
17fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lampackage com.android.setupwizardlib.test;
18fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam
192614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lamimport static org.junit.Assert.assertEquals;
2039056e5dc4b068745c2d9be69e5cd0c008bdc117Maurice Lamimport static org.junit.Assert.assertFalse;
212614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lamimport static org.junit.Assert.assertNotNull;
222614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lamimport static org.junit.Assert.assertTrue;
232614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lam
24fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport android.content.Context;
25ac40f3676511e1c7c09a948f5cb8481e5ea12d57Maurice Lamimport android.content.res.ColorStateList;
26ac40f3676511e1c7c09a948f5cb8481e5ea12d57Maurice Lamimport android.graphics.Color;
27d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lamimport android.os.Build.VERSION;
28d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lamimport android.os.Build.VERSION_CODES;
292614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lamimport android.support.test.InstrumentationRegistry;
302614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lamimport android.support.test.filters.SmallTest;
312614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lamimport android.support.test.runner.AndroidJUnit4;
32fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport android.view.ContextThemeWrapper;
33d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lamimport android.view.Gravity;
34fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport android.view.LayoutInflater;
35fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport android.view.View;
36d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lamimport android.widget.LinearLayout;
37fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport android.widget.ScrollView;
38fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport android.widget.TextView;
39fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam
40fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport com.android.setupwizardlib.GlifLayout;
41fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam
422614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lamimport org.junit.Before;
432614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lamimport org.junit.Test;
442614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lamimport org.junit.runner.RunWith;
452614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lam
462614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lam@RunWith(AndroidJUnit4.class)
472614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lam@SmallTest
482614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lampublic class GlifLayoutTest {
49fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam
50fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam    private Context mContext;
51fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam
522614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lam    @Before
532614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lam    public void setUp() throws Exception {
542614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lam        mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(),
55fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam                R.style.SuwThemeGlif_Light);
56fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam    }
57fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam
582614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lam    @Test
59fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam    public void testInflateFromXml() {
60fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam        LayoutInflater inflater = LayoutInflater.from(mContext);
61fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam        GlifLayout layout = (GlifLayout) inflater.inflate(R.layout.test_glif_layout, null);
62fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam        assertDefaultTemplateInflated(layout);
63fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam        View content = layout.findViewById(R.id.test_content);
64fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam        assertTrue("@id/test_content should be a TextView", content instanceof TextView);
65fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam    }
66fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam
672614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lam    @Test
68a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam    public void testPrimaryColorFromXml() {
69a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam        LayoutInflater inflater = LayoutInflater.from(mContext);
70a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam        GlifLayout layout =
71a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam                (GlifLayout) inflater.inflate(R.layout.test_glif_layout_primary_color, null);
72a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam        assertDefaultTemplateInflated(layout);
73a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam
74a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam        assertEquals(ColorStateList.valueOf(Color.RED), layout.getPrimaryColor());
75a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam    }
76a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam
772614260933341bd4ea7bb37a3435f46f82cacef7Maurice Lam    @Test
78d66273847cb8cb69513db37e26e6ab0ee0590cc3Maurice Lam    public void testSetProgressBarShownInvalid() {
79d66273847cb8cb69513db37e26e6ab0ee0590cc3Maurice Lam        GlifLayout layout = new GlifLayout(mContext, R.layout.test_template);
80d66273847cb8cb69513db37e26e6ab0ee0590cc3Maurice Lam        layout.setProgressBarShown(true);
81d66273847cb8cb69513db37e26e6ab0ee0590cc3Maurice Lam        // This is a no-op because there is no progress bar stub
82d66273847cb8cb69513db37e26e6ab0ee0590cc3Maurice Lam    }
83d66273847cb8cb69513db37e26e6ab0ee0590cc3Maurice Lam
84d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam    @Test
85921c65f4b9dd01d331e8a80e3d165966c915d4feMaurice Lam    public void testGlifTheme() {
86921c65f4b9dd01d331e8a80e3d165966c915d4feMaurice Lam        mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(),
87921c65f4b9dd01d331e8a80e3d165966c915d4feMaurice Lam                R.style.SuwThemeGlif_Light);
88921c65f4b9dd01d331e8a80e3d165966c915d4feMaurice Lam        final GlifLayout glifLayout = new GlifLayout(mContext);
89921c65f4b9dd01d331e8a80e3d165966c915d4feMaurice Lam
90921c65f4b9dd01d331e8a80e3d165966c915d4feMaurice Lam        if (VERSION.SDK_INT >= VERSION_CODES.M) {
91921c65f4b9dd01d331e8a80e3d165966c915d4feMaurice Lam            // Scroll indicators are only available on versions >= M
92921c65f4b9dd01d331e8a80e3d165966c915d4feMaurice Lam            assertEquals(View.SCROLL_INDICATOR_BOTTOM,
93921c65f4b9dd01d331e8a80e3d165966c915d4feMaurice Lam                    glifLayout.getScrollView().getScrollIndicators());
94921c65f4b9dd01d331e8a80e3d165966c915d4feMaurice Lam        }
95921c65f4b9dd01d331e8a80e3d165966c915d4feMaurice Lam    }
96921c65f4b9dd01d331e8a80e3d165966c915d4feMaurice Lam
97921c65f4b9dd01d331e8a80e3d165966c915d4feMaurice Lam    @Test
98d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam    public void testGlifPixelTheme() {
99d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam        mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(),
100d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam                R.style.SuwThemeGlifPixel_Light);
101d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam        final GlifLayout glifLayout = new GlifLayout(mContext);
102d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam        final TextView titleView = (TextView) glifLayout.findManagedViewById(R.id.suw_layout_title);
103d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam        if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
104d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam            assertEquals(View.TEXT_ALIGNMENT_GRAVITY, titleView.getTextAlignment());
105d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam        }
106d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam        assertEquals("Title text should be center aligned on GLIF Pixel theme",
107d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam                Gravity.CENTER_HORIZONTAL, titleView.getGravity() & Gravity.CENTER_HORIZONTAL);
108d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam
109d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam        if (VERSION.SDK_INT >= VERSION_CODES.N) {
110d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam            // LinearLayout.getGravity is only available on versions >= N
111d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam            final View iconView = glifLayout.findManagedViewById(R.id.suw_layout_icon);
112d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam            final LinearLayout parent = (LinearLayout) iconView.getParent();
113d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam            assertEquals("Icon should be center aligned on GLIF Pixel theme",
114d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam                    Gravity.CENTER_HORIZONTAL, parent.getGravity() & Gravity.CENTER_HORIZONTAL);
115d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam        }
11639056e5dc4b068745c2d9be69e5cd0c008bdc117Maurice Lam
11739056e5dc4b068745c2d9be69e5cd0c008bdc117Maurice Lam        assertEquals("Status bar color should be white in GLIF Pixel theme",
11839056e5dc4b068745c2d9be69e5cd0c008bdc117Maurice Lam                "fffafafa",
11939056e5dc4b068745c2d9be69e5cd0c008bdc117Maurice Lam                Integer.toHexString(glifLayout.getBackgroundBaseColor().getDefaultColor()));
12039056e5dc4b068745c2d9be69e5cd0c008bdc117Maurice Lam        assertFalse("GLIF Pixel theme shuold not have patterned background",
12139056e5dc4b068745c2d9be69e5cd0c008bdc117Maurice Lam                glifLayout.isBackgroundPatterned());
122039a55c144bc968ca9e242640ea107df10ac2867Maurice Lam
123039a55c144bc968ca9e242640ea107df10ac2867Maurice Lam        if (VERSION.SDK_INT >= VERSION_CODES.M) {
124039a55c144bc968ca9e242640ea107df10ac2867Maurice Lam            // Scroll indicators are only available on versions >= M
125039a55c144bc968ca9e242640ea107df10ac2867Maurice Lam            assertEquals(View.SCROLL_INDICATOR_TOP | View.SCROLL_INDICATOR_BOTTOM,
126039a55c144bc968ca9e242640ea107df10ac2867Maurice Lam                    glifLayout.getScrollView().getScrollIndicators());
127039a55c144bc968ca9e242640ea107df10ac2867Maurice Lam        }
128d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam    }
129d3ffc713be68823409d5dc6d2d44bb21bde5bfd0Maurice Lam
130fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam    private void assertDefaultTemplateInflated(GlifLayout layout) {
131fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam        View title = layout.findViewById(R.id.suw_layout_title);
132fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam        assertNotNull("@id/suw_layout_title should not be null", title);
133fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam
134fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam        View icon = layout.findViewById(R.id.suw_layout_icon);
135fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam        assertNotNull("@id/suw_layout_icon should not be null", icon);
136fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam
137fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam        View scrollView = layout.findViewById(R.id.suw_scroll_view);
138fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam        assertTrue("@id/suw_scroll_view should be a ScrollView", scrollView instanceof ScrollView);
139fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam    }
140fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam}
141