1 /* 2 * Copyright (C) 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.server.wm.other; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertTrue; 22 23 import android.app.PendingIntent; 24 import android.app.PictureInPictureParams; 25 import android.app.RemoteAction; 26 import android.content.Intent; 27 import android.graphics.Bitmap; 28 import android.graphics.Rect; 29 import android.graphics.drawable.Icon; 30 import android.platform.test.annotations.Presubmit; 31 import android.server.wm.WindowManagerTestBase; 32 import android.util.Rational; 33 34 import androidx.test.filters.SmallTest; 35 import androidx.test.runner.AndroidJUnit4; 36 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 import java.util.ArrayList; 41 import java.util.List; 42 43 /** 44 * Tests the {@link PictureInPictureParams} class. 45 * 46 * Build/Install/Run: 47 * atest CtsWindowManagerDeviceOther:PictureInPictureParamsTest 48 */ 49 @Presubmit 50 @SmallTest 51 @RunWith(AndroidJUnit4.class) 52 public class PictureInPictureParamsTest extends WindowManagerTestBase { 53 54 /** 55 * Tests that we get the same values back from the public PictureInPicture params getters that 56 * were set via the PictureInPictureParams.Builder. 57 */ 58 @Test testPictureInPictureParamsGetters()59 public void testPictureInPictureParamsGetters() { 60 ArrayList<RemoteAction> actions = new ArrayList<>(); 61 for (int i = 0; i < 4; i++) { 62 actions.add(createRemoteAction(0)); 63 } 64 65 assertPictureInPictureParamsGettersMatchValues( 66 actions, 67 createRemoteAction(1), 68 new Rational(1, 2), 69 new Rational(100, 1), 70 "Title", 71 "Subtitle", 72 new Rect(0, 0, 100, 100), 73 true, 74 true); 75 } 76 77 @Test testPictureInPictureParamsGettersNullValues()78 public void testPictureInPictureParamsGettersNullValues() { 79 assertPictureInPictureParamsGettersMatchValues(null, null, null, null, null, null, null, 80 false, false); 81 } 82 83 @Test testPictureInPictureParams_autoEnterEnabledDefaultFalse()84 public void testPictureInPictureParams_autoEnterEnabledDefaultFalse() { 85 PictureInPictureParams params = new PictureInPictureParams.Builder().build(); 86 87 assertFalse(params.isAutoEnterEnabled()); 88 } 89 90 @Test testPictureInPictureParams_seamlessResizeEnabledDefaultFalse()91 public void testPictureInPictureParams_seamlessResizeEnabledDefaultFalse() { 92 PictureInPictureParams params = new PictureInPictureParams.Builder().build(); 93 94 assertFalse(params.isSeamlessResizeEnabled()); 95 } 96 97 @Test testIsSameAspectRatio_exactMatch_returnTrue()98 public void testIsSameAspectRatio_exactMatch_returnTrue() { 99 final Rect bounds = new Rect(0, 0, 100, 200); 100 final Rational aspectRatio = new Rational(1, 2); 101 102 assertTrue(PictureInPictureParams.isSameAspectRatio(bounds, aspectRatio)); 103 } 104 105 @Test testIsSameAspectRatio_width1PixelShorter_returnTrue()106 public void testIsSameAspectRatio_width1PixelShorter_returnTrue() { 107 final Rect bounds = new Rect(0, 0, 99, 200); 108 final Rational aspectRatio = new Rational(1, 2); 109 110 assertTrue(PictureInPictureParams.isSameAspectRatio(bounds, aspectRatio)); 111 } 112 113 @Test testIsSameAspectRatio_width1PixelLonger_returnTrue()114 public void testIsSameAspectRatio_width1PixelLonger_returnTrue() { 115 final Rect bounds = new Rect(0, 0, 101, 200); 116 final Rational aspectRatio = new Rational(1, 2); 117 118 assertTrue(PictureInPictureParams.isSameAspectRatio(bounds, aspectRatio)); 119 } 120 121 @Test testIsSameAspectRatio_height1PixelShorter_returnTrue()122 public void testIsSameAspectRatio_height1PixelShorter_returnTrue() { 123 final Rect bounds = new Rect(0, 0, 100, 199); 124 final Rational aspectRatio = new Rational(1, 2); 125 126 assertTrue(PictureInPictureParams.isSameAspectRatio(bounds, aspectRatio)); 127 } 128 129 @Test testIsSameAspectRatio_height1PixelLonger_returnTrue()130 public void testIsSameAspectRatio_height1PixelLonger_returnTrue() { 131 final Rect bounds = new Rect(0, 0, 100, 201); 132 final Rational aspectRatio = new Rational(1, 2); 133 134 assertTrue(PictureInPictureParams.isSameAspectRatio(bounds, aspectRatio)); 135 } 136 137 @Test testIsSameAspectRatio_width5PixelShorter_returnFalse()138 public void testIsSameAspectRatio_width5PixelShorter_returnFalse() { 139 final Rect bounds = new Rect(0, 0, 95, 200); 140 final Rational aspectRatio = new Rational(1, 2); 141 142 assertFalse(PictureInPictureParams.isSameAspectRatio(bounds, aspectRatio)); 143 } 144 145 @Test testIsSameAspectRatio_width5PixelLonger_returnFalse()146 public void testIsSameAspectRatio_width5PixelLonger_returnFalse() { 147 final Rect bounds = new Rect(0, 0, 105, 200); 148 final Rational aspectRatio = new Rational(1, 2); 149 150 assertFalse(PictureInPictureParams.isSameAspectRatio(bounds, aspectRatio)); 151 } 152 153 @Test testIsSameAspectRatio_height5PixelShorter_returnFalse()154 public void testIsSameAspectRatio_height5PixelShorter_returnFalse() { 155 final Rect bounds = new Rect(0, 0, 100, 195); 156 final Rational aspectRatio = new Rational(1, 2); 157 158 assertFalse(PictureInPictureParams.isSameAspectRatio(bounds, aspectRatio)); 159 } 160 161 @Test testIsSameAspectRatio_height5PixelLonger_returnFalse()162 public void testIsSameAspectRatio_height5PixelLonger_returnFalse() { 163 final Rect bounds = new Rect(0, 0, 100, 205); 164 final Rational aspectRatio = new Rational(1, 2); 165 166 assertFalse(PictureInPictureParams.isSameAspectRatio(bounds, aspectRatio)); 167 } 168 assertPictureInPictureParamsGettersMatchValues(List<RemoteAction> actions, RemoteAction closeAction, Rational aspectRatio, Rational expandedAspectRatio, String title, String subtitle, Rect sourceRectHint, boolean isAutoEnterEnabled, boolean isSeamlessResizeEnabled)169 private void assertPictureInPictureParamsGettersMatchValues(List<RemoteAction> actions, 170 RemoteAction closeAction, Rational aspectRatio, Rational expandedAspectRatio, 171 String title, String subtitle, Rect sourceRectHint, boolean isAutoEnterEnabled, 172 boolean isSeamlessResizeEnabled) { 173 174 PictureInPictureParams params = new PictureInPictureParams.Builder() 175 .setActions(actions) 176 .setCloseAction(closeAction) 177 .setAspectRatio(aspectRatio) 178 .setExpandedAspectRatio(expandedAspectRatio) 179 .setTitle(title) 180 .setSubtitle(subtitle) 181 .setSourceRectHint(sourceRectHint) 182 .setAutoEnterEnabled(isAutoEnterEnabled) 183 .setSeamlessResizeEnabled(isSeamlessResizeEnabled) 184 .build(); 185 186 if (actions == null) { 187 assertEquals(new ArrayList<>(), params.getActions()); 188 } else { 189 assertEquals(actions, params.getActions()); 190 } 191 assertEquals(closeAction, params.getCloseAction()); 192 assertEquals(aspectRatio, params.getAspectRatio()); 193 assertEquals(expandedAspectRatio, params.getExpandedAspectRatio()); 194 assertEquals(title, params.getTitle() == null ? null : params.getTitle().toString()); 195 assertEquals(subtitle, 196 params.getSubtitle() == null ? null : params.getSubtitle().toString()); 197 assertEquals(sourceRectHint, params.getSourceRectHint()); 198 assertEquals(isAutoEnterEnabled, params.isAutoEnterEnabled()); 199 assertEquals(isSeamlessResizeEnabled, params.isSeamlessResizeEnabled()); 200 } 201 202 /** @return {@link RemoteAction} instance titled after a given index */ createRemoteAction(int index)203 private RemoteAction createRemoteAction(int index) { 204 return new RemoteAction( 205 Icon.createWithBitmap(Bitmap.createBitmap(24, 24, Bitmap.Config.ARGB_8888)), 206 "action " + index, 207 "contentDescription " + index, 208 PendingIntent.getBroadcast(mContext, 0, new Intent(), 209 PendingIntent.FLAG_IMMUTABLE)); 210 } 211 } 212