1 /* 2 * Copyright (C) 2023 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 package android.server.wm; 17 18 19 import static org.junit.Assert.assertThrows; 20 import static org.junit.Assume.assumeNoException; 21 22 import android.platform.test.annotations.Presubmit; 23 24 import org.junit.Test; 25 import org.junit.function.ThrowingRunnable; 26 27 import java.lang.reflect.InvocationTargetException; 28 import java.lang.reflect.Method; 29 30 @Presubmit 31 public class WindowManagerReflectionTests { 32 33 /** Regression test for b/273906410. */ 34 @Test requestAppKeyboardShortcuts_requiresPermission_b273906410()35 public void requestAppKeyboardShortcuts_requiresPermission_b273906410() throws Throwable { 36 Object wms = Class.forName("android.view.WindowManagerGlobal") 37 .getMethod("getWindowManagerService") 38 .invoke(null); 39 40 assertThrows(SecurityException.class, () -> { 41 runAndUnwrapTargetException(() -> { 42 Class.forName("android.view.IWindowManager") 43 .getMethod("requestAppKeyboardShortcuts", 44 Class.forName("com.android.internal.os.IResultReceiver"), 45 Integer.TYPE) 46 .invoke(wms, null, 0); 47 }); 48 }); 49 } 50 51 @Test requestImeKeyboardShortcuts_requiresPermission()52 public void requestImeKeyboardShortcuts_requiresPermission() throws Throwable { 53 Object wms = Class.forName("android.view.WindowManagerGlobal") 54 .getMethod("getWindowManagerService") 55 .invoke(null); 56 try { 57 Method method = Class.forName("android.view.IWindowManager") 58 .getMethod("requestImeKeyboardShortcuts", 59 Class.forName("com.android.internal.os.IResultReceiver"), 60 Integer.TYPE); 61 assertThrows(SecurityException.class, 62 () -> runAndUnwrapTargetException(() -> method.invoke(wms, null, 0))); 63 } catch (NoSuchMethodException e) { 64 // Ignores this test if the method is not available. 65 assumeNoException(e); 66 } 67 } 68 runAndUnwrapTargetException(ThrowingRunnable r)69 private void runAndUnwrapTargetException(ThrowingRunnable r) throws Throwable { 70 try { 71 r.run(); 72 } catch (InvocationTargetException e) { 73 throw e.getTargetException(); 74 } 75 } 76 } 77