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