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 17 package android.content.cts; 18 19 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import android.app.Activity; 24 import android.app.Instrumentation; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.os.Process; 28 import android.os.UserHandle; 29 import android.platform.test.annotations.AppModeFull; 30 31 import androidx.test.ext.junit.runners.AndroidJUnit4; 32 import androidx.test.platform.app.InstrumentationRegistry; 33 34 import com.android.compatibility.common.util.SystemUtil; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 @AppModeFull 41 @RunWith(AndroidJUnit4.class) 42 public class DocumentsUIUsedContentApiTest { 43 44 private Context mContext; 45 46 /** 47 * Returns the Context object that's being tested. 48 */ getContextUnderTest()49 private Context getContextUnderTest() { 50 return InstrumentationRegistry.getInstrumentation().getTargetContext(); 51 } 52 53 @Before setUp()54 public final void setUp() throws Exception { 55 mContext = getContextUnderTest(); 56 } 57 58 @Test testCreatePackageContextAsUser()59 public void testCreatePackageContextAsUser() throws Exception { 60 for (UserHandle user : new UserHandle[]{ 61 Process.myUserHandle(), 62 UserHandle.ALL, UserHandle.CURRENT, UserHandle.SYSTEM 63 }) { 64 assertThat(mContext.createPackageContextAsUser("com.android.shell", 0, user) 65 .getUser()).isEqualTo(user); 66 } 67 } 68 69 @Test testStartActivityAsUser()70 public void testStartActivityAsUser() { 71 try (ActivitySession activitySession = new ActivitySession()) { 72 Intent intent = new Intent(mContext, AvailableIntentsActivity.class); 73 74 activitySession.assertActivityLaunched(intent.getComponent().getClassName(), 75 () -> SystemUtil.runWithShellPermissionIdentity(() -> 76 mContext.startActivityAsUser(intent, UserHandle.CURRENT))); 77 } 78 } 79 80 /** 81 * Helper class to launch / close test activity. 82 */ 83 private class ActivitySession implements AutoCloseable { 84 private Activity mTestActivity; 85 private static final int ACTIVITY_LAUNCH_TIMEOUT = 5000; 86 assertActivityLaunched(String activityClassName, Runnable activityStarter)87 void assertActivityLaunched(String activityClassName, Runnable activityStarter) { 88 final Instrumentation.ActivityMonitor monitor = getInstrumentation() 89 .addMonitor(activityClassName, null /* result */, 90 false /* block */); 91 activityStarter.run(); 92 // Wait for activity launch with timeout. 93 mTestActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 94 ACTIVITY_LAUNCH_TIMEOUT); 95 assertThat(mTestActivity).isNotNull(); 96 } 97 98 @Override close()99 public void close() { 100 if (mTestActivity != null) { 101 mTestActivity.finishAndRemoveTask(); 102 } 103 } 104 } 105 } 106