xref: /aosp_15_r20/cts/tests/tests/widget/src/android/widget/cts/TableRowTest.java (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
2  * Copyright (C) 2008 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 android.widget.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.verifyNoMoreInteractions;
28 
29 import android.Manifest;
30 import android.app.Activity;
31 import android.content.Context;
32 import android.content.res.Resources;
33 import android.content.res.XmlResourceParser;
34 import android.util.AttributeSet;
35 import android.util.Xml;
36 import android.view.View;
37 import android.view.View.MeasureSpec;
38 import android.view.ViewGroup;
39 import android.widget.LinearLayout;
40 import android.widget.RelativeLayout;
41 import android.widget.TableLayout;
42 import android.widget.TableRow;
43 import android.widget.TextView;
44 
45 import androidx.test.annotation.UiThreadTest;
46 import androidx.test.filters.MediumTest;
47 import androidx.test.rule.ActivityTestRule;
48 import androidx.test.runner.AndroidJUnit4;
49 
50 import com.android.compatibility.common.util.AdoptShellPermissionsRule;
51 
52 import org.junit.Before;
53 import org.junit.Rule;
54 import org.junit.Test;
55 import org.junit.runner.RunWith;
56 
57 /**
58  * Test {@link TableRow}.
59  */
60 @MediumTest
61 @RunWith(AndroidJUnit4.class)
62 public class TableRowTest {
63     private Activity mActivity;
64 
65     @Rule(order = 0)
66     public AdoptShellPermissionsRule mAdoptShellPermissionsRule = new AdoptShellPermissionsRule(
67             androidx.test.platform.app.InstrumentationRegistry
68                     .getInstrumentation().getUiAutomation(),
69             Manifest.permission.START_ACTIVITIES_FROM_SDK_SANDBOX);
70 
71     @Rule(order = 1)
72     public ActivityTestRule<TableCtsActivity> mActivityRule =
73             new ActivityTestRule<>(TableCtsActivity.class);
74 
75     @Before
setup()76     public void setup() {
77         mActivity = mActivityRule.getActivity();
78     }
79 
80     @Test
testConstructor()81     public void testConstructor() {
82         new TableRow(mActivity);
83 
84         new TableRow(mActivity, null);
85     }
86 
87     @UiThreadTest
88     @Test
testSetOnHierarchyChangeListener()89     public void testSetOnHierarchyChangeListener() {
90         TableRow tableRow = new TableRow(mActivity);
91 
92         ViewGroup.OnHierarchyChangeListener mockHierarchyChangeListener =
93                 mock(ViewGroup.OnHierarchyChangeListener.class);
94         tableRow.setOnHierarchyChangeListener(mockHierarchyChangeListener);
95 
96         View toAdd = new TextView(mActivity);
97         tableRow.addView(toAdd);
98         verify(mockHierarchyChangeListener, times(1)).onChildViewAdded(tableRow, toAdd);
99         tableRow.removeViewAt(0);
100         verify(mockHierarchyChangeListener, times(1)).onChildViewRemoved(tableRow, toAdd);
101         verifyNoMoreInteractions(mockHierarchyChangeListener);
102 
103         tableRow.setOnHierarchyChangeListener(null);
104         tableRow.addView(new TextView(mActivity));
105         tableRow.removeViewAt(0);
106         verifyNoMoreInteractions(mockHierarchyChangeListener);
107     }
108 
109     @UiThreadTest
110     @Test
testGetVirtualChildAt()111     public void testGetVirtualChildAt() {
112         mActivity.setContentView(android.widget.cts.R.layout.table_layout_1);
113         TableLayout tableLayout = (TableLayout) mActivity
114                 .findViewById(android.widget.cts.R.id.table1);
115 
116         TableRow tableRow = (TableRow) tableLayout.getChildAt(0);
117         Resources resources = mActivity.getResources();
118         assertEquals(resources.getString(R.string.table_layout_first),
119                 ((TextView) tableRow.getVirtualChildAt(0)).getText().toString());
120         assertEquals(resources.getString(R.string.table_layout_second),
121                 ((TextView) tableRow.getVirtualChildAt(1)).getText().toString());
122         assertEquals(resources.getString(R.string.table_layout_third),
123                 ((TextView) tableRow.getVirtualChildAt(2)).getText().toString());
124 
125         mActivity.setContentView(android.widget.cts.R.layout.table_layout_2);
126         tableLayout = (TableLayout) mActivity.findViewById(android.widget.cts.R.id.table2);
127 
128         tableRow = (TableRow) tableLayout.getChildAt(0);
129         assertNull(tableRow.getVirtualChildAt(0));
130         assertEquals(resources.getString(R.string.table_layout_long),
131                 ((TextView) tableRow.getVirtualChildAt(1)).getText().toString());
132         assertEquals(resources.getString(R.string.table_layout_second),
133                 ((TextView) tableRow.getVirtualChildAt(2)).getText().toString());
134         assertEquals(resources.getString(R.string.table_layout_second),
135                 ((TextView) tableRow.getVirtualChildAt(3)).getText().toString());
136         assertEquals(resources.getString(R.string.table_layout_third),
137                 ((TextView) tableRow.getVirtualChildAt(4)).getText().toString());
138     }
139 
140     @UiThreadTest
141     @Test
testGetVirtualChildCount()142     public void testGetVirtualChildCount() {
143         mActivity.setContentView(android.widget.cts.R.layout.table_layout_1);
144         TableLayout tableLayout = (TableLayout) mActivity
145                 .findViewById(android.widget.cts.R.id.table1);
146 
147         TableRow tableRow = (TableRow) tableLayout.getChildAt(0);
148         assertEquals(3, tableRow.getVirtualChildCount());
149 
150         mActivity.setContentView(android.widget.cts.R.layout.table_layout_2);
151         tableLayout = (TableLayout) mActivity.findViewById(android.widget.cts.R.id.table2);
152 
153         tableRow = (TableRow) tableLayout.getChildAt(0);
154         assertEquals(5, tableRow.getVirtualChildCount());
155     }
156 
157     @Test
testGenerateLayoutParamsFromAttributeSet()158     public void testGenerateLayoutParamsFromAttributeSet() {
159         TableRow tableRow = new TableRow(mActivity);
160 
161         Resources resources = mActivity.getResources();
162         XmlResourceParser parser = resources.getLayout(R.layout.table_layout_1);
163         AttributeSet attr = Xml.asAttributeSet(parser);
164 
165         assertNotNull(tableRow.generateLayoutParams(attr));
166 
167         assertNotNull(tableRow.generateLayoutParams((AttributeSet) null));
168     }
169 
170     @Test
testCheckLayoutParams()171     public void testCheckLayoutParams() {
172         MockTableRow mockTableRow = new MockTableRow(mActivity);
173 
174         assertTrue(mockTableRow.checkLayoutParams(new TableRow.LayoutParams(200, 300)));
175 
176         assertFalse(mockTableRow.checkLayoutParams(new ViewGroup.LayoutParams(200, 300)));
177 
178         assertFalse(mockTableRow.checkLayoutParams(new RelativeLayout.LayoutParams(200, 300)));
179 
180         assertFalse(mockTableRow.checkLayoutParams(null));
181     }
182 
183     @Test
testGenerateDefaultLayoutParams()184     public void testGenerateDefaultLayoutParams() {
185         MockTableRow mockTableRow = new MockTableRow(mActivity);
186 
187         LinearLayout.LayoutParams layoutParams = mockTableRow.generateDefaultLayoutParams();
188         assertNotNull(layoutParams);
189         assertTrue(layoutParams instanceof TableRow.LayoutParams);
190     }
191 
192     @Test
testGenerateLayoutParamsFromLayoutParams()193     public void testGenerateLayoutParamsFromLayoutParams() {
194         MockTableRow mockTableRow = new MockTableRow(mActivity);
195 
196         LinearLayout.LayoutParams layoutParams = mockTableRow.generateLayoutParams(
197                 new ViewGroup.LayoutParams(200, 300));
198         assertNotNull(layoutParams);
199         assertEquals(200, layoutParams.width);
200         assertEquals(300, layoutParams.height);
201         assertTrue(layoutParams instanceof TableRow.LayoutParams);
202     }
203 
204     @Test(expected=NullPointerException.class)
testGenerateLayoutParamsFromLayoutParamsNull()205     public void testGenerateLayoutParamsFromLayoutParamsNull() {
206         MockTableRow mockTableRow = new MockTableRow(mActivity);
207 
208         mockTableRow.generateLayoutParams((ViewGroup.LayoutParams) null);
209     }
210 
211     @Test
testOnLayout()212     public void testOnLayout() {
213         MockTableRow mockTableRow = new MockTableRow(mActivity);
214 
215         mockTableRow.onLayout(false, 0, 0, 200, 300);
216     }
217 
218     @Test
testOnMeasure()219     public void testOnMeasure() {
220         MockTableRow mockTableRow = new MockTableRow(mActivity);
221 
222         mockTableRow.onMeasure(MeasureSpec.EXACTLY, MeasureSpec.EXACTLY);
223     }
224 
225     /*
226      * Mock class for TableRow to test protected methods
227      */
228     private class MockTableRow extends TableRow {
MockTableRow(Context context)229         public MockTableRow(Context context) {
230             super(context);
231         }
232 
233         @Override
checkLayoutParams(ViewGroup.LayoutParams p)234         protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
235             return super.checkLayoutParams(p);
236         }
237 
238         @Override
generateDefaultLayoutParams()239         protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
240             return super.generateDefaultLayoutParams();
241         }
242 
243         @Override
generateLayoutParams( ViewGroup.LayoutParams p)244         protected LinearLayout.LayoutParams generateLayoutParams(
245                 ViewGroup.LayoutParams p) {
246             return super.generateLayoutParams(p);
247         }
248 
249         @Override
onLayout(boolean changed, int l, int t, int r, int b)250         protected void onLayout(boolean changed, int l, int t, int r, int b) {
251             super.onLayout(changed, l, t, r, b);
252         }
253 
254         @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)255         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
256             super.onMeasure(widthMeasureSpec, heightMeasureSpec);
257         }
258     }
259 }
260