1 /*
2  * Copyright (C) 2024 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 package android.app.sdksandbox;
17 
18 import static android.app.sdksandbox.SandboxLatencyInfo.RESULT_CODE_LOAD_SDK_ALREADY_LOADED;
19 import static android.app.sdksandbox.SandboxLatencyInfo.RESULT_CODE_LOAD_SDK_INTERNAL_ERROR;
20 import static android.app.sdksandbox.SandboxLatencyInfo.RESULT_CODE_LOAD_SDK_NOT_FOUND;
21 import static android.app.sdksandbox.SandboxLatencyInfo.RESULT_CODE_LOAD_SDK_SDK_DEFINED_ERROR;
22 import static android.app.sdksandbox.SandboxLatencyInfo.RESULT_CODE_LOAD_SDK_SDK_SANDBOX_DISABLED;
23 import static android.app.sdksandbox.SandboxLatencyInfo.RESULT_CODE_SDK_SANDBOX_PROCESS_NOT_AVAILABLE;
24 import static android.app.sdksandbox.SandboxLatencyInfo.RESULT_CODE_UNSPECIFIED;
25 import static android.app.sdksandbox.SdkSandboxManager.LOAD_SDK_ALREADY_LOADED;
26 import static android.app.sdksandbox.SdkSandboxManager.LOAD_SDK_INTERNAL_ERROR;
27 import static android.app.sdksandbox.SdkSandboxManager.LOAD_SDK_NOT_FOUND;
28 import static android.app.sdksandbox.SdkSandboxManager.LOAD_SDK_SDK_DEFINED_ERROR;
29 import static android.app.sdksandbox.SdkSandboxManager.LOAD_SDK_SDK_SANDBOX_DISABLED;
30 import static android.app.sdksandbox.SdkSandboxManager.SDK_SANDBOX_PROCESS_NOT_AVAILABLE;
31 
32 import static org.junit.Assert.assertEquals;
33 
34 import android.app.sdksandbox.testutils.FakeOutcomeReceiver;
35 import android.content.Context;
36 import android.os.Bundle;
37 
38 import androidx.test.platform.app.InstrumentationRegistry;
39 
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.junit.runners.Parameterized;
44 import org.mockito.ArgumentCaptor;
45 import org.mockito.Mockito;
46 
47 import java.util.Arrays;
48 import java.util.Collection;
49 
50 /** Tests for the result code logging logic in {@link SdkSandboxManager}. */
51 @RunWith(Parameterized.class)
52 public class SdkSandboxManagerResultCodeLoggingUnitTest {
53     @SdkSandboxManager.LoadSdkErrorCode
54     @Parameterized.Parameter(0)
55     public int mLoadSdkErrorCode;
56 
57     @SandboxLatencyInfo.ResultCode
58     @Parameterized.Parameter(1)
59     public int mExpectedLoggingResultCode;
60 
61     /** Parameters for the test. */
62     @Parameterized.Parameters
data()63     public static Collection<Object[]> data() {
64         return Arrays.asList(
65                 new Object[][] {
66                     {LOAD_SDK_NOT_FOUND, RESULT_CODE_LOAD_SDK_NOT_FOUND},
67                     {LOAD_SDK_ALREADY_LOADED, RESULT_CODE_LOAD_SDK_ALREADY_LOADED},
68                     {LOAD_SDK_SDK_DEFINED_ERROR, RESULT_CODE_LOAD_SDK_SDK_DEFINED_ERROR},
69                     {LOAD_SDK_SDK_SANDBOX_DISABLED, RESULT_CODE_LOAD_SDK_SDK_SANDBOX_DISABLED},
70                     {LOAD_SDK_INTERNAL_ERROR, RESULT_CODE_LOAD_SDK_INTERNAL_ERROR},
71                     {
72                         SDK_SANDBOX_PROCESS_NOT_AVAILABLE,
73                         RESULT_CODE_SDK_SANDBOX_PROCESS_NOT_AVAILABLE
74                     },
75                     {-1, RESULT_CODE_UNSPECIFIED}
76                 });
77     }
78 
79     private SdkSandboxManager mSdkSandboxManager;
80     private ISdkSandboxManager mBinder;
81 
82     private static final String SDK_NAME = "com.android.codeproviderresources";
83     private static final String ERROR_MSG = "Error";
84 
85     @Before
setup()86     public void setup() {
87         Context context = InstrumentationRegistry.getInstrumentation().getContext();
88         mBinder = Mockito.mock(ISdkSandboxManager.class);
89         mSdkSandboxManager = new SdkSandboxManager(context, mBinder);
90     }
91 
92     @Test
testLoadSdk_callFailsWithException_logSandboxApiLatencyCalledWithResultCode()93     public void testLoadSdk_callFailsWithException_logSandboxApiLatencyCalledWithResultCode()
94             throws Exception {
95         mSdkSandboxManager.loadSdk(
96                 SDK_NAME, new Bundle(), Runnable::run, new FakeOutcomeReceiver<>());
97         ArgumentCaptor<SandboxLatencyInfo> sandboxLatencyInfoCaptor =
98                 ArgumentCaptor.forClass(SandboxLatencyInfo.class);
99         ArgumentCaptor<ILoadSdkCallback> callbackArgumentCaptor =
100                 ArgumentCaptor.forClass(ILoadSdkCallback.class);
101         Mockito.verify(mBinder)
102                 .loadSdk(
103                         Mockito.any(),
104                         Mockito.any(),
105                         Mockito.any(),
106                         sandboxLatencyInfoCaptor.capture(),
107                         Mockito.any(),
108                         callbackArgumentCaptor.capture());
109         SandboxLatencyInfo sandboxLatencyInfo = sandboxLatencyInfoCaptor.getValue();
110         // Simulate the error callback
111         callbackArgumentCaptor
112                 .getValue()
113                 .onLoadSdkFailure(
114                         new LoadSdkException(mLoadSdkErrorCode, ERROR_MSG), sandboxLatencyInfo);
115 
116         assertEquals(mExpectedLoggingResultCode, sandboxLatencyInfo.getResultCode());
117     }
118 }
119