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 1924ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lamimport android.content.Context; 20170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.graphics.Color; 21170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.graphics.drawable.ColorDrawable; 22170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.test.InstrumentationTestCase; 23170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.test.suitebuilder.annotation.SmallTest; 2424ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lamimport android.view.ContextThemeWrapper; 25170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.view.LayoutInflater; 26170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.view.View; 27d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lamimport android.widget.ProgressBar; 28170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport android.widget.TextView; 29170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam 30170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport com.android.setupwizardlib.SetupWizardLayout; 31170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lamimport com.android.setupwizardlib.view.NavigationBar; 32170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam 33170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lampublic class SetupWizardLayoutTests extends InstrumentationTestCase { 34170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam 3524ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam private Context mContext; 3624ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam 3724ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam @Override 3824ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam protected void setUp() throws Exception { 3924ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam super.setUp(); 4024ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam mContext = new ContextThemeWrapper(getInstrumentation().getContext(), 4124ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam R.style.SuwThemeMaterial_Light); 4224ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam } 4324ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam 44170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam @SmallTest 45170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam public void testDefaultTemplate() { 4624ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam SetupWizardLayout layout = new SetupWizardLayout(mContext); 47170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam assertDefaultTemplateInflated(layout); 48170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam } 49170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam 50170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam @SmallTest 51170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam public void testSetHeaderText() { 5224ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam SetupWizardLayout layout = new SetupWizardLayout(mContext); 53170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam TextView title = (TextView) layout.findViewById(R.id.suw_layout_title); 54170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam layout.setHeaderText("Abracadabra"); 55170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam assertEquals("Header text should be \"Abracadabra\"", "Abracadabra", title.getText()); 56170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam } 57170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam 58170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam @SmallTest 59170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam public void testAddView() { 6024ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam SetupWizardLayout layout = new SetupWizardLayout(mContext); 6124ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam TextView tv = new TextView(mContext); 629f9367672191190f903955d09a4314d40869acc6Maurice Lam tv.setId(R.id.test_view_id); 63170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam layout.addView(tv); 64170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam assertDefaultTemplateInflated(layout); 659f9367672191190f903955d09a4314d40869acc6Maurice Lam View view = layout.findViewById(R.id.test_view_id); 66170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam assertSame("The view added should be the same text view", tv, view); 67170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam } 68170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam 69170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam @SmallTest 70170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam public void testInflateFromXml() { 7124ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam LayoutInflater inflater = LayoutInflater.from(mContext); 72170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam SetupWizardLayout layout = (SetupWizardLayout) inflater.inflate(R.layout.test_layout, null); 73170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam assertDefaultTemplateInflated(layout); 74170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam View content = layout.findViewById(R.id.test_content); 75170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam assertTrue("@id/test_content should be a TextView", content instanceof TextView); 76170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam } 77170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam 78170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam @SmallTest 79170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam public void testCustomTemplate() { 8024ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template); 81170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam View templateView = layout.findViewById(R.id.test_template_view); 82170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam assertNotNull("@id/test_template_view should exist in template", templateView); 83170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam 8424ab59ef4f9726316743d94d31c93bf9420e7717Maurice Lam TextView tv = new TextView(mContext); 859f9367672191190f903955d09a4314d40869acc6Maurice Lam tv.setId(R.id.test_view_id); 86170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam layout.addView(tv); 87170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam 88170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam templateView = layout.findViewById(R.id.test_template_view); 89170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam assertNotNull("@id/test_template_view should exist in template", templateView); 909f9367672191190f903955d09a4314d40869acc6Maurice Lam View contentView = layout.findViewById(R.id.test_view_id); 91170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam assertSame("The view added should be the same text view", tv, contentView); 92170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam 93170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam // The following methods should be no-ops because the custom template doesn't contain the 94170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam // corresponding optional views. Just check that they don't throw exceptions. 95170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam layout.setHeaderText("Abracadabra"); 96170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam layout.setIllustration(new ColorDrawable(Color.MAGENTA)); 97170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam layout.setLayoutBackground(new ColorDrawable(Color.RED)); 98170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam } 99170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam 1007e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam @SmallTest 1017e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam public void testGetNavigationBar() { 1027e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam final SetupWizardLayout layout = new SetupWizardLayout(mContext); 1037e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam final NavigationBar navigationBar = layout.getNavigationBar(); 1047e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam assertEquals("Navigation bar should have ID = @id/suw_layout_navigation_bar", 1057e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam R.id.suw_layout_navigation_bar, navigationBar.getId()); 1067e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam } 1077e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam 1087e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam @SmallTest 1097e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam public void testGetNavigationBarNull() { 1107e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam // test_template does not have navigation bar so getNavigationBar() should return null. 1117e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam final SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template); 1127e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam final NavigationBar navigationBar = layout.getNavigationBar(); 1137e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam assertNull("getNavigationBar() in test_template should return null", navigationBar); 1147e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam } 1157e5f0f0ea3b3075258cac9d26f90fd97c1a71dcaMaurice Lam 116d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam @SmallTest 117d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam public void testShowProgressBar() { 118d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam final SetupWizardLayout layout = new SetupWizardLayout(mContext); 119d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam layout.showProgressBar(); 120d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam assertTrue("Progress bar should be shown", layout.isProgressBarShown()); 121d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam final View progressBar = layout.findViewById(R.id.suw_layout_progress); 122d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam assertTrue("Progress bar view should be shown", 123d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam progressBar instanceof ProgressBar && progressBar.getVisibility() == View.VISIBLE); 124d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam } 125d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam 126d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam @SmallTest 127d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam public void testHideProgressBar() { 128d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam final SetupWizardLayout layout = new SetupWizardLayout(mContext); 129d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam layout.showProgressBar(); 130d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam assertTrue("Progress bar should be shown", layout.isProgressBarShown()); 131d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam layout.hideProgressBar(); 132d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam assertFalse("Progress bar should be hidden", layout.isProgressBarShown()); 133d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam final View progressBar = layout.findViewById(R.id.suw_layout_progress); 134d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam assertTrue("Progress bar view should exist", 135d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam progressBar == null || progressBar.getVisibility() != View.VISIBLE); 136d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam } 137d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam 138d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam @SmallTest 139d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam public void testShowProgressBarNotExist() { 140d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam // test_template does not have progress bar, so showNavigationBar() should do nothing. 141d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam final SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template); 142d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam layout.showProgressBar(); 143d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam assertFalse("Progress bar should not be shown", layout.isProgressBarShown()); 144d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam } 145d61674efcfaa9f591a44fc75d59566cdd5b409ebMaurice Lam 146170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam private void assertDefaultTemplateInflated(SetupWizardLayout layout) { 147170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam View decorView = layout.findViewById(R.id.suw_layout_decor); 148170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam View navbar = layout.findViewById(R.id.suw_layout_navigation_bar); 149170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam View title = layout.findViewById(R.id.suw_layout_title); 150170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam assertNotNull("@id/suw_layout_decor_view should not be null", decorView); 151170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam assertTrue("@id/suw_layout_navigation_bar should be an instance of NavigationBar", 152170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam navbar instanceof NavigationBar); 153170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam assertNotNull("@id/suw_layout_title should not be null", title); 154170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam } 155170a720fe110de6cc1a8c1cd660e591295562cfaMaurice Lam} 156