1 /*
2  * Copyright (C) 2018 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.server.wm;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 
23 import android.app.ActivityOptions;
24 import android.os.IBinder;
25 import android.platform.test.annotations.Presubmit;
26 import android.view.RemoteAnimationAdapter;
27 
28 import androidx.test.filters.SmallTest;
29 
30 import com.android.server.AnimationThread;
31 import com.android.server.testutils.OffsettableClock;
32 import com.android.server.testutils.TestHandler;
33 
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 
40 /**
41  * Build/Install/Run:
42  *  atest WmTests:PendingRemoteAnimationRegistryTest
43  */
44 @SmallTest
45 @Presubmit
46 public class PendingRemoteAnimationRegistryTest {
47 
48     @Mock RemoteAnimationAdapter mAdapter;
49     @Mock IBinder mLaunchCookie;
50     private PendingRemoteAnimationRegistry mRegistry;
51     private final OffsettableClock mClock = new OffsettableClock.Stopped();
52     private TestHandler mHandler;
53 
54     @Before
setUp()55     public void setUp() throws Exception {
56         MockitoAnnotations.initMocks(this);
57         AnimationThread.getHandler().runWithScissors(() -> {
58             mHandler = new TestHandler(null, mClock);
59         }, 0);
60         mRegistry = new PendingRemoteAnimationRegistry(new WindowManagerGlobalLock(), mHandler);
61     }
62 
63     @After
teadDown()64     public void teadDown() {
65         AnimationThread.dispose();
66     }
67 
68     @Test
testOverrideActivityOptions()69     public void testOverrideActivityOptions() {
70         mRegistry.addPendingAnimation("com.android.test", mAdapter, null /* launchCookie */);
71         ActivityOptions opts = ActivityOptions.makeBasic();
72         opts = mRegistry.overrideOptionsIfNeeded("com.android.test", opts);
73         assertEquals(mAdapter, opts.getRemoteAnimationAdapter());
74     }
75 
76     @Test
testOverrideActivityOptions_null()77     public void testOverrideActivityOptions_null() {
78         mRegistry.addPendingAnimation("com.android.test", mAdapter, null /* launchCookie */);
79         final ActivityOptions opts = mRegistry.overrideOptionsIfNeeded("com.android.test", null);
80         assertNotNull(opts);
81         assertEquals(mAdapter, opts.getRemoteAnimationAdapter());
82     }
83 
84     @Test
testOverrideLaunchCookie()85     public void testOverrideLaunchCookie() {
86         mRegistry.addPendingAnimation("com.android.test", mAdapter, mLaunchCookie);
87         ActivityOptions opts = ActivityOptions.makeBasic();
88         opts = mRegistry.overrideOptionsIfNeeded("com.android.test", opts);
89         assertNotNull(opts);
90         assertEquals(mLaunchCookie, opts.getLaunchCookie());
91     }
92 
93     @Test
testTimeout()94     public void testTimeout() {
95         mRegistry.addPendingAnimation("com.android.test", mAdapter, null /* launchCookie */);
96         mClock.fastForward(5000);
97         mHandler.timeAdvance();
98         assertNull(mRegistry.overrideOptionsIfNeeded("com.android.test", null));
99     }
100 
101     @Test
testTimeout_overridenEntry()102     public void testTimeout_overridenEntry() {
103         mRegistry.addPendingAnimation("com.android.test", mAdapter, null /* launchCookie */);
104         mClock.fastForward(2500);
105         mHandler.timeAdvance();
106         mRegistry.addPendingAnimation("com.android.test", mAdapter, null /* launchCookie */);
107         mClock.fastForward(1000);
108         mHandler.timeAdvance();
109         final ActivityOptions opts = mRegistry.overrideOptionsIfNeeded("com.android.test", null);
110         assertEquals(mAdapter, opts.getRemoteAnimationAdapter());
111     }
112 }
113