1 /*
2  * Copyright (C) 2020 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.inline;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.Manifest;
22 import android.app.Instrumentation;
23 import android.content.Context;
24 import android.util.Log;
25 import android.view.SurfaceControl;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.cts.R;
29 import android.widget.inline.InlineContentView;
30 
31 import androidx.test.InstrumentationRegistry;
32 import androidx.test.filters.SmallTest;
33 import androidx.test.rule.ActivityTestRule;
34 import androidx.test.runner.AndroidJUnit4;
35 
36 import com.android.compatibility.common.util.AdoptShellPermissionsRule;
37 import com.android.compatibility.common.util.WindowUtil;
38 
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 
44 import java.util.concurrent.CountDownLatch;
45 
46 @SmallTest
47 @RunWith(AndroidJUnit4.class)
48 public class InlineContentViewTest {
49     private static final String LOG_TAG = "InlineContentViewTest";
50 
51     private Context mContext;
52     private InlineContentView mInlineContentView;
53     private InlineContentViewCtsActivity mActivity;
54     private Instrumentation mInstrumentation;
55 
56     @Rule(order = 0)
57     public AdoptShellPermissionsRule mAdoptShellPermissionsRule = new AdoptShellPermissionsRule(
58             androidx.test.platform.app.InstrumentationRegistry
59                     .getInstrumentation().getUiAutomation(),
60             Manifest.permission.START_ACTIVITIES_FROM_SDK_SANDBOX);
61 
62     @Rule(order = 1)
63     public ActivityTestRule<InlineContentViewCtsActivity> mActivityRule =
64             new ActivityTestRule<>(InlineContentViewCtsActivity.class);
65 
66     @Before
setup()67     public void setup() {
68         mInstrumentation = InstrumentationRegistry.getInstrumentation();
69         mContext = mInstrumentation.getTargetContext();
70         mActivity = mActivityRule.getActivity();
71         WindowUtil.waitForFocus(mActivity);
72         mInlineContentView = new InlineContentView(mContext);
73     }
74 
75     @Test
testConstructor()76     public void testConstructor() {
77         new InlineContentView(mContext);
78 
79         new InlineContentView(mContext, /* attrs */ null);
80 
81         new InlineContentView(mContext, /* attrs */ null, /* defStyleAttr */ 0);
82 
83         new InlineContentView(mContext, /* attrs */ null, /* defStyleAttr */ 0,
84                 /* defStyleRes */ 0);
85     }
86 
87     @Test(expected = NullPointerException.class)
testConstructorNullContext()88     public void testConstructorNullContext() {
89         new InlineContentView(/* context */ null);
90     }
91 
92     @Test
testGetSurfaceControl()93     public void testGetSurfaceControl() {
94         attachInlineContentView();
95 
96         final ViewGroup parent = (ViewGroup) mActivity.findViewById(R.id.inlinecontentview);
97 
98         assertThat(parent.getChildCount()).isEqualTo(1);
99 
100         final View view = parent.getChildAt(0);
101 
102         assertThat(view instanceof InlineContentView).isTrue();
103 
104         final SurfaceControl surfaceControl = ((InlineContentView) view).getSurfaceControl();
105 
106         assertThat(surfaceControl).isNotNull();
107     }
108 
109     @Test
testSetSurfaceControlCallback()110     public void testSetSurfaceControlCallback() throws Exception {
111         final CountDownLatch latch = new CountDownLatch(1);
112         mInlineContentView.setSurfaceControlCallback(
113                 new InlineContentView.SurfaceControlCallback() {
114                     @Override
115                     public void onCreated(SurfaceControl surfaceControl) {
116                         assertThat(surfaceControl).isNotNull();
117                         latch.countDown();
118                     }
119 
120                     @Override
121                     public void onDestroyed(SurfaceControl surfaceControl) {
122                         /* do nothing */
123                     }
124                 });
125 
126         attachInlineContentView();
127 
128         latch.await();
129     }
130 
131     @Test
testSetZOrderedOnTop()132     public void testSetZOrderedOnTop() {
133         mInlineContentView.setZOrderedOnTop(false);
134 
135         assertThat(mInlineContentView.isZOrderedOnTop()).isFalse();
136 
137         mInlineContentView.setZOrderedOnTop(true);
138 
139         assertThat(mInlineContentView.isZOrderedOnTop()).isTrue();
140     }
141 
attachInlineContentView()142     private void attachInlineContentView() {
143         final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.inlinecontentview);
144         try {
145             mActivityRule.runOnUiThread(
146                     () -> viewGroup.addView(mInlineContentView, new ViewGroup.LayoutParams(
147                             ViewGroup.LayoutParams.MATCH_PARENT,
148                             ViewGroup.LayoutParams.WRAP_CONTENT)));
149             mInstrumentation.waitForIdleSync();
150         } catch (Throwable e) {
151             Log.e(LOG_TAG, "attachInlineContentView fail");
152         }
153     }
154 }
155