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.virtualdevice.cts.util; 18 19 import android.app.ActivityOptions; 20 import android.hardware.display.VirtualDisplay; 21 import android.os.Bundle; 22 import android.os.Handler; 23 import android.os.Looper; 24 import android.os.Parcel; 25 import android.os.ResultReceiver; 26 27 /** 28 * Test utilities for Virtual Device tests. 29 */ 30 public final class VirtualDeviceTestUtils { 31 createResultReceiver(OnReceiveResultListener listener)32 public static ResultReceiver createResultReceiver(OnReceiveResultListener listener) { 33 ResultReceiver receiver = new ResultReceiver(new Handler(Looper.getMainLooper())) { 34 @Override 35 protected void onReceiveResult(int resultCode, Bundle resultData) { 36 listener.onReceiveResult(resultCode, resultData); 37 } 38 }; 39 // Erase the subclass to make the given result receiver safe to include inside Bundles 40 // (See b/177985835). 41 Parcel parcel = Parcel.obtain(); 42 receiver.writeToParcel(parcel, 0); 43 parcel.setDataPosition(0); 44 receiver = ResultReceiver.CREATOR.createFromParcel(parcel); 45 parcel.recycle(); 46 return receiver; 47 } 48 49 /** 50 * Interface mimicking {@link ResultReceiver}, allowing it to be mocked. 51 */ 52 public interface OnReceiveResultListener { onReceiveResult(int resultCode, Bundle resultData)53 void onReceiveResult(int resultCode, Bundle resultData); 54 } 55 createActivityOptions(VirtualDisplay virtualDisplay)56 public static Bundle createActivityOptions(VirtualDisplay virtualDisplay) { 57 return ActivityOptions.makeBasic() 58 .setLaunchDisplayId(virtualDisplay.getDisplay().getDisplayId()) 59 .toBundle(); 60 } 61 VirtualDeviceTestUtils()62 private VirtualDeviceTestUtils() {} 63 } 64