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