Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2016 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.provider.cts;
     18 
     19 import android.app.UiAutomation;
     20 import android.os.ParcelFileDescriptor;
     21 
     22 import java.io.BufferedReader;
     23 import java.io.FileInputStream;
     24 import java.io.IOException;
     25 import java.io.InputStream;
     26 import java.io.InputStreamReader;
     27 import java.nio.charset.StandardCharsets;
     28 import java.util.Objects;
     29 import java.util.regex.Matcher;
     30 import java.util.regex.Pattern;
     31 
     32 /**
     33  * Utility methods for provider cts tests.
     34  */
     35 public class ProviderTestUtils {
     36 
     37     private static final int BACKUP_TIMEOUT_MILLIS = 4000;
     38     private static final Pattern BMGR_ENABLED_PATTERN = Pattern.compile(
     39             "^Backup Manager currently (enabled|disabled)$");
     40 
     41     static void setDefaultSmsApp(boolean setToSmsApp, String packageName, UiAutomation uiAutomation)
     42             throws Exception {
     43         String command = String.format(
     44                 "appops set %s WRITE_SMS %s", packageName, setToSmsApp ? "allow" : "default");
     45         executeShellCommand(command, uiAutomation);
     46     }
     47 
     48     static String executeShellCommand(String command, UiAutomation uiAutomation)
     49             throws IOException {
     50         ParcelFileDescriptor pfd = uiAutomation.executeShellCommand(command.toString());
     51         BufferedReader br = null;
     52         try (InputStream in = new FileInputStream(pfd.getFileDescriptor());) {
     53             br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
     54             String str = null;
     55             StringBuilder out = new StringBuilder();
     56             while ((str = br.readLine()) != null) {
     57                 out.append(str);
     58             }
     59             return out.toString();
     60         } finally {
     61             if (br != null) {
     62                 br.close();
     63             }
     64         }
     65     }
     66 
     67     static String setBackupTransport(String transport, UiAutomation uiAutomation) throws Exception {
     68         String output = executeShellCommand("bmgr transport " + transport, uiAutomation);
     69         Pattern pattern = Pattern.compile("\\(formerly (.*)\\)$");
     70         Matcher matcher = pattern.matcher(output);
     71         if (matcher.find()) {
     72             return matcher.group(1);
     73         } else {
     74             throw new Exception("non-parsable output setting bmgr transport: " + output);
     75         }
     76     }
     77 
     78     static boolean setBackupEnabled(boolean enable, UiAutomation uiAutomation) throws Exception {
     79         // Check to see the previous state of the backup service
     80         boolean previouslyEnabled = false;
     81         String output = executeShellCommand("bmgr enabled", uiAutomation);
     82         Matcher matcher = BMGR_ENABLED_PATTERN.matcher(output.trim());
     83         if (matcher.find()) {
     84             previouslyEnabled = "enabled".equals(matcher.group(1));
     85         } else {
     86             throw new RuntimeException("Backup output format changed.  No longer matches"
     87                     + " expected regex: " + BMGR_ENABLED_PATTERN + "\nactual: '" + output + "'");
     88         }
     89 
     90         executeShellCommand("bmgr enable " + enable, uiAutomation);
     91         return previouslyEnabled;
     92     }
     93 
     94     static boolean hasBackupTransport(String transport, UiAutomation uiAutomation)
     95             throws Exception {
     96         String output = executeShellCommand("bmgr list transports", uiAutomation);
     97         for (String t : output.split(" ")) {
     98             if ("*".equals(t)) {
     99                 // skip the current selection marker.
    100                 continue;
    101             } else if (Objects.equals(transport, t)) {
    102                 return true;
    103             }
    104         }
    105         return false;
    106     }
    107 
    108     static void runBackup(String packageName, UiAutomation uiAutomation) throws Exception {
    109         executeShellCommand("bmgr backupnow " + packageName, uiAutomation);
    110         Thread.sleep(BACKUP_TIMEOUT_MILLIS);
    111     }
    112 
    113     static void runRestore(String packageName, UiAutomation uiAutomation) throws Exception {
    114         executeShellCommand("bmgr restore 1 " + packageName, uiAutomation);
    115         Thread.sleep(BACKUP_TIMEOUT_MILLIS);
    116     }
    117 
    118     static void wipeBackup(String backupTransport, String packageName, UiAutomation uiAutomation)
    119             throws Exception {
    120         executeShellCommand("bmgr wipe " + backupTransport + " " + packageName, uiAutomation);
    121     }
    122 }
    123