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.server.am;
18 
19 import static android.server.am.Components.KeyguardDismissLoggerCallback.ENTRY_ON_DISMISS_CANCELLED;
20 import static android.server.am.Components.KeyguardDismissLoggerCallback.ENTRY_ON_DISMISS_ERROR;
21 import static android.server.am.Components.KeyguardDismissLoggerCallback.ENTRY_ON_DISMISS_SUCCEEDED;
22 import static android.server.am.Components.KeyguardDismissLoggerCallback.KEYGUARD_DISMISS_LOG_TAG;
23 import static android.server.am.StateLogger.log;
24 import static android.server.am.StateLogger.logAlways;
25 
26 import static org.junit.Assert.fail;
27 
28 import android.app.KeyguardManager;
29 import android.os.SystemClock;
30 
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33 
34 class KeyguardTestBase extends ActivityManagerTestBase {
35 
36     KeyguardManager mKeyguardManager;
37 
38     @Override
setUp()39     public void setUp() throws Exception {
40         super.setUp();
41         mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
42     }
43 
assertOnDismissSucceededInLogcat(LogSeparator logSeparator)44     void assertOnDismissSucceededInLogcat(LogSeparator logSeparator) {
45         assertInLogcat(KEYGUARD_DISMISS_LOG_TAG, ENTRY_ON_DISMISS_SUCCEEDED, logSeparator);
46     }
47 
assertOnDismissCancelledInLogcat(LogSeparator logSeparator)48     void assertOnDismissCancelledInLogcat(LogSeparator logSeparator) {
49         assertInLogcat(KEYGUARD_DISMISS_LOG_TAG, ENTRY_ON_DISMISS_CANCELLED, logSeparator);
50     }
51 
assertOnDismissErrorInLogcat(LogSeparator logSeparator)52     void assertOnDismissErrorInLogcat(LogSeparator logSeparator) {
53         assertInLogcat(KEYGUARD_DISMISS_LOG_TAG, ENTRY_ON_DISMISS_ERROR, logSeparator);
54     }
55 
assertInLogcat(String logTag, String entry, LogSeparator logSeparator)56     private void assertInLogcat(String logTag, String entry, LogSeparator logSeparator) {
57         final Pattern pattern = Pattern.compile("(.+)" + entry);
58         for (int retry = 1; retry <= 5; retry++) {
59             final String[] lines = getDeviceLogsForComponents(logSeparator, logTag);
60             for (int i = lines.length - 1; i >= 0; i--) {
61                 final String line = lines[i].trim();
62                 log(line);
63                 Matcher matcher = pattern.matcher(line);
64                 if (matcher.matches()) {
65                     return;
66                 }
67             }
68             logAlways("Waiting for " + entry + "... retry=" + retry);
69             SystemClock.sleep(500);
70         }
71         fail("Waiting for " + entry + " failed");
72     }
73 }
74