Home | History | Annotate | Download | only in common
      1 /*
      2  * Copyright (C) 2017 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.autofillservice.cts.common;
     18 
     19 import androidx.annotation.NonNull;
     20 import android.support.test.InstrumentationRegistry;
     21 import android.text.TextUtils;
     22 import android.util.Log;
     23 import android.view.View;
     24 
     25 import com.android.compatibility.common.util.SystemUtil;
     26 
     27 /**
     28  * Provides Shell-based utilities such as running a command.
     29  */
     30 public final class ShellHelper {
     31 
     32     private static final String TAG = "ShellHelper";
     33 
     34     /**
     35      * Runs a Shell command, returning a trimmed response.
     36      */
     37     @NonNull
     38     public static String runShellCommand(@NonNull String template, Object...args) {
     39         final String command = String.format(template, args);
     40         Log.d(TAG, "runShellCommand(): " + command);
     41         try {
     42             final String result = SystemUtil
     43                     .runShellCommand(InstrumentationRegistry.getInstrumentation(), command);
     44             return TextUtils.isEmpty(result) ? "" : result.trim();
     45         } catch (Exception e) {
     46             throw new RuntimeException("Command '" + command + "' failed: ", e);
     47         }
     48     }
     49 
     50     /**
     51      * Tap on the view center, it may change window focus.
     52      */
     53     public static void tap(View view) {
     54         final int[] xy = new int[2];
     55         view.getLocationOnScreen(xy);
     56         final int viewWidth = view.getWidth();
     57         final int viewHeight = view.getHeight();
     58         final int x = (int) (xy[0] + (viewWidth / 2.0f));
     59         final int y = (int) (xy[1] + (viewHeight / 2.0f));
     60 
     61         runShellCommand("input touchscreen tap %d %d", x, y);
     62     }
     63 
     64 
     65     private ShellHelper() {
     66         throw new UnsupportedOperationException("contain static methods only");
     67     }
     68 }
     69