• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.am;
18 
19 import static android.app.ActivityManager.PROCESS_STATE_UNKNOWN;
20 
21 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
22 import static com.android.server.am.BroadcastRecord.DELIVERY_DEFERRED;
23 import static com.android.server.am.BroadcastRecord.DELIVERY_DELIVERED;
24 import static com.android.server.am.BroadcastRecord.DELIVERY_PENDING;
25 import static com.android.server.am.BroadcastRecord.DELIVERY_SKIPPED;
26 import static com.android.server.am.BroadcastRecord.DELIVERY_TIMEOUT;
27 import static com.android.server.am.BroadcastRecord.LIMIT_PRIORITY_SCOPE;
28 import static com.android.server.am.BroadcastRecord.calculateBlockedUntilBeyondCount;
29 import static com.android.server.am.BroadcastRecord.calculateDeferUntilActive;
30 import static com.android.server.am.BroadcastRecord.calculateUrgent;
31 import static com.android.server.am.BroadcastRecord.isReceiverEquals;
32 
33 import static com.google.common.truth.Truth.assertThat;
34 
35 import static org.junit.Assert.assertArrayEquals;
36 import static org.junit.Assert.assertEquals;
37 import static org.junit.Assert.assertFalse;
38 import static org.junit.Assert.assertNotNull;
39 import static org.junit.Assert.assertNull;
40 import static org.junit.Assert.assertTrue;
41 import static org.mockito.ArgumentMatchers.any;
42 import static org.mockito.ArgumentMatchers.argThat;
43 import static org.mockito.ArgumentMatchers.eq;
44 import static org.mockito.Mockito.doNothing;
45 import static org.mockito.Mockito.times;
46 import static org.mockito.Mockito.verify;
47 
48 import android.app.BackgroundStartPrivileges;
49 import android.app.BroadcastOptions;
50 import android.content.IIntentReceiver;
51 import android.content.Intent;
52 import android.content.IntentFilter;
53 import android.content.pm.ActivityInfo;
54 import android.content.pm.ApplicationInfo;
55 import android.content.pm.ResolveInfo;
56 import android.os.Bundle;
57 import android.os.PersistableBundle;
58 import android.os.Process;
59 import android.os.UserHandle;
60 import android.platform.test.annotations.DisableFlags;
61 import android.platform.test.annotations.EnableFlags;
62 import android.platform.test.flag.junit.SetFlagsRule;
63 import android.telephony.SubscriptionManager;
64 
65 import androidx.test.filters.SmallTest;
66 
67 import com.android.server.compat.PlatformCompat;
68 
69 import org.junit.Before;
70 import org.junit.Rule;
71 import org.junit.Test;
72 import org.junit.runner.RunWith;
73 import org.mockito.ArgumentMatcher;
74 import org.mockito.Mock;
75 import org.mockito.Mockito;
76 import org.mockito.MockitoAnnotations;
77 import org.mockito.junit.MockitoJUnitRunner;
78 
79 import java.util.ArrayList;
80 import java.util.Collections;
81 import java.util.List;
82 import java.util.function.BiFunction;
83 
84 /**
85  * Test class for {@link BroadcastRecord}.
86  *
87  * Build/Install/Run:
88  *  atest FrameworksServicesTests:BroadcastRecordTest
89  */
90 @SmallTest
91 @RunWith(MockitoJUnitRunner.class)
92 public class BroadcastRecordTest {
93     private static final String TAG = "BroadcastRecordTest";
94 
95     @Rule
96     public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
97 
98     private static final int USER0 = UserHandle.USER_SYSTEM;
99     private static final String PACKAGE1 = "pkg1";
100     private static final String PACKAGE2 = "pkg2";
101     private static final String PACKAGE3 = "pkg3";
102 
103     private static final String PROCESS1 = "process1";
104     private static final String PROCESS2 = "process2";
105 
106     private static final int SYSTEM_UID = android.os.Process.SYSTEM_UID;
107     private static final int APP_UID = android.os.Process.FIRST_APPLICATION_UID;
108 
109     private static final BroadcastOptions OPT_DEFAULT = BroadcastOptions.makeBasic();
110     private static final BroadcastOptions OPT_NONE = BroadcastOptions.makeBasic()
111             .setDeferralPolicy(BroadcastOptions.DEFERRAL_POLICY_NONE);
112     private static final BroadcastOptions OPT_UNTIL_ACTIVE = BroadcastOptions.makeBasic()
113             .setDeferralPolicy(BroadcastOptions.DEFERRAL_POLICY_UNTIL_ACTIVE);
114 
115     @Mock BroadcastQueue mQueue;
116     @Mock ProcessRecord mProcess;
117     @Mock PlatformCompat mPlatformCompat;
118 
119     @Before
setUp()120     public void setUp() throws Exception {
121         MockitoAnnotations.initMocks(this);
122 
123         doReturn(true).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
124                 eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE), any(ApplicationInfo.class));
125     }
126 
127     @Test
testIsPrioritized_Empty()128     public void testIsPrioritized_Empty() {
129         assertFalse(isPrioritized(List.of()));
130     }
131 
132     @Test
testIsPrioritized_Single()133     public void testIsPrioritized_Single() {
134         assertFalse(isPrioritized(List.of(createResolveInfo(PACKAGE1, getAppId(1), 0))));
135         assertFalse(isPrioritized(List.of(createResolveInfo(PACKAGE1, getAppId(1), -10))));
136         assertFalse(isPrioritized(List.of(createResolveInfo(PACKAGE1, getAppId(1), 10))));
137 
138         assertArrayEquals(new int[] {-1},
139                 calculateBlockedUntilBeyondCount(List.of(
140                         createResolveInfo(PACKAGE1, getAppId(1), 0)), false, mPlatformCompat));
141         assertArrayEquals(new int[] {-1},
142                 calculateBlockedUntilBeyondCount(List.of(
143                         createResolveInfo(PACKAGE1, getAppId(1), -10)), false, mPlatformCompat));
144         assertArrayEquals(new int[] {-1},
145                 calculateBlockedUntilBeyondCount(List.of(
146                         createResolveInfo(PACKAGE1, getAppId(1), 10)), false, mPlatformCompat));
147     }
148 
149     @Test
testIsPrioritized_No()150     public void testIsPrioritized_No() {
151         assertFalse(isPrioritized(List.of(
152                 createResolveInfo(PACKAGE1, getAppId(1), 0),
153                 createResolveInfo(PACKAGE2, getAppId(2), 0),
154                 createResolveInfo(PACKAGE3, getAppId(3), 0))));
155         assertFalse(isPrioritized(List.of(
156                 createResolveInfo(PACKAGE1, getAppId(1), 10),
157                 createResolveInfo(PACKAGE2, getAppId(2), 10),
158                 createResolveInfo(PACKAGE3, getAppId(3), 10))));
159 
160         assertArrayEquals(new int[] {-1, -1, -1},
161                 calculateBlockedUntilBeyondCount(List.of(
162                         createResolveInfo(PACKAGE1, getAppId(1), 0),
163                         createResolveInfo(PACKAGE2, getAppId(2), 0),
164                         createResolveInfo(PACKAGE3, getAppId(3), 0)), false, mPlatformCompat));
165         assertArrayEquals(new int[] {-1, -1, -1},
166                 calculateBlockedUntilBeyondCount(List.of(
167                         createResolveInfo(PACKAGE1, getAppId(1), 10),
168                         createResolveInfo(PACKAGE2, getAppId(2), 10),
169                         createResolveInfo(PACKAGE3, getAppId(3), 10)), false, mPlatformCompat));
170     }
171 
172     @DisableFlags(Flags.FLAG_LIMIT_PRIORITY_SCOPE)
173     @Test
testIsPrioritized_Yes()174     public void testIsPrioritized_Yes() {
175         assertTrue(isPrioritized(List.of(
176                 createResolveInfo(PACKAGE1, getAppId(1), 10),
177                 createResolveInfo(PACKAGE2, getAppId(2), 0),
178                 createResolveInfo(PACKAGE3, getAppId(3), -10))));
179         assertTrue(isPrioritized(List.of(
180                 createResolveInfo(PACKAGE1, getAppId(1), 10),
181                 createResolveInfo(PACKAGE2, getAppId(2), 0),
182                 createResolveInfo(PACKAGE3, getAppId(3), 0))));
183 
184         assertArrayEquals(new int[] {0, 1, 2},
185                 calculateBlockedUntilBeyondCount(List.of(
186                         createResolveInfo(PACKAGE1, getAppId(1), 10),
187                         createResolveInfo(PACKAGE2, getAppId(2), 0),
188                         createResolveInfo(PACKAGE3, getAppId(3), -10)), false, mPlatformCompat));
189         assertArrayEquals(new int[] {0, 0, 2, 3, 3},
190                 calculateBlockedUntilBeyondCount(List.of(
191                         createResolveInfo(PACKAGE1, getAppId(1), 20),
192                         createResolveInfo(PACKAGE2, getAppId(2), 20),
193                         createResolveInfo(PACKAGE3, getAppId(3), 10),
194                         createResolveInfo(PACKAGE3, getAppId(3), 0),
195                         createResolveInfo(PACKAGE3, getAppId(3), 0)), false, mPlatformCompat));
196     }
197 
198     @EnableFlags(Flags.FLAG_LIMIT_PRIORITY_SCOPE)
199     @Test
testIsPrioritized_withDifferentPriorities()200     public void testIsPrioritized_withDifferentPriorities() {
201         assertFalse(isPrioritized(List.of(
202                 createResolveInfo(PACKAGE1, getAppId(1), 10),
203                 createResolveInfo(PACKAGE2, getAppId(2), 0),
204                 createResolveInfo(PACKAGE3, getAppId(3), -10))));
205         assertFalse(isPrioritized(List.of(
206                 createResolveInfo(PACKAGE1, getAppId(1), 10),
207                 createResolveInfo(PACKAGE2, getAppId(2), 0),
208                 createResolveInfo(PACKAGE3, getAppId(3), 0))));
209 
210         assertArrayEquals(new int[] {-1, -1, -1},
211                 calculateBlockedUntilBeyondCount(List.of(
212                         createResolveInfo(PACKAGE1, getAppId(1), 10),
213                         createResolveInfo(PACKAGE2, getAppId(2), 0),
214                         createResolveInfo(PACKAGE3, getAppId(3), -10)), false, mPlatformCompat));
215         assertArrayEquals(new int[] {-1, -1, -1},
216                 calculateBlockedUntilBeyondCount(List.of(
217                         createResolveInfo(PACKAGE1, getAppId(1), 10),
218                         createResolveInfo(PACKAGE2, getAppId(2), 10),
219                         createResolveInfo(PACKAGE3, getAppId(3), -10)), false, mPlatformCompat));
220         assertArrayEquals(new int[] {-1, -1, -1},
221                 calculateBlockedUntilBeyondCount(List.of(
222                         createResolveInfo(PACKAGE1, getAppId(1), 10),
223                         createResolveInfo(PACKAGE2, getAppId(2), 0),
224                         createResolveInfo(PACKAGE3, getAppId(3), 0)), false, mPlatformCompat));
225         assertArrayEquals(new int[] {-1, -1, -1, -1, -1},
226                 calculateBlockedUntilBeyondCount(List.of(
227                         createResolveInfo(PACKAGE1, getAppId(1), 20),
228                         createResolveInfo(PACKAGE2, getAppId(2), 20),
229                         createResolveInfo(PACKAGE3, getAppId(3), 10),
230                         createResolveInfo(PACKAGE3, getAppId(3), 0),
231                         createResolveInfo(PACKAGE3, getAppId(3), 0)), false, mPlatformCompat));
232     }
233 
234     @EnableFlags(Flags.FLAG_LIMIT_PRIORITY_SCOPE)
235     @Test
testIsPrioritized_withDifferentPriorities_withFirstUidChangeIdDisabled()236     public void testIsPrioritized_withDifferentPriorities_withFirstUidChangeIdDisabled() {
237         doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
238                 eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE), argThat(appInfoEquals(getAppId(1))));
239 
240         assertTrue(isPrioritized(List.of(
241                 createResolveInfo(PACKAGE1, getAppId(1), 10),
242                 createResolveInfo(PACKAGE2, getAppId(2), 0),
243                 createResolveInfo(PACKAGE3, getAppId(3), -10))));
244         assertTrue(isPrioritized(List.of(
245                 createResolveInfo(PACKAGE1, getAppId(1), 10),
246                 createResolveInfo(PACKAGE2, getAppId(2), 0),
247                 createResolveInfo(PACKAGE3, getAppId(3), 0))));
248 
249         assertArrayEquals(new int[] {0, 1, 1},
250                 calculateBlockedUntilBeyondCount(List.of(
251                         createResolveInfo(PACKAGE1, getAppId(1), 10),
252                         createResolveInfo(PACKAGE2, getAppId(2), 0),
253                         createResolveInfo(PACKAGE3, getAppId(3), -10)), false, mPlatformCompat));
254         assertArrayEquals(new int[] {0, 0, 1},
255                 calculateBlockedUntilBeyondCount(List.of(
256                         createResolveInfo(PACKAGE1, getAppId(1), 10),
257                         createResolveInfo(PACKAGE2, getAppId(2), 10),
258                         createResolveInfo(PACKAGE3, getAppId(3), -10)), false, mPlatformCompat));
259         assertArrayEquals(new int[] {0, 0, 1, 1, 1},
260                 calculateBlockedUntilBeyondCount(List.of(
261                         createResolveInfo(PACKAGE1, getAppId(1), 20),
262                         createResolveInfo(PACKAGE2, getAppId(2), 20),
263                         createResolveInfo(PACKAGE3, getAppId(3), 10),
264                         createResolveInfo(PACKAGE3, getAppId(3), 0),
265                         createResolveInfo(PACKAGE3, getAppId(3), 0)), false, mPlatformCompat));
266     }
267 
268     @EnableFlags(Flags.FLAG_LIMIT_PRIORITY_SCOPE)
269     @Test
testIsPrioritized_withDifferentPriorities_withLastUidChangeIdDisabled()270     public void testIsPrioritized_withDifferentPriorities_withLastUidChangeIdDisabled() {
271         doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
272                 eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE), argThat(appInfoEquals(getAppId(3))));
273 
274         assertTrue(isPrioritized(List.of(
275                 createResolveInfo(PACKAGE1, getAppId(1), 10),
276                 createResolveInfo(PACKAGE2, getAppId(2), 0),
277                 createResolveInfo(PACKAGE3, getAppId(3), -10))));
278         assertTrue(isPrioritized(List.of(
279                 createResolveInfo(PACKAGE1, getAppId(1), 10),
280                 createResolveInfo(PACKAGE2, getAppId(2), 0),
281                 createResolveInfo(PACKAGE3, getAppId(3), 0))));
282 
283         assertArrayEquals(new int[] {0, 0, 2},
284                 calculateBlockedUntilBeyondCount(List.of(
285                         createResolveInfo(PACKAGE1, getAppId(1), 10),
286                         createResolveInfo(PACKAGE2, getAppId(2), 0),
287                         createResolveInfo(PACKAGE3, getAppId(3), -10)), false, mPlatformCompat));
288         assertArrayEquals(new int[] {0, 1},
289                 calculateBlockedUntilBeyondCount(List.of(
290                         createResolveInfo(PACKAGE1, getAppId(1), 10),
291                         createResolveInfo(PACKAGE3, getAppId(3), 0)), false, mPlatformCompat));
292         assertArrayEquals(new int[] {0, 0, 1},
293                 calculateBlockedUntilBeyondCount(List.of(
294                         createResolveInfo(PACKAGE1, getAppId(1), 10),
295                         createResolveInfo(PACKAGE2, getAppId(2), 0),
296                         createResolveInfo(PACKAGE3, getAppId(3), 0)), false, mPlatformCompat));
297         assertArrayEquals(new int[] {0, 0, 2, 3, 3},
298                 calculateBlockedUntilBeyondCount(List.of(
299                         createResolveInfo(PACKAGE1, getAppId(1), 20),
300                         createResolveInfo(PACKAGE2, getAppId(2), 20),
301                         createResolveInfo(PACKAGE3, getAppId(3), 10),
302                         createResolveInfo(PACKAGE3, getAppId(3), 0),
303                         createResolveInfo(PACKAGE3, getAppId(3), 0)), false, mPlatformCompat));
304     }
305 
306     @EnableFlags(Flags.FLAG_LIMIT_PRIORITY_SCOPE)
307     @Test
testIsPrioritized_withDifferentPriorities_withUidChangeIdDisabled()308     public void testIsPrioritized_withDifferentPriorities_withUidChangeIdDisabled() {
309         doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
310                 eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE), argThat(appInfoEquals(getAppId(2))));
311 
312         assertTrue(isPrioritized(List.of(
313                 createResolveInfo(PACKAGE1, getAppId(1), 10),
314                 createResolveInfo(PACKAGE2, getAppId(2), 0),
315                 createResolveInfo(PACKAGE3, getAppId(3), -10))));
316         assertTrue(isPrioritized(List.of(
317                 createResolveInfo(PACKAGE1, getAppId(1), 10),
318                 createResolveInfo(PACKAGE2, getAppId(2), 0),
319                 createResolveInfo(PACKAGE3, getAppId(3), 0))));
320 
321         assertArrayEquals(new int[] {0, 1, 2},
322                 calculateBlockedUntilBeyondCount(List.of(
323                         createResolveInfo(PACKAGE1, getAppId(1), 10),
324                         createResolveInfo(PACKAGE2, getAppId(2), 0),
325                         createResolveInfo(PACKAGE3, getAppId(3), -10)), false, mPlatformCompat));
326         assertArrayEquals(new int[] {0, 1, 0},
327                 calculateBlockedUntilBeyondCount(List.of(
328                         createResolveInfo(PACKAGE1, getAppId(1), 10),
329                         createResolveInfo(PACKAGE2, getAppId(2), 0),
330                         createResolveInfo(PACKAGE3, getAppId(3), 0)), false, mPlatformCompat));
331         assertArrayEquals(new int[] {0, 0, 2, 2, 2},
332                 calculateBlockedUntilBeyondCount(List.of(
333                         createResolveInfo(PACKAGE1, getAppId(1), 20),
334                         createResolveInfo(PACKAGE2, getAppId(2), 20),
335                         createResolveInfo(PACKAGE3, getAppId(3), 10),
336                         createResolveInfo(PACKAGE3, getAppId(3), 0),
337                         createResolveInfo(PACKAGE3, getAppId(4), 0)), false, mPlatformCompat));
338     }
339 
340     @EnableFlags(Flags.FLAG_LIMIT_PRIORITY_SCOPE)
341     @Test
testIsPrioritized_withDifferentPriorities_withMultipleUidChangeIdDisabled()342     public void testIsPrioritized_withDifferentPriorities_withMultipleUidChangeIdDisabled() {
343         doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
344                 eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE), argThat(appInfoEquals(getAppId(1))));
345         doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
346                 eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE), argThat(appInfoEquals(getAppId(2))));
347 
348         assertTrue(isPrioritized(List.of(
349                 createResolveInfo(PACKAGE1, getAppId(1), 10),
350                 createResolveInfo(PACKAGE2, getAppId(2), 0),
351                 createResolveInfo(PACKAGE3, getAppId(3), -10))));
352         assertTrue(isPrioritized(List.of(
353                 createResolveInfo(PACKAGE1, getAppId(1), 10),
354                 createResolveInfo(PACKAGE2, getAppId(2), 0),
355                 createResolveInfo(PACKAGE3, getAppId(3), 0))));
356 
357         assertArrayEquals(new int[] {0, 1, 2},
358                 calculateBlockedUntilBeyondCount(List.of(
359                         createResolveInfo(PACKAGE1, getAppId(1), 10),
360                         createResolveInfo(PACKAGE2, getAppId(2), 0),
361                         createResolveInfo(PACKAGE3, getAppId(3), -10)), false, mPlatformCompat));
362         assertArrayEquals(new int[] {0, 1, 1},
363                 calculateBlockedUntilBeyondCount(List.of(
364                         createResolveInfo(PACKAGE1, getAppId(1), 10),
365                         createResolveInfo(PACKAGE2, getAppId(2), 0),
366                         createResolveInfo(PACKAGE3, getAppId(3), 0)), false, mPlatformCompat));
367         assertArrayEquals(new int[] {0, 0, 2, 2, 2},
368                 calculateBlockedUntilBeyondCount(List.of(
369                         createResolveInfo(PACKAGE1, getAppId(1), 20),
370                         createResolveInfo(PACKAGE2, getAppId(2), 20),
371                         createResolveInfo(PACKAGE3, getAppId(3), 10),
372                         createResolveInfo(PACKAGE3, getAppId(3), 0),
373                         createResolveInfo(PACKAGE3, getAppId(4), 0)), false, mPlatformCompat));
374         assertArrayEquals(new int[] {0, 0, 1, 1, 3},
375                 calculateBlockedUntilBeyondCount(List.of(
376                         createResolveInfo(PACKAGE1, getAppId(1), 20),
377                         createResolveInfo(PACKAGE3, getAppId(3), 20),
378                         createResolveInfo(PACKAGE3, getAppId(3), 10),
379                         createResolveInfo(PACKAGE3, getAppId(3), 0),
380                         createResolveInfo(PACKAGE2, getAppId(2), 0)), false, mPlatformCompat));
381     }
382 
383     @Test
testSetDeliveryState_Single()384     public void testSetDeliveryState_Single() {
385         final BroadcastRecord r = createBroadcastRecord(
386                 new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED), List.of(
387                         createResolveInfoWithPriority(0)));
388         assertEquals(DELIVERY_PENDING, r.getDeliveryState(0));
389         assertBlocked(r, false);
390         assertTerminalDeferredBeyond(r, 0, 0, 0);
391 
392         r.setDeliveryState(0, DELIVERY_DEFERRED, TAG);
393         assertEquals(DELIVERY_DEFERRED, r.getDeliveryState(0));
394         assertBlocked(r, false);
395         assertTerminalDeferredBeyond(r, 0, 1, 1);
396 
397         // Identical state change has no effect
398         r.setDeliveryState(0, DELIVERY_DEFERRED, TAG);
399         assertEquals(DELIVERY_DEFERRED, r.getDeliveryState(0));
400         assertBlocked(r, false);
401         assertTerminalDeferredBeyond(r, 0, 1, 1);
402 
403         // Moving to terminal state updates counters
404         r.setDeliveryState(0, DELIVERY_DELIVERED, TAG);
405         assertEquals(DELIVERY_DELIVERED, r.getDeliveryState(0));
406         assertBlocked(r, false);
407         assertTerminalDeferredBeyond(r, 1, 0, 1);
408 
409         // Trying to change terminal state has no effect
410         r.setDeliveryState(0, DELIVERY_TIMEOUT, TAG);
411         assertEquals(DELIVERY_DELIVERED, r.getDeliveryState(0));
412         assertBlocked(r, false);
413         assertTerminalDeferredBeyond(r, 1, 0, 1);
414     }
415 
416     @Test
testSetDeliveryState_Unordered()417     public void testSetDeliveryState_Unordered() {
418         final BroadcastRecord r = createBroadcastRecord(
419                 new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED), List.of(
420                         createResolveInfoWithPriority(0),
421                         createResolveInfoWithPriority(0),
422                         createResolveInfoWithPriority(0)));
423         assertBlocked(r, false, false, false);
424         assertTerminalDeferredBeyond(r, 0, 0, 0);
425 
426         // Even though we finish a middle item in the tranche, we're not
427         // "beyond" it because there is still unfinished work before it
428         r.setDeliveryState(1, DELIVERY_DELIVERED, TAG);
429         assertBlocked(r, false, false, false);
430         assertTerminalDeferredBeyond(r, 1, 0, 0);
431 
432         r.setDeliveryState(0, DELIVERY_DELIVERED, TAG);
433         assertBlocked(r, false, false, false);
434         assertTerminalDeferredBeyond(r, 2, 0, 2);
435 
436         r.setDeliveryState(2, DELIVERY_DELIVERED, TAG);
437         assertBlocked(r, false, false, false);
438         assertTerminalDeferredBeyond(r, 3, 0, 3);
439     }
440 
441     @Test
testSetDeliveryState_Ordered()442     public void testSetDeliveryState_Ordered() {
443         final BroadcastRecord r = createOrderedBroadcastRecord(
444                 new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED), List.of(
445                         createResolveInfoWithPriority(0),
446                         createResolveInfoWithPriority(0),
447                         createResolveInfoWithPriority(0)));
448         assertBlocked(r, false, true, true);
449         assertTerminalDeferredBeyond(r, 0, 0, 0);
450 
451         r.setDeliveryState(0, DELIVERY_DELIVERED, TAG);
452         assertBlocked(r, false, false, true);
453         assertTerminalDeferredBeyond(r, 1, 0, 1);
454 
455         r.setDeliveryState(1, DELIVERY_DELIVERED, TAG);
456         assertBlocked(r, false, false, false);
457         assertTerminalDeferredBeyond(r, 2, 0, 2);
458 
459         r.setDeliveryState(2, DELIVERY_DELIVERED, TAG);
460         assertBlocked(r, false, false, false);
461         assertTerminalDeferredBeyond(r, 3, 0, 3);
462     }
463 
464     @DisableFlags(Flags.FLAG_LIMIT_PRIORITY_SCOPE)
465     @Test
testSetDeliveryState_DeferUntilActive_flagDisabled()466     public void testSetDeliveryState_DeferUntilActive_flagDisabled() {
467         final BroadcastRecord r = createBroadcastRecord(
468                 new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED), List.of(
469                         createResolveInfoWithPriority(10),
470                         createResolveInfoWithPriority(10),
471                         createResolveInfoWithPriority(10),
472                         createResolveInfoWithPriority(0),
473                         createResolveInfoWithPriority(0),
474                         createResolveInfoWithPriority(0),
475                         createResolveInfoWithPriority(-10),
476                         createResolveInfoWithPriority(-10),
477                         createResolveInfoWithPriority(-10)));
478         assertBlocked(r, false, false, false, true, true, true, true, true, true);
479         assertTerminalDeferredBeyond(r, 0, 0, 0);
480 
481         r.setDeliveryState(0, DELIVERY_PENDING, TAG);
482         r.setDeliveryState(1, DELIVERY_DEFERRED, TAG);
483         r.setDeliveryState(2, DELIVERY_PENDING, TAG);
484         r.setDeliveryState(3, DELIVERY_DEFERRED, TAG);
485         r.setDeliveryState(4, DELIVERY_DEFERRED, TAG);
486         r.setDeliveryState(5, DELIVERY_DEFERRED, TAG);
487         r.setDeliveryState(6, DELIVERY_DEFERRED, TAG);
488         r.setDeliveryState(7, DELIVERY_PENDING, TAG);
489         r.setDeliveryState(8, DELIVERY_DEFERRED, TAG);
490 
491         // Verify deferred counts ratchet up, but we're not "beyond" the first
492         // still-pending receiver
493         assertBlocked(r, false, false, false, true, true, true, true, true, true);
494         assertTerminalDeferredBeyond(r, 0, 6, 0);
495 
496         // We're still not "beyond" the first still-pending receiver, even when
497         // we finish a receiver later in the first tranche
498         r.setDeliveryState(2, DELIVERY_DELIVERED, TAG);
499         assertBlocked(r, false, false, false, true, true, true, true, true, true);
500         assertTerminalDeferredBeyond(r, 1, 6, 0);
501 
502         // Completing that last item in first tranche means we now unblock the
503         // second tranche, and since it's entirely deferred, the third traunche
504         // is unblocked too
505         r.setDeliveryState(0, DELIVERY_DELIVERED, TAG);
506         assertBlocked(r, false, false, false, false, false, false, false, false, false);
507         assertTerminalDeferredBeyond(r, 2, 6, 7);
508 
509         // Moving a deferred item in an earlier tranche back to being pending
510         // doesn't change the fact that we've already moved beyond it
511         r.setDeliveryState(1, DELIVERY_PENDING, TAG);
512         assertBlocked(r, false, false, false, false, false, false, false, false, false);
513         assertTerminalDeferredBeyond(r, 2, 5, 7);
514         r.setDeliveryState(1, DELIVERY_DELIVERED, TAG);
515         assertBlocked(r, false, false, false, false, false, false, false, false, false);
516         assertTerminalDeferredBeyond(r, 3, 5, 7);
517 
518         // Completing middle pending item is enough to fast-forward to end
519         r.setDeliveryState(7, DELIVERY_DELIVERED, TAG);
520         assertBlocked(r, false, false, false, false, false, false, false, false, false);
521         assertTerminalDeferredBeyond(r, 4, 5, 9);
522 
523         // Moving everyone else directly into a finished state updates all the
524         // terminal counters
525         r.setDeliveryState(3, DELIVERY_SKIPPED, TAG);
526         r.setDeliveryState(4, DELIVERY_SKIPPED, TAG);
527         r.setDeliveryState(5, DELIVERY_SKIPPED, TAG);
528         r.setDeliveryState(6, DELIVERY_SKIPPED, TAG);
529         r.setDeliveryState(8, DELIVERY_SKIPPED, TAG);
530         assertBlocked(r, false, false, false, false, false, false, false, false, false);
531         assertTerminalDeferredBeyond(r, 9, 0, 9);
532     }
533 
534     @EnableFlags(Flags.FLAG_LIMIT_PRIORITY_SCOPE)
535     @Test
testSetDeliveryState_DeferUntilActive()536     public void testSetDeliveryState_DeferUntilActive() {
537         final BroadcastRecord r = createBroadcastRecord(
538                 new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED), List.of(
539                         createResolveInfoWithPriority(10),
540                         createResolveInfoWithPriority(10),
541                         createResolveInfoWithPriority(10),
542                         createResolveInfoWithPriority(0),
543                         createResolveInfoWithPriority(0),
544                         createResolveInfoWithPriority(0),
545                         createResolveInfoWithPriority(-10),
546                         createResolveInfoWithPriority(-10),
547                         createResolveInfoWithPriority(-10)));
548         assertBlocked(r, false, false, false, false, false, false, false, false, false);
549         assertTerminalDeferredBeyond(r, 0, 0, 0);
550 
551         r.setDeliveryState(0, DELIVERY_PENDING, TAG);
552         r.setDeliveryState(1, DELIVERY_DEFERRED, TAG);
553         r.setDeliveryState(2, DELIVERY_PENDING, TAG);
554         r.setDeliveryState(3, DELIVERY_DEFERRED, TAG);
555         r.setDeliveryState(4, DELIVERY_DEFERRED, TAG);
556         r.setDeliveryState(5, DELIVERY_DEFERRED, TAG);
557         r.setDeliveryState(6, DELIVERY_DEFERRED, TAG);
558         r.setDeliveryState(7, DELIVERY_PENDING, TAG);
559         r.setDeliveryState(8, DELIVERY_DEFERRED, TAG);
560 
561         // Verify deferred counts ratchet up, but we're not "beyond" the first
562         // still-pending receiver
563         assertBlocked(r, false, false, false, false, false, false, false, false, false);
564         assertTerminalDeferredBeyond(r, 0, 6, 0);
565 
566         // We're still not "beyond" the first still-pending receiver, even when
567         // we finish a receiver later in the first tranche
568         r.setDeliveryState(2, DELIVERY_DELIVERED, TAG);
569         assertBlocked(r, false, false, false, false, false, false, false, false, false);
570         assertTerminalDeferredBeyond(r, 1, 6, 0);
571 
572         // Completing that last item in first tranche means we now unblock the
573         // second tranche, and since it's entirely deferred, the third traunche
574         // is unblocked too
575         r.setDeliveryState(0, DELIVERY_DELIVERED, TAG);
576         assertBlocked(r, false, false, false, false, false, false, false, false, false);
577         assertTerminalDeferredBeyond(r, 2, 6, 7);
578 
579         // Moving a deferred item in an earlier tranche back to being pending
580         // doesn't change the fact that we've already moved beyond it
581         r.setDeliveryState(1, DELIVERY_PENDING, TAG);
582         assertBlocked(r, false, false, false, false, false, false, false, false, false);
583         assertTerminalDeferredBeyond(r, 2, 5, 7);
584         r.setDeliveryState(1, DELIVERY_DELIVERED, TAG);
585         assertBlocked(r, false, false, false, false, false, false, false, false, false);
586         assertTerminalDeferredBeyond(r, 3, 5, 7);
587 
588         // Completing middle pending item is enough to fast-forward to end
589         r.setDeliveryState(7, DELIVERY_DELIVERED, TAG);
590         assertBlocked(r, false, false, false, false, false, false, false, false, false);
591         assertTerminalDeferredBeyond(r, 4, 5, 9);
592 
593         // Moving everyone else directly into a finished state updates all the
594         // terminal counters
595         r.setDeliveryState(3, DELIVERY_SKIPPED, TAG);
596         r.setDeliveryState(4, DELIVERY_SKIPPED, TAG);
597         r.setDeliveryState(5, DELIVERY_SKIPPED, TAG);
598         r.setDeliveryState(6, DELIVERY_SKIPPED, TAG);
599         r.setDeliveryState(8, DELIVERY_SKIPPED, TAG);
600         assertBlocked(r, false, false, false, false, false, false, false, false, false);
601         assertTerminalDeferredBeyond(r, 9, 0, 9);
602     }
603 
604     @EnableFlags(Flags.FLAG_LIMIT_PRIORITY_SCOPE)
605     @Test
testSetDeliveryState_DeferUntilActive_changeIdDisabled()606     public void testSetDeliveryState_DeferUntilActive_changeIdDisabled() {
607         doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
608                 eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE), argThat(appInfoEquals(getAppId(1))));
609         final BroadcastRecord r = createBroadcastRecord(
610                 new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED), List.of(
611                         createResolveInfo(PACKAGE1, getAppId(1), 10),
612                         createResolveInfo(PACKAGE1, getAppId(1), 10),
613                         createResolveInfo(PACKAGE1, getAppId(1), 10),
614                         createResolveInfo(PACKAGE1, getAppId(1), 0),
615                         createResolveInfo(PACKAGE1, getAppId(1), 0),
616                         createResolveInfo(PACKAGE1, getAppId(1), 0),
617                         createResolveInfo(PACKAGE1, getAppId(1), -10),
618                         createResolveInfo(PACKAGE1, getAppId(1), -10),
619                         createResolveInfo(PACKAGE1, getAppId(1), -10)));
620         assertBlocked(r, false, false, false, true, true, true, true, true, true);
621         assertTerminalDeferredBeyond(r, 0, 0, 0);
622 
623         r.setDeliveryState(0, DELIVERY_PENDING, TAG);
624         r.setDeliveryState(1, DELIVERY_DEFERRED, TAG);
625         r.setDeliveryState(2, DELIVERY_PENDING, TAG);
626         r.setDeliveryState(3, DELIVERY_DEFERRED, TAG);
627         r.setDeliveryState(4, DELIVERY_DEFERRED, TAG);
628         r.setDeliveryState(5, DELIVERY_DEFERRED, TAG);
629         r.setDeliveryState(6, DELIVERY_DEFERRED, TAG);
630         r.setDeliveryState(7, DELIVERY_PENDING, TAG);
631         r.setDeliveryState(8, DELIVERY_DEFERRED, TAG);
632 
633         // Verify deferred counts ratchet up, but we're not "beyond" the first
634         // still-pending receiver
635         assertBlocked(r, false, false, false, true, true, true, true, true, true);
636         assertTerminalDeferredBeyond(r, 0, 6, 0);
637 
638         // We're still not "beyond" the first still-pending receiver, even when
639         // we finish a receiver later in the first tranche
640         r.setDeliveryState(2, DELIVERY_DELIVERED, TAG);
641         assertBlocked(r, false, false, false, true, true, true, true, true, true);
642         assertTerminalDeferredBeyond(r, 1, 6, 0);
643 
644         // Completing that last item in first tranche means we now unblock the
645         // second tranche, and since it's entirely deferred, the third traunche
646         // is unblocked too
647         r.setDeliveryState(0, DELIVERY_DELIVERED, TAG);
648         assertBlocked(r, false, false, false, false, false, false, false, false, false);
649         assertTerminalDeferredBeyond(r, 2, 6, 7);
650 
651         // Moving a deferred item in an earlier tranche back to being pending
652         // doesn't change the fact that we've already moved beyond it
653         r.setDeliveryState(1, DELIVERY_PENDING, TAG);
654         assertBlocked(r, false, false, false, false, false, false, false, false, false);
655         assertTerminalDeferredBeyond(r, 2, 5, 7);
656         r.setDeliveryState(1, DELIVERY_DELIVERED, TAG);
657         assertBlocked(r, false, false, false, false, false, false, false, false, false);
658         assertTerminalDeferredBeyond(r, 3, 5, 7);
659 
660         // Completing middle pending item is enough to fast-forward to end
661         r.setDeliveryState(7, DELIVERY_DELIVERED, TAG);
662         assertBlocked(r, false, false, false, false, false, false, false, false, false);
663         assertTerminalDeferredBeyond(r, 4, 5, 9);
664 
665         // Moving everyone else directly into a finished state updates all the
666         // terminal counters
667         r.setDeliveryState(3, DELIVERY_SKIPPED, TAG);
668         r.setDeliveryState(4, DELIVERY_SKIPPED, TAG);
669         r.setDeliveryState(5, DELIVERY_SKIPPED, TAG);
670         r.setDeliveryState(6, DELIVERY_SKIPPED, TAG);
671         r.setDeliveryState(8, DELIVERY_SKIPPED, TAG);
672         assertBlocked(r, false, false, false, false, false, false, false, false, false);
673         assertTerminalDeferredBeyond(r, 9, 0, 9);
674     }
675 
676     @Test
testGetReceiverIntent_Simple()677     public void testGetReceiverIntent_Simple() {
678         final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
679         final BroadcastRecord r = createBroadcastRecord(
680                 List.of(createResolveInfo(PACKAGE1, getAppId(1))), UserHandle.USER_ALL, intent);
681         final Intent actual = r.getReceiverIntent(r.receivers.get(0));
682         assertEquals(PACKAGE1, actual.getComponent().getPackageName());
683         assertNull(r.intent.getComponent());
684     }
685 
686     @Test
testGetReceiverIntent_Filtered_Partial()687     public void testGetReceiverIntent_Filtered_Partial() {
688         final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
689         intent.putExtra(Intent.EXTRA_INDEX, 42);
690         final BroadcastRecord r = createBroadcastRecord(
691                 List.of(createResolveInfo(PACKAGE1, getAppId(1))), UserHandle.USER_ALL, intent,
692                 (uid, extras) -> Bundle.EMPTY,
693                 null /* options */);
694         final Intent actual = r.getReceiverIntent(r.receivers.get(0));
695         assertEquals(PACKAGE1, actual.getComponent().getPackageName());
696         assertEquals(-1, actual.getIntExtra(Intent.EXTRA_INDEX, -1));
697         assertNull(r.intent.getComponent());
698         assertEquals(42, r.intent.getIntExtra(Intent.EXTRA_INDEX, -1));
699     }
700 
701     @Test
testGetReceiverIntent_Filtered_Complete()702     public void testGetReceiverIntent_Filtered_Complete() {
703         final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
704         intent.putExtra(Intent.EXTRA_INDEX, 42);
705         final BroadcastRecord r = createBroadcastRecord(
706                 List.of(createResolveInfo(PACKAGE1, getAppId(1))), UserHandle.USER_ALL, intent,
707                 (uid, extras) -> null,
708                 null /* options */);
709         final Intent actual = r.getReceiverIntent(r.receivers.get(0));
710         assertNull(actual);
711         assertNull(r.intent.getComponent());
712         assertEquals(42, r.intent.getIntExtra(Intent.EXTRA_INDEX, -1));
713     }
714 
715     @Test
testIsReceiverEquals()716     public void testIsReceiverEquals() {
717         final ResolveInfo info = createResolveInfo(PACKAGE1, getAppId(1));
718         assertTrue(isReceiverEquals(info, info));
719         assertTrue(isReceiverEquals(info, createResolveInfo(PACKAGE1, getAppId(1))));
720         assertFalse(isReceiverEquals(info, createResolveInfo(PACKAGE2, getAppId(2))));
721     }
722 
723     @Test
testCalculateUrgent()724     public void testCalculateUrgent() {
725         final Intent intent = new Intent();
726         final Intent intentForeground = new Intent()
727                 .setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
728 
729         assertFalse(calculateUrgent(intent, null));
730         assertTrue(calculateUrgent(intentForeground, null));
731 
732         {
733             final BroadcastOptions opts = BroadcastOptions.makeBasic();
734             assertFalse(calculateUrgent(intent, opts));
735         }
736         {
737             final BroadcastOptions opts = BroadcastOptions.makeBasic();
738             opts.setInteractive(true);
739             assertTrue(calculateUrgent(intent, opts));
740         }
741         {
742             final BroadcastOptions opts = BroadcastOptions.makeBasic();
743             opts.setAlarmBroadcast(true);
744             assertTrue(calculateUrgent(intent, opts));
745         }
746     }
747 
748     @Test
testCalculateDeferUntilActive_App()749     public void testCalculateDeferUntilActive_App() {
750         // Verify non-urgent behavior
751         assertFalse(calculateDeferUntilActive(APP_UID, null, null, false, false));
752         assertFalse(calculateDeferUntilActive(APP_UID, OPT_DEFAULT, null, false, false));
753         assertFalse(calculateDeferUntilActive(APP_UID, OPT_NONE, null, false, false));
754         assertTrue(calculateDeferUntilActive(APP_UID, OPT_UNTIL_ACTIVE, null, false, false));
755 
756         // Verify urgent behavior
757         assertFalse(calculateDeferUntilActive(APP_UID, null, null, false, true));
758         assertFalse(calculateDeferUntilActive(APP_UID, OPT_DEFAULT, null, false, true));
759         assertFalse(calculateDeferUntilActive(APP_UID, OPT_NONE, null, false, true));
760         assertTrue(calculateDeferUntilActive(APP_UID, OPT_UNTIL_ACTIVE, null, false, true));
761     }
762 
763     @Test
testCalculateDeferUntilActive_System()764     public void testCalculateDeferUntilActive_System() {
765         BroadcastRecord.CORE_DEFER_UNTIL_ACTIVE = true;
766 
767         // Verify non-urgent behavior
768         assertTrue(calculateDeferUntilActive(SYSTEM_UID, null, null, false, false));
769         assertTrue(calculateDeferUntilActive(SYSTEM_UID, OPT_DEFAULT, null, false, false));
770         assertFalse(calculateDeferUntilActive(SYSTEM_UID, OPT_NONE, null, false, false));
771         assertTrue(calculateDeferUntilActive(SYSTEM_UID, OPT_UNTIL_ACTIVE, null, false, false));
772 
773         // Verify urgent behavior
774         assertFalse(calculateDeferUntilActive(SYSTEM_UID, null, null, false, true));
775         assertFalse(calculateDeferUntilActive(SYSTEM_UID, OPT_DEFAULT, null, false, true));
776         assertFalse(calculateDeferUntilActive(SYSTEM_UID, OPT_NONE, null, false, true));
777         assertTrue(calculateDeferUntilActive(SYSTEM_UID, OPT_UNTIL_ACTIVE, null, false, true));
778     }
779 
780     @Test
testCalculateDeferUntilActive_Overrides()781     public void testCalculateDeferUntilActive_Overrides() {
782         final IIntentReceiver resultTo = new IIntentReceiver.Default();
783 
784         // Ordered broadcasts never deferred; requested option is ignored
785         assertFalse(calculateDeferUntilActive(APP_UID, OPT_UNTIL_ACTIVE, null, true, false));
786         assertFalse(calculateDeferUntilActive(APP_UID, OPT_UNTIL_ACTIVE, resultTo, true, false));
787 
788         // Unordered with result is always deferred; requested option is ignored
789         assertTrue(calculateDeferUntilActive(APP_UID, OPT_NONE, resultTo, false, false));
790     }
791 
792     @Test
testCleanupDisabledPackageReceivers()793     public void testCleanupDisabledPackageReceivers() {
794         final int user0 = UserHandle.USER_SYSTEM;
795         final int user1 = user0 + 1;
796         final String pkgToCleanup = "pkg.a";
797         final String pkgOther = "pkg.b";
798 
799         // Receivers contain multiple-user (contains [pkg.a@u0, pkg.a@u1, pkg.b@u0, pkg.b@u1]).
800         final List<ResolveInfo> receiversM = createReceiverInfos(
801                 new String[] { pkgToCleanup, pkgOther },
802                 new int[] { user0, user1 });
803         // Receivers only contain one user (contains [pkg.a@u0, pkg.b@u0]).
804         final List<ResolveInfo> receiversU0 = excludeReceivers(
805                 receiversM, null /* packageName */, user1);
806 
807         // With given package:
808         // Send to all users, cleanup a package of all users.
809         final BroadcastRecord recordAllAll = createBroadcastRecord(receiversM, UserHandle.USER_ALL,
810                 new Intent());
811         cleanupDisabledPackageReceivers(recordAllAll, pkgToCleanup, UserHandle.USER_ALL);
812         assertNull(verifyRemaining(recordAllAll, excludeReceivers(receiversM, pkgToCleanup, -1)));
813 
814         // Send to all users, cleanup a package of one user.
815         final BroadcastRecord recordAllOne = createBroadcastRecord(receiversM, UserHandle.USER_ALL,
816                 new Intent());
817         cleanupDisabledPackageReceivers(recordAllOne, pkgToCleanup, user0);
818         assertNull(verifyRemaining(recordAllOne,
819                 excludeReceivers(receiversM, pkgToCleanup, user0)));
820 
821         // Send to one user, cleanup a package of all users.
822         final BroadcastRecord recordOneAll = createBroadcastRecord(receiversU0, user0,
823                 new Intent());
824         cleanupDisabledPackageReceivers(recordOneAll, pkgToCleanup, UserHandle.USER_ALL);
825         assertNull(verifyRemaining(recordOneAll, excludeReceivers(receiversU0, pkgToCleanup, -1)));
826 
827         // Send to one user, cleanup a package one user.
828         final BroadcastRecord recordOneOne = createBroadcastRecord(receiversU0, user0,
829                 new Intent());
830         cleanupDisabledPackageReceivers(recordOneOne, pkgToCleanup, user0);
831         assertNull(verifyRemaining(recordOneOne, excludeReceivers(receiversU0, pkgToCleanup, -1)));
832 
833         // Without given package (e.g. stop user):
834         // Send to all users, cleanup one user.
835         final BroadcastRecord recordAllM = createBroadcastRecord(receiversM, UserHandle.USER_ALL,
836                 new Intent());
837         cleanupDisabledPackageReceivers(recordAllM, null /* packageName */, user1);
838         assertNull(verifyRemaining(recordAllM,
839                 excludeReceivers(receiversM, null /* packageName */, user1)));
840 
841         // Send to one user, cleanup one user.
842         final BroadcastRecord recordU0 = createBroadcastRecord(receiversU0, user0, new Intent());
843         cleanupDisabledPackageReceivers(recordU0, null /* packageName */, user0);
844         assertNull(verifyRemaining(recordU0, Collections.emptyList()));
845     }
846 
847     @Test
testMatchesDeliveryGroup()848     public void testMatchesDeliveryGroup() {
849         final List<ResolveInfo> receivers = List.of(createResolveInfo(PACKAGE1, getAppId(1)));
850 
851         final Intent intent1 = new Intent(Intent.ACTION_SERVICE_STATE);
852         intent1.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, 0);
853         intent1.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, 1);
854         final BroadcastOptions options1 = BroadcastOptions.makeBasic();
855         final BroadcastRecord record1 = createBroadcastRecord(receivers, UserHandle.USER_ALL,
856                 intent1, options1);
857 
858         final Intent intent2 = new Intent(Intent.ACTION_SERVICE_STATE);
859         intent2.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, 0);
860         intent2.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, 2);
861         final BroadcastOptions options2 = BroadcastOptions.makeBasic();
862         final BroadcastRecord record2 = createBroadcastRecord(receivers, UserHandle.USER_ALL,
863                 intent2, options2);
864 
865         assertTrue(record2.matchesDeliveryGroup(record1));
866     }
867 
868     @Test
testMatchesDeliveryGroup_withMatchingKey()869     public void testMatchesDeliveryGroup_withMatchingKey() {
870         final List<ResolveInfo> receivers = List.of(createResolveInfo(PACKAGE1, getAppId(1)));
871 
872         final Intent intent1 = new Intent(Intent.ACTION_SERVICE_STATE);
873         intent1.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, 0);
874         intent1.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, 1);
875         final BroadcastOptions options1 = BroadcastOptions.makeBasic();
876         options1.setDeliveryGroupMatchingKey(Intent.ACTION_SERVICE_STATE, "key1");
877         final BroadcastRecord record1 = createBroadcastRecord(receivers, UserHandle.USER_ALL,
878                 intent1, options1);
879 
880         final Intent intent2 = new Intent(Intent.ACTION_SERVICE_STATE);
881         intent2.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, 0);
882         intent2.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, 2);
883         final BroadcastOptions options2 = BroadcastOptions.makeBasic();
884         options2.setDeliveryGroupMatchingKey(Intent.ACTION_SERVICE_STATE, "key2");
885         final BroadcastRecord record2 = createBroadcastRecord(receivers, UserHandle.USER_ALL,
886                 intent2, options2);
887 
888         final Intent intent3 = new Intent(Intent.ACTION_SERVICE_STATE);
889         intent3.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, 1);
890         intent3.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, 3);
891         final BroadcastOptions options3 = BroadcastOptions.makeBasic();
892         options3.setDeliveryGroupMatchingKey(Intent.ACTION_SERVICE_STATE, "key1");
893         final BroadcastRecord record3 = createBroadcastRecord(receivers, UserHandle.USER_ALL,
894                 intent3, options3);
895 
896         // record2 and record1 have different matching keys, so their delivery groups
897         // shouldn't match
898         assertFalse(record2.matchesDeliveryGroup(record1));
899         // record3 and record2 have different matching keys, so their delivery groups
900         // shouldn't match
901         assertFalse(record3.matchesDeliveryGroup(record2));
902         // record3 and record1 have same matching keys, so their delivery groups should match even
903         // if the intent has different extras.
904         assertTrue(record3.matchesDeliveryGroup(record1));
905     }
906 
907     @Test
testMatchesDeliveryGroup_withMatchingFilter()908     public void testMatchesDeliveryGroup_withMatchingFilter() {
909         final List<ResolveInfo> receivers = List.of(createResolveInfo(PACKAGE1, getAppId(1)));
910 
911         final Intent intent1 = new Intent(Intent.ACTION_SERVICE_STATE);
912         intent1.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, 0);
913         intent1.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, 1);
914         intent1.putExtra(Intent.EXTRA_REASON, "reason1");
915         final IntentFilter filter1 = new IntentFilter(Intent.ACTION_SERVICE_STATE);
916         final PersistableBundle bundle1 = new PersistableBundle();
917         bundle1.putInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, 0);
918         bundle1.putInt(SubscriptionManager.EXTRA_SLOT_INDEX, 1);
919         filter1.setExtras(bundle1);
920         final BroadcastOptions options1 = BroadcastOptions.makeBasic();
921         options1.setDeliveryGroupMatchingFilter(filter1);
922         final BroadcastRecord record1 = createBroadcastRecord(receivers, UserHandle.USER_ALL,
923                 intent1, options1);
924 
925         final Intent intent2 = new Intent(Intent.ACTION_SERVICE_STATE);
926         intent2.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, 0);
927         intent2.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, 2);
928         intent2.putExtra(Intent.EXTRA_REASON, "reason2");
929         final IntentFilter filter2 = new IntentFilter(Intent.ACTION_SERVICE_STATE);
930         final PersistableBundle bundle2 = new PersistableBundle();
931         bundle2.putInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, 0);
932         bundle2.putInt(SubscriptionManager.EXTRA_SLOT_INDEX, 2);
933         filter2.setExtras(bundle2);
934         final BroadcastOptions options2 = BroadcastOptions.makeBasic();
935         options2.setDeliveryGroupMatchingFilter(filter2);
936         final BroadcastRecord record2 = createBroadcastRecord(receivers, UserHandle.USER_ALL,
937                 intent2, options2);
938 
939         final Intent intent3 = new Intent(Intent.ACTION_SERVICE_STATE);
940         intent3.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, 1);
941         intent3.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, 3);
942         intent3.putExtra(Intent.EXTRA_REASON, "reason3");
943         final IntentFilter filter3 = new IntentFilter(Intent.ACTION_SERVICE_STATE);
944         final PersistableBundle bundle3 = new PersistableBundle();
945         bundle3.putInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, 0);
946         bundle3.putInt(SubscriptionManager.EXTRA_SLOT_INDEX, 1);
947         filter3.setExtras(bundle3);
948         final BroadcastOptions options3 = BroadcastOptions.makeBasic();
949         options3.setDeliveryGroupMatchingFilter(filter3);
950         final BroadcastRecord record3 = createBroadcastRecord(receivers, UserHandle.USER_ALL,
951                 intent3, options3);
952 
953         // record2's matchingFilter doesn't match record1's intent, so their delivery groups
954         // shouldn't match
955         assertFalse(record2.matchesDeliveryGroup(record1));
956         // record3's matchingFilter doesn't match record2's intent, so their delivery groups
957         // shouldn't match
958         assertFalse(record3.matchesDeliveryGroup(record2));
959         // record3's matchingFilter matches record1's intent, so their delivery groups should match.
960         assertTrue(record3.matchesDeliveryGroup(record1));
961     }
962 
963     @Test
testCalculateChangeStateForReceivers()964     public void testCalculateChangeStateForReceivers() {
965         assertArrayEquals(new boolean[] {true, true, true}, calculateChangeState(
966                 List.of(createResolveInfo(PACKAGE1, getAppId(1)),
967                         createResolveInfo(PACKAGE2, getAppId(2)),
968                         createResolveInfo(PACKAGE3, getAppId(3)))));
969         assertArrayEquals(new boolean[] {true, true, true, true}, calculateChangeState(
970                 List.of(createResolveInfo(PACKAGE1, getAppId(1)),
971                         createResolveInfo(PACKAGE2, getAppId(2)),
972                         createResolveInfo(PACKAGE2, getAppId(2)),
973                         createResolveInfo(PACKAGE3, getAppId(3)))));
974 
975         doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
976                 eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE), argThat(appInfoEquals(getAppId(1))));
977         assertArrayEquals(new boolean[] {false, true, true}, calculateChangeState(
978                 List.of(createResolveInfo(PACKAGE1, getAppId(1)),
979                         createResolveInfo(PACKAGE2, getAppId(2)),
980                         createResolveInfo(PACKAGE3, getAppId(3)))));
981         assertArrayEquals(new boolean[] {false, true, false, true}, calculateChangeState(
982                 List.of(createResolveInfo(PACKAGE1, getAppId(1)),
983                         createResolveInfo(PACKAGE2, getAppId(2)),
984                         createResolveInfo(PACKAGE1, getAppId(1)),
985                         createResolveInfo(PACKAGE3, getAppId(3)))));
986 
987         doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
988                 eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE), argThat(appInfoEquals(getAppId(2))));
989         assertArrayEquals(new boolean[] {false, false, true}, calculateChangeState(
990                 List.of(createResolveInfo(PACKAGE1, getAppId(1)),
991                         createResolveInfo(PACKAGE2, getAppId(2)),
992                         createResolveInfo(PACKAGE3, getAppId(3)))));
993         assertArrayEquals(new boolean[] {false, true, false, false, false, true},
994                 calculateChangeState(
995                         List.of(createResolveInfo(PACKAGE1, getAppId(1)),
996                                 createResolveInfo(PACKAGE3, getAppId(3)),
997                                 createResolveInfo(PACKAGE2, getAppId(2)),
998                                 createResolveInfo(PACKAGE2, getAppId(1)),
999                                 createResolveInfo(PACKAGE2, getAppId(2)),
1000                                 createResolveInfo(PACKAGE3, getAppId(3)))));
1001 
1002         doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
1003                 eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE), argThat(appInfoEquals(getAppId(3))));
1004         assertArrayEquals(new boolean[] {false, false, false}, calculateChangeState(
1005                 List.of(createResolveInfo(PACKAGE1, getAppId(1)),
1006                         createResolveInfo(PACKAGE2, getAppId(2)),
1007                         createResolveInfo(PACKAGE3, getAppId(3)))));
1008         assertArrayEquals(new boolean[] {false, false, false, false, false, false},
1009                 calculateChangeState(
1010                         List.of(createResolveInfo(PACKAGE1, getAppId(1)),
1011                                 createResolveInfo(PACKAGE3, getAppId(3)),
1012                                 createResolveInfo(PACKAGE2, getAppId(2)),
1013                                 createResolveInfo(PACKAGE1, getAppId(1)),
1014                                 createResolveInfo(PACKAGE2, getAppId(2)),
1015                                 createResolveInfo(PACKAGE3, getAppId(3)))));
1016     }
1017 
1018 
1019     @Test
1020     @DisableFlags(Flags.FLAG_LOG_BROADCAST_PROCESSED_EVENT)
testUpdateBroadcastProcessedEventRecord_flagDisabled()1021     public void testUpdateBroadcastProcessedEventRecord_flagDisabled() {
1022         final ResolveInfo receiver = createResolveInfo(PACKAGE1, getAppId(1));
1023         final BroadcastRecord record = createBroadcastRecord(
1024                 new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED),
1025                 List.of(receiver));
1026 
1027         record.updateBroadcastProcessedEventRecord(receiver, 10);
1028 
1029         assertThat(record.getBroadcastProcessedRecordsForTest()).isEmpty();
1030     }
1031 
1032     @Test
1033     @EnableFlags(Flags.FLAG_LOG_BROADCAST_PROCESSED_EVENT)
testUpdateBroadcastProcessedEventRecord_withNewReceiver_newBroadcastProcessedEventRecordCreated()1034     public void testUpdateBroadcastProcessedEventRecord_withNewReceiver_newBroadcastProcessedEventRecordCreated() {
1035         final ResolveInfo receiver =
1036                 createResolveInfoWithProcessName(PACKAGE1, getAppId(1), PROCESS1);
1037         final BroadcastRecord record = createBroadcastRecord(
1038                 new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED),
1039                 List.of(receiver));
1040 
1041         record.updateBroadcastProcessedEventRecord(receiver, 10);
1042 
1043         assertThat(record.getBroadcastProcessedRecordsForTest()).isNotEmpty();
1044         final BroadcastProcessedEventRecord broadcastProcessedEventRecord =
1045                 record.getBroadcastProcessedRecordsForTest().get(
1046                         BroadcastRecord.getReceiverProcessName(receiver));
1047 
1048         assertBroadcastProcessedEvent(
1049                 broadcastProcessedEventRecord,
1050                 10001,
1051                 PROCESS1,
1052                 1,
1053                 2,
1054                 10,
1055                 10);
1056     }
1057 
1058     @Test
1059     @EnableFlags(Flags.FLAG_LOG_BROADCAST_PROCESSED_EVENT)
testUpdateBroadcastProcessedEventRecord_withNewAndExistingReceiver_multipleBroadcastProcessedEventRecordCreated()1060     public void testUpdateBroadcastProcessedEventRecord_withNewAndExistingReceiver_multipleBroadcastProcessedEventRecordCreated() {
1061         final ResolveInfo receiver1 =
1062                 createResolveInfoWithProcessName(PACKAGE1, getAppId(1), PROCESS1);
1063 
1064         final ResolveInfo receiver2 =
1065                 createResolveInfoWithProcessName(PACKAGE2, getAppId(2), PROCESS2);
1066 
1067         final BroadcastRecord record = createBroadcastRecord(
1068                 new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED),
1069                 List.of(receiver1, receiver2));
1070 
1071         record.updateBroadcastProcessedEventRecord(receiver1, 11);
1072         record.updateBroadcastProcessedEventRecord(receiver2, 11);
1073         record.updateBroadcastProcessedEventRecord(receiver1, 20);
1074 
1075         assertThat(record.getBroadcastProcessedRecordsForTest().size()).isEqualTo(2);
1076         final BroadcastProcessedEventRecord broadcastProcessedEventRecord1 =
1077                 record.getBroadcastProcessedRecordsForTest().get(
1078                         BroadcastRecord.getReceiverProcessName(receiver1));
1079         final BroadcastProcessedEventRecord broadcastProcessedEventRecord2 =
1080                 record.getBroadcastProcessedRecordsForTest().get(
1081                         BroadcastRecord.getReceiverProcessName(receiver2));
1082 
1083         assertBroadcastProcessedEvent(
1084                 broadcastProcessedEventRecord1,
1085                 10001,
1086                 PROCESS1,
1087                 2,
1088                 1,
1089                 31,
1090                 20);
1091         assertBroadcastProcessedEvent(
1092                 broadcastProcessedEventRecord2,
1093                 10002,
1094                 PROCESS2,
1095                 1,
1096                 1,
1097                 11,
1098                 11);
1099     }
1100 
1101     @Test
1102     @DisableFlags(Flags.FLAG_LOG_BROADCAST_PROCESSED_EVENT)
testLogBroadcastProcessedEventRecord_flagDisabled()1103     public void testLogBroadcastProcessedEventRecord_flagDisabled() {
1104         testLogBroadcastProcessedEventRecord(0);
1105     }
1106 
1107     @Test
1108     @EnableFlags(Flags.FLAG_LOG_BROADCAST_PROCESSED_EVENT)
testLogBroadcastProcessedEventRecord_flagEnabled_allBroadcastProcessedEventLogged()1109     public void testLogBroadcastProcessedEventRecord_flagEnabled_allBroadcastProcessedEventLogged() {
1110         testLogBroadcastProcessedEventRecord(1);
1111     }
1112 
testLogBroadcastProcessedEventRecord(int times)1113     private void testLogBroadcastProcessedEventRecord(int times) {
1114         final ResolveInfo receiver = createResolveInfo(PACKAGE1, getAppId(1));
1115         final BroadcastRecord record = createBroadcastRecord(
1116                 new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED),
1117                 List.of(receiver));
1118 
1119         final BroadcastProcessedEventRecord broadcastProcessedEventRecord = Mockito.mock(
1120                 BroadcastProcessedEventRecord.class);
1121         record.getBroadcastProcessedRecordsForTest()
1122                 .put("process", broadcastProcessedEventRecord);
1123         record.logBroadcastProcessedEventRecord();
1124         doNothing().when(broadcastProcessedEventRecord).logToStatsD();
1125 
1126         verify(broadcastProcessedEventRecord, times(times)).logToStatsD();
1127     }
1128 
assertBroadcastProcessedEvent( BroadcastProcessedEventRecord broadcastProcessedEventRecord, int receiverUid, String processName, int numberOfReceivers, int broadcastTypeLength, long totalBroadcastFinishTimeMillis, long maxReceiverFinishTimeMillis)1129     private void assertBroadcastProcessedEvent(
1130             BroadcastProcessedEventRecord broadcastProcessedEventRecord,
1131             int receiverUid,
1132             String processName,
1133             int numberOfReceivers,
1134             int broadcastTypeLength,
1135             long totalBroadcastFinishTimeMillis,
1136             long maxReceiverFinishTimeMillis) {
1137         assertNotNull(broadcastProcessedEventRecord);
1138         assertThat(broadcastProcessedEventRecord.getReceiverUidForTest()).isEqualTo(receiverUid);
1139         assertThat(broadcastProcessedEventRecord.getSenderUidForTest()).isEqualTo(0);
1140         assertThat(broadcastProcessedEventRecord.getIntentActionForTest())
1141                 .isEqualTo(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1142         assertThat(broadcastProcessedEventRecord.getReceiverProcessNameForTest())
1143                 .isEqualTo(processName);
1144         assertThat(broadcastProcessedEventRecord.getBroadcastTypesForTest().length)
1145                 .isEqualTo(broadcastTypeLength);
1146         assertThat(broadcastProcessedEventRecord.getNumberOfReceiversForTest())
1147                 .isEqualTo(numberOfReceivers);
1148         assertThat(broadcastProcessedEventRecord.getTotalBroadcastFinishTimeMillisForTest())
1149                 .isEqualTo(totalBroadcastFinishTimeMillis);
1150         assertThat(broadcastProcessedEventRecord.getMaxReceiverFinishTimeMillisForTest())
1151                 .isEqualTo(maxReceiverFinishTimeMillis);
1152     }
1153 
calculateChangeState(List<Object> receivers)1154     private boolean[] calculateChangeState(List<Object> receivers) {
1155         return BroadcastRecord.calculateChangeStateForReceivers(receivers,
1156                 LIMIT_PRIORITY_SCOPE, mPlatformCompat);
1157     }
1158 
cleanupDisabledPackageReceivers(BroadcastRecord record, String packageName, int userId)1159     private static void cleanupDisabledPackageReceivers(BroadcastRecord record,
1160             String packageName, int userId) {
1161         record.cleanupDisabledPackageReceiversLocked(packageName, null /* filterByClasses */,
1162                 userId, true /* doit */);
1163     }
1164 
verifyRemaining(BroadcastRecord record, List<ResolveInfo> expectedRemainingReceivers)1165     private static String verifyRemaining(BroadcastRecord record,
1166             List<ResolveInfo> expectedRemainingReceivers) {
1167         final StringBuilder errorMsg = new StringBuilder();
1168 
1169         for (final Object receiver : record.receivers) {
1170             final ResolveInfo resolveInfo = (ResolveInfo) receiver;
1171             final ApplicationInfo appInfo = resolveInfo.activityInfo.applicationInfo;
1172 
1173             boolean foundExpected = false;
1174             for (final ResolveInfo expectedReceiver : expectedRemainingReceivers) {
1175                 final ApplicationInfo expectedAppInfo =
1176                         expectedReceiver.activityInfo.applicationInfo;
1177                 if (appInfo.packageName.equals(expectedAppInfo.packageName)
1178                         && UserHandle.getUserId(appInfo.uid) == UserHandle
1179                                 .getUserId(expectedAppInfo.uid)) {
1180                     foundExpected = true;
1181                     break;
1182                 }
1183             }
1184             if (!foundExpected) {
1185                 errorMsg.append(appInfo.packageName).append("@")
1186                         .append('u').append(UserHandle.getUserId(appInfo.uid)).append(' ');
1187             }
1188         }
1189 
1190         return errorMsg.length() == 0 ? null
1191                 : errorMsg.insert(0, "Contains unexpected receiver: ").toString();
1192     }
1193 
createResolveInfoWithPriority(int priority)1194     private static ResolveInfo createResolveInfoWithPriority(int priority) {
1195         return createResolveInfo(PACKAGE1, getAppId(1), priority);
1196     }
1197 
createResolveInfoWithProcessName( String packageName, int uid, String processName)1198     private static ResolveInfo createResolveInfoWithProcessName(
1199             String packageName,
1200             int uid,
1201             String processName) {
1202         final ResolveInfo resolveInfo = createResolveInfo(packageName, uid);
1203         resolveInfo.activityInfo.processName = processName;
1204 
1205         return resolveInfo;
1206     }
1207 
createResolveInfo(String packageName, int uid)1208     private static ResolveInfo createResolveInfo(String packageName, int uid) {
1209         return createResolveInfo(packageName, uid, 0);
1210     }
1211 
createResolveInfo(String packageName, int uid, int priority)1212     private static ResolveInfo createResolveInfo(String packageName, int uid, int priority) {
1213         final ResolveInfo resolveInfo = new ResolveInfo();
1214         final ActivityInfo activityInfo = new ActivityInfo();
1215         final ApplicationInfo appInfo = new ApplicationInfo();
1216         appInfo.packageName = packageName;
1217         appInfo.uid = uid;
1218         activityInfo.applicationInfo = appInfo;
1219         activityInfo.packageName = packageName;
1220         activityInfo.name = packageName + ".MyReceiver";
1221         resolveInfo.activityInfo = activityInfo;
1222         resolveInfo.priority = priority;
1223         return resolveInfo;
1224     }
1225 
1226     /**
1227      * Generate (packages.length * userIds.length) receivers.
1228      */
createReceiverInfos(String[] packages, int[] userIds)1229     private static List<ResolveInfo> createReceiverInfos(String[] packages, int[] userIds) {
1230         final List<ResolveInfo> receivers = new ArrayList<>();
1231         for (int i = 0; i < packages.length; i++) {
1232             for (final int userId : userIds) {
1233                 receivers.add(createResolveInfo(packages[i],
1234                         UserHandle.getUid(userId, getAppId(i))));
1235             }
1236         }
1237         return receivers;
1238     }
1239 
1240     /**
1241      * Create a new list which filters out item if package name or user id is matched.
1242      * Null package name or user id < 0 will be considered as don't care.
1243      */
excludeReceivers(List<ResolveInfo> receivers, String packageName, int userId)1244     private static List<ResolveInfo> excludeReceivers(List<ResolveInfo> receivers,
1245             String packageName, int userId) {
1246         final List<ResolveInfo> excludedList = new ArrayList<>();
1247         for (final ResolveInfo receiver : receivers) {
1248             if ((packageName != null
1249                     && !packageName.equals(receiver.activityInfo.applicationInfo.packageName))
1250                     || (userId > -1 && userId != UserHandle
1251                             .getUserId(receiver.activityInfo.applicationInfo.uid))) {
1252                 excludedList.add(receiver);
1253             }
1254         }
1255         return excludedList;
1256     }
1257 
createBroadcastRecord(Intent intent, List<ResolveInfo> receivers)1258     private BroadcastRecord createBroadcastRecord(Intent intent,
1259             List<ResolveInfo> receivers) {
1260         return createBroadcastRecord(receivers, USER0, intent, null /* filterExtrasForReceiver */,
1261                 null /* options */, false);
1262     }
1263 
createOrderedBroadcastRecord(Intent intent, List<ResolveInfo> receivers)1264     private BroadcastRecord createOrderedBroadcastRecord(Intent intent,
1265             List<ResolveInfo> receivers) {
1266         return createBroadcastRecord(receivers, USER0, intent, null /* filterExtrasForReceiver */,
1267                 null /* options */, true);
1268     }
1269 
createBroadcastRecord(List<ResolveInfo> receivers, int userId, Intent intent)1270     private BroadcastRecord createBroadcastRecord(List<ResolveInfo> receivers, int userId,
1271             Intent intent) {
1272         return createBroadcastRecord(receivers, userId, intent, null /* filterExtrasForReceiver */,
1273                 null /* options */, false);
1274     }
1275 
createBroadcastRecord(List<ResolveInfo> receivers, int userId, Intent intent, BroadcastOptions options)1276     private BroadcastRecord createBroadcastRecord(List<ResolveInfo> receivers, int userId,
1277             Intent intent, BroadcastOptions options) {
1278         return createBroadcastRecord(receivers, userId, intent, null /* filterExtrasForReceiver */,
1279                 options, false);
1280     }
1281 
createBroadcastRecord(List<ResolveInfo> receivers, int userId, Intent intent, BiFunction<Integer, Bundle, Bundle> filterExtrasForReceiver, BroadcastOptions options)1282     private BroadcastRecord createBroadcastRecord(List<ResolveInfo> receivers, int userId,
1283             Intent intent, BiFunction<Integer, Bundle, Bundle> filterExtrasForReceiver,
1284             BroadcastOptions options) {
1285         return createBroadcastRecord(receivers, userId, intent, filterExtrasForReceiver,
1286                 options, false);
1287     }
1288 
createBroadcastRecord(List<ResolveInfo> receivers, int userId, Intent intent, BiFunction<Integer, Bundle, Bundle> filterExtrasForReceiver, BroadcastOptions options, boolean ordered)1289     private BroadcastRecord createBroadcastRecord(List<ResolveInfo> receivers, int userId,
1290             Intent intent, BiFunction<Integer, Bundle, Bundle> filterExtrasForReceiver,
1291             BroadcastOptions options, boolean ordered) {
1292         return new BroadcastRecord(
1293                 mQueue /* queue */,
1294                 intent,
1295                 mProcess /* callerApp */,
1296                 PACKAGE1 /* callerPackage */,
1297                 null /* callerFeatureId */,
1298                 0 /* callingPid */,
1299                 0 /* callingUid */,
1300                 false /* callerInstantApp */,
1301                 null /* resolvedType */,
1302                 null /* requiredPermissions */,
1303                 null /* excludedPermissions */,
1304                 null /* excludedPackages */,
1305                 0 /* appOp */,
1306                 options,
1307                 new ArrayList<>(receivers), // Make a copy to not affect the original list.
1308                 null /* resultToApp */,
1309                 null /* resultTo */,
1310                 0 /* resultCode */,
1311                 null /* resultData */,
1312                 null /* resultExtras */,
1313                 ordered /* serialized */,
1314                 false /* sticky */,
1315                 false /* initialSticky */,
1316                 userId,
1317                 BackgroundStartPrivileges.NONE,
1318                 false /* timeoutExempt */,
1319                 filterExtrasForReceiver,
1320                 PROCESS_STATE_UNKNOWN,
1321                 mPlatformCompat);
1322     }
1323 
getAppId(int i)1324     private static int getAppId(int i) {
1325         return Process.FIRST_APPLICATION_UID + i;
1326     }
1327 
isPrioritized(List<Object> receivers)1328     private boolean isPrioritized(List<Object> receivers) {
1329         return BroadcastRecord.isPrioritized(
1330                 calculateBlockedUntilBeyondCount(receivers, false, mPlatformCompat), false);
1331     }
1332 
assertBlocked(BroadcastRecord r, boolean... blocked)1333     private static void assertBlocked(BroadcastRecord r, boolean... blocked) {
1334         assertEquals(r.receivers.size(), blocked.length);
1335         for (int i = 0; i < blocked.length; i++) {
1336             assertEquals("blocked " + i, blocked[i], r.isBlocked(i));
1337         }
1338     }
1339 
assertTerminalDeferredBeyond(BroadcastRecord r, int expectedTerminalCount, int expectedDeferredCount, int expectedBeyondCount)1340     private static void assertTerminalDeferredBeyond(BroadcastRecord r,
1341             int expectedTerminalCount, int expectedDeferredCount, int expectedBeyondCount) {
1342         assertEquals("terminal", expectedTerminalCount, r.terminalCount);
1343         assertEquals("deferred", expectedDeferredCount, r.deferredCount);
1344         assertEquals("beyond", expectedBeyondCount, r.beyondCount);
1345     }
1346 
appInfoEquals(int uid)1347     private ArgumentMatcher<ApplicationInfo> appInfoEquals(int uid) {
1348         return test -> (test.uid == uid);
1349     }
1350 }
1351