GlifLayoutTest.java revision a6d30093e9308a88fc875b484cf16cda385c2f98
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 19fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport android.content.Context; 20ac40f3676511e1c7c09a948f5cb8481e5ea12d57Maurice Lamimport android.content.res.ColorStateList; 21ac40f3676511e1c7c09a948f5cb8481e5ea12d57Maurice Lamimport android.graphics.Color; 22d6fe3672c84594bf53882eb5ac5c26bbd6b714e7Maurice Lamimport android.os.Build; 23fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport android.test.InstrumentationTestCase; 24fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport android.test.suitebuilder.annotation.SmallTest; 25fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport android.view.ContextThemeWrapper; 2699586481628659cd2982a0248bc0d09a6ec4590eMaurice Lamimport android.view.InflateException; 27fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport android.view.LayoutInflater; 28fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport android.view.View; 29ac40f3676511e1c7c09a948f5cb8481e5ea12d57Maurice Lamimport android.widget.ProgressBar; 30fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport android.widget.ScrollView; 31fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport android.widget.TextView; 32fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam 33fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lamimport com.android.setupwizardlib.GlifLayout; 34fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam 35fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lampublic class GlifLayoutTest extends InstrumentationTestCase { 36fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam 37fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam private Context mContext; 38fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam 39fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam @Override 40fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam protected void setUp() throws Exception { 41fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam super.setUp(); 42fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam mContext = new ContextThemeWrapper(getInstrumentation().getContext(), 43fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam R.style.SuwThemeGlif_Light); 44fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam } 45fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam 46fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam @SmallTest 47fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam public void testDefaultTemplate() { 48fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam GlifLayout layout = new GlifLayout(mContext); 49fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam assertDefaultTemplateInflated(layout); 50fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam } 51fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam 52fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam @SmallTest 53fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam public void testSetHeaderText() { 54fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam GlifLayout layout = new GlifLayout(mContext); 55fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam TextView title = (TextView) layout.findViewById(R.id.suw_layout_title); 56fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam layout.setHeaderText("Abracadabra"); 57fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam assertEquals("Header text should be \"Abracadabra\"", "Abracadabra", title.getText()); 58fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam } 59fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam 60fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam @SmallTest 61fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam public void testAddView() { 62fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam GlifLayout layout = new GlifLayout(mContext); 63fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam TextView tv = new TextView(mContext); 64fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam tv.setId(R.id.test_view_id); 65fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam layout.addView(tv); 66fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam assertDefaultTemplateInflated(layout); 67fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam View view = layout.findViewById(R.id.test_view_id); 68fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam assertSame("The view added should be the same text view", tv, view); 69fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam } 70fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam 71fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam @SmallTest 72fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam public void testInflateFromXml() { 73fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam LayoutInflater inflater = LayoutInflater.from(mContext); 74fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam GlifLayout layout = (GlifLayout) inflater.inflate(R.layout.test_glif_layout, null); 75fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam assertDefaultTemplateInflated(layout); 76fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam View content = layout.findViewById(R.id.test_content); 77fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam assertTrue("@id/test_content should be a TextView", content instanceof TextView); 78fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam } 79fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam 80fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam @SmallTest 81a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam public void testPrimaryColorFromXml() { 82a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam LayoutInflater inflater = LayoutInflater.from(mContext); 83a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam GlifLayout layout = 84a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam (GlifLayout) inflater.inflate(R.layout.test_glif_layout_primary_color, null); 85a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam assertDefaultTemplateInflated(layout); 86a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam 87a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam assertEquals(ColorStateList.valueOf(Color.RED), layout.getPrimaryColor()); 88a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam } 89a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam 90a6d30093e9308a88fc875b484cf16cda385c2f98Maurice Lam @SmallTest 91fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam public void testGetScrollView() { 92fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam GlifLayout layout = new GlifLayout(mContext); 93fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam assertNotNull("Get scroll view should not be null with default template", 94fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam layout.getScrollView()); 95fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam } 96fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam 97ac40f3676511e1c7c09a948f5cb8481e5ea12d57Maurice Lam @SmallTest 98ac40f3676511e1c7c09a948f5cb8481e5ea12d57Maurice Lam public void testSetPrimaryColor() { 99ac40f3676511e1c7c09a948f5cb8481e5ea12d57Maurice Lam GlifLayout layout = new GlifLayout(mContext); 100d6fe3672c84594bf53882eb5ac5c26bbd6b714e7Maurice Lam layout.setProgressBarShown(true); 101ac40f3676511e1c7c09a948f5cb8481e5ea12d57Maurice Lam layout.setPrimaryColor(ColorStateList.valueOf(Color.RED)); 102ac40f3676511e1c7c09a948f5cb8481e5ea12d57Maurice Lam assertEquals("Primary color should be red", 103ac40f3676511e1c7c09a948f5cb8481e5ea12d57Maurice Lam ColorStateList.valueOf(Color.RED), layout.getPrimaryColor()); 104ac40f3676511e1c7c09a948f5cb8481e5ea12d57Maurice Lam 105d6fe3672c84594bf53882eb5ac5c26bbd6b714e7Maurice Lam if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 106d6fe3672c84594bf53882eb5ac5c26bbd6b714e7Maurice Lam ProgressBar progressBar = (ProgressBar) layout.findViewById(R.id.suw_layout_progress); 107d6fe3672c84594bf53882eb5ac5c26bbd6b714e7Maurice Lam assertEquals("Progress bar should be tinted red", 108d6fe3672c84594bf53882eb5ac5c26bbd6b714e7Maurice Lam ColorStateList.valueOf(Color.RED), progressBar.getIndeterminateTintList()); 109bc89919a0287256bf8255e8013c1c7c2ccf49f58Ajay Nadathur assertEquals("Determinate progress bar should also be tinted red", 110bc89919a0287256bf8255e8013c1c7c2ccf49f58Ajay Nadathur ColorStateList.valueOf(Color.RED), progressBar.getProgressBackgroundTintList()); 111d6fe3672c84594bf53882eb5ac5c26bbd6b714e7Maurice Lam } 112ac40f3676511e1c7c09a948f5cb8481e5ea12d57Maurice Lam } 113ac40f3676511e1c7c09a948f5cb8481e5ea12d57Maurice Lam 11499586481628659cd2982a0248bc0d09a6ec4590eMaurice Lam @SmallTest 11599586481628659cd2982a0248bc0d09a6ec4590eMaurice Lam public void testWrongTheme() { 11699586481628659cd2982a0248bc0d09a6ec4590eMaurice Lam // Test the error message when using the wrong theme 11799586481628659cd2982a0248bc0d09a6ec4590eMaurice Lam mContext = new ContextThemeWrapper(getInstrumentation().getContext(), 11899586481628659cd2982a0248bc0d09a6ec4590eMaurice Lam android.R.style.Theme); 11999586481628659cd2982a0248bc0d09a6ec4590eMaurice Lam try { 12099586481628659cd2982a0248bc0d09a6ec4590eMaurice Lam new GlifLayout(mContext); 12199586481628659cd2982a0248bc0d09a6ec4590eMaurice Lam fail("Should have thrown InflateException"); 12299586481628659cd2982a0248bc0d09a6ec4590eMaurice Lam } catch (InflateException e) { 12399586481628659cd2982a0248bc0d09a6ec4590eMaurice Lam assertEquals("Exception message should mention correct theme to use", 12499586481628659cd2982a0248bc0d09a6ec4590eMaurice Lam "Unable to inflate layout. Are you using @style/SuwThemeGlif " 12599586481628659cd2982a0248bc0d09a6ec4590eMaurice Lam + "(or its descendant) as your theme?", e.getMessage()); 12699586481628659cd2982a0248bc0d09a6ec4590eMaurice Lam } 12799586481628659cd2982a0248bc0d09a6ec4590eMaurice Lam } 12899586481628659cd2982a0248bc0d09a6ec4590eMaurice Lam 129b6fb8bb15f4c6b08eb7f875c21df1005a8679594Maurice Lam @SmallTest 130b6fb8bb15f4c6b08eb7f875c21df1005a8679594Maurice Lam public void testPeekProgressBarNull() { 131b6fb8bb15f4c6b08eb7f875c21df1005a8679594Maurice Lam GlifLayout layout = new GlifLayout(mContext); 132b6fb8bb15f4c6b08eb7f875c21df1005a8679594Maurice Lam assertNull("PeekProgressBar should return null initially", layout.peekProgressBar()); 133b6fb8bb15f4c6b08eb7f875c21df1005a8679594Maurice Lam } 134b6fb8bb15f4c6b08eb7f875c21df1005a8679594Maurice Lam 135b6fb8bb15f4c6b08eb7f875c21df1005a8679594Maurice Lam @SmallTest 136d66273847cb8cb69513db37e26e6ab0ee0590cc3Maurice Lam public void testPeekProgressBar() { 137b6fb8bb15f4c6b08eb7f875c21df1005a8679594Maurice Lam GlifLayout layout = new GlifLayout(mContext); 138b6fb8bb15f4c6b08eb7f875c21df1005a8679594Maurice Lam layout.setProgressBarShown(true); 139b6fb8bb15f4c6b08eb7f875c21df1005a8679594Maurice Lam assertNotNull("Peek progress bar should return the bar after setProgressBarShown(true)", 140b6fb8bb15f4c6b08eb7f875c21df1005a8679594Maurice Lam layout.peekProgressBar()); 141b6fb8bb15f4c6b08eb7f875c21df1005a8679594Maurice Lam } 142b6fb8bb15f4c6b08eb7f875c21df1005a8679594Maurice Lam 143d66273847cb8cb69513db37e26e6ab0ee0590cc3Maurice Lam @SmallTest 144d66273847cb8cb69513db37e26e6ab0ee0590cc3Maurice Lam public void testSetProgressBarShownInvalid() { 145d66273847cb8cb69513db37e26e6ab0ee0590cc3Maurice Lam GlifLayout layout = new GlifLayout(mContext, R.layout.test_template); 146d66273847cb8cb69513db37e26e6ab0ee0590cc3Maurice Lam layout.setProgressBarShown(true); 147d66273847cb8cb69513db37e26e6ab0ee0590cc3Maurice Lam // This is a no-op because there is no progress bar stub 148d66273847cb8cb69513db37e26e6ab0ee0590cc3Maurice Lam } 149d66273847cb8cb69513db37e26e6ab0ee0590cc3Maurice Lam 150fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam private void assertDefaultTemplateInflated(GlifLayout layout) { 151fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam View title = layout.findViewById(R.id.suw_layout_title); 152fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam assertNotNull("@id/suw_layout_title should not be null", title); 153fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam 154fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam View icon = layout.findViewById(R.id.suw_layout_icon); 155fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam assertNotNull("@id/suw_layout_icon should not be null", icon); 156fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam 157fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam View scrollView = layout.findViewById(R.id.suw_scroll_view); 158fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam assertTrue("@id/suw_scroll_view should be a ScrollView", scrollView instanceof ScrollView); 159fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam } 160fce4cf6161b2a1644ad21034f0afe4087d659ab4Maurice Lam} 161