• 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 package android.multiuser.cts;
17 
18 import static com.android.compatibility.common.util.SystemUtil.runShellCommand;
19 
20 import static org.junit.Assume.assumeFalse;
21 
22 import android.app.Instrumentation;
23 import android.content.Context;
24 import android.content.pm.PackageManager;
25 import android.os.UserHandle;
26 import android.util.Log;
27 
28 import com.android.bedstead.nene.TestApis;
29 
30 import java.io.IOException;
31 
32 final class TestingUtils {
33     private static final String TAG = TestingUtils.class.getSimpleName();
34 
35     static final Context sContext = TestApis.context().instrumentedContext();
36 
getBooleanProperty(Instrumentation instrumentation, String property)37     public static boolean getBooleanProperty(Instrumentation instrumentation, String property)
38             throws IOException {
39         String value = trim(runShellCommand(instrumentation, "getprop " + property));
40         return "y".equals(value) || "yes".equals(value) || "1".equals(value) || "true".equals(value)
41                 || "on".equals(value);
42     }
43 
getContextForOtherUser()44     static Context getContextForOtherUser() {
45         // TODO(b/240207590): TestApis.context().instrumentedContextForUser(TestApis.users()
46         // .nonExisting() doesn't work, it throws:
47         // IllegalStateException: Own package not found for user 1: package=android.multiuser.cts
48         // There might be some bug (or WAI :-) on ContextImpl that makes it behave different for
49         // negative user ids. Anyways, for the purpose of this test, this workaround is fine (i.e.
50         // the context user id is passed to the binder call and the service checks if it matches the
51         // caller or the caller has the proper permission when it doesn't.
52         return getContextForUser(-42);
53     }
54 
getContextForUser(int userId)55     static Context getContextForUser(int userId) {
56         Log.d(TAG, "Getting context for user " + userId);
57         Context context = sContext.createContextAsUser(UserHandle.of(userId), /* flags= */ 0);
58         Log.d(TAG, "Got it: " + context);
59         return context;
60     }
61 
assumeTvNotSupported()62     static void assumeTvNotSupported() {
63         assumeFalse(
64                 "Skipping test as TV does not support lock screen",
65                 sContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK));
66     }
67 
trim(String s)68     private static String trim(String s) {
69         return s == null ? null : s.trim();
70     }
71 
TestingUtils()72     private TestingUtils() {
73         throw new UnsupportedOperationException("contains only static methods");
74     }
75 }
76