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 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; 19 20 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; 21 22 import static org.junit.Assert.assertTrue; 23 24 import android.content.Context; 25 import android.hardware.display.DisplayManager; 26 import android.platform.test.annotations.Presubmit; 27 import android.view.Display; 28 import android.view.WindowManager; 29 30 import com.android.compatibility.common.util.ApiTest; 31 32 import org.junit.Test; 33 34 import java.util.Objects; 35 import java.util.concurrent.CountDownLatch; 36 import java.util.concurrent.TimeUnit; 37 import java.util.function.IntConsumer; 38 39 @Presubmit 40 public class WindowManagerTests { 41 42 @Test 43 @ApiTest(apis = {"android.view.WindowManager#addProposedRotationListener", 44 "android.view.WindowManager#removeProposedRotationListener"}) testProposedRotationListener()45 public void testProposedRotationListener() throws InterruptedException { 46 final Context context = getInstrumentation().getContext(); 47 final Display display = Objects.requireNonNull( 48 context.getSystemService(DisplayManager.class)).getDisplay(Display.DEFAULT_DISPLAY); 49 final Context windowContext = context.createWindowContext(display, 50 TYPE_APPLICATION_OVERLAY, null /* options */); 51 final CountDownLatch latch = new CountDownLatch(1); 52 final WindowManager wm = Objects.requireNonNull( 53 windowContext.getSystemService(WindowManager.class)); 54 final IntConsumer listener = rotation -> latch.countDown(); 55 wm.addProposedRotationListener(context.getMainExecutor(), listener); 56 try { 57 assertTrue("Timed out while waiting for proposed rotation to be received", 58 latch.await(5, TimeUnit.SECONDS)); 59 } finally { 60 wm.removeProposedRotationListener(listener); 61 } 62 } 63 } 64