• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.hdmicec.cts;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static com.google.common.truth.Truth.assertWithMessage;
21 
22 import static org.junit.Assume.assumeTrue;
23 
24 import com.android.tradefed.device.ITestDevice;
25 
26 import com.google.common.collect.Lists;
27 
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.Scanner;
32 import java.util.concurrent.TimeUnit;
33 
34 /** Helper class to get for device logcat. */
35 public final class LogHelper {
36 
37     private static final int WAIT_TIME = 10;
38 
39     /**
40      * The tag of the SadConfigurationReaderTest launched by the APK.
41      */
42     private static final String SAD_READER = "SadConfigurationReaderTest";
43 
44     private static final String SAD_CONFIGURATION_MARKER = "Supported Audio Formats";
45 
getLog(ITestDevice device, String tag)46     private static String getLog(ITestDevice device, String tag) throws Exception {
47         TimeUnit.SECONDS.sleep(WAIT_TIME);
48         String logs = device.executeAdbCommand("logcat", "-v", "brief", "-d", tag + ":I", tag + ":V", "*:S");
49         // Search for string.
50         StringBuilder testString = new StringBuilder();
51         Scanner in = new Scanner(logs);
52         while (in.hasNextLine()) {
53             String line = in.nextLine();
54             if (line.startsWith("I/" + tag)
55                     || line.startsWith("V/" + tag)) {
56                 testString.append(line.split(":")[1].trim());
57             }
58         }
59         device.executeAdbCommand("logcat", "-c");
60         return testString.toString();
61     }
62 
waitForLog( ITestDevice device, String tag, int waitSeconds, String expectedOutput)63     public static void waitForLog(
64             ITestDevice device, String tag, int waitSeconds, String expectedOutput)
65             throws Exception {
66         long timeoutMillis = waitSeconds * 1000;
67         long startTime = System.currentTimeMillis();
68         long endTime = startTime;
69 
70         while ((endTime - startTime <= timeoutMillis)) {
71             String testString = getLog(device, tag);
72             if (testString.contains(expectedOutput)) {
73                 return;
74             }
75             endTime = System.currentTimeMillis();
76         }
77 
78         throw new Exception("Timed out, could not find the log message.");
79     }
80 
assertLog(ITestDevice device, String tag, String ...expectedOutput)81     public static void assertLog(ITestDevice device, String tag, String ...expectedOutput)
82             throws Exception {
83         String testString = getLog(device, tag);
84         List<String> expectedOutputs = new ArrayList<>(Arrays.asList(expectedOutput));
85         assertThat(testString).isIn(expectedOutputs);
86     }
87 
88     /** Skip the test if the expectedOutput was not found in the device logs. */
assumeLog(ITestDevice device, String tag, String expectedOutput)89     public static void assumeLog(ITestDevice device, String tag, String expectedOutput)
90             throws Exception {
91         String testString = getLog(device, tag);
92         assumeTrue(
93                 "Skip the test since "
94                         + expectedOutput
95                         + " message is not found in the device logs.",
96                 testString.contains(expectedOutput));
97     }
98 
99     /** This method will return the DUT volume. */
parseDutVolume(ITestDevice device, String tag)100     public static int parseDutVolume(ITestDevice device, String tag) throws Exception {
101         String testString = getLog(device, tag);
102         assertWithMessage("No log from Audio Manager which reports the DUT volume percentage")
103                 .that(testString)
104                 .isNotEqualTo("");
105         try {
106             String volume = testString.split("at")[1].trim().replaceAll("%", "");
107             return Integer.parseInt(volume);
108         } catch (NumberFormatException e) {
109             throw new NumberFormatException(
110                     "Volume obtained from Audio Manager can" + "not be parsed");
111         }
112     }
113 
assertLogDoesNotContain(ITestDevice device, String tag, String expectedOutput)114     public static void assertLogDoesNotContain(ITestDevice device, String tag,
115                                                String expectedOutput) throws Exception {
116         String testString = getLog(device, tag);
117         assertThat(testString).doesNotContain(expectedOutput);
118     }
119 
getSupportedAudioFormats(ITestDevice device)120     public static List<Integer> getSupportedAudioFormats(ITestDevice device) throws Exception {
121         TimeUnit.SECONDS.sleep(WAIT_TIME);
122         String logs =
123                 device.executeAdbCommand("logcat", "-v", "brief", "-d", SAD_READER + ":I", "*:S");
124         // Search for string.
125         String testString = "";
126         Scanner in = new Scanner(logs);
127         List<Integer> mSupportedAudioFormats = null;
128         while (in.hasNextLine()) {
129             String line = in.nextLine();
130             if (line.startsWith("I/" + SAD_READER)) {
131                 testString = line.split(":")[1].trim();
132                 if (testString.equals(SAD_CONFIGURATION_MARKER)) {
133                     List<String> mFormatsLine =
134                             Arrays.asList(in.nextLine().split(":")[1].trim().split(", "));
135                     List<String> mCodecSADsLine =
136                             Arrays.asList(in.nextLine().split(":")[1].trim().split(", "));
137                     mSupportedAudioFormats =
138                             Lists.transform(mFormatsLine, fl -> Integer.parseInt(fl));
139                 }
140             }
141         }
142         device.executeAdbCommand("logcat", "-c");
143         assumeTrue(testString.equals(SAD_CONFIGURATION_MARKER));
144         return mSupportedAudioFormats;
145     }
146 }
147