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