• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.pm.PackageManager;
20 
21 import androidx.test.platform.app.InstrumentationRegistry;
22 
23 import com.android.bedstead.nene.exceptions.NeneException;
24 import com.android.bedstead.nene.users.UserReference;
25 
26 /** Test APIs related to Context. */
27 public final class Context {
28 
29     private static final String ANDROID_PACKAGE = "android";
30 
31     private static final android.content.Context sInstrumentedContext =
32             InstrumentationRegistry.getInstrumentation().getTargetContext();
33     private static final android.content.Context sInstrumentationContext =
34             InstrumentationRegistry.getInstrumentation().getContext();
35 
36     public static final Context sInstance = new Context();
37 
Context()38     private Context() {
39 
40     }
41 
42     /**
43      * Get the {@link android.content.Context} for the instrumented test app.
44      */
instrumentedContext()45     public android.content.Context instrumentedContext() {
46         return sInstrumentedContext;
47     }
48 
49     /**
50      * Get the {@link android.content.Context} for the instrumented test app in another user.
51      */
instrumentedContextAsUser(UserReference user)52     public android.content.Context instrumentedContextAsUser(UserReference user) {
53         return sInstrumentedContext.createContextAsUser(user.userHandle(), /* flags= */ 0);
54     }
55 
56     /**
57      * Get the {@link android.content.Context} for the instrumentation app.
58      */
instrumentationContext()59     public android.content.Context instrumentationContext() {
60         return sInstrumentationContext;
61     }
62 
63     /**
64      * Get the {@link android.content.Context} for the instrumentation app in another user.
65      */
instrumentationContextAsUser(UserReference user)66     public android.content.Context instrumentationContextAsUser(UserReference user) {
67         return sInstrumentationContext.createContextAsUser(user.userHandle(), /* flags= */ 0);
68     }
69 
70     /**
71      * Get the {@link android.content.Context} for the "android" package in the given user.
72      */
androidContextAsUser(UserReference user)73     public android.content.Context androidContextAsUser(UserReference user) {
74         try {
75             return sInstrumentedContext.createPackageContextAsUser(
76                     ANDROID_PACKAGE, /* flags= */ 0, user.userHandle());
77         } catch (PackageManager.NameNotFoundException e) {
78             throw new NeneException("Could not find android installed in user " + user, e);
79         }
80     }
81 }
82