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.template; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertNotNull; 21import static org.junit.Assert.assertNull; 22import static org.mockito.Matchers.eq; 23import static org.mockito.Mockito.doReturn; 24import static org.mockito.Mockito.spy; 25 26import android.content.Context; 27import android.support.annotation.IdRes; 28import android.support.test.InstrumentationRegistry; 29import android.support.test.filters.SmallTest; 30import android.support.test.runner.AndroidJUnit4; 31import android.util.DisplayMetrics; 32import android.util.TypedValue; 33import android.view.View; 34import android.view.ViewStub; 35import android.widget.Button; 36import android.widget.FrameLayout; 37import android.widget.LinearLayout; 38 39import com.android.setupwizardlib.TemplateLayout; 40import com.android.setupwizardlib.test.R; 41 42import org.junit.Before; 43import org.junit.Test; 44import org.junit.runner.RunWith; 45 46@RunWith(AndroidJUnit4.class) 47@SmallTest 48public class ButtonFooterMixinTest { 49 50 private Context mContext; 51 private TemplateLayout mTemplateLayout; 52 53 // The parent view to contain the view stub and views it inflates. 54 private FrameLayout mStubParent; 55 private ViewStub mFooterStub; 56 57 @Before 58 public void setUp() { 59 mContext = InstrumentationRegistry.getTargetContext(); 60 mTemplateLayout = spy(new TemplateLayout(mContext, R.layout.test_template, 61 R.id.suw_layout_content)); 62 63 mFooterStub = new ViewStub(mContext, R.layout.suw_glif_footer_button_bar); 64 mStubParent = new FrameLayout(mContext); 65 mStubParent.addView(mFooterStub); 66 doReturn(mFooterStub).when(mTemplateLayout).findManagedViewById(eq(R.id.suw_layout_footer)); 67 } 68 69 @Test 70 public void testAddButton() { 71 ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); 72 final Button button = mixin.addButton("foobar", R.style.SuwGlifButton_Primary); 73 74 assertNotNull(button); 75 @IdRes final int id = 12345; 76 button.setId(id); 77 assertNotNull(mStubParent.findViewById(id)); 78 79 assertEquals("foobar", button.getText()); 80 81 // Make sure the style is applied by checking the paddings 82 assertEquals(dp2Px(16), button.getPaddingLeft()); 83 assertEquals(dp2Px(16), button.getPaddingRight()); 84 } 85 86 @Test 87 public void testAddButtonTextRes() { 88 ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); 89 final Button button = mixin.addButton(R.string.suw_next_button_label, 90 R.style.SuwGlifButton_Primary); 91 92 assertNotNull(button); 93 button.setTag("button"); 94 assertNotNull(mStubParent.findViewWithTag("button")); 95 96 assertEquals("Next", button.getText()); 97 98 // Make sure the style is applied by checking the paddings 99 assertEquals(dp2Px(16), button.getPaddingLeft()); 100 assertEquals(dp2Px(16), button.getPaddingRight()); 101 } 102 103 @Test 104 public void testAddSpace() { 105 ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); 106 mixin.addButton("foo", R.style.SuwGlifButton_Secondary); 107 final View space = mixin.addSpace(); 108 mixin.addButton("bar", R.style.SuwGlifButton_Primary); 109 110 space.setTag("space"); 111 assertNotNull(mStubParent.findViewWithTag("space")); 112 assertEquals("Space should have weight of 1", 113 1f, ((LinearLayout.LayoutParams) space.getLayoutParams()).weight, 0.001); 114 } 115 116 @Test 117 public void testRemoveButton() { 118 ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); 119 final Button fooButton = mixin.addButton("foo", R.style.SuwGlifButton_Secondary); 120 final Button barButton = mixin.addButton("bar", R.style.SuwGlifButton_Secondary); 121 122 fooButton.setTag("foo"); 123 barButton.setTag("bar"); 124 assertNotNull("Foo button should exist", mStubParent.findViewWithTag("foo")); 125 assertNotNull("Bar button should exist", mStubParent.findViewWithTag("bar")); 126 127 mixin.removeButton(fooButton); 128 129 assertNull("Foo button should be removed", mStubParent.findViewWithTag("foo")); 130 assertNotNull("Bar button should not be removed", mStubParent.findViewWithTag("bar")); 131 } 132 133 @Test 134 public void testRemoveSpace() { 135 ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); 136 final Button fooButton = mixin.addButton("foo", R.style.SuwGlifButton_Secondary); 137 final View space = mixin.addSpace(); 138 139 fooButton.setTag("foo"); 140 space.setTag("space"); 141 assertNotNull("Foo button should exist", mStubParent.findViewWithTag("foo")); 142 assertNotNull("space should exist", mStubParent.findViewWithTag("space")); 143 144 mixin.removeSpace(space); 145 146 assertNotNull("Foo button should not be removed", mStubParent.findViewWithTag("foo")); 147 assertNull("Space should be removed", mStubParent.findViewWithTag("space")); 148 } 149 150 @Test 151 public void testRemoveAllViews() { 152 ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); 153 final Button fooButton = mixin.addButton("foo", R.style.SuwGlifButton_Secondary); 154 final View space = mixin.addSpace(); 155 156 fooButton.setTag("foo"); 157 space.setTag("space"); 158 assertNotNull("Foo button should exist", mStubParent.findViewWithTag("foo")); 159 assertNotNull("space should exist", mStubParent.findViewWithTag("space")); 160 161 mixin.removeAllViews(); 162 163 assertNull("Foo button should be removed", mStubParent.findViewWithTag("foo")); 164 assertNull("Space should be removed", mStubParent.findViewWithTag("space")); 165 } 166 167 private int dp2Px(float dp) { 168 DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); 169 return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics); 170 } 171} 172