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 android.app.sdksandbox.SdkSandboxManager.EXTRA_SURFACE_PACKAGE;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import android.app.sdksandbox.RequestSurfacePackageException;
24 import android.os.Bundle;
25 import android.os.OutcomeReceiver;
26 import android.view.SurfaceControlViewHost;
27 
28 public class FakeRequestSurfacePackageCallback
29         implements OutcomeReceiver<Bundle, RequestSurfacePackageException> {
30     private final WaitableCountDownLatch mSurfacePackageLatch = new WaitableCountDownLatch(5);
31 
32     private boolean mSurfacePackageSuccess;
33 
34     private RequestSurfacePackageException mSurfacePackageException;
35     private SurfaceControlViewHost.SurfacePackage mSurfacePackage;
36 
37     @Override
onError(RequestSurfacePackageException exception)38     public void onError(RequestSurfacePackageException exception) {
39         mSurfacePackageSuccess = false;
40         mSurfacePackageException = exception;
41         mSurfacePackageLatch.countDown();
42     }
43 
44     @Override
onResult(Bundle response)45     public void onResult(Bundle response) {
46         mSurfacePackageSuccess = true;
47         mSurfacePackage =
48                 response.getParcelable(
49                         EXTRA_SURFACE_PACKAGE, SurfaceControlViewHost.SurfacePackage.class);
50         mSurfacePackageLatch.countDown();
51     }
52 
isRequestSurfacePackageSuccessful()53     public boolean isRequestSurfacePackageSuccessful() {
54         mSurfacePackageLatch.waitForLatch();
55         return mSurfacePackageSuccess;
56     }
57 
getExtraErrorInformation()58     public Bundle getExtraErrorInformation() {
59         mSurfacePackageLatch.waitForLatch();
60         assertThat(mSurfacePackageSuccess).isFalse();
61         return mSurfacePackageException.getExtraErrorInformation();
62     }
63 
getSurfacePackage()64     public SurfaceControlViewHost.SurfacePackage getSurfacePackage() {
65         mSurfacePackageLatch.waitForLatch();
66         assertThat(mSurfacePackageSuccess).isTrue();
67         return mSurfacePackage;
68     }
69 
getSurfacePackageErrorCode()70     public int getSurfacePackageErrorCode() {
71         mSurfacePackageLatch.waitForLatch();
72         assertThat(mSurfacePackageSuccess).isFalse();
73         return mSurfacePackageException.getRequestSurfacePackageErrorCode();
74     }
75 
getSurfacePackageErrorMsg()76     public String getSurfacePackageErrorMsg() {
77         mSurfacePackageLatch.waitForLatch();
78         assertThat(mSurfacePackageSuccess).isFalse();
79         return mSurfacePackageException.getMessage();
80     }
81 
getSurfacePackageException()82     public RequestSurfacePackageException getSurfacePackageException() {
83         mSurfacePackageLatch.waitForLatch();
84         assertThat(mSurfacePackageSuccess).isFalse();
85         return mSurfacePackageException;
86     }
87 }
88