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.app.sdksandbox.testutils; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static com.google.common.truth.Truth.assertWithMessage; 21 22 import android.app.sdksandbox.IRemoteSdkCallback; 23 import android.os.Bundle; 24 import android.os.IBinder; 25 import android.view.SurfaceControlViewHost; 26 27 import java.util.concurrent.CountDownLatch; 28 import java.util.concurrent.TimeUnit; 29 30 public class FakeRemoteSdkCallback extends IRemoteSdkCallback.Stub { 31 private final CountDownLatch mLoadSdkLatch = new CountDownLatch(1); 32 private final CountDownLatch mSurfacePackageLatch = new CountDownLatch(1); 33 34 private boolean mLoadSdkSuccess; 35 private boolean mSurfacePackageSuccess; 36 37 private int mErrorCode; 38 private String mErrorMsg; 39 40 private IBinder mSdkToken; 41 42 @Override onLoadSdkSuccess(IBinder sdkToken, Bundle params)43 public void onLoadSdkSuccess(IBinder sdkToken, Bundle params) { 44 mSdkToken = sdkToken; 45 mLoadSdkSuccess = true; 46 mLoadSdkLatch.countDown(); 47 } 48 49 @Override onLoadSdkFailure(int errorCode, String errorMsg)50 public void onLoadSdkFailure(int errorCode, String errorMsg) { 51 mLoadSdkSuccess = false; 52 mErrorCode = errorCode; 53 mErrorMsg = errorMsg; 54 mLoadSdkLatch.countDown(); 55 } 56 57 @Override onSurfacePackageError(int errorCode, String errorMsg)58 public void onSurfacePackageError(int errorCode, String errorMsg) { 59 mSurfacePackageSuccess = false; 60 mErrorCode = errorCode; 61 mErrorMsg = errorMsg; 62 mSurfacePackageLatch.countDown(); 63 } 64 65 @Override onSurfacePackageReady(SurfaceControlViewHost.SurfacePackage surfacePackage, int surfacePackageId, Bundle params)66 public void onSurfacePackageReady(SurfaceControlViewHost.SurfacePackage surfacePackage, 67 int surfacePackageId, Bundle params) { 68 mSurfacePackageSuccess = true; 69 mSurfacePackageLatch.countDown(); 70 } 71 isLoadSdkSuccessful()72 public boolean isLoadSdkSuccessful() { 73 waitForLatch(mLoadSdkLatch); 74 return mLoadSdkSuccess; 75 } 76 getSdkToken()77 public IBinder getSdkToken() { 78 waitForLatch(mLoadSdkLatch); 79 assertWithMessage("Failed to load sdk: " + getLoadSdkErrorMsg()) 80 .that(mLoadSdkSuccess).isTrue(); 81 return mSdkToken; 82 } 83 getLoadSdkErrorCode()84 public int getLoadSdkErrorCode() { 85 waitForLatch(mLoadSdkLatch); 86 assertThat(mLoadSdkSuccess).isFalse(); 87 return mErrorCode; 88 } 89 getLoadSdkErrorMsg()90 public String getLoadSdkErrorMsg() { 91 waitForLatch(mLoadSdkLatch); 92 return mErrorMsg; 93 } 94 isRequestSurfacePackageSuccessful()95 public boolean isRequestSurfacePackageSuccessful() throws InterruptedException { 96 waitForLatch(mSurfacePackageLatch); 97 return mSurfacePackageSuccess; 98 } 99 getSurfacePackageErrorCode()100 public int getSurfacePackageErrorCode() { 101 waitForLatch(mSurfacePackageLatch); 102 assertThat(mSurfacePackageSuccess).isFalse(); 103 return mErrorCode; 104 } 105 getSurfacePackageErrorMsg()106 public String getSurfacePackageErrorMsg() { 107 waitForLatch(mSurfacePackageLatch); 108 assertThat(mSurfacePackageSuccess).isFalse(); 109 return mErrorMsg; 110 } 111 waitForLatch(CountDownLatch latch)112 private void waitForLatch(CountDownLatch latch) { 113 try { 114 // Wait for callback to be called 115 final int waitTime = 5; 116 if (!latch.await(waitTime, TimeUnit.SECONDS)) { 117 throw new IllegalStateException( 118 "Callback not called within " + waitTime + " seconds"); 119 } 120 } catch (InterruptedException e) { 121 throw new IllegalStateException( 122 "Interrupted while waiting on callback: " + e.getMessage()); 123 } 124 } 125 } 126