• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.scopedstorage.cts.host;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.android.tradefed.device.DeviceNotAvailableException;
22 import com.android.tradefed.device.ITestDevice;
23 import com.android.tradefed.device.NativeDevice;
24 import com.android.tradefed.log.LogUtil.CLog;
25 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
26 import com.android.tradefed.util.CommandResult;
27 import com.android.tradefed.util.CommandStatus;
28 import com.android.tradefed.util.RunInterruptedException;
29 import com.android.tradefed.util.RunUtil;
30 
31 
32 abstract class BaseHostTestCase extends BaseHostJUnit4Test {
33     private int mCurrentUserId = NativeDevice.INVALID_USER_ID;
34     private static final String ERROR_MESSAGE_TAG = "[ERROR]";
35 
executeShellCommand(String cmd, Object... args)36     protected String executeShellCommand(String cmd, Object... args) throws Exception {
37         return getDevice().executeShellCommand(String.format(cmd, args));
38     }
39 
executeShellV2Command(String cmd, Object... args)40     protected CommandResult executeShellV2Command(String cmd, Object... args) throws Exception {
41         return getDevice().executeShellV2Command(String.format(cmd, args));
42     }
43 
isPackageInstalled(String packageName, String userId)44     protected boolean isPackageInstalled(String packageName, String userId) throws Exception {
45         return getDevice().isPackageInstalled(packageName, userId);
46     }
47 
isHeadlessSystemUserMode(ITestDevice device)48     protected static boolean isHeadlessSystemUserMode(ITestDevice device)
49             throws DeviceNotAvailableException {
50         return device.isHeadlessSystemUserMode();
51     }
52 
isAtLeastS(ITestDevice device)53     protected static boolean isAtLeastS(ITestDevice device) throws DeviceNotAvailableException {
54         return device.getApiLevel() >= 31 /* BUILD.VERSION_CODES.S */;
55     }
56 
eventually(ThrowingRunnable r, long timeoutMillis)57     protected static void eventually(ThrowingRunnable r, long timeoutMillis) {
58         long start = System.currentTimeMillis();
59 
60         while (true) {
61             try {
62                 r.run();
63                 return;
64             } catch (Throwable e) {
65                 if (System.currentTimeMillis() - start < timeoutMillis) {
66                     try {
67                         RunUtil.getDefault().sleep(100);
68                     } catch (RunInterruptedException ignored) {
69                         throw new RuntimeException(e);
70                     }
71                 } else {
72                     throw new RuntimeException(e);
73                 }
74             }
75         }
76     }
77 
getCurrentUserId()78     protected int getCurrentUserId() throws Exception {
79         setCurrentUserId();
80 
81         return mCurrentUserId;
82     }
83 
isSuccessful(CommandResult result)84     protected static boolean isSuccessful(CommandResult result) {
85         if (!CommandStatus.SUCCESS.equals(result.getStatus())) {
86             return false;
87         }
88         String stdout = result.getStdout();
89         if (stdout.contains(ERROR_MESSAGE_TAG)) {
90             return false;
91         }
92         String stderr = result.getStderr();
93         return (stderr == null || stderr.trim().isEmpty());
94     }
95 
supportsMultipleUsers(ITestDevice device)96     protected static boolean supportsMultipleUsers(ITestDevice device)
97             throws DeviceNotAvailableException {
98         return device.getMaxNumberOfUsersSupported() > 1;
99     }
100 
usesSdcardFs(ITestDevice device)101     protected static boolean usesSdcardFs(ITestDevice device) throws Exception {
102         CommandResult out = device.executeShellV2Command("cat /proc/mounts");
103         assertThat(isSuccessful(out)).isTrue();
104         for (String line : out.getStdout().split("\n")) {
105             String[] split = line.split(" ");
106             if (split.length >= 3 && split[2].equals("sdcardfs")) {
107                 return true;
108             }
109         }
110         return false;
111     }
112 
setCurrentUserId()113     private void setCurrentUserId() throws Exception {
114         if (mCurrentUserId != NativeDevice.INVALID_USER_ID) return;
115 
116         ITestDevice device = getDevice();
117         mCurrentUserId = device.getCurrentUser();
118         CLog.i("Current user: %d");
119     }
120 
121     protected interface ThrowingRunnable {
122         /**
123          * Similar to {@link Runnable#run} but has {@code throws Exception}.
124          */
run()125         void run() throws Exception;
126     }
127 }
128