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.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertSame;
23 
24 import android.Manifest;
25 import android.app.Activity;
26 import android.content.Context;
27 import android.content.res.Resources;
28 import android.util.AttributeSet;
29 import android.widget.RelativeLayout.LayoutParams;
30 import android.widget.TextView;
31 import android.widget.TwoLineListItem;
32 
33 import androidx.test.annotation.UiThreadTest;
34 import androidx.test.filters.MediumTest;
35 import androidx.test.rule.ActivityTestRule;
36 import androidx.test.runner.AndroidJUnit4;
37 
38 import com.android.compatibility.common.util.AdoptShellPermissionsRule;
39 
40 import org.junit.Before;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 
45 /**
46  * Test {@link TwoLineListItem}.
47  */
48 @MediumTest
49 @RunWith(AndroidJUnit4.class)
50 public class TwoLineListItemTest {
51     private Activity mActivity;
52 
53     @Rule(order = 0)
54     public AdoptShellPermissionsRule mAdoptShellPermissionsRule = new AdoptShellPermissionsRule(
55             androidx.test.platform.app.InstrumentationRegistry
56                     .getInstrumentation().getUiAutomation(),
57             Manifest.permission.START_ACTIVITIES_FROM_SDK_SANDBOX);
58 
59     @Rule(order = 1)
60     public ActivityTestRule<TwoLineListItemCtsActivity> mActivityRule =
61             new ActivityTestRule<>(TwoLineListItemCtsActivity.class);
62 
63     @Before
setup()64     public void setup() {
65         mActivity = mActivityRule.getActivity();
66     }
67 
68     @Test
testConstructor()69     public void testConstructor() {
70         AttributeSet attrs = mActivity.getResources().getLayout(R.layout.twolinelistitem);
71         assertNotNull(attrs);
72 
73         new TwoLineListItem(mActivity);
74         new TwoLineListItem(mActivity, attrs);
75         new TwoLineListItem(mActivity, null);
76         new TwoLineListItem(mActivity, attrs, 0);
77         new TwoLineListItem(mActivity, null, 0);
78         new TwoLineListItem(mActivity, attrs, Integer.MAX_VALUE);
79         new TwoLineListItem(mActivity, attrs, Integer.MIN_VALUE);
80     }
81 
82     @Test(expected=NullPointerException.class)
testConstructorWithNullContext1()83     public void testConstructorWithNullContext1() {
84         new TwoLineListItem(null);
85     }
86 
87     @Test(expected=NullPointerException.class)
testConstructorWithNullContext2()88     public void testConstructorWithNullContext2() {
89         AttributeSet attrs = mActivity.getResources().getLayout(R.layout.twolinelistitem);
90         new TwoLineListItem(null, attrs);
91     }
92 
93     @Test(expected=NullPointerException.class)
testConstructorWithNullContext3()94     public void testConstructorWithNullContext3() {
95         AttributeSet attrs = mActivity.getResources().getLayout(R.layout.twolinelistitem);
96         new TwoLineListItem(null, attrs, 0);
97     }
98 
99     @Test
testGetTexts()100     public void testGetTexts() {
101         TwoLineListItem twoLineListItem =
102             (TwoLineListItem) mActivity.findViewById(R.id.twoLineListItem);
103 
104         Resources res = mActivity.getResources();
105         assertNotNull(twoLineListItem.getText1());
106         assertEquals(res.getString(R.string.twolinelistitem_test_text1),
107                 twoLineListItem.getText1().getText().toString());
108         assertNotNull(twoLineListItem.getText2());
109         assertEquals(res.getString(R.string.twolinelistitem_test_text2),
110                 twoLineListItem.getText2().getText().toString());
111     }
112 
113     @UiThreadTest
114     @Test
testOnFinishInflate()115     public void testOnFinishInflate() {
116         MockTwoLineListItem twoLineListItem = new MockTwoLineListItem(mActivity);
117         TextView text1 = new TextView(mActivity);
118         text1.setId(android.R.id.text1);
119         TextView text2 = new TextView(mActivity);
120         text2.setId(android.R.id.text2);
121         LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
122                 LayoutParams.WRAP_CONTENT);
123         twoLineListItem.addView(text1, params);
124         twoLineListItem.addView(text2, params);
125 
126         assertNull(twoLineListItem.getText1());
127         assertNull(twoLineListItem.getText2());
128         twoLineListItem.onFinishInflate();
129         assertSame(text1, twoLineListItem.getText1());
130         assertSame(text2, twoLineListItem.getText2());
131     }
132 
133     /**
134      * The Class MockTwoLineListItem is just a wrapper of TwoLineListItem to
135      * make access to protected method possible .
136      */
137     private class MockTwoLineListItem extends TwoLineListItem {
MockTwoLineListItem(Context context)138         public MockTwoLineListItem(Context context) {
139             super(context);
140         }
141 
142         @Override
onFinishInflate()143         protected void onFinishInflate() {
144             super.onFinishInflate();
145         }
146     }
147 }
148