1 /*
2  * Copyright (C) 2023 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.tests.sdksandbox.secondary;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.app.sdksandbox.SdkSandboxManager;
22 import android.app.sdksandbox.testutils.EmptyActivity;
23 import android.app.sdksandbox.testutils.FakeLoadSdkCallback;
24 import android.content.Context;
25 import android.os.Bundle;
26 
27 import androidx.test.ext.junit.rules.ActivityScenarioRule;
28 import androidx.test.platform.app.InstrumentationRegistry;
29 
30 import com.android.tests.sdkprovider.crashtest.ICrashTestSdkApi;
31 
32 import org.junit.Before;
33 import org.junit.Rule;
34 import org.junit.Test;
35 
36 import java.util.concurrent.CountDownLatch;
37 import java.util.concurrent.TimeUnit;
38 
39 public class SdkSandboxMetricsSecondaryTestApp {
40 
41     private SdkSandboxManager mSdkSandboxManager;
42     private static final String SDK_PACKAGE = "com.android.tests.sdkprovider.crashtest";
43 
44     @Rule public final ActivityScenarioRule mRule = new ActivityScenarioRule<>(EmptyActivity.class);
45 
46     @Before
setup()47     public void setup() {
48         Context context = InstrumentationRegistry.getInstrumentation().getContext();
49         mSdkSandboxManager = context.getSystemService(SdkSandboxManager.class);
50         assertThat(mSdkSandboxManager).isNotNull();
51     }
52 
53     @Test
startAndCrashSdkSandbox()54     public void startAndCrashSdkSandbox() throws Exception {
55         mRule.getScenario();
56         generateSdkSandboxCrash();
57     }
58 
generateSdkSandboxCrash()59     private void generateSdkSandboxCrash() throws Exception {
60         final CountDownLatch deathLatch = new CountDownLatch(1);
61         // Avoid being killed when sandbox crashes
62         mSdkSandboxManager.addSdkSandboxProcessDeathCallback(Runnable::run, deathLatch::countDown);
63 
64         final FakeLoadSdkCallback callback = new FakeLoadSdkCallback();
65 
66         // Start and crash the SDK sandbox
67         mSdkSandboxManager.loadSdk(SDK_PACKAGE, new Bundle(), Runnable::run, callback);
68         callback.assertLoadSdkIsSuccessful();
69 
70         final ICrashTestSdkApi sdk =
71                 ICrashTestSdkApi.Stub.asInterface(callback.getSandboxedSdk().getInterface());
72         sdk.triggerCrash();
73         assertThat(deathLatch.await(5, TimeUnit.SECONDS)).isTrue();
74     }
75 }
76