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 17 package android.server.wm.jetpack.embedding; 18 19 import static android.server.wm.jetpack.extensions.util.ExtensionsUtil.getWindowExtensions; 20 import static android.server.wm.jetpack.utils.ActivityEmbeddingUtil.assumeActivityEmbeddingSupportedDevice; 21 22 import android.server.wm.UiDeviceUtils; 23 import android.server.wm.jetpack.extensions.util.TestValueCountConsumer; 24 import android.server.wm.jetpack.utils.WindowManagerJetpackTestBase; 25 26 import androidx.annotation.Nullable; 27 import androidx.window.extensions.WindowExtensions; 28 import androidx.window.extensions.embedding.ActivityEmbeddingComponent; 29 import androidx.window.extensions.embedding.ActivityStack; 30 import androidx.window.extensions.embedding.SplitInfo; 31 32 import org.junit.After; 33 import org.junit.Before; 34 35 import java.util.Collections; 36 import java.util.List; 37 38 /** 39 * Base test class for the {@link androidx.window.extensions} implementation provided on the device 40 * (and only if one is available) for the Activity Embedding functionality. 41 */ 42 public class ActivityEmbeddingTestBase extends WindowManagerJetpackTestBase { 43 44 protected ActivityEmbeddingComponent mActivityEmbeddingComponent; 45 protected TestValueCountConsumer<List<SplitInfo>> mSplitInfoConsumer; 46 protected TestValueCountConsumer<List<ActivityStack>> mActivityStackCallback; 47 protected ReportedDisplayMetrics mReportedDisplayMetrics; 48 49 @Override 50 @Before setUp()51 public void setUp() throws Exception { 52 super.setUp(); 53 assumeActivityEmbeddingSupportedDevice(); 54 final int displayId = getLaunchingDisplayId() != null 55 ? getLaunchingDisplayId() : getMainDisplayId(); 56 mReportedDisplayMetrics = ReportedDisplayMetrics.getDisplayMetrics(displayId); 57 58 final WindowExtensions windowExtensions = getWindowExtensions(); 59 mActivityEmbeddingComponent = windowExtensions.getActivityEmbeddingComponent(); 60 61 mSplitInfoConsumer = new TestValueCountConsumer<>(); 62 mActivityEmbeddingComponent.setSplitInfoCallback(mSplitInfoConsumer); 63 // The splitInfoCallback will be triggered once upon register, so clear the queue before 64 // test starts. 65 mSplitInfoConsumer.clearQueue(); 66 67 mActivityStackCallback = new TestValueCountConsumer<>(); 68 mActivityEmbeddingComponent.registerActivityStackCallback( 69 Runnable::run, mActivityStackCallback); 70 // The ActivityStackCallback will be triggered once upon register, so clear the queue before 71 // test starts. 72 mActivityStackCallback.clearQueue(); 73 74 UiDeviceUtils.pressWakeupButton(); 75 UiDeviceUtils.pressUnlockButton(); 76 } 77 78 @Override 79 @After tearDown()80 public void tearDown() throws Throwable { 81 super.tearDown(); 82 if (mReportedDisplayMetrics != null) { 83 mReportedDisplayMetrics.restoreDisplayMetrics(); 84 } 85 if (mActivityEmbeddingComponent != null) { 86 mActivityEmbeddingComponent.setEmbeddingRules(Collections.emptySet()); 87 mActivityEmbeddingComponent.clearActivityStackAttributesCalculator(); 88 mActivityEmbeddingComponent.clearEmbeddedActivityWindowInfoCallback(); 89 mActivityEmbeddingComponent.clearSplitAttributesCalculator(); 90 mActivityEmbeddingComponent.clearSplitInfoCallback(); 91 } 92 } 93 94 /** 95 * Used to specify the display ID of the launching activity. 96 * {@code null} to use the system default. 97 */ 98 @Nullable getLaunchingDisplayId()99 public Integer getLaunchingDisplayId() { 100 return null; 101 } 102 } 103