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 import android.view.Display;
26 
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             ReportedDisplayMetrics.getDisplayMetrics(Display.DEFAULT_DISPLAY);
46 
47     @Override
48     @Before
setUp()49     public void setUp() throws Exception {
50         super.setUp();
51         assumeActivityEmbeddingSupportedDevice();
52 
53         mActivityEmbeddingComponent = getWindowExtensions().getActivityEmbeddingComponent();
54         mSplitInfoConsumer = new TestValueCountConsumer<>();
55         mActivityEmbeddingComponent.setSplitInfoCallback(mSplitInfoConsumer);
56         // The splitInfoCallback will be triggered once upon register, so clear the queue before
57         // test starts.
58         mSplitInfoConsumer.clearQueue();
59 
60         UiDeviceUtils.pressWakeupButton();
61         UiDeviceUtils.pressUnlockButton();
62     }
63 
64     @Override
65     @After
tearDown()66     public void tearDown() throws Throwable {
67         super.tearDown();
68         mReportedDisplayMetrics.restoreDisplayMetrics();
69         if (mActivityEmbeddingComponent != null) {
70             mActivityEmbeddingComponent.setEmbeddingRules(Collections.emptySet());
71             mActivityEmbeddingComponent.clearActivityStackAttributesCalculator();
72             mActivityEmbeddingComponent.clearEmbeddedActivityWindowInfoCallback();
73             mActivityEmbeddingComponent.clearSplitAttributesCalculator();
74             mActivityEmbeddingComponent.clearSplitInfoCallback();
75         }
76     }
77 }
78