• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.settings.applications;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.ArgumentMatchers.eq;
23 import static org.mockito.Mockito.when;
24 
25 import android.app.role.RoleManager;
26 import android.content.Context;
27 import android.content.pm.ApplicationInfo;
28 import android.content.pm.PackageManager;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.robolectric.RobolectricTestRunner;
36 
37 import java.util.Collections;
38 
39 @RunWith(RobolectricTestRunner.class)
40 public class DefaultAppsPreferenceControllerTest {
41 
42     private static final String PREFERENCE_KEY = "DefaultApps";
43 
44     private static final String BROWSER_PACKAGE_NAME = "com.example.browser1";
45     private static final String DIALER_PACKAGE_NAME = "com.example.dialer1";
46     private static final String SMS_PACKAGE_NAME = "com.example.sms1";
47 
48     @Mock
49     private Context mContext;
50     @Mock
51     private PackageManager mPackageManager;
52     @Mock
53     private RoleManager mRoleManager;
54     @Mock
55     private ApplicationInfo mBrowserApplicationInfo;
56     @Mock
57     private ApplicationInfo mDialerApplicationInfo;
58     @Mock
59     private ApplicationInfo mSmsApplicationInfo;
60 
61     private DefaultAppsPreferenceController mPreferenceController;
62 
63     @Before
setUp()64     public void setUp() throws PackageManager.NameNotFoundException {
65         MockitoAnnotations.initMocks(this);
66 
67         when(mContext.getPackageManager()).thenReturn(mPackageManager);
68         when(mContext.getSystemService(RoleManager.class)).thenReturn(mRoleManager);
69 
70         when(mBrowserApplicationInfo.loadLabel(mPackageManager)).thenReturn("Browser1");
71         when(mPackageManager.getApplicationInfo(eq(BROWSER_PACKAGE_NAME), anyInt())).thenReturn(
72                 mBrowserApplicationInfo);
73         when(mDialerApplicationInfo.loadLabel(mPackageManager)).thenReturn("Phone1");
74         when(mPackageManager.getApplicationInfo(eq(DIALER_PACKAGE_NAME), anyInt())).thenReturn(
75                 mDialerApplicationInfo);
76         when(mSmsApplicationInfo.loadLabel(mPackageManager)).thenReturn("Sms1");
77         when(mPackageManager.getApplicationInfo(eq(SMS_PACKAGE_NAME), anyInt())).thenReturn(
78                 mSmsApplicationInfo);
79 
80         mPreferenceController = new DefaultAppsPreferenceController(mContext, PREFERENCE_KEY);
81     }
82 
83     @Test
isAvailable_shouldReturnTrue()84     public void isAvailable_shouldReturnTrue() {
85         assertThat(mPreferenceController.isAvailable()).isTrue();
86     }
87 
88     @Test
getSummary_allAvailable_shouldReturnAll()89     public void getSummary_allAvailable_shouldReturnAll() {
90         when(mRoleManager.getRoleHolders(RoleManager.ROLE_BROWSER)).thenReturn(
91                 Collections.singletonList(BROWSER_PACKAGE_NAME));
92         when(mRoleManager.getRoleHolders(RoleManager.ROLE_DIALER)).thenReturn(
93                 Collections.singletonList(DIALER_PACKAGE_NAME));
94         when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(
95                 Collections.singletonList(SMS_PACKAGE_NAME));
96 
97         assertThat(mPreferenceController.getSummary()).isEqualTo("Browser1, Phone1, and Sms1");
98     }
99 
100     @Test
getSummary_browserAndDialerAvailable_shouldReturnBrowserAndDialer()101     public void getSummary_browserAndDialerAvailable_shouldReturnBrowserAndDialer() {
102         when(mRoleManager.getRoleHolders(RoleManager.ROLE_BROWSER)).thenReturn(
103                 Collections.singletonList(BROWSER_PACKAGE_NAME));
104         when(mRoleManager.getRoleHolders(RoleManager.ROLE_DIALER)).thenReturn(
105                 Collections.singletonList(DIALER_PACKAGE_NAME));
106         when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(Collections.emptyList());
107 
108         assertThat(mPreferenceController.getSummary()).isEqualTo("Browser1 and Phone1");
109     }
110 
111     @Test
getSummary_browserAndSmsAvailable_shouldReturnBrowserAndSms()112     public void getSummary_browserAndSmsAvailable_shouldReturnBrowserAndSms() {
113         when(mRoleManager.getRoleHolders(RoleManager.ROLE_BROWSER)).thenReturn(
114                 Collections.singletonList(BROWSER_PACKAGE_NAME));
115         when(mRoleManager.getRoleHolders(RoleManager.ROLE_DIALER)).thenReturn(
116                 Collections.emptyList());
117         when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(
118                 Collections.singletonList(SMS_PACKAGE_NAME));
119 
120         assertThat(mPreferenceController.getSummary()).isEqualTo("Browser1 and Sms1");
121     }
122 
123     @Test
getSummary_dialerAndSmsAvailable_shouldReturnDialerAndSms()124     public void getSummary_dialerAndSmsAvailable_shouldReturnDialerAndSms() {
125         when(mRoleManager.getRoleHolders(RoleManager.ROLE_BROWSER)).thenReturn(
126                 Collections.emptyList());
127         when(mRoleManager.getRoleHolders(RoleManager.ROLE_DIALER)).thenReturn(
128                 Collections.singletonList(DIALER_PACKAGE_NAME));
129         when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(
130                 Collections.singletonList(SMS_PACKAGE_NAME));
131 
132         assertThat(mPreferenceController.getSummary()).isEqualTo("Phone1 and Sms1");
133     }
134 
135     @Test
getSummary_browserAvailable_shouldReturnBrowser()136     public void getSummary_browserAvailable_shouldReturnBrowser() {
137         when(mRoleManager.getRoleHolders(RoleManager.ROLE_BROWSER)).thenReturn(
138                 Collections.singletonList(BROWSER_PACKAGE_NAME));
139         when(mRoleManager.getRoleHolders(RoleManager.ROLE_DIALER)).thenReturn(
140                 Collections.emptyList());
141         when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(Collections.emptyList());
142 
143         assertThat(mPreferenceController.getSummary()).isEqualTo("Browser1");
144     }
145 
146     @Test
getSummary_dialerAvailable_shouldReturnDialer()147     public void getSummary_dialerAvailable_shouldReturnDialer() {
148         when(mRoleManager.getRoleHolders(RoleManager.ROLE_BROWSER)).thenReturn(
149                 Collections.emptyList());
150         when(mRoleManager.getRoleHolders(RoleManager.ROLE_DIALER)).thenReturn(
151                 Collections.singletonList(DIALER_PACKAGE_NAME));
152         when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(Collections.emptyList());
153 
154         assertThat(mPreferenceController.getSummary()).isEqualTo("Phone1");
155     }
156 
157     @Test
getSummary_smsAvailable_shouldReturnSms()158     public void getSummary_smsAvailable_shouldReturnSms() {
159         when(mRoleManager.getRoleHolders(RoleManager.ROLE_BROWSER)).thenReturn(
160                 Collections.emptyList());
161         when(mRoleManager.getRoleHolders(RoleManager.ROLE_DIALER)).thenReturn(
162                 Collections.emptyList());
163         when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(
164                 Collections.singletonList(SMS_PACKAGE_NAME));
165 
166         assertThat(mPreferenceController.getSummary()).isEqualTo("Sms1");
167     }
168 
169     @Test
getSummary_noneAvailable_shouldReturnNull()170     public void getSummary_noneAvailable_shouldReturnNull() {
171         when(mRoleManager.getRoleHolders(RoleManager.ROLE_BROWSER)).thenReturn(
172                 Collections.emptyList());
173         when(mRoleManager.getRoleHolders(RoleManager.ROLE_DIALER)).thenReturn(
174                 Collections.emptyList());
175         when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(Collections.emptyList());
176 
177         assertThat(mPreferenceController.getSummary()).isNull();
178     }
179 }
180