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 com.android.sdksandbox.shared.app2;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.app.sdksandbox.AppOwnedSdkSandboxInterface;
22 import android.app.sdksandbox.SdkSandboxManager;
23 import android.app.sdksandbox.testutils.FakeLoadSdkCallback;
24 import android.content.Context;
25 import android.os.Binder;
26 import android.os.Bundle;
27 
28 import androidx.test.core.app.ApplicationProvider;
29 import androidx.test.ext.junit.rules.ActivityScenarioRule;
30 
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.JUnit4;
36 
37 @RunWith(JUnit4.class)
38 public class SdkSandboxTestSharedApp2  {
39 
40     private static final String APP_OWNED_SDK_SANDBOX_INTERFACE_NAME = "com.android.testinterface";
41     private static final String SDK_PACKAGE_NAME = "com.android.testcode";
42 
43     private SdkSandboxManager mSdkSandboxManager;
44 
45     @Rule
46     public final ActivityScenarioRule<SdkSandboxEmptyActivity> mRule =
47             new ActivityScenarioRule<>(SdkSandboxEmptyActivity.class);
48 
49     @Before
setup()50     public void setup() {
51         Context sContext = ApplicationProvider.getApplicationContext();
52         mSdkSandboxManager = sContext.getSystemService(
53                 SdkSandboxManager.class);
54         assertThat(mSdkSandboxManager).isNotNull();
55     }
56 
57     @Test
testLoadSdkIsSuccessful()58     public void testLoadSdkIsSuccessful() {
59         mRule.getScenario();
60 
61         Bundle params = new Bundle();
62         FakeLoadSdkCallback callback = new FakeLoadSdkCallback();
63         mSdkSandboxManager.loadSdk(SDK_PACKAGE_NAME, params, Runnable::run, callback);
64         callback.assertLoadSdkIsSuccessful();
65     }
66 
67     @Test
testRegisterAppOwedSdkSandboxInterfacesBeforeAppDeath()68     public void testRegisterAppOwedSdkSandboxInterfacesBeforeAppDeath() {
69         mRule.getScenario();
70 
71         mSdkSandboxManager.registerAppOwnedSdkSandboxInterface(
72                 new AppOwnedSdkSandboxInterface(
73                         APP_OWNED_SDK_SANDBOX_INTERFACE_NAME,
74                         /*version=*/ 0,
75                         /*interfaceIBinder=*/ new Binder()));
76         assertThat(mSdkSandboxManager.getAppOwnedSdkSandboxInterfaces()).hasSize(1);
77     }
78 
79     @Test
testGetAppOwedSdkSandboxInterfacesOnAppDeath()80     public void testGetAppOwedSdkSandboxInterfacesOnAppDeath() {
81         mRule.getScenario();
82 
83         assertThat(mSdkSandboxManager.getAppOwnedSdkSandboxInterfaces()).hasSize(0);
84     }
85 }
86