1 /*
2  * Copyright (C) 2020 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.settings.testutils;
18 
19 import android.os.ParcelFileDescriptor;
20 
21 import androidx.test.platform.app.InstrumentationRegistry;
22 
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.nio.charset.StandardCharsets;
27 import java.util.stream.Collectors;
28 
29 public class AdbUtils {
getCallerClassName()30     public static String getCallerClassName() {
31         StackTraceElement[] stElements = Thread.currentThread().getStackTrace();
32         for (int i = 1; i < stElements.length; i++) {
33             StackTraceElement ste = stElements[i];
34             if (!ste.getClassName().equals(new Object() {
35             }.getClass().getEnclosingClass().getName()) && ste.getClassName().indexOf(
36                     "java.lang.Thread") != 0) {
37                 return ste.getClassName();
38             }
39         }
40         return null;
41     }
42 
checkStringInAdbCommandOutput(String logTag, String command, String prefix, String target, int timeoutInMillis)43     public static boolean checkStringInAdbCommandOutput(String logTag, String command,
44             String prefix, String target, int timeoutInMillis) throws Exception {
45         long start = System.nanoTime();
46 
47         //Sometimes the change do no reflect in adn output immediately, so need a wait and poll here
48         while (System.nanoTime() - start < (timeoutInMillis * 1000000)) {
49             String result = shell(command);
50             if (result.contains(prefix == null ? "" : prefix)
51                     && result.contains(target == null ? "" : target)) {
52                 return true;
53             } else {
54                 Thread.sleep(100);
55             }
56         }
57 
58         return false;
59     }
60 
shell(String shellCommand)61     public static String shell(String shellCommand) {
62         String returnValue = "";
63         try (ParcelFileDescriptor.AutoCloseInputStream in =
64                      new ParcelFileDescriptor.AutoCloseInputStream(
65                              InstrumentationRegistry.getInstrumentation()
66                                      .getUiAutomation()
67                                      .executeShellCommand(shellCommand))) {
68             try (BufferedReader br =
69                          new BufferedReader(
70                                  new InputStreamReader(in, StandardCharsets.UTF_8))) {
71                 returnValue = br.lines().collect(Collectors.joining());
72             }
73         } catch (IOException e) {
74             e.printStackTrace();
75         }
76 
77         return returnValue;
78     }
79 }
80