Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2015 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.accessibilityservice.cts;
     18 
     19 import android.app.UiAutomation;
     20 import android.os.ParcelFileDescriptor;
     21 import android.test.InstrumentationTestCase;
     22 
     23 import java.io.BufferedReader;
     24 import java.io.FileInputStream;
     25 import java.io.IOException;
     26 import java.io.InputStream;
     27 import java.io.InputStreamReader;
     28 import java.nio.charset.StandardCharsets;
     29 import java.util.LinkedList;
     30 
     31 public class ShellCommandBuilder {
     32     private final LinkedList<String> mCommands = new LinkedList<>();
     33 
     34     private final InstrumentationTestCase mTestCase;
     35 
     36     public static ShellCommandBuilder create(InstrumentationTestCase testCase) {
     37         return new ShellCommandBuilder(testCase);
     38     }
     39 
     40     private ShellCommandBuilder(InstrumentationTestCase testCase) {
     41         mTestCase = testCase;
     42     }
     43 
     44     public void run() {
     45         final UiAutomation automation = mTestCase.getInstrumentation().getUiAutomation(
     46                 UiAutomation.FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES);
     47         for (String command : mCommands) {
     48             execShellCommand(automation, command);
     49         }
     50     }
     51 
     52     public ShellCommandBuilder deleteSecureSetting(String name) {
     53         mCommands.add("settings delete secure " + name);
     54         return this;
     55     }
     56 
     57     public ShellCommandBuilder putSecureSetting(String name, String value) {
     58         mCommands.add("settings put secure " + name + " " + value);
     59         return this;
     60     }
     61 
     62     public ShellCommandBuilder grantPermission(String packageName, String permission) {
     63         mCommands.add("pm grant " + packageName + " " + permission);
     64         return this;
     65     }
     66 
     67     public ShellCommandBuilder addCommand(String command) {
     68         mCommands.add(command);
     69         return this;
     70     }
     71 
     72     private static void execShellCommand(UiAutomation automation, String command) {
     73         try (ParcelFileDescriptor fd = automation.executeShellCommand(command)) {
     74             try (InputStream inputStream = new FileInputStream(fd.getFileDescriptor())) {
     75                 try (BufferedReader reader = new BufferedReader(
     76                         new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
     77                     while (reader.readLine() != null) {
     78                         // Keep reading.
     79                     }
     80                 }
     81             }
     82         } catch (IOException e) {
     83             throw new RuntimeException("Failed to exec shell command", e);
     84         }
     85     }
     86 }
     87