1 /* 2 * Copyright (C) 2023 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 com.android.tests.chromium.host; 18 19 import com.android.tradefed.util.Pair; 20 21 import java.util.ArrayList; 22 import java.util.List; 23 24 public class InstrumentationCommandBuilder { 25 private final List<Pair<String, String>> arguments; 26 private final String activityName; 27 // Instrument and wait until execution has finished before returning 28 private static final String BASE_CMD = "am instrument -w --no-isolated-storage "; 29 InstrumentationCommandBuilder(String activity)30 public InstrumentationCommandBuilder(String activity) { 31 this.activityName = activity; 32 this.arguments = new ArrayList<>(); 33 } 34 addArgument(String key, String value)35 public InstrumentationCommandBuilder addArgument(String key, String value) { 36 arguments.add(new Pair<>(key, value)); 37 return this; 38 } 39 40 appendTupleToCommand(StringBuilder cmd, String key, String value)41 private void appendTupleToCommand(StringBuilder cmd, String key, String value) { 42 cmd.append("-e "); 43 cmd.append(key).append(" ").append(value).append(" "); 44 } 45 build()46 public String build() { 47 StringBuilder commandAsString = new StringBuilder(BASE_CMD); 48 for (Pair<String, String> arg : arguments) { 49 appendTupleToCommand(commandAsString, arg.first, arg.second); 50 } 51 commandAsString.append(activityName); 52 return commandAsString.toString(); 53 } 54 } 55 56