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.app.cts;
18 
19 import android.app.PendingIntent;
20 import android.app.PictureInPictureParams;
21 import android.app.RemoteAction;
22 import android.content.Intent;
23 import android.graphics.Bitmap;
24 import android.graphics.Rect;
25 import android.graphics.drawable.Icon;
26 import android.test.AndroidTestCase;
27 import android.util.Rational;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 public class PictureInPictureParamsTest extends AndroidTestCase {
33 
34     /**
35      * Tests that we get the same values back from the public PictureInPicture params getters that
36      * were set via the PictureInPictureParams.Builder.
37      */
testPictureInPictureParamsGetters()38     public void testPictureInPictureParamsGetters() {
39         ArrayList<RemoteAction> actions = new ArrayList<>();
40         for (int i = 0; i < 4; i++) {
41             actions.add(createRemoteAction(0));
42         }
43 
44         assertPictureInPictureParamsGettersMatchValues(
45                 actions,
46                 createRemoteAction(1),
47                 new Rational(1, 2),
48                 new Rational(100, 1),
49                 "Title",
50                 "Subtitle",
51                 new Rect(0, 0, 100, 100),
52                 true,
53                 true);
54     }
55 
testPictureInPictureParamsGettersNullValues()56     public void testPictureInPictureParamsGettersNullValues() {
57         assertPictureInPictureParamsGettersMatchValues(null, null, null, null, null, null, null,
58                 false, false);
59     }
60 
assertPictureInPictureParamsGettersMatchValues(List<RemoteAction> actions, RemoteAction closeAction, Rational aspectRatio, Rational expandedAspectRatio, String title, String subtitle, Rect sourceRectHint, boolean isAutoEnterEnabled, boolean isSeamlessResizeEnabled)61     private void assertPictureInPictureParamsGettersMatchValues(List<RemoteAction> actions,
62             RemoteAction closeAction, Rational aspectRatio, Rational expandedAspectRatio,
63             String title, String subtitle, Rect sourceRectHint, boolean isAutoEnterEnabled,
64             boolean isSeamlessResizeEnabled) {
65 
66         PictureInPictureParams params = new PictureInPictureParams.Builder()
67                 .setActions(actions)
68                 .setCloseAction(closeAction)
69                 .setAspectRatio(aspectRatio)
70                 .setExpandedAspectRatio(expandedAspectRatio)
71                 .setTitle(title)
72                 .setSubtitle(subtitle)
73                 .setSourceRectHint(sourceRectHint)
74                 .setAutoEnterEnabled(isAutoEnterEnabled)
75                 .setSeamlessResizeEnabled(isSeamlessResizeEnabled)
76                 .build();
77 
78         if (actions == null) {
79             assertEquals(new ArrayList<>(), params.getActions());
80         } else {
81             assertEquals(actions, params.getActions());
82         }
83         assertEquals(closeAction, params.getCloseAction());
84         assertEquals(aspectRatio, params.getAspectRatio());
85         assertEquals(expandedAspectRatio, params.getExpandedAspectRatio());
86         assertEquals(title, params.getTitle());
87         assertEquals(subtitle, params.getSubtitle());
88         assertEquals(sourceRectHint, params.getSourceRectHint());
89         assertEquals(isAutoEnterEnabled, params.isAutoEnterEnabled());
90         assertEquals(isSeamlessResizeEnabled, params.isSeamlessResizeEnabled());
91     }
92 
93     /** @return {@link RemoteAction} instance titled after a given index */
createRemoteAction(int index)94     private RemoteAction createRemoteAction(int index) {
95         return new RemoteAction(
96                 Icon.createWithBitmap(Bitmap.createBitmap(24, 24, Bitmap.Config.ARGB_8888)),
97                 "action " + index,
98                 "contentDescription " + index,
99                 PendingIntent.getBroadcast(getContext(), 0, new Intent(),
100                         PendingIntent.FLAG_IMMUTABLE));
101     }
102 }
103