1 /* 2 * Copyright (C) 2022 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.car.cts.builtin; 18 19 import com.android.tradefed.device.ITestDevice; 20 21 /** 22 * Base class for all Car Builtin API test related shell command invocation. 23 * 24 * It is the extended subclass command to construct the shell command string and decide 25 * if the command's return is successful or not. Further, it is the extended subclass command 26 * to extra all necessary information from the return string if needed. 27 */ 28 public abstract class CtsCarShellCommand { 29 private final ITestDevice mDevice; 30 31 protected final String mCommand; 32 protected String[] mCommandArgs; 33 protected String mCommandReturn; 34 CtsCarShellCommand(String commandName, ITestDevice device)35 protected CtsCarShellCommand(String commandName, ITestDevice device) { 36 mCommand = commandName; 37 mDevice = device; 38 } 39 executeWith(String... args)40 public CtsCarShellCommand executeWith(String... args) throws Exception { 41 mCommandArgs = args; 42 43 String cmd = mCommand; 44 if (mCommandArgs != null && mCommandArgs.length > 0) { 45 cmd = mCommand + " " + String.join(" ", mCommandArgs); 46 } 47 mCommandReturn = mDevice.executeShellCommand(cmd).trim(); 48 parseCommandReturn(); 49 return this; 50 } 51 returnStartsWith(String str)52 public boolean returnStartsWith(String str) throws Exception { 53 if (mCommandReturn == null) { 54 throw new Exception("command return is null. not executed?"); 55 } 56 return mCommandReturn.startsWith(str); 57 } 58 parseCommandReturn()59 protected abstract void parseCommandReturn() throws Exception; 60 } 61