1 /* 2 * Copyright (C) 2020 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.appsecurity.cts; 18 19 import static org.junit.Assert.fail; 20 21 import com.android.server.wm.KeyguardServiceDelegateProto; 22 import com.android.server.wm.WindowManagerServiceDumpProto; 23 import com.android.tradefed.device.CollectingByteOutputReceiver; 24 import com.android.tradefed.device.ITestDevice; 25 import com.android.tradefed.log.LogUtil.CLog; 26 27 import com.google.protobuf.MessageLite; 28 import com.google.protobuf.Parser; 29 30 class LockScreenInspector { 31 private static final String DUMPSYS_WINDOW = "dumpsys window --proto"; 32 33 private Boolean mKeyguardShowingAndNotOccluded; 34 private final ITestDevice mDevice; 35 LockScreenInspector(ITestDevice device)36 private LockScreenInspector(ITestDevice device) { 37 mDevice = device; 38 } 39 newInstance(ITestDevice device)40 static LockScreenInspector newInstance(ITestDevice device) { 41 LockScreenInspector inspector = new LockScreenInspector(device); 42 inspector.checkCurrentState(); 43 return inspector; 44 } 45 isDisplayedAndNotOccluded()46 boolean isDisplayedAndNotOccluded() { 47 return mKeyguardShowingAndNotOccluded; 48 } 49 checkCurrentState()50 private void checkCurrentState() { 51 int retriesLeft = 3; 52 boolean retry = false; 53 54 do { 55 if (retry) { 56 CLog.w("***Retrying dump****"); 57 try { 58 Thread.sleep(500); 59 } catch (InterruptedException ignored) { 60 } 61 } 62 try { 63 WindowManagerServiceDumpProto dumpProto = getDump( 64 WindowManagerServiceDumpProto.parser(), DUMPSYS_WINDOW); 65 KeyguardServiceDelegateProto keyguardProto = 66 dumpProto.getPolicy().getKeyguardDelegate(); 67 mKeyguardShowingAndNotOccluded = keyguardProto.getShowing() 68 && !keyguardProto.getOccluded(); 69 retry = false; 70 } catch (Exception e) { 71 CLog.w(e); 72 retry = true; 73 } 74 } while (retry && retriesLeft-- > 0); 75 76 if (retry) { 77 fail("Could not obtain lockscreen state"); 78 } 79 } 80 getDump(Parser<T> parser, String command)81 private <T extends MessageLite> T getDump(Parser<T> parser, String command) throws Exception { 82 final CollectingByteOutputReceiver receiver = new CollectingByteOutputReceiver(); 83 mDevice.executeShellCommand(command, receiver); 84 return parser.parseFrom(receiver.getOutput()); 85 } 86 } 87