Home | History | Annotate | Download | only in am
      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.am;
     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.annotation.Nullable;
     24 import android.app.ActivityOptions;
     25 import android.os.Handler;
     26 import android.platform.test.annotations.Presubmit;
     27 import android.support.test.filters.FlakyTest;
     28 import android.support.test.filters.SmallTest;
     29 import android.support.test.runner.AndroidJUnit4;
     30 import android.util.ArrayMap;
     31 import android.view.RemoteAnimationAdapter;
     32 
     33 import com.android.server.testutils.OffsettableClock;
     34 import com.android.server.testutils.TestHandler;
     35 
     36 import org.junit.Before;
     37 import org.junit.Test;
     38 import org.junit.runner.RunWith;
     39 import org.mockito.Mock;
     40 import org.mockito.MockitoAnnotations;
     41 
     42 /**
     43  * atest PendingRemoteAnimationRegistryTest
     44  */
     45 @SmallTest
     46 @Presubmit
     47 @FlakyTest
     48 @RunWith(AndroidJUnit4.class)
     49 public class PendingRemoteAnimationRegistryTest extends ActivityTestsBase {
     50 
     51     @Mock RemoteAnimationAdapter mAdapter;
     52     private PendingRemoteAnimationRegistry mRegistry;
     53     private final OffsettableClock mClock = new OffsettableClock.Stopped();
     54     private TestHandler mHandler;
     55     private ActivityManagerService mService;
     56 
     57     @Before
     58     public void setUp() throws Exception {
     59         super.setUp();
     60         MockitoAnnotations.initMocks(this);
     61         mService = createActivityManagerService();
     62         mService.mHandlerThread.getThreadHandler().runWithScissors(() -> {
     63             mHandler = new TestHandler(null, mClock);
     64         }, 0);
     65         mRegistry = new PendingRemoteAnimationRegistry(mService, mHandler);
     66     }
     67 
     68     @Test
     69     public void testOverrideActivityOptions() {
     70         mRegistry.addPendingAnimation("com.android.test", mAdapter);
     71         ActivityOptions opts = ActivityOptions.makeBasic();
     72         opts = mRegistry.overrideOptionsIfNeeded("com.android.test", opts);
     73         assertEquals(mAdapter, opts.getRemoteAnimationAdapter());
     74     }
     75 
     76     @Test
     77     public void testOverrideActivityOptions_null() {
     78         mRegistry.addPendingAnimation("com.android.test", mAdapter);
     79         final ActivityOptions opts = mRegistry.overrideOptionsIfNeeded("com.android.test", null);
     80         assertNotNull(opts);
     81         assertEquals(mAdapter, opts.getRemoteAnimationAdapter());
     82     }
     83 
     84     @Test
     85     public void testTimeout() {
     86         mRegistry.addPendingAnimation("com.android.test", mAdapter);
     87         mClock.fastForward(5000);
     88         mHandler.timeAdvance();
     89         assertNull(mRegistry.overrideOptionsIfNeeded("com.android.test", null));
     90     }
     91 
     92     @Test
     93     public void testTimeout_overridenEntry() {
     94         mRegistry.addPendingAnimation("com.android.test", mAdapter);
     95         mClock.fastForward(2500);
     96         mHandler.timeAdvance();
     97         mRegistry.addPendingAnimation("com.android.test", mAdapter);
     98         mClock.fastForward(1000);
     99         mHandler.timeAdvance();
    100         final ActivityOptions opts = mRegistry.overrideOptionsIfNeeded("com.android.test", null);
    101         assertEquals(mAdapter, opts.getRemoteAnimationAdapter());
    102     }
    103 }
    104