1 /*
2  * Copyright (C) 2024 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 com.android.wm.shell.pip2.phone;
18 
19 import android.os.Bundle;
20 import android.os.Handler;
21 import android.os.Parcelable;
22 import android.testing.AndroidTestingRunner;
23 
24 import com.android.wm.shell.ShellTestCase;
25 
26 import junit.framework.Assert;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 
33 /**
34  * Unit test against {@link PipTransitionState}.
35  *
36  * This test mocks the PiP2 flag to be true.
37  */
38 @RunWith(AndroidTestingRunner.class)
39 public class PipTransitionStateTest extends ShellTestCase {
40     private static final String EXTRA_ENTRY_KEY = "extra_entry_key";
41     private PipTransitionState mPipTransitionState;
42     private PipTransitionState.PipTransitionStateChangedListener mStateChangedListener;
43     private Parcelable mEmptyParcelable;
44 
45     @Mock
46     private Handler mMainHandler;
47 
48     @Before
setUp()49     public void setUp() {
50         mPipTransitionState = new PipTransitionState(mMainHandler);
51         mPipTransitionState.setState(PipTransitionState.UNDEFINED);
52         mEmptyParcelable = new Bundle();
53     }
54 
55     @Test
testEnteredState_withoutExtra()56     public void testEnteredState_withoutExtra() {
57         mStateChangedListener = (oldState, newState, extra) -> {
58             Assert.assertEquals(PipTransitionState.ENTERED_PIP, newState);
59             Assert.assertNull(extra);
60         };
61         mPipTransitionState.addPipTransitionStateChangedListener(mStateChangedListener);
62         mPipTransitionState.setState(PipTransitionState.ENTERED_PIP);
63         mPipTransitionState.removePipTransitionStateChangedListener(mStateChangedListener);
64     }
65 
66     @Test
testEnteredState_withExtra()67     public void testEnteredState_withExtra() {
68         mStateChangedListener = (oldState, newState, extra) -> {
69             Assert.assertEquals(PipTransitionState.ENTERED_PIP, newState);
70             Assert.assertNotNull(extra);
71             Assert.assertEquals(mEmptyParcelable, extra.getParcelable(EXTRA_ENTRY_KEY));
72         };
73         Bundle extra = new Bundle();
74         extra.putParcelable(EXTRA_ENTRY_KEY, mEmptyParcelable);
75 
76         mPipTransitionState.addPipTransitionStateChangedListener(mStateChangedListener);
77         mPipTransitionState.setState(PipTransitionState.ENTERED_PIP, extra);
78         mPipTransitionState.removePipTransitionStateChangedListener(mStateChangedListener);
79     }
80 
81     @Test(expected = IllegalArgumentException.class)
testEnteringState_withoutExtra()82     public void testEnteringState_withoutExtra() {
83         mPipTransitionState.setState(PipTransitionState.ENTERING_PIP);
84     }
85 
86     @Test(expected = IllegalArgumentException.class)
testSwipingToPipState_withoutExtra()87     public void testSwipingToPipState_withoutExtra() {
88         mPipTransitionState.setState(PipTransitionState.SWIPING_TO_PIP);
89     }
90 
91     @Test
testCustomState_withExtra_thenEntered_withoutExtra()92     public void testCustomState_withExtra_thenEntered_withoutExtra() {
93         final int customState = mPipTransitionState.getCustomState();
94         mStateChangedListener = (oldState, newState, extra) -> {
95             if (newState == customState) {
96                 Assert.assertNotNull(extra);
97                 Assert.assertEquals(mEmptyParcelable, extra.getParcelable(EXTRA_ENTRY_KEY));
98                 return;
99             } else if (newState == PipTransitionState.ENTERED_PIP) {
100                 Assert.assertNull(extra);
101                 return;
102             }
103             Assert.fail("Neither custom not ENTERED_PIP state is received.");
104         };
105         Bundle extra = new Bundle();
106         extra.putParcelable(EXTRA_ENTRY_KEY, mEmptyParcelable);
107 
108         mPipTransitionState.addPipTransitionStateChangedListener(mStateChangedListener);
109         mPipTransitionState.setState(customState, extra);
110         mPipTransitionState.setState(PipTransitionState.ENTERED_PIP);
111         mPipTransitionState.removePipTransitionStateChangedListener(mStateChangedListener);
112     }
113 }
114