1/* 2 * Copyright (C) 2015 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 android.support.v7.widget; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertTrue; 21 22import android.app.Activity; 23import android.app.Instrumentation; 24import android.content.Context; 25import android.support.test.InstrumentationRegistry; 26import android.support.test.filters.SmallTest; 27import android.support.test.rule.ActivityTestRule; 28import android.support.test.runner.AndroidJUnit4; 29import android.support.v7.gridlayout.test.R; 30import android.util.AttributeSet; 31import android.view.Gravity; 32import android.view.View; 33import android.view.ViewGroup; 34 35import org.junit.Rule; 36import org.junit.Test; 37import org.junit.runner.RunWith; 38 39@RunWith(AndroidJUnit4.class) 40@SmallTest 41public class GridLayoutTest { 42 @Rule public final ActivityTestRule<GridLayoutTestActivity> mActivityTestRule; 43 44 View mLeftView; 45 View mRightView; 46 View mGridView; 47 48 public GridLayoutTest() { 49 mActivityTestRule = new ActivityTestRule<>(GridLayoutTestActivity.class); 50 } 51 52 private void setContentView(final int layoutId) throws Throwable { 53 final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); 54 mActivityTestRule.runOnUiThread(new Runnable() { 55 @Override 56 public void run() { 57 final Activity activity = mActivityTestRule.getActivity(); 58 activity.setContentView(layoutId); 59 // Now that we've set the content view, find the views we'll be testing 60 mLeftView = activity.findViewById(R.id.leftView); 61 mRightView = activity.findViewById(R.id.rightView); 62 mGridView = activity.findViewById(R.id.gridView); 63 } 64 }); 65 instrumentation.waitForIdleSync(); 66 } 67 68 @Test 69 public void testUseDefaultMargin() throws Throwable { 70 setContentView(R.layout.use_default_margin_test); 71 int left = mLeftView.getWidth(); 72 int right = mRightView.getWidth(); 73 int total = mGridView.getWidth(); 74 assertTrue("left item should get some width", left > 0); 75 assertTrue("right item should get some width", right > 0); 76 assertTrue("test sanity", total > 0); 77 assertTrue("left view should be almost two times right view " + left + " vs " + right, 78 Math.abs(right * 2 - left) < 2); 79 } 80 81 @Test 82 public void testImplicitFillHorizontal() throws Throwable { 83 setContentView(R.layout.fill_horizontal_test); 84 int left = mLeftView.getWidth(); 85 int right = mRightView.getWidth(); 86 int total = mGridView.getWidth(); 87 assertTrue("left item should get some width", left > 0); 88 assertTrue("right item should get some width", right > 0); 89 assertTrue("test sanity", total > 0); 90 assertTrue("left view should be almost two times right view " + left + " vs " + right, 91 Math.abs(right * 2 - left) < 2); 92 } 93 94 @Test 95 public void testMakeViewGone() throws Throwable { 96 setContentView(R.layout.make_view_gone_test); 97 int left = mLeftView.getWidth(); 98 int right = mRightView.getWidth(); 99 int total = mGridView.getWidth(); 100 assertTrue("left item should get some width", left > 0); 101 assertTrue("right item should get some width", right > 0); 102 assertTrue("test sanity", total > 0); 103 // set second view to gone 104 final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); 105 mActivityTestRule.runOnUiThread(new Runnable() { 106 @Override 107 public void run() { 108 final View rightView = mActivityTestRule.getActivity().findViewById(R.id.rightView); 109 GridLayout.LayoutParams lp = (GridLayout.LayoutParams) rightView.getLayoutParams(); 110 lp.setGravity(Gravity.NO_GRAVITY); 111 rightView.setVisibility(View.GONE); 112 } 113 }); 114 instrumentation.waitForIdleSync(); 115 left = mActivityTestRule.getActivity().findViewById(R.id.leftView).getWidth(); 116 assertEquals(total, left); 117 } 118 119 @Test 120 public void testWrapContentInOtherDirection() throws Throwable { 121 setContentView(R.layout.height_wrap_content_test); 122 int left = mLeftView.getHeight(); 123 int right = mRightView.getHeight(); 124 int total = mGridView.getHeight(); 125 assertTrue("test sanity", left > 0); 126 assertTrue("test sanity", right > 0); 127 assertTrue("test sanity", total > 0); 128 assertTrue("right should be taller than left", right >= left); 129 assertTrue("total height should be smaller than what it could be", 130 total < ((ViewGroup) mGridView.getParent()).getHeight()); 131 } 132 133 @Test 134 public void testGenerateLayoutParamsFromMarginParams() { 135 MyGridLayout gridLayout = new MyGridLayout(mActivityTestRule.getActivity()); 136 ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(3, 5); 137 lp.leftMargin = 1; 138 lp.topMargin = 2; 139 lp.rightMargin = 3; 140 lp.bottomMargin = 4; 141 GridLayout.LayoutParams generated = gridLayout.generateLayoutParams(lp); 142 assertEquals(3, generated.width); 143 assertEquals(5, generated.height); 144 145 assertEquals(1, generated.leftMargin); 146 assertEquals(2, generated.topMargin); 147 assertEquals(3, generated.rightMargin); 148 assertEquals(4, generated.bottomMargin); 149 } 150 151 private static class MyGridLayout extends GridLayout { 152 153 public MyGridLayout(Context context) { 154 super(context); 155 } 156 157 public MyGridLayout(Context context, AttributeSet attrs) { 158 super(context, attrs); 159 } 160 161 public MyGridLayout(Context context, AttributeSet attrs, int defStyleAttr) { 162 super(context, attrs, defStyleAttr); 163 } 164 165 @Override 166 protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { 167 return super.generateLayoutParams(p); 168 } 169 } 170} 171