1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.applications;
import static android.os.UserHandle.MU_ENABLED;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.shadow.api.Shadow.extract;
import android.annotation.UserIdInt;
import android.app.Application;
import android.app.ApplicationPackageManager;
import android.app.usage.StorageStats;
import android.app.usage.StorageStatsManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.ModuleInfo;
import android.content.pm.PackageManager;
import android.content.pm.ParceledListSlice;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
import android.text.TextUtils;
import android.util.IconDrawableFactory;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
import com.android.settingslib.applications.ApplicationsState.Callbacks;
import com.android.settingslib.applications.ApplicationsState.Session;
import com.android.settingslib.testutils.shadow.ShadowUserManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowContextImpl;
import org.robolectric.shadows.ShadowLooper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowUserManager.class,
ApplicationsStateRoboTest.ShadowIconDrawableFactory.class,
ApplicationsStateRoboTest.ShadowPackageManager.class})
public class ApplicationsStateRoboTest {
private final static String HOME_PACKAGE_NAME = "com.android.home";
private final static String LAUNCHABLE_PACKAGE_NAME = "com.android.launchable";
private static final int PROFILE_USERID = 10;
private static final String PKG_1 = "PKG1";
private static final int OWNER_UID_1 = 1001;
private static final int PROFILE_UID_1 = UserHandle.getUid(PROFILE_USERID, OWNER_UID_1);
private static final String PKG_2 = "PKG2";
private static final int OWNER_UID_2 = 1002;
private static final int PROFILE_UID_2 = UserHandle.getUid(PROFILE_USERID, OWNER_UID_2);
private static final String PKG_3 = "PKG3";
private static final int OWNER_UID_3 = 1003;
/** Class under test */
private ApplicationsState mApplicationsState;
private Session mSession;
private Application mApplication;
@Mock
private Callbacks mCallbacks;
@Captor
private ArgumentCaptor<ArrayList<AppEntry>> mAppEntriesCaptor;
@Mock
private StorageStatsManager mStorageStatsManager;
@Mock
private IPackageManager mPackageManagerService;
@Implements(value = IconDrawableFactory.class)
public static class ShadowIconDrawableFactory {
@Implementation
protected Drawable getBadgedIcon(ApplicationInfo appInfo) {
return new ColorDrawable(0);
}
}
@Implements(value = ApplicationPackageManager.class)
public static class ShadowPackageManager extends
org.robolectric.shadows.ShadowApplicationPackageManager {
// test installed modules, 2 regular, 2 hidden
private final String[] mModuleNames = {
"test.module.1", "test.hidden.module.2", "test.hidden.module.3", "test.module.4"};
private final List<ModuleInfo> mInstalledModules = new ArrayList<>();
@Implementation
protected ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.activityInfo = new ActivityInfo();
resolveInfo.activityInfo.packageName = HOME_PACKAGE_NAME;
resolveInfo.activityInfo.enabled = true;
outActivities.add(resolveInfo);
return ComponentName.createRelative(resolveInfo.activityInfo.packageName, "foo");
}
@Implementation
public List<ModuleInfo> getInstalledModules(int flags) {
if (mInstalledModules.isEmpty()) {
for (String moduleName : mModuleNames) {
mInstalledModules.add(createModuleInfo(moduleName));
}
}
return mInstalledModules;
}
public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
@PackageManager.ResolveInfoFlagsBits int flags, @UserIdInt int userId) {
List<ResolveInfo> resolveInfos = new ArrayList<>();
ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.activityInfo = new ActivityInfo();
resolveInfo.activityInfo.packageName = LAUNCHABLE_PACKAGE_NAME;
resolveInfo.activityInfo.enabled = true;
resolveInfo.filter = new IntentFilter();
resolveInfo.filter.addCategory(Intent.CATEGORY_LAUNCHER);
resolveInfos.add(resolveInfo);
return resolveInfos;
}
private ModuleInfo createModuleInfo(String packageName) {
final ModuleInfo info = new ModuleInfo();
info.setName(packageName);
info.setPackageName(packageName);
// will treat any app with package name that contains "hidden" as hidden module
info.setHidden(!TextUtils.isEmpty(packageName) && packageName.contains("hidden"));
return info;
}
}
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
// Robolectric does not know about the StorageStatsManager as a system service.
// Registering a mock of this service as a replacement.
ShadowContextImpl shadowContext = Shadow.extract(
RuntimeEnvironment.application.getBaseContext());
shadowContext.setSystemService(Context.STORAGE_STATS_SERVICE, mStorageStatsManager);
mApplication = spy(RuntimeEnvironment.application);
StorageStats storageStats = new StorageStats();
storageStats.codeBytes = 10;
storageStats.cacheBytes = 30;
// Data bytes are a superset of cache bytes.
storageStats.dataBytes = storageStats.cacheBytes + 20;
when(mStorageStatsManager.queryStatsForPackage(any(UUID.class),
anyString(), any(UserHandle.class))).thenReturn(storageStats);
// Set up 3 installed apps, in which 1 is hidden module
final List<ApplicationInfo> infos = new ArrayList<>();
infos.add(createApplicationInfo("test.package.1"));
infos.add(createApplicationInfo("test.hidden.module.2"));
infos.add(createApplicationInfo("test.package.3"));
when(mPackageManagerService.getInstalledApplications(
anyLong() /* flags */, anyInt() /* userId */)).thenReturn(new ParceledListSlice(infos));
ApplicationsState.sInstance = null;
mApplicationsState = ApplicationsState.getInstance(mApplication, mPackageManagerService);
mApplicationsState.clearEntries();
mSession = mApplicationsState.newSession(mCallbacks);
}
@After
public void tearDown() {
mSession.onDestroy();
}
private ApplicationInfo createApplicationInfo(String packageName) {
return createApplicationInfo(packageName, 0);
}
private ApplicationInfo createApplicationInfo(String packageName, int uid) {
ApplicationInfo appInfo = new ApplicationInfo();
appInfo.sourceDir = "foo";
appInfo.flags |= ApplicationInfo.FLAG_INSTALLED;
appInfo.storageUuid = UUID.randomUUID();
appInfo.packageName = packageName;
appInfo.uid = uid;
return appInfo;
}
private AppEntry createAppEntry(ApplicationInfo appInfo, int id) {
AppEntry appEntry = new AppEntry(RuntimeEnvironment.application, appInfo, id);
appEntry.label = "label";
appEntry.mounted = true;
return appEntry;
}
private void addApp(String packageName, int id) {
addApp(packageName, id, 0);
}
private void addApp(String packageName, int id, int userId) {
ApplicationInfo appInfo = createApplicationInfo(packageName, id);
AppEntry appEntry = createAppEntry(appInfo, id);
mApplicationsState.mAppEntries.add(appEntry);
mApplicationsState.mEntriesMap.get(userId).put(appInfo.packageName, appEntry);
}
private void processAllMessages() {
Handler mainHandler = mApplicationsState.mMainHandler;
Handler bkgHandler = mApplicationsState.mBackgroundHandler;
ShadowLooper shadowBkgLooper = extract(bkgHandler.getLooper());
ShadowLooper shadowMainLooper = extract(mainHandler.getLooper());
shadowBkgLooper.idle();
shadowMainLooper.idle();
}
private AppEntry findAppEntry(List<AppEntry> appEntries, long id) {
for (AppEntry appEntry : appEntries) {
if (appEntry.id == id) {
return appEntry;
}
}
return null;
}
@Test
public void testDefaultSession_isResumed_LoadsAll() {
mSession.onResume();
addApp(HOME_PACKAGE_NAME, 1);
addApp(LAUNCHABLE_PACKAGE_NAME, 2);
mSession.rebuild(ApplicationsState.FILTER_EVERYTHING, ApplicationsState.SIZE_COMPARATOR);
processAllMessages();
verify(mCallbacks).onRebuildComplete(mAppEntriesCaptor.capture());
List<AppEntry> appEntries = mAppEntriesCaptor.getValue();
assertThat(appEntries.size()).isEqualTo(2);
for (AppEntry appEntry : appEntries) {
assertThat(appEntry.size).isGreaterThan(0L);
assertThat(appEntry.icon).isNotNull();
}
AppEntry homeEntry = findAppEntry(appEntries, 1);
assertThat(homeEntry.isHomeApp).isTrue();
assertThat(homeEntry.hasLauncherEntry).isFalse();
AppEntry launchableEntry = findAppEntry(appEntries, 2);
assertThat(launchableEntry.hasLauncherEntry).isTrue();
assertThat(launchableEntry.launcherEntryEnabled).isTrue();
}
@Test
public void testDefaultSession_isPaused_NotLoadsAll() {
mSession.onResume();
addApp(HOME_PACKAGE_NAME, 1);
addApp(LAUNCHABLE_PACKAGE_NAME, 2);
mSession.mResumed = false;
mSession.rebuild(ApplicationsState.FILTER_EVERYTHING, ApplicationsState.SIZE_COMPARATOR);
processAllMessages();
verify(mCallbacks, never()).onRebuildComplete(mAppEntriesCaptor.capture());
}
@Test
public void testCustomSessionLoadsIconsOnly() {
mSession.setSessionFlags(ApplicationsState.FLAG_SESSION_REQUEST_ICONS);
mSession.onResume();
addApp(LAUNCHABLE_PACKAGE_NAME, 1);
mSession.rebuild(ApplicationsState.FILTER_EVERYTHING, ApplicationsState.SIZE_COMPARATOR);
processAllMessages();
verify(mCallbacks).onRebuildComplete(mAppEntriesCaptor.capture());
List<AppEntry> appEntries = mAppEntriesCaptor.getValue();
assertThat(appEntries.size()).isEqualTo(1);
AppEntry launchableEntry = findAppEntry(appEntries, 1);
assertThat(launchableEntry.icon).isNotNull();
assertThat(launchableEntry.size).isEqualTo(-1);
assertThat(launchableEntry.hasLauncherEntry).isFalse();
}
@Test
public void testCustomSessionLoadsSizesOnly() {
mSession.setSessionFlags(ApplicationsState.FLAG_SESSION_REQUEST_SIZES);
mSession.onResume();
addApp(LAUNCHABLE_PACKAGE_NAME, 1);
mSession.rebuild(ApplicationsState.FILTER_EVERYTHING, ApplicationsState.SIZE_COMPARATOR);
processAllMessages();
verify(mCallbacks).onRebuildComplete(mAppEntriesCaptor.capture());
List<AppEntry> appEntries = mAppEntriesCaptor.getValue();
assertThat(appEntries.size()).isEqualTo(1);
AppEntry launchableEntry = findAppEntry(appEntries, 1);
assertThat(launchableEntry.hasLauncherEntry).isFalse();
assertThat(launchableEntry.size).isGreaterThan(0L);
}
@Test
public void testCustomSessionLoadsHomeOnly() {
mSession.setSessionFlags(ApplicationsState.FLAG_SESSION_REQUEST_HOME_APP);
mSession.onResume();
addApp(HOME_PACKAGE_NAME, 1);
mSession.rebuild(ApplicationsState.FILTER_EVERYTHING, ApplicationsState.SIZE_COMPARATOR);
processAllMessages();
verify(mCallbacks).onRebuildComplete(mAppEntriesCaptor.capture());
List<AppEntry> appEntries = mAppEntriesCaptor.getValue();
assertThat(appEntries.size()).isEqualTo(1);
AppEntry launchableEntry = findAppEntry(appEntries, 1);
assertThat(launchableEntry.hasLauncherEntry).isFalse();
assertThat(launchableEntry.size).isEqualTo(-1);
assertThat(launchableEntry.isHomeApp).isTrue();
}
@Test
public void testCustomSessionLoadsLeanbackOnly() {
mSession.setSessionFlags(ApplicationsState.FLAG_SESSION_REQUEST_LEANBACK_LAUNCHER);
mSession.onResume();
addApp(LAUNCHABLE_PACKAGE_NAME, 1);
mSession.rebuild(ApplicationsState.FILTER_EVERYTHING, ApplicationsState.SIZE_COMPARATOR);
processAllMessages();
verify(mCallbacks).onRebuildComplete(mAppEntriesCaptor.capture());
List<AppEntry> appEntries = mAppEntriesCaptor.getValue();
assertThat(appEntries.size()).isEqualTo(1);
AppEntry launchableEntry = findAppEntry(appEntries, 1);
assertThat(launchableEntry.size).isEqualTo(-1);
assertThat(launchableEntry.isHomeApp).isFalse();
assertThat(launchableEntry.hasLauncherEntry).isTrue();
assertThat(launchableEntry.launcherEntryEnabled).isTrue();
}
@Test
public void onResume_shouldNotIncludeSystemHiddenModule() {
mSession.onResume();
final List<ApplicationInfo> mApplications = mApplicationsState.mApplications;
assertThat(mApplications).hasSize(2);
assertThat(mApplications.get(0).packageName).isEqualTo("test.package.1");
assertThat(mApplications.get(1).packageName).isEqualTo("test.package.3");
}
@Test
public void removeAndInstall_noWorkprofile_doResumeIfNeededLocked_shouldClearEntries()
throws RemoteException {
// scenario: only owner user
// (PKG_1, PKG_2) -> (PKG_2, PKG_3)
// PKG_1 is removed and PKG_3 is installed before app is resumed.
ApplicationsState.sInstance = null;
mApplicationsState = spy(
ApplicationsState
.getInstance(RuntimeEnvironment.application, mock(IPackageManager.class)));
// Previous Applications:
ApplicationInfo appInfo;
final ArrayList<ApplicationInfo> prevAppList = new ArrayList<>();
appInfo = createApplicationInfo(PKG_1, OWNER_UID_1);
prevAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_2, OWNER_UID_2);
prevAppList.add(appInfo);
mApplicationsState.mApplications = prevAppList;
// Previous Entries:
// (PKG_1, PKG_2)
addApp(PKG_1, OWNER_UID_1, 0);
addApp(PKG_2, OWNER_UID_2, 0);
// latest Applications:
// (PKG_2, PKG_3)
final ArrayList<ApplicationInfo> appList = new ArrayList<>();
appInfo = createApplicationInfo(PKG_2, OWNER_UID_2);
appList.add(appInfo);
appInfo = createApplicationInfo(PKG_3, OWNER_UID_3);
appList.add(appInfo);
setupDoResumeIfNeededLocked(appList, null);
mApplicationsState.doResumeIfNeededLocked();
verify(mApplicationsState).clearEntries();
}
@Test
public void noAppRemoved_noWorkprofile_doResumeIfNeededLocked_shouldNotClearEntries()
throws RemoteException {
// scenario: only owner user
// (PKG_1, PKG_2)
ApplicationsState.sInstance = null;
mApplicationsState = spy(
ApplicationsState
.getInstance(RuntimeEnvironment.application, mock(IPackageManager.class)));
ApplicationInfo appInfo;
// Previous Applications
final ArrayList<ApplicationInfo> prevAppList = new ArrayList<>();
appInfo = createApplicationInfo(PKG_1, OWNER_UID_1);
prevAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_2, OWNER_UID_2);
prevAppList.add(appInfo);
mApplicationsState.mApplications = prevAppList;
// Previous Entries:
// (pk1, PKG_2)
addApp(PKG_1, OWNER_UID_1, 0);
addApp(PKG_2, OWNER_UID_2, 0);
// latest Applications:
// (PKG_2, PKG_3)
final ArrayList<ApplicationInfo> appList = new ArrayList<>();
appInfo = createApplicationInfo(PKG_1, OWNER_UID_1);
appList.add(appInfo);
appInfo = createApplicationInfo(PKG_2, OWNER_UID_2);
appList.add(appInfo);
setupDoResumeIfNeededLocked(appList, null);
mApplicationsState.doResumeIfNeededLocked();
verify(mApplicationsState, never()).clearEntries();
}
@Test
public void removeProfileApp_workprofileExists_doResumeIfNeededLocked_shouldClearEntries()
throws RemoteException {
if (!MU_ENABLED) {
return;
}
// [Preconditions]
// 2 apps (PKG_1, PKG_2) for owner, PKG_1 is not in installed state
// 2 apps (PKG_1, PKG_2) for non-owner.
//
// [Actions]
// profile user's PKG_2 is removed before resume
//
// Applications:
// owner - (PKG_1 - uninstalled, PKG_2) -> (PKG_1 - uninstalled, PKG_2)
// profile - (PKG_1, PKG_2) -> (PKG_1)
//
// Previous Entries:
// owner - (PKG_2)
// profile - (PKG_1, PKG_2)
ShadowUserManager shadowUserManager = Shadow
.extract(RuntimeEnvironment.application.getSystemService(UserManager.class));
shadowUserManager.addProfile(PROFILE_USERID, "profile");
ApplicationsState.sInstance = null;
mApplicationsState = spy(
ApplicationsState
.getInstance(RuntimeEnvironment.application, mock(IPackageManager.class)));
ApplicationInfo appInfo;
// Previous Applications
// owner - (PKG_1 - uninstalled, PKG_2)
// profile - (PKG_1, PKG_2)
final ArrayList<ApplicationInfo> prevAppList = new ArrayList<>();
appInfo = createApplicationInfo(PKG_1, OWNER_UID_1);
appInfo.flags ^= ApplicationInfo.FLAG_INSTALLED;
prevAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_2, OWNER_UID_2);
prevAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_1, PROFILE_UID_1);
prevAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_2, PROFILE_UID_2);
prevAppList.add(appInfo);
mApplicationsState.mApplications = prevAppList;
// Previous Entries:
// owner (PKG_2), profile (pk1, PKG_2)
// PKG_1 is not installed for owner, hence it's removed from entries
addApp(PKG_2, OWNER_UID_2, 0);
addApp(PKG_1, PROFILE_UID_1, PROFILE_USERID);
addApp(PKG_2, PROFILE_UID_2, PROFILE_USERID);
// latest Applications:
// owner (PKG_1, PKG_2), profile (PKG_1)
// owner's PKG_1 is still listed and is in non-installed state
// profile user's PKG_2 is removed by a user before resume
//owner
final ArrayList<ApplicationInfo> ownerAppList = new ArrayList<>();
appInfo = createApplicationInfo(PKG_1, OWNER_UID_1);
appInfo.flags ^= ApplicationInfo.FLAG_INSTALLED;
ownerAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_2, OWNER_UID_2);
ownerAppList.add(appInfo);
//profile
appInfo = createApplicationInfo(PKG_1, PROFILE_UID_1);
setupDoResumeIfNeededLocked(ownerAppList, new ArrayList<>(Arrays.asList(appInfo)));
mApplicationsState.doResumeIfNeededLocked();
verify(mApplicationsState).clearEntries();
}
@Test
public void removeOwnerApp_workprofileExists_doResumeIfNeededLocked_shouldClearEntries()
throws RemoteException {
if (!MU_ENABLED) {
return;
}
// [Preconditions]
// 2 apps (PKG_1, PKG_2) for owner, PKG_1 is not in installed state
// 2 apps (PKG_1, PKG_2) for non-owner.
//
// [Actions]
// Owner user's PKG_2 is removed before resume
//
// Applications:
// owner - (PKG_1 - uninstalled, PKG_2) -> (PKG_1 - uninstalled, PKG_2 - uninstalled)
// profile - (PKG_1, PKG_2) -> (PKG_1, PKG_2)
//
// Previous Entries:
// owner - (PKG_2)
// profile - (PKG_1, PKG_2)
ShadowUserManager shadowUserManager = Shadow
.extract(RuntimeEnvironment.application.getSystemService(UserManager.class));
shadowUserManager.addProfile(PROFILE_USERID, "profile");
ApplicationsState.sInstance = null;
mApplicationsState = spy(
ApplicationsState
.getInstance(RuntimeEnvironment.application, mock(IPackageManager.class)));
ApplicationInfo appInfo;
// Previous Applications:
// owner - (PKG_1 - uninstalled, PKG_2)
// profile - (PKG_1, PKG_2)
final ArrayList<ApplicationInfo> prevAppList = new ArrayList<>();
appInfo = createApplicationInfo(PKG_1, OWNER_UID_1);
appInfo.flags ^= ApplicationInfo.FLAG_INSTALLED;
prevAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_2, OWNER_UID_2);
prevAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_1, PROFILE_UID_1);
prevAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_2, PROFILE_UID_2);
prevAppList.add(appInfo);
mApplicationsState.mApplications = prevAppList;
// Previous Entries:
// owner (PKG_2), profile (pk1, PKG_2)
// PKG_1 is not installed for owner, hence it's removed from entries
addApp(PKG_2, OWNER_UID_2, 0);
addApp(PKG_1, PROFILE_UID_1, PROFILE_USERID);
addApp(PKG_2, PROFILE_UID_2, PROFILE_USERID);
// latest Applications:
// owner (PKG_1 - uninstalled, PKG_2 - uninstalled), profile (PKG_1, PKG_2)
// owner's PKG_1, PKG_2 is still listed and is in non-installed state
// profile user's PKG_2 is removed before resume
//owner
final ArrayList<ApplicationInfo> ownerAppList = new ArrayList<>();
appInfo = createApplicationInfo(PKG_1, OWNER_UID_1);
appInfo.flags ^= ApplicationInfo.FLAG_INSTALLED;
ownerAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_2, OWNER_UID_2);
appInfo.flags ^= ApplicationInfo.FLAG_INSTALLED;
ownerAppList.add(appInfo);
//profile
final ArrayList<ApplicationInfo> profileAppList = new ArrayList<>();
appInfo = createApplicationInfo(PKG_1, PROFILE_UID_1);
profileAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_2, PROFILE_UID_2);
profileAppList.add(appInfo);
setupDoResumeIfNeededLocked(ownerAppList, profileAppList);
mApplicationsState.doResumeIfNeededLocked();
verify(mApplicationsState).clearEntries();
}
@Test
public void noAppRemoved_workprofileExists_doResumeIfNeededLocked_shouldNotClearEntries()
throws RemoteException {
if (!MU_ENABLED) {
return;
}
// [Preconditions]
// 2 apps (PKG_1, PKG_2) for owner, PKG_1 is not in installed state
// 2 apps (PKG_1, PKG_2) for non-owner.
//
// Applications:
// owner - (PKG_1 - uninstalled, PKG_2)
// profile - (PKG_1, PKG_2)
//
// Previous Entries:
// owner - (PKG_2)
// profile - (PKG_1, PKG_2)
ShadowUserManager shadowUserManager = Shadow
.extract(RuntimeEnvironment.application.getSystemService(UserManager.class));
shadowUserManager.addProfile(PROFILE_USERID, "profile");
ApplicationsState.sInstance = null;
mApplicationsState = spy(
ApplicationsState
.getInstance(RuntimeEnvironment.application, mock(IPackageManager.class)));
ApplicationInfo appInfo;
// Previous Applications:
// owner - (PKG_1 - uninstalled, PKG_2)
// profile - (PKG_1, PKG_2)
final ArrayList<ApplicationInfo> prevAppList = new ArrayList<>();
appInfo = createApplicationInfo(PKG_1, OWNER_UID_1);
appInfo.flags ^= ApplicationInfo.FLAG_INSTALLED;
prevAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_2, OWNER_UID_2);
prevAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_1, PROFILE_UID_1);
prevAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_2, PROFILE_UID_2);
prevAppList.add(appInfo);
mApplicationsState.mApplications = prevAppList;
// Previous Entries:
// owner (PKG_2), profile (pk1, PKG_2)
// PKG_1 is not installed for owner, hence it's removed from entries
addApp(PKG_2, OWNER_UID_2, 0);
addApp(PKG_1, PROFILE_UID_1, PROFILE_USERID);
addApp(PKG_2, PROFILE_UID_2, PROFILE_USERID);
// latest Applications:
// owner (PKG_1 - uninstalled, PKG_2), profile (PKG_1, PKG_2)
// owner's PKG_1 is still listed and is in non-installed state
// owner
final ArrayList<ApplicationInfo> ownerAppList = new ArrayList<>();
appInfo = createApplicationInfo(PKG_1, OWNER_UID_1);
appInfo.flags ^= ApplicationInfo.FLAG_INSTALLED;
ownerAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_2, OWNER_UID_2);
ownerAppList.add(appInfo);
// profile
final ArrayList<ApplicationInfo> profileAppList = new ArrayList<>();
appInfo = createApplicationInfo(PKG_1, PROFILE_UID_1);
profileAppList.add(appInfo);
appInfo = createApplicationInfo(PKG_2, PROFILE_UID_2);
profileAppList.add(appInfo);
setupDoResumeIfNeededLocked(ownerAppList, profileAppList);
mApplicationsState.doResumeIfNeededLocked();
verify(mApplicationsState, never()).clearEntries();
}
@Test
public void testDefaultSession_enabledAppIconCache_shouldSkipPreloadIcon() {
when(mApplication.getPackageName()).thenReturn("com.android.settings");
mSession.onResume();
addApp(HOME_PACKAGE_NAME, 1);
addApp(LAUNCHABLE_PACKAGE_NAME, 2);
mSession.rebuild(ApplicationsState.FILTER_EVERYTHING, ApplicationsState.SIZE_COMPARATOR);
processAllMessages();
verify(mCallbacks).onRebuildComplete(mAppEntriesCaptor.capture());
List<AppEntry> appEntries = mAppEntriesCaptor.getValue();
for (AppEntry appEntry : appEntries) {
assertThat(appEntry.icon).isNull();
}
}
private void setupDoResumeIfNeededLocked(ArrayList<ApplicationInfo> ownerApps,
ArrayList<ApplicationInfo> profileApps)
throws RemoteException {
if (ownerApps != null) {
when(mApplicationsState.mIpm.getInstalledApplications(anyLong(), eq(0)))
.thenReturn(new ParceledListSlice<>(ownerApps));
}
if (profileApps != null) {
when(mApplicationsState.mIpm.getInstalledApplications(anyLong(), eq(PROFILE_USERID)))
.thenReturn(new ParceledListSlice<>(profileApps));
}
final InterestingConfigChanges configChanges = mock(InterestingConfigChanges.class);
when(configChanges.applyNewConfig(any(Resources.class))).thenReturn(false);
mApplicationsState.setInterestingConfigChanges(configChanges);
}
}
|