1 /* 2 * Copyright (C) 2016 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 com.android.server.wm.WindowStateAnimator.STACK_CLIP_AFTER_ANIM; 20 import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_BEFORE_ANIM; 21 import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_NONE; 22 23 import static org.mockito.ArgumentMatchers.argThat; 24 import static org.mockito.ArgumentMatchers.eq; 25 import static org.mockito.Mockito.mock; 26 import static org.mockito.Mockito.verify; 27 28 import android.graphics.Point; 29 import android.graphics.Rect; 30 import android.platform.test.annotations.Presubmit; 31 import android.support.test.filters.SmallTest; 32 import android.support.test.runner.AndroidJUnit4; 33 import android.view.SurfaceControl; 34 import android.view.animation.Animation; 35 import android.view.animation.ClipRectAnimation; 36 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 /** 41 * Tests for the {@link WindowAnimationSpec} class. 42 * 43 * atest FrameworksServicesTests:com.android.server.wm.WindowAnimationSpecTest 44 */ 45 @SmallTest 46 @Presubmit 47 @RunWith(AndroidJUnit4.class) 48 public class WindowAnimationSpecTest { 49 private final SurfaceControl mSurfaceControl = mock(SurfaceControl.class); 50 private final SurfaceControl.Transaction mTransaction = mock(SurfaceControl.Transaction.class); 51 private final Animation mAnimation = mock(Animation.class); 52 private final Rect mStackBounds = new Rect(0, 0, 10, 10); 53 54 @Test 55 public void testApply_clipNone() { 56 Rect windowCrop = new Rect(0, 0, 20, 20); 57 Animation a = createClipRectAnimation(windowCrop, windowCrop); 58 WindowAnimationSpec windowAnimationSpec = new WindowAnimationSpec(a, null, 59 mStackBounds, false /* canSkipFirstFrame */, STACK_CLIP_NONE, 60 true /* isAppAnimation */); 61 windowAnimationSpec.apply(mTransaction, mSurfaceControl, 0); 62 verify(mTransaction).setWindowCrop(eq(mSurfaceControl), 63 argThat(rect -> rect.equals(windowCrop))); 64 } 65 66 @Test 67 public void testApply_clipAfter() { 68 WindowAnimationSpec windowAnimationSpec = new WindowAnimationSpec(mAnimation, null, 69 mStackBounds, false /* canSkipFirstFrame */, STACK_CLIP_AFTER_ANIM, 70 true /* isAppAnimation */); 71 windowAnimationSpec.apply(mTransaction, mSurfaceControl, 0); 72 verify(mTransaction).setWindowCrop(eq(mSurfaceControl), argThat(Rect::isEmpty)); 73 verify(mTransaction).setFinalCrop(eq(mSurfaceControl), 74 argThat(rect -> rect.equals(mStackBounds))); 75 } 76 77 @Test 78 public void testApply_clipAfterOffsetPosition() { 79 // Stack bounds is (0, 0, 10, 10) position is (20, 40) 80 WindowAnimationSpec windowAnimationSpec = new WindowAnimationSpec(mAnimation, 81 new Point(20, 40), mStackBounds, false /* canSkipFirstFrame */, 82 STACK_CLIP_AFTER_ANIM, 83 true /* isAppAnimation */); 84 windowAnimationSpec.apply(mTransaction, mSurfaceControl, 0); 85 verify(mTransaction).setWindowCrop(eq(mSurfaceControl), argThat(Rect::isEmpty)); 86 verify(mTransaction).setFinalCrop(eq(mSurfaceControl), 87 argThat(rect -> rect.left == 20 && rect.top == 40 && rect.right == 30 88 && rect.bottom == 50)); 89 } 90 91 @Test 92 public void testApply_clipBeforeNoAnimationBounds() { 93 // Stack bounds is (0, 0, 10, 10) animation clip is (0, 0, 0, 0) 94 WindowAnimationSpec windowAnimationSpec = new WindowAnimationSpec(mAnimation, null, 95 mStackBounds, false /* canSkipFirstFrame */, STACK_CLIP_BEFORE_ANIM, 96 true /* isAppAnimation */); 97 windowAnimationSpec.apply(mTransaction, mSurfaceControl, 0); 98 verify(mTransaction).setWindowCrop(eq(mSurfaceControl), 99 argThat(rect -> rect.equals(mStackBounds))); 100 } 101 102 @Test 103 public void testApply_clipBeforeNoStackBounds() { 104 // Stack bounds is (0, 0, 0, 0) animation clip is (0, 0, 20, 20) 105 Rect windowCrop = new Rect(0, 0, 20, 20); 106 Animation a = createClipRectAnimation(windowCrop, windowCrop); 107 a.initialize(0, 0, 0, 0); 108 WindowAnimationSpec windowAnimationSpec = new WindowAnimationSpec(a, null, 109 null, false /* canSkipFirstFrame */, STACK_CLIP_BEFORE_ANIM, 110 true /* isAppAnimation */); 111 windowAnimationSpec.apply(mTransaction, mSurfaceControl, 0); 112 verify(mTransaction).setWindowCrop(eq(mSurfaceControl), argThat(Rect::isEmpty)); 113 } 114 115 @Test 116 public void testApply_clipBeforeSmallerAnimationClip() { 117 // Stack bounds is (0, 0, 10, 10) animation clip is (0, 0, 5, 5) 118 Rect windowCrop = new Rect(0, 0, 5, 5); 119 Animation a = createClipRectAnimation(windowCrop, windowCrop); 120 WindowAnimationSpec windowAnimationSpec = new WindowAnimationSpec(a, null, 121 mStackBounds, false /* canSkipFirstFrame */, STACK_CLIP_BEFORE_ANIM, 122 true /* isAppAnimation */); 123 windowAnimationSpec.apply(mTransaction, mSurfaceControl, 0); 124 verify(mTransaction).setWindowCrop(eq(mSurfaceControl), 125 argThat(rect -> rect.equals(windowCrop))); 126 } 127 128 @Test 129 public void testApply_clipBeforeSmallerStackClip() { 130 // Stack bounds is (0, 0, 10, 10) animation clip is (0, 0, 20, 20) 131 Rect windowCrop = new Rect(0, 0, 20, 20); 132 Animation a = createClipRectAnimation(windowCrop, windowCrop); 133 WindowAnimationSpec windowAnimationSpec = new WindowAnimationSpec(a, null, 134 mStackBounds, false /* canSkipFirstFrame */, STACK_CLIP_BEFORE_ANIM, 135 true /* isAppAnimation */); 136 windowAnimationSpec.apply(mTransaction, mSurfaceControl, 0); 137 verify(mTransaction).setWindowCrop(eq(mSurfaceControl), 138 argThat(rect -> rect.equals(mStackBounds))); 139 } 140 141 private Animation createClipRectAnimation(Rect fromClip, Rect toClip) { 142 Animation a = new ClipRectAnimation(fromClip, toClip); 143 a.initialize(0, 0, 0, 0); 144 return a; 145 } 146 } 147