• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 package com.android.settings.applications;
17 
18 import android.content.pm.ApplicationInfo;
19 import android.content.pm.PackageManager;
20 import android.content.pm.UserInfo;
21 import android.os.UserManager;
22 import android.support.test.filters.SmallTest;
23 import android.support.test.runner.AndroidJUnit4;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 import static junit.framework.Assert.assertEquals;
34 import static org.mockito.Mockito.when;
35 
36 @RunWith(AndroidJUnit4.class)
37 @SmallTest
38 public class PackageUtilTest {
39     private static final String ALL_USERS_APP_NAME = "com.google.allusers.app";
40     private static final String ONE_USER_APP_NAME = "com.google.oneuser.app";
41     private static final int USER1_ID = 1;
42     private static final int USER2_ID = 11;
43 
44     @Mock
45     private PackageManager mMockPackageManager;
46     @Mock
47     private UserManager mMockUserManager;
48 
49     private InstalledAppDetails.PackageUtil mPackageUtil;
50     private List<UserInfo> mUserInfos;
51 
52     @Before
setUp()53     public void setUp() throws PackageManager.NameNotFoundException {
54         MockitoAnnotations.initMocks(this);
55 
56         mUserInfos = new ArrayList<>();
57         mUserInfos.add(new UserInfo(USER1_ID, "lei", 0));
58         mUserInfos.add(new UserInfo(USER2_ID, "yue", 0));
59         when(mMockUserManager.getUsers(true)).thenReturn(mUserInfos);
60 
61         ApplicationInfo usersApp = new ApplicationInfo();
62         usersApp.flags = ApplicationInfo.FLAG_INSTALLED;
63 
64         when(mMockPackageManager.getApplicationInfoAsUser(
65                 ALL_USERS_APP_NAME, PackageManager.GET_META_DATA, USER1_ID))
66                 .thenReturn(usersApp);
67         when(mMockPackageManager.getApplicationInfoAsUser(
68                 ALL_USERS_APP_NAME, PackageManager.GET_META_DATA, USER2_ID))
69                 .thenReturn(usersApp);
70 
71         when(mMockPackageManager.getApplicationInfoAsUser(
72                 ONE_USER_APP_NAME, PackageManager.GET_META_DATA, USER1_ID))
73                 .thenReturn(usersApp);
74 
75         when(mMockPackageManager.getApplicationInfoAsUser(
76                 ONE_USER_APP_NAME, PackageManager.GET_META_DATA, USER2_ID))
77                 .thenThrow(new PackageManager.NameNotFoundException());
78 
79         mPackageUtil = new InstalledAppDetails.PackageUtil();
80     }
81 
82     @Test
testCountPackageInUsers_twoUsersInstalled_returnTwo()83     public void testCountPackageInUsers_twoUsersInstalled_returnTwo() {
84         assertEquals(2, mPackageUtil.countPackageInUsers(
85                 mMockPackageManager, mMockUserManager, ALL_USERS_APP_NAME));
86     }
87 
88     @Test
testCountPackageInUsers_oneUsersInstalled_returnOne()89     public void testCountPackageInUsers_oneUsersInstalled_returnOne() {
90         assertEquals(1, mPackageUtil.countPackageInUsers(
91                 mMockPackageManager, mMockUserManager, ONE_USER_APP_NAME));
92     }
93 }
94