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
17package android.server.am;
18
19import static android.server.am.Components.KeyguardDismissLoggerCallback.ENTRY_ON_DISMISS_CANCELLED;
20import static android.server.am.Components.KeyguardDismissLoggerCallback.ENTRY_ON_DISMISS_ERROR;
21import static android.server.am.Components.KeyguardDismissLoggerCallback.ENTRY_ON_DISMISS_SUCCEEDED;
22import static android.server.am.Components.KeyguardDismissLoggerCallback.KEYGUARD_DISMISS_LOG_TAG;
23import static android.server.am.StateLogger.log;
24import static android.server.am.StateLogger.logAlways;
25
26import static org.junit.Assert.fail;
27
28import android.app.KeyguardManager;
29import android.os.SystemClock;
30
31import java.util.regex.Matcher;
32import java.util.regex.Pattern;
33
34class KeyguardTestBase extends ActivityManagerTestBase {
35
36    KeyguardManager mKeyguardManager;
37
38    @Override
39    public void setUp() throws Exception {
40        super.setUp();
41        mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
42    }
43
44    void assertOnDismissSucceededInLogcat(LogSeparator logSeparator) {
45        assertInLogcat(KEYGUARD_DISMISS_LOG_TAG, ENTRY_ON_DISMISS_SUCCEEDED, logSeparator);
46    }
47
48    void assertOnDismissCancelledInLogcat(LogSeparator logSeparator) {
49        assertInLogcat(KEYGUARD_DISMISS_LOG_TAG, ENTRY_ON_DISMISS_CANCELLED, logSeparator);
50    }
51
52    void assertOnDismissErrorInLogcat(LogSeparator logSeparator) {
53        assertInLogcat(KEYGUARD_DISMISS_LOG_TAG, ENTRY_ON_DISMISS_ERROR, logSeparator);
54    }
55
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