GlifLayoutTest.java revision 48c121912f865be865e564a4d0a899988a100d37
1/* 2 * Copyright (C) 2017 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; 18 19import static org.hamcrest.Matchers.instanceOf; 20import static org.junit.Assert.assertEquals; 21import static org.junit.Assert.assertFalse; 22import static org.junit.Assert.assertNotNull; 23import static org.junit.Assert.assertNull; 24import static org.junit.Assert.assertSame; 25import static org.junit.Assert.assertThat; 26import static org.junit.Assert.assertTrue; 27import static org.robolectric.RuntimeEnvironment.application; 28 29import android.content.Context; 30import android.content.res.ColorStateList; 31import android.graphics.Color; 32import android.graphics.drawable.ColorDrawable; 33import android.graphics.drawable.Drawable; 34import android.os.Build; 35import android.support.annotation.IdRes; 36import android.view.ContextThemeWrapper; 37import android.view.View; 38import android.widget.ProgressBar; 39import android.widget.ScrollView; 40import android.widget.TextView; 41 42import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; 43import com.android.setupwizardlib.template.ColoredHeaderMixin; 44import com.android.setupwizardlib.template.HeaderMixin; 45import com.android.setupwizardlib.template.IconMixin; 46import com.android.setupwizardlib.template.ProgressBarMixin; 47import com.android.setupwizardlib.view.StatusBarBackgroundLayout; 48 49import org.junit.Before; 50import org.junit.Test; 51import org.junit.runner.RunWith; 52import org.robolectric.Robolectric; 53import org.robolectric.annotation.Config; 54 55@RunWith(SuwLibRobolectricTestRunner.class) 56@Config(constants = BuildConfig.class, sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK }) 57public class GlifLayoutTest { 58 59 private Context mContext; 60 61 @Before 62 public void setUp() throws Exception { 63 mContext = new ContextThemeWrapper(application, R.style.SuwThemeGlif_Light); 64 } 65 66 @Test 67 public void testDefaultTemplate() { 68 GlifLayout layout = new GlifLayout(mContext); 69 assertDefaultTemplateInflated(layout); 70 } 71 72 @Test 73 public void testSetHeaderText() { 74 GlifLayout layout = new GlifLayout(mContext); 75 TextView title = (TextView) layout.findViewById(R.id.suw_layout_title); 76 layout.setHeaderText("Abracadabra"); 77 assertEquals("Header text should be \"Abracadabra\"", "Abracadabra", title.getText()); 78 } 79 80 @Test 81 public void testAddView() { 82 @IdRes int testViewId = 123456; 83 GlifLayout layout = new GlifLayout(mContext); 84 TextView tv = new TextView(mContext); 85 tv.setId(testViewId); 86 layout.addView(tv); 87 assertDefaultTemplateInflated(layout); 88 View view = layout.findViewById(testViewId); 89 assertSame("The view added should be the same text view", tv, view); 90 } 91 92 @Test 93 public void testGetScrollView() { 94 GlifLayout layout = new GlifLayout(mContext); 95 assertNotNull("Get scroll view should not be null with default template", 96 layout.getScrollView()); 97 } 98 99 @Test 100 public void testSetPrimaryColor() { 101 GlifLayout layout = new GlifLayout(mContext); 102 layout.setProgressBarShown(true); 103 layout.setPrimaryColor(ColorStateList.valueOf(Color.RED)); 104 assertEquals("Primary color should be red", 105 ColorStateList.valueOf(Color.RED), layout.getPrimaryColor()); 106 107 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 108 ProgressBar progressBar = (ProgressBar) layout.findViewById(R.id.suw_layout_progress); 109 assertEquals("Progress bar should be tinted red", 110 ColorStateList.valueOf(Color.RED), progressBar.getIndeterminateTintList()); 111 assertEquals("Determinate progress bar should also be tinted red", 112 ColorStateList.valueOf(Color.RED), progressBar.getProgressBackgroundTintList()); 113 } 114 } 115 116 @Config(qualifiers = "sw600dp") 117 @Test 118 public void testSetPrimaryColorTablet() { 119 GlifLayout layout = new GlifLayout(mContext); 120 layout.setProgressBarShown(true); 121 layout.setPrimaryColor(ColorStateList.valueOf(Color.RED)); 122 assertEquals("Primary color should be red", 123 ColorStateList.valueOf(Color.RED), layout.getPrimaryColor()); 124 125 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 126 ProgressBar progressBar = (ProgressBar) layout.findViewById(R.id.suw_layout_progress); 127 assertEquals("Progress bar should be tinted red", 128 ColorStateList.valueOf(Color.RED), progressBar.getIndeterminateTintList()); 129 assertEquals("Determinate progress bar should also be tinted red", 130 ColorStateList.valueOf(Color.RED), progressBar.getProgressBackgroundTintList()); 131 } 132 133 assertEquals(Color.RED, ((GlifPatternDrawable) getTabletBackground(layout)).getColor()); 134 } 135 136 @Test 137 public void testSetBackgroundBaseColor() { 138 GlifLayout layout = new GlifLayout(mContext); 139 layout.setPrimaryColor(ColorStateList.valueOf(Color.BLUE)); 140 layout.setBackgroundBaseColor(ColorStateList.valueOf(Color.RED)); 141 142 assertEquals(Color.RED, ((GlifPatternDrawable) getPhoneBackground(layout)).getColor()); 143 assertEquals(Color.RED, layout.getBackgroundBaseColor().getDefaultColor()); 144 } 145 146 @Config(qualifiers = "sw600dp") 147 @Test 148 public void testSetBackgroundBaseColorTablet() { 149 GlifLayout layout = new GlifLayout(mContext); 150 layout.setPrimaryColor(ColorStateList.valueOf(Color.BLUE)); 151 layout.setBackgroundBaseColor(ColorStateList.valueOf(Color.RED)); 152 153 assertEquals(Color.RED, ((GlifPatternDrawable) getTabletBackground(layout)).getColor()); 154 assertEquals(Color.RED, layout.getBackgroundBaseColor().getDefaultColor()); 155 } 156 157 @Test 158 public void testSetBackgroundPatternedTrue() { 159 GlifLayout layout = new GlifLayout(mContext); 160 layout.setBackgroundPatterned(true); 161 162 assertThat(getPhoneBackground(layout), instanceOf(GlifPatternDrawable.class)); 163 assertTrue("Background should be patterned", layout.isBackgroundPatterned()); 164 } 165 166 @Test 167 public void testSetBackgroundPatternedFalse() { 168 GlifLayout layout = new GlifLayout(mContext); 169 layout.setBackgroundPatterned(false); 170 171 assertThat(getPhoneBackground(layout), instanceOf(ColorDrawable.class)); 172 assertFalse("Background should not be patterned", layout.isBackgroundPatterned()); 173 } 174 175 @Config(qualifiers = "sw600dp") 176 @Test 177 public void testSetBackgroundPatternedTrueTablet() { 178 GlifLayout layout = new GlifLayout(mContext); 179 layout.setBackgroundPatterned(true); 180 181 assertThat(getTabletBackground(layout), instanceOf(GlifPatternDrawable.class)); 182 assertTrue("Background should be patterned", layout.isBackgroundPatterned()); 183 } 184 185 @Config(qualifiers = "sw600dp") 186 @Test 187 public void testSetBackgroundPatternedFalseTablet() { 188 GlifLayout layout = new GlifLayout(mContext); 189 layout.setBackgroundPatterned(false); 190 191 assertThat(getTabletBackground(layout), instanceOf(ColorDrawable.class)); 192 assertFalse("Background should not be patterned", layout.isBackgroundPatterned()); 193 } 194 195 @Test 196 public void testNonGlifTheme() { 197 mContext = new ContextThemeWrapper(application, android.R.style.Theme); 198 new GlifLayout(mContext); 199 // Inflating with a non-GLIF theme should not crash 200 } 201 202 @Test 203 public void testPeekProgressBarNull() { 204 GlifLayout layout = new GlifLayout(mContext); 205 assertNull("PeekProgressBar should return null initially", layout.peekProgressBar()); 206 } 207 208 @Test 209 public void testPeekProgressBar() { 210 GlifLayout layout = new GlifLayout(mContext); 211 layout.setProgressBarShown(true); 212 assertNotNull("Peek progress bar should return the bar after setProgressBarShown(true)", 213 layout.peekProgressBar()); 214 } 215 216 @Test 217 public void testMixins() { 218 GlifLayout layout = new GlifLayout(mContext); 219 final HeaderMixin header = layout.getMixin(HeaderMixin.class); 220 assertTrue("Header should be instance of ColoredHeaderMixin. " 221 + "Found " + header.getClass() + " instead.", header instanceof ColoredHeaderMixin); 222 223 assertNotNull("GlifLayout should have icon mixin", layout.getMixin(IconMixin.class)); 224 assertNotNull("GlifLayout should have progress bar mixin", 225 layout.getMixin(ProgressBarMixin.class)); 226 } 227 228 @Test 229 public void testInflateFooter() { 230 GlifLayout layout = new GlifLayout(mContext); 231 232 final View view = layout.inflateFooter(android.R.layout.simple_list_item_1); 233 assertEquals(android.R.id.text1, view.getId()); 234 assertNotNull(layout.findViewById(android.R.id.text1)); 235 } 236 237 @Config(qualifiers = "sw600dp") 238 @Test 239 public void testInflateFooterTablet() { 240 testInflateFooter(); 241 } 242 243 @Test 244 public void testFooterXml() { 245 GlifLayout layout = new GlifLayout( 246 mContext, 247 Robolectric.buildAttributeSet() 248 .addAttribute(R.attr.suwFooter, "@android:layout/simple_list_item_1") 249 .build()); 250 251 assertNotNull(layout.findViewById(android.R.id.text1)); 252 } 253 254 private Drawable getPhoneBackground(GlifLayout layout) { 255 final StatusBarBackgroundLayout patternBg = 256 (StatusBarBackgroundLayout) layout.findManagedViewById(R.id.suw_pattern_bg); 257 return patternBg.getStatusBarBackground(); 258 } 259 260 private Drawable getTabletBackground(GlifLayout layout) { 261 final View patternBg = layout.findManagedViewById(R.id.suw_pattern_bg); 262 return patternBg.getBackground(); 263 } 264 265 private void assertDefaultTemplateInflated(GlifLayout layout) { 266 View title = layout.findViewById(R.id.suw_layout_title); 267 assertNotNull("@id/suw_layout_title should not be null", title); 268 269 View icon = layout.findViewById(R.id.suw_layout_icon); 270 assertNotNull("@id/suw_layout_icon should not be null", icon); 271 272 View scrollView = layout.findViewById(R.id.suw_scroll_view); 273 assertTrue("@id/suw_scroll_view should be a ScrollView", scrollView instanceof ScrollView); 274 } 275} 276