• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED;
20 import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_DENIED;
21 import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static junit.framework.Assert.assertEquals;
26 import static junit.framework.Assert.assertFalse;
27 import static junit.framework.Assert.assertTrue;
28 
29 import static org.junit.Assert.assertNull;
30 
31 import android.app.BroadcastOptions;
32 import android.os.Build;
33 import android.os.PowerExemptionManager;
34 
35 import androidx.test.ext.junit.runners.AndroidJUnit4;
36 
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 
40 @RunWith(AndroidJUnit4.class)
41 public class BroadcastOptionsTest {
42     /**
43      * Creates a clone of BroadcastOptions, using toBundle().
44      */
cloneViaBundle(BroadcastOptions bo)45     static BroadcastOptions cloneViaBundle(BroadcastOptions bo) {
46         return BroadcastOptions.fromBundle(bo.toBundle());
47     }
48 
assertBroadcastOptionTemporaryAppAllowList( BroadcastOptions bo, long expectedDuration, int expectedAllowListType, int expectedReasonCode, String expectedReason)49     private void assertBroadcastOptionTemporaryAppAllowList(
50             BroadcastOptions bo,
51             long expectedDuration,
52             int expectedAllowListType,
53             int expectedReasonCode,
54             String expectedReason) {
55         assertEquals(expectedAllowListType, bo.getTemporaryAppAllowlistType());
56         assertEquals(expectedDuration, bo.getTemporaryAppAllowlistDuration());
57         assertEquals(expectedReasonCode, bo.getTemporaryAppAllowlistReasonCode());
58         assertEquals(expectedReason, bo.getTemporaryAppAllowlistReason());
59 
60         // Clone the BO and check it too.
61         BroadcastOptions cloned = cloneViaBundle(bo);
62         assertEquals(expectedAllowListType, cloned.getTemporaryAppAllowlistType());
63         assertEquals(expectedDuration, cloned.getTemporaryAppAllowlistDuration());
64         assertEquals(expectedReasonCode, cloned.getTemporaryAppAllowlistReasonCode());
65         assertEquals(expectedReason, cloned.getTemporaryAppAllowlistReason());
66     }
67 
assertBroadcastOption_noTemporaryAppAllowList(BroadcastOptions bo)68     private void assertBroadcastOption_noTemporaryAppAllowList(BroadcastOptions bo) {
69         assertBroadcastOptionTemporaryAppAllowList(bo,
70                 /* duration= */ 0,
71                 PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_NONE,
72                 PowerExemptionManager.REASON_UNKNOWN,
73                 /* reason= */ null);
74     }
75 
76     @Test
testTemporaryAppAllowlistBroadcastOptions_noDefaultValues()77     public void testTemporaryAppAllowlistBroadcastOptions_noDefaultValues() {
78         BroadcastOptions bo;
79 
80         // Check the default values about temp-allowlist.
81         bo = BroadcastOptions.makeBasic();
82         assertBroadcastOption_noTemporaryAppAllowList(bo);
83     }
84 
85     @Test
testSetTemporaryAppWhitelistDuration_legacyApi()86     public void testSetTemporaryAppWhitelistDuration_legacyApi() {
87         BroadcastOptions bo;
88 
89         bo = BroadcastOptions.makeBasic();
90 
91         bo.setTemporaryAppWhitelistDuration(10);
92 
93         assertBroadcastOptionTemporaryAppAllowList(bo,
94                 /* duration= */ 10,
95                 PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED,
96                 PowerExemptionManager.REASON_UNKNOWN,
97                 /* reason= */ null);
98 
99         // Clear the temp-allowlist.
100         bo.setTemporaryAppWhitelistDuration(0);
101 
102         // Check the default values about temp-allowlist.
103         assertBroadcastOption_noTemporaryAppAllowList(bo);
104     }
105 
106     @Test
testSetTemporaryAppWhitelistDuration()107     public void testSetTemporaryAppWhitelistDuration() {
108         BroadcastOptions bo;
109 
110         bo = BroadcastOptions.makeBasic();
111 
112         bo.setTemporaryAppAllowlist(10,
113                 PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED,
114                 PowerExemptionManager.REASON_GEOFENCING,
115                 null);
116 
117         assertBroadcastOptionTemporaryAppAllowList(bo,
118                 /* duration= */ 10,
119                 PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED,
120                 PowerExemptionManager.REASON_GEOFENCING,
121                 /* reason= */ null);
122 
123         // Setting duration 0 will clear the previous call.
124         bo.setTemporaryAppAllowlist(0,
125                 PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED,
126                 PowerExemptionManager.REASON_ACTIVITY_RECOGNITION,
127                 "reason");
128         assertBroadcastOption_noTemporaryAppAllowList(bo);
129 
130         // Set again.
131         bo.setTemporaryAppAllowlist(20,
132                 PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED,
133                 PowerExemptionManager.REASON_GEOFENCING,
134                 "reason");
135 
136         assertBroadcastOptionTemporaryAppAllowList(bo,
137                 /* duration= */ 20,
138                 PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED,
139                 PowerExemptionManager.REASON_GEOFENCING,
140                 /* reason= */ "reason");
141 
142         // Set to NONE will clear the previous call too.
143         bo.setTemporaryAppAllowlist(10,
144                 PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_NONE,
145                 PowerExemptionManager.REASON_ACTIVITY_RECOGNITION,
146                 "reason");
147 
148         assertBroadcastOption_noTemporaryAppAllowList(bo);
149     }
150 
151     @Test
testMaxManifestReceiverApiLevel()152     public void testMaxManifestReceiverApiLevel() {
153         final BroadcastOptions bo = BroadcastOptions.makeBasic();
154         // No MaxManifestReceiverApiLevel set, the default value should be CUR_DEVELOPMENT.
155         assertEquals(Build.VERSION_CODES.CUR_DEVELOPMENT, bo.getMaxManifestReceiverApiLevel());
156 
157         // Set MaxManifestReceiverApiLevel to P.
158         bo.setMaxManifestReceiverApiLevel(Build.VERSION_CODES.P);
159         assertEquals(Build.VERSION_CODES.P, bo.getMaxManifestReceiverApiLevel());
160 
161         // Clone the BroadcastOptions and check it too.
162         final BroadcastOptions cloned = cloneViaBundle(bo);
163         assertEquals(Build.VERSION_CODES.P, bo.getMaxManifestReceiverApiLevel());
164     }
165 
166     @Test
testGetPendingIntentBackgroundActivityLaunchAllowedDefault()167     public void testGetPendingIntentBackgroundActivityLaunchAllowedDefault() {
168         BroadcastOptions options = BroadcastOptions.makeBasic();
169         // backwards compatibility
170         assertTrue(options.isPendingIntentBackgroundActivityLaunchAllowed());
171         assertThat(options.getPendingIntentBackgroundActivityStartMode()).isEqualTo(
172                 MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED);
173     }
174 
175     @Test
testGetSetPendingIntentBackgroundActivityLaunchAllowedTrue()176     public void testGetSetPendingIntentBackgroundActivityLaunchAllowedTrue() {
177         BroadcastOptions options = BroadcastOptions.makeBasic();
178         options.setPendingIntentBackgroundActivityLaunchAllowed(true);
179         assertTrue(options.isPendingIntentBackgroundActivityLaunchAllowed());
180         assertThat(options.getPendingIntentBackgroundActivityStartMode()).isEqualTo(
181                 MODE_BACKGROUND_ACTIVITY_START_ALLOWED);
182     }
183 
184     @Test
testGetSetPendingIntentBackgroundActivityLaunchAllowedFalse()185     public void testGetSetPendingIntentBackgroundActivityLaunchAllowedFalse() {
186         BroadcastOptions options = BroadcastOptions.makeBasic();
187         options.setPendingIntentBackgroundActivityLaunchAllowed(false);
188         assertFalse(options.isPendingIntentBackgroundActivityLaunchAllowed());
189         assertThat(options.getPendingIntentBackgroundActivityStartMode()).isEqualTo(
190                 MODE_BACKGROUND_ACTIVITY_START_DENIED);
191     }
192 
193     @Test
testGetSetPendingIntentBackgroundActivityStartModeAllowed()194     public void testGetSetPendingIntentBackgroundActivityStartModeAllowed() {
195         BroadcastOptions options = BroadcastOptions.makeBasic()
196                 .setPendingIntentBackgroundActivityStartMode(
197                         MODE_BACKGROUND_ACTIVITY_START_ALLOWED);
198         assertTrue(options.isPendingIntentBackgroundActivityLaunchAllowed());
199         assertThat(options.getPendingIntentBackgroundActivityStartMode()).isEqualTo(
200                 MODE_BACKGROUND_ACTIVITY_START_ALLOWED);
201     }
202 
203     @Test
testGetSetPendingIntentBackgroundActivityStartModeDenied()204     public void testGetSetPendingIntentBackgroundActivityStartModeDenied() {
205         BroadcastOptions options = BroadcastOptions.makeBasic()
206                 .setPendingIntentBackgroundActivityStartMode(
207                         MODE_BACKGROUND_ACTIVITY_START_DENIED);
208         assertFalse(options.isPendingIntentBackgroundActivityLaunchAllowed());
209         assertThat(options.getPendingIntentBackgroundActivityStartMode()).isEqualTo(
210                 MODE_BACKGROUND_ACTIVITY_START_DENIED);
211     }
212 
213     @Test
testSetGetDeferralPolicy()214     public void testSetGetDeferralPolicy() {
215         final BroadcastOptions options = BroadcastOptions.makeBasic();
216         assertEquals(BroadcastOptions.DEFERRAL_POLICY_DEFAULT,
217                 options.getDeferralPolicy());
218 
219         options.setDeferralPolicy(BroadcastOptions.DEFERRAL_POLICY_UNTIL_ACTIVE);
220         assertEquals(BroadcastOptions.DEFERRAL_POLICY_UNTIL_ACTIVE,
221                 options.getDeferralPolicy());
222 
223         final BroadcastOptions options2 = cloneViaBundle(options);
224         assertEquals(BroadcastOptions.DEFERRAL_POLICY_UNTIL_ACTIVE,
225                 options2.getDeferralPolicy());
226 
227         options.clearDeferralPolicy();
228         assertEquals(BroadcastOptions.DEFERRAL_POLICY_DEFAULT,
229                 options.getDeferralPolicy());
230     }
231 
232     @Test
testSetGetDeliveryGroupPolicy()233     public void testSetGetDeliveryGroupPolicy() {
234         final BroadcastOptions options = BroadcastOptions.makeBasic();
235         final int defaultPolicy = options.getDeliveryGroupPolicy();
236 
237         options.setDeliveryGroupPolicy(BroadcastOptions.DELIVERY_GROUP_POLICY_MOST_RECENT);
238         assertEquals(BroadcastOptions.DELIVERY_GROUP_POLICY_MOST_RECENT,
239                 options.getDeliveryGroupPolicy());
240 
241         final BroadcastOptions options2 = cloneViaBundle(options);
242         assertEquals(BroadcastOptions.DELIVERY_GROUP_POLICY_MOST_RECENT,
243                 options2.getDeliveryGroupPolicy());
244 
245         options.clearDeliveryGroupPolicy();
246         assertEquals(defaultPolicy, options.getDeliveryGroupPolicy());
247 
248         // TODO(249160234): Verify the behavior of the set policy.
249     }
250 
251     @Test
testSetGetDeliveryGroupMatchingKey()252     public void testSetGetDeliveryGroupMatchingKey() {
253         final BroadcastOptions options = BroadcastOptions.makeBasic();
254 
255         final String namespace = "test_namespace";
256         final String key = "test_key";
257         options.setDeliveryGroupMatchingKey(namespace, key);
258         assertEquals(String.join(":", namespace, key),
259                 options.getDeliveryGroupMatchingKey());
260 
261         final BroadcastOptions options2 = cloneViaBundle(options);
262         assertEquals(String.join(":", namespace, key),
263                 options2.getDeliveryGroupMatchingKey());
264 
265         options.clearDeliveryGroupMatchingKey();
266         assertNull(options.getDeliveryGroupMatchingKey());
267     }
268 }
269