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 17 package com.android.setupwizardlib.template; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotEquals; 22 import static org.junit.Assert.assertNotNull; 23 import static org.junit.Assert.assertNull; 24 import static org.junit.Assert.assertTrue; 25 26 import android.content.Context; 27 import android.content.res.ColorStateList; 28 import android.graphics.Canvas; 29 import android.graphics.Color; 30 import android.os.Build.VERSION; 31 import android.os.Build.VERSION_CODES; 32 import android.support.test.InstrumentationRegistry; 33 import android.support.test.filters.SmallTest; 34 import android.support.test.runner.AndroidJUnit4; 35 import android.view.ContextThemeWrapper; 36 import android.view.View; 37 import android.widget.ProgressBar; 38 39 import com.android.setupwizardlib.TemplateLayout; 40 import com.android.setupwizardlib.test.R; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 46 @RunWith(AndroidJUnit4.class) 47 @SmallTest 48 public class ProgressBarMixinTest { 49 50 private TemplateLayout mTemplateLayout; 51 52 @Before 53 public void setUp() { 54 Context context = new ContextThemeWrapper(InstrumentationRegistry.getContext(), 55 R.style.SuwThemeMaterial_Light); 56 mTemplateLayout = new TemplateLayout( 57 context, 58 R.layout.test_progress_bar_template, R.id.suw_layout_content); 59 } 60 61 @Test 62 public void testSetShown() { 63 ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); 64 mixin.setShown(true); 65 66 ProgressBar progressBar = (ProgressBar) mTemplateLayout.findViewById( 67 R.id.suw_layout_progress); 68 assertNotNull("Progress bar should be available after setting to shown", progressBar); 69 assertEquals(View.VISIBLE, progressBar.getVisibility()); 70 } 71 72 @Test 73 public void testNotShown() { 74 ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); 75 mixin.setShown(true); 76 mixin.setShown(false); 77 78 ProgressBar progressBar = (ProgressBar) mTemplateLayout.findViewById( 79 R.id.suw_layout_progress); 80 assertNotEquals(View.VISIBLE, progressBar.getVisibility()); 81 } 82 83 @Test 84 public void testIsShown() { 85 ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); 86 87 mixin.setShown(true); 88 assertTrue(mixin.isShown()); 89 90 mixin.setShown(false); 91 assertFalse(mixin.isShown()); 92 } 93 94 @Test 95 public void testPeekProgressBar() { 96 ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); 97 assertNull("PeekProgressBar should return null when stub not inflated yet", 98 mixin.peekProgressBar()); 99 100 mixin.setShown(true); 101 assertNotNull("PeekProgressBar should be available after setting to shown", 102 mixin.peekProgressBar()); 103 } 104 105 @Test 106 public void testSetColorBeforeSetShown() { 107 ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); 108 mixin.setColor(ColorStateList.valueOf(Color.MAGENTA)); 109 110 mixin.setShown(true); 111 112 if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { 113 ProgressBar progressBar = (ProgressBar) mTemplateLayout.findViewById( 114 R.id.suw_layout_progress); 115 assertEquals(ColorStateList.valueOf(Color.MAGENTA), 116 progressBar.getIndeterminateTintList()); 117 assertEquals(ColorStateList.valueOf(Color.MAGENTA), 118 progressBar.getProgressBackgroundTintList()); 119 } 120 // this method is a no-op on versions < lollipop. Just check that it doesn't crash. 121 } 122 123 @Test 124 public void testSetColorAfterSetShown() { 125 ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); 126 mixin.setShown(true); 127 128 mixin.setColor(ColorStateList.valueOf(Color.YELLOW)); 129 130 if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { 131 ProgressBar progressBar = (ProgressBar) mTemplateLayout.findViewById( 132 R.id.suw_layout_progress); 133 assertEquals(ColorStateList.valueOf(Color.YELLOW), 134 progressBar.getIndeterminateTintList()); 135 assertEquals(ColorStateList.valueOf(Color.YELLOW), 136 progressBar.getProgressBackgroundTintList()); 137 } 138 // this method is a no-op on versions < lollipop. Just check that it doesn't crash. 139 } 140 141 @Test 142 public void testDeterminateProgressBarNullTint() { 143 ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); 144 mixin.setShown(true); 145 mixin.peekProgressBar().setIndeterminate(false); 146 147 mixin.setColor(null); 148 149 if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { 150 ProgressBar progressBar = (ProgressBar) mTemplateLayout.findViewById( 151 R.id.suw_layout_progress); 152 assertEquals(null, progressBar.getProgressBackgroundTintList()); 153 progressBar.draw(new Canvas()); 154 } 155 // setColor is a no-op on versions < lollipop. Just check that it doesn't crash. 156 } 157 } 158