1 /*
2  * Copyright (C) 2022 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.server.wm.scvh;
18 
19 import static android.server.wm.BuildUtils.HW_TIMEOUT_MULTIPLIER;
20 
21 import static org.junit.Assert.assertTrue;
22 
23 import android.app.Service;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.Handler;
27 import android.os.IBinder;
28 import android.os.Looper;
29 import android.server.wm.CtsWindowInfoUtils;
30 import android.util.Log;
31 import android.view.Display;
32 import android.view.MotionEvent;
33 import android.view.SurfaceControlViewHost;
34 import android.view.View;
35 import android.view.WindowManager;
36 
37 import androidx.annotation.Nullable;
38 
39 import java.util.concurrent.BlockingQueue;
40 import java.util.concurrent.CountDownLatch;
41 import java.util.concurrent.LinkedBlockingQueue;
42 import java.util.concurrent.TimeUnit;
43 
44 public class CrossProcessSurfaceControlViewHostTestService extends Service {
45     private static final long WAIT_TIMEOUT_S = HW_TIMEOUT_MULTIPLIER * 5L;
46     private static final String TAG = "CrossProcessSurfaceControlViewHostTestService";
47 
48     private final ICrossProcessSurfaceControlViewHostTestService mBinder = new ServiceImpl();
49     private Handler mHandler;
50 
51     class MotionRecordingView extends View {
52         BlockingQueue<MotionEvent> mEvents = new LinkedBlockingQueue<>();
53 
MotionRecordingView(Context c)54         MotionRecordingView(Context c) {
55             super(c);
56         }
57 
onTouchEvent(MotionEvent e)58         public boolean onTouchEvent(MotionEvent e) {
59             super.onTouchEvent(e);
60             mEvents.add(MotionEvent.obtain(e));
61             return true;
62         }
63     }
64 
65     @Override
onCreate()66     public void onCreate() {
67         mHandler = new Handler(Looper.getMainLooper());
68     }
69 
70     @Nullable
71     @Override
onBind(Intent intent)72     public IBinder onBind(Intent intent) {
73         return mBinder.asBinder();
74     }
75 
getDefaultDisplay()76     Display getDefaultDisplay() {
77         WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
78         return wm.getDefaultDisplay();
79     }
80 
81     SurfaceControlViewHost mSurfaceControlViewHost;
82     MotionRecordingView mView;
83 
createSurfacePackage(IBinder hostInputToken)84     SurfaceControlViewHost.SurfacePackage createSurfacePackage(IBinder hostInputToken) {
85         mView = new MotionRecordingView(this);
86         mSurfaceControlViewHost = new SurfaceControlViewHost(this, getDefaultDisplay(),
87                 hostInputToken);
88         mSurfaceControlViewHost.setView(mView, 100, 100);
89         return mSurfaceControlViewHost.getSurfacePackage();
90     }
91 
92     private class ServiceImpl extends ICrossProcessSurfaceControlViewHostTestService.Stub {
drainHandler()93         private void drainHandler() {
94             final CountDownLatch latch = new CountDownLatch(1);
95             mHandler.post(latch::countDown);
96             try {
97                 assertTrue("Failed to wait for handler to drain",
98                         latch.await(HW_TIMEOUT_MULTIPLIER * 1L, TimeUnit.SECONDS));
99             } catch (Exception e) {
100             }
101         }
102 
103         @Nullable
104         @Override
getMotionEvent()105         public MotionEvent getMotionEvent() {
106             try {
107                 return mView.mEvents.poll(WAIT_TIMEOUT_S, TimeUnit.SECONDS);
108             } catch (Exception e) {
109                 throw new RuntimeException(e);
110             }
111         }
112 
113         @Override
getSurfacePackage(IBinder hostInputToken)114         public SurfaceControlViewHost.SurfacePackage getSurfacePackage(IBinder hostInputToken) {
115             final CountDownLatch latch = new CountDownLatch(1);
116             mHandler.post(() -> {
117                 createSurfacePackage(hostInputToken);
118                 mView.getViewTreeObserver().registerFrameCommitCallback(latch::countDown);
119                 mView.invalidate();
120             });
121             try {
122                 assertTrue("Failed to wait for frame to draw",
123                         latch.await(HW_TIMEOUT_MULTIPLIER * 1L, TimeUnit.SECONDS));
124             } catch (Exception e) {
125                 return null;
126             }
127             return mSurfaceControlViewHost.getSurfacePackage();
128         }
129 
130         @Override
getWindowToken()131         public IBinder getWindowToken() {
132             return mView.getWindowToken();
133         }
134 
135         @Override
waitForFocus(boolean waitForFocus)136         public boolean waitForFocus(boolean waitForFocus) {
137             return CtsWindowInfoUtils.waitForWindowFocus(mView, waitForFocus);
138         }
139 
140         @Override
setKeepScreenOnFlag(boolean keepScreenOn)141         public void setKeepScreenOnFlag(boolean keepScreenOn) {
142             CountDownLatch countDownLatch = new CountDownLatch(1);
143             mHandler.post(() -> {
144                 mView.setKeepScreenOn(keepScreenOn);
145                 mView.getViewTreeObserver().addOnDrawListener(countDownLatch::countDown);
146             });
147 
148             try {
149                 countDownLatch.await(WAIT_TIMEOUT_S, TimeUnit.SECONDS);
150             } catch (InterruptedException e) {
151                 Log.e(TAG, "Failed to set keep screen on flag");
152             }
153         }
154     }
155 }
156