1 /*
2  * Copyright (C) 2022 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 androidx.test.uiautomator;
18 
19 import android.util.Log;
20 
21 import androidx.annotation.NonNull;
22 import androidx.test.platform.app.InstrumentationRegistry;
23 
24 import java.io.IOException;
25 import java.time.Duration;
26 import java.util.Date;
27 
28 /** A helper class to wait the specific log appear in the logcat logs. */
29 public class LogcatWaitMixin extends WaitMixin<UiDevice> {
30 
31     private static final String LOG_TAG = LogcatWaitMixin.class.getSimpleName();
32 
LogcatWaitMixin()33     public LogcatWaitMixin() {
34         this(UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()));
35     }
36 
LogcatWaitMixin(UiDevice device)37     public LogcatWaitMixin(UiDevice device) {
38         super(device);
39     }
40 
41     /**
42      * Waits the {@code specificLog} appear in the logcat logs after the specific {@code startTime}.
43      *
44      * @param waitTime the maximum time for waiting
45      * @return true if the specific log appear within timeout and after the startTime
46      */
waitForSpecificLog( @onNull String specificLog, @NonNull Date startTime, @NonNull Duration waitTime)47     public boolean waitForSpecificLog(
48             @NonNull String specificLog, @NonNull Date startTime, @NonNull Duration waitTime) {
49         return wait(createWaitCondition(specificLog, startTime), waitTime.toMillis());
50     }
51 
52     @NonNull
createWaitCondition( @onNull String specificLog, @NonNull Date startTime)53     Condition<UiDevice, Boolean> createWaitCondition(
54             @NonNull String specificLog, @NonNull Date startTime) {
55         return new Condition<UiDevice, Boolean>() {
56             @Override
57             public Boolean apply(UiDevice device) {
58                 String logcatLogs;
59                 try {
60                     logcatLogs = device.executeShellCommand("logcat -v time -v year -d");
61                 } catch (IOException e) {
62                     Log.e(LOG_TAG, "Fail to dump logcat logs on the device!", e);
63                     return Boolean.FALSE;
64                 }
65                 return !LogcatParser.INSTANCE
66                         .findSpecificLogAfter(logcatLogs, specificLog, startTime)
67                         .isEmpty();
68             }
69         };
70     }
71 }
72