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", "*: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                 testString.append(line.split(":")[1].trim());
56             }
57         }
58         device.executeAdbCommand("logcat", "-c");
59         return testString.toString();
60     }
61 
waitForLog( ITestDevice device, String tag, int waitSeconds, String expectedOutput)62     public static void waitForLog(
63             ITestDevice device, String tag, int waitSeconds, String expectedOutput)
64             throws Exception {
65         long timeoutMillis = waitSeconds * 1000;
66         long startTime = System.currentTimeMillis();
67         long endTime = startTime;
68 
69         while ((endTime - startTime <= timeoutMillis)) {
70             String testString = getLog(device, tag);
71             if (testString.contains(expectedOutput)) {
72                 return;
73             }
74             endTime = System.currentTimeMillis();
75         }
76 
77         throw new Exception("Timed out, could not find the log message.");
78     }
79 
assertLog(ITestDevice device, String tag, String ...expectedOutput)80     public static void assertLog(ITestDevice device, String tag, String ...expectedOutput)
81             throws Exception {
82         String testString = getLog(device, tag);
83         List<String> expectedOutputs = new ArrayList<>(Arrays.asList(expectedOutput));
84         assertThat(testString).isIn(expectedOutputs);
85     }
86 
87     /** Skip the test if the expectedOutput was not found in the device logs. */
assumeLog(ITestDevice device, String tag, String expectedOutput)88     public static void assumeLog(ITestDevice device, String tag, String expectedOutput)
89             throws Exception {
90         String testString = getLog(device, tag);
91         assumeTrue(
92                 "Skip the test since "
93                         + expectedOutput
94                         + " message is not found in the device logs.",
95                 testString.contains(expectedOutput));
96     }
97 
98     /** This method will return the DUT volume. */
parseDutVolume(ITestDevice device, String tag)99     public static int parseDutVolume(ITestDevice device, String tag) throws Exception {
100         String testString = getLog(device, tag);
101         assertWithMessage("No log from Audio Manager which reports the DUT volume percentage")
102                 .that(testString)
103                 .isNotEqualTo("");
104         try {
105             String volume = testString.split("at")[1].trim().replaceAll("%", "");
106             return Integer.parseInt(volume);
107         } catch (NumberFormatException e) {
108             throw new NumberFormatException(
109                     "Volume obtained from Audio Manager can" + "not be parsed");
110         }
111     }
112 
assertLogDoesNotContain(ITestDevice device, String tag, String expectedOutput)113     public static void assertLogDoesNotContain(ITestDevice device, String tag,
114                                                String expectedOutput) throws Exception {
115         String testString = getLog(device, tag);
116         assertThat(testString).doesNotContain(expectedOutput);
117     }
118 
getSupportedAudioFormats(ITestDevice device)119     public static List<Integer> getSupportedAudioFormats(ITestDevice device) throws Exception {
120         TimeUnit.SECONDS.sleep(WAIT_TIME);
121         String logs =
122                 device.executeAdbCommand("logcat", "-v", "brief", "-d", SAD_READER + ":I", "*:S");
123         // Search for string.
124         String testString = "";
125         Scanner in = new Scanner(logs);
126         List<Integer> mSupportedAudioFormats = null;
127         while (in.hasNextLine()) {
128             String line = in.nextLine();
129             if (line.startsWith("I/" + SAD_READER)) {
130                 testString = line.split(":")[1].trim();
131                 if (testString.equals(SAD_CONFIGURATION_MARKER)) {
132                     List<String> mFormatsLine =
133                             Arrays.asList(in.nextLine().split(":")[1].trim().split(", "));
134                     List<String> mCodecSADsLine =
135                             Arrays.asList(in.nextLine().split(":")[1].trim().split(", "));
136                     mSupportedAudioFormats =
137                             Lists.transform(mFormatsLine, fl -> Integer.parseInt(fl));
138                 }
139             }
140         }
141         device.executeAdbCommand("logcat", "-c");
142         assumeTrue(testString.equals(SAD_CONFIGURATION_MARKER));
143         return mSupportedAudioFormats;
144     }
145 }
146