1 /* 2 * Copyright 2022 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.app.activity; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.app.Activity; 22 import android.app.ActivityOptions; 23 import android.content.Intent; 24 import android.os.Bundle; 25 import android.platform.test.annotations.Presubmit; 26 import android.transition.Fade; 27 import android.view.View; 28 import android.view.Window; 29 30 import androidx.test.filters.MediumTest; 31 import androidx.test.rule.ActivityTestRule; 32 import androidx.test.runner.AndroidJUnit4; 33 34 import org.junit.Rule; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 38 import java.util.ArrayList; 39 import java.util.concurrent.CountDownLatch; 40 import java.util.concurrent.TimeUnit; 41 42 /** 43 * Test for verifying Activity Transitions Drawable behavior 44 */ 45 @RunWith(AndroidJUnit4.class) 46 @MediumTest 47 @Presubmit 48 public class ActivityTransitionDrawableTest { 49 private static final String LAUNCH_ON_START = "launch on start"; 50 51 @Rule 52 public final ActivityTestRule<TestActivity> mActivityTestRule = 53 new ActivityTestRule<>(TestActivity.class, true); 54 55 @Test stopTransitionDrawableAlphaRestored()56 public void stopTransitionDrawableAlphaRestored() throws Throwable { 57 mActivityTestRule.runOnUiThread(() -> { 58 Activity activity = mActivityTestRule.getActivity(); 59 Intent intent = new Intent(activity, TestActivity.class); 60 intent.putExtra(LAUNCH_ON_START, true); 61 Bundle bundle = ActivityOptions.makeSceneTransitionAnimation(activity).toBundle(); 62 activity.startActivity(intent, bundle); 63 }); 64 65 assertThat(TestActivity.activityAdded.await(5, TimeUnit.SECONDS)).isTrue(); 66 TestActivity topActivity = TestActivity.sInstances.get(2); 67 TestActivity middleActivity = TestActivity.sInstances.get(1); 68 assertThat(topActivity.startedLatch.await(5, TimeUnit.SECONDS)).isTrue(); 69 assertThat(middleActivity.stoppedLatch.await(5, TimeUnit.SECONDS)).isTrue(); 70 mActivityTestRule.runOnUiThread(() -> { 71 assertThat(middleActivity.getWindow().getDecorView().getBackground().getAlpha()) 72 .isEqualTo(255); 73 }); 74 } 75 76 public static class TestActivity extends Activity { 77 public static final ArrayList<TestActivity> sInstances = new ArrayList<TestActivity>(); 78 public static CountDownLatch activityAdded = new CountDownLatch(3); 79 80 private boolean mLaunchOnStart = false; 81 public CountDownLatch startedLatch = new CountDownLatch(1); 82 public CountDownLatch stoppedLatch = new CountDownLatch(1); 83 84 @Override onCreate(Bundle savedInstanceState)85 protected void onCreate(Bundle savedInstanceState) { 86 getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS); 87 getWindow().setAllowEnterTransitionOverlap(false); 88 setContentView(new View(this)); 89 Fade longFade = new Fade(); 90 longFade.setDuration(2000); 91 getWindow().setEnterTransition(longFade); 92 getWindow().setExitTransition(longFade); 93 super.onCreate(savedInstanceState); 94 mLaunchOnStart = getIntent().getBooleanExtra(LAUNCH_ON_START, false); 95 sInstances.add(this); 96 activityAdded.countDown(); 97 } 98 99 @Override onStart()100 protected void onStart() { 101 super.onStart(); 102 if (mLaunchOnStart) { 103 mLaunchOnStart = false; 104 Intent intent = new Intent(this, TestActivity.class); 105 startActivity(intent); 106 } 107 startedLatch.countDown(); 108 } 109 110 @Override onStop()111 protected void onStop() { 112 super.onStop(); 113 stoppedLatch.countDown(); 114 } 115 116 @Override onDestroy()117 protected void onDestroy() { 118 super.onDestroy(); 119 sInstances.remove(this); 120 } 121 } 122 } 123