1 /*
2  * Copyright (C) 2021 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.bedstead.nene.context;
18 
19 import android.annotation.SuppressLint;
20 import android.content.pm.PackageManager;
21 
22 import androidx.test.platform.app.InstrumentationRegistry;
23 
24 import com.android.bedstead.nene.exceptions.NeneException;
25 import com.android.bedstead.nene.users.UserReference;
26 
27 /** Test APIs related to Context. */
28 public final class Context {
29 
30     private static final String ANDROID_PACKAGE = "android";
31 
32     private static final android.content.Context sInstrumentedContext =
33             InstrumentationRegistry.getInstrumentation().getTargetContext();
34     private static final android.content.Context sInstrumentationContext =
35             InstrumentationRegistry.getInstrumentation().getContext();
36 
37     public static final Context sInstance = new Context();
38 
Context()39     private Context() {
40 
41     }
42 
43     /**
44      * Get the {@link android.content.Context} for the instrumented test app.
45      */
instrumentedContext()46     public android.content.Context instrumentedContext() {
47         return sInstrumentedContext;
48     }
49 
50     /**
51      * Get the {@link android.content.Context} for the instrumented test app in another user.
52      */
53     @SuppressLint("NewApi")
instrumentedContextAsUser(UserReference user)54     public android.content.Context instrumentedContextAsUser(UserReference user) {
55         return sInstrumentedContext.createContextAsUser(user.userHandle(), /* flags= */ 0);
56     }
57 
58     /**
59      * Get the {@link android.content.Context} for the instrumentation app.
60      */
instrumentationContext()61     public android.content.Context instrumentationContext() {
62         return sInstrumentationContext;
63     }
64 
65     /**
66      * Get the {@link android.content.Context} for the instrumentation app in another user.
67      */
68     @SuppressLint("NewApi")
instrumentationContextAsUser(UserReference user)69     public android.content.Context instrumentationContextAsUser(UserReference user) {
70         return sInstrumentationContext.createContextAsUser(user.userHandle(), /* flags= */ 0);
71     }
72 
73     /**
74      * Get the {@link android.content.Context} for the "android" package in the given user.
75      * Note: The packageName returned will match the test package rather than 'android'.
76      * This is a property of [createPackageContextAsUser]
77      */
androidContextAsUser(UserReference user)78     public android.content.Context androidContextAsUser(UserReference user) {
79         try {
80             return sInstrumentedContext.createPackageContextAsUser(
81                     ANDROID_PACKAGE, /* flags= */ 0, user.userHandle());
82         } catch (PackageManager.NameNotFoundException e) {
83             throw new NeneException("Could not find android installed in user " + user, e);
84         }
85     }
86 }
87