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 com.android.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.device.ITestDevice; 21 import com.android.tradefed.device.NativeDevice; 22 import com.android.tradefed.log.LogUtil.CLog; 23 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 24 import com.android.tradefed.util.CommandResult; 25 import com.android.tradefed.util.CommandStatus; 26 27 28 abstract class BaseHostTestCase extends BaseHostJUnit4Test { 29 private int mCurrentUserId = NativeDevice.INVALID_USER_ID; 30 private static final String ERROR_MESSAGE_TAG = "[ERROR]"; 31 executeShellCommand(String cmd, Object... args)32 protected String executeShellCommand(String cmd, Object... args) throws Exception { 33 return getDevice().executeShellCommand(String.format(cmd, args)); 34 } 35 executeShellV2Command(String cmd, Object... args)36 protected CommandResult executeShellV2Command(String cmd, Object... args) throws Exception { 37 return getDevice().executeShellV2Command(String.format(cmd, args)); 38 } 39 isPackageInstalled(String packageName, String userId)40 protected boolean isPackageInstalled(String packageName, String userId) throws Exception { 41 return getDevice().isPackageInstalled(packageName, userId); 42 } 43 44 // TODO (b/174775905) remove after exposing the check from ITestDevice. isHeadlessSystemUserMode()45 protected boolean isHeadlessSystemUserMode() throws DeviceNotAvailableException { 46 String result = getDevice() 47 .executeShellCommand("getprop ro.fw.mu.headless_system_user").trim(); 48 return "true".equalsIgnoreCase(result); 49 } 50 isAtLeastS()51 protected boolean isAtLeastS() throws DeviceNotAvailableException { 52 return getDevice().getApiLevel() >= 31 /* BUILD.VERSION_CODES.S */; 53 } 54 eventually(ThrowingRunnable r, long timeoutMillis)55 protected static void eventually(ThrowingRunnable r, long timeoutMillis) { 56 long start = System.currentTimeMillis(); 57 58 while (true) { 59 try { 60 r.run(); 61 return; 62 } catch (Throwable e) { 63 if (System.currentTimeMillis() - start < timeoutMillis) { 64 try { 65 Thread.sleep(100); 66 } catch (InterruptedException ignored) { 67 throw new RuntimeException(e); 68 } 69 } else { 70 throw new RuntimeException(e); 71 } 72 } 73 } 74 } 75 getCurrentUserId()76 protected int getCurrentUserId() throws Exception { 77 setCurrentUserId(); 78 79 return mCurrentUserId; 80 } 81 isSuccessful(CommandResult result)82 protected boolean isSuccessful(CommandResult result) { 83 if (!CommandStatus.SUCCESS.equals(result.getStatus())) { 84 return false; 85 } 86 String stdout = result.getStdout(); 87 if (stdout.contains(ERROR_MESSAGE_TAG)) { 88 return false; 89 } 90 String stderr = result.getStderr(); 91 return (stderr == null || stderr.trim().isEmpty()); 92 } 93 setCurrentUserId()94 private void setCurrentUserId() throws Exception { 95 if (mCurrentUserId != NativeDevice.INVALID_USER_ID) return; 96 97 ITestDevice device = getDevice(); 98 mCurrentUserId = device.getCurrentUser(); 99 CLog.i("Current user: %d"); 100 } 101 102 protected interface ThrowingRunnable { 103 /** 104 * Similar to {@link Runnable#run} but has {@code throws Exception}. 105 */ run()106 void run() throws Exception; 107 } 108 } 109