1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.security;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.content.pm.CrossProfileApps;
26 import android.os.UserHandle;
27 
28 import androidx.test.core.app.ApplicationProvider;
29 import androidx.test.ext.junit.runners.AndroidJUnit4;
30 
31 import com.android.settings.testutils.ResourcesUtils;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 
39 import java.util.Collections;
40 
41 @RunWith(AndroidJUnit4.class)
42 public class SecurityAdvancedSettingsControllerTest {
43 
44     private static final String PREFERENCE_KEY = "security_advanced_settings";
45     private static final String NO_WORK_PROFILE_SUMMARY_RESOURCE_NAME =
46             "security_advanced_settings_no_work_profile_settings_summary";
47     private static final String WORK_PROFILE_SUMMARY_RESOURCE_NAME =
48             "security_advanced_settings_work_profile_settings_summary";
49 
50     @Mock
51     private CrossProfileApps mCrossProfileApps;
52     private Context mContext;
53 
54     private SecurityAdvancedSettingsController mController;
55 
56     @Before
setUp()57     public void setUp() {
58         MockitoAnnotations.initMocks(this);
59         mContext = spy(ApplicationProvider.getApplicationContext());
60         when(mContext.getSystemService(CrossProfileApps.class)).thenReturn(mCrossProfileApps);
61 
62         mController = new SecurityAdvancedSettingsController(mContext, PREFERENCE_KEY);
63     }
64 
65     @Test
getSummary_withoutWorkProfile()66     public void getSummary_withoutWorkProfile() {
67         when(mCrossProfileApps.getTargetUserProfiles()).thenReturn(Collections.emptyList());
68 
69         assertThat(mController.getSummary())
70                 .isEqualTo(getResourcesString(NO_WORK_PROFILE_SUMMARY_RESOURCE_NAME));
71     }
72 
73     @Test
getSummary_withWorkProfile()74     public void getSummary_withWorkProfile() {
75         when(mCrossProfileApps.getTargetUserProfiles()).thenReturn(
76                 Collections.singletonList(new UserHandle(0))
77         );
78 
79         assertThat(mController.getSummary())
80                 .isEqualTo(getResourcesString(WORK_PROFILE_SUMMARY_RESOURCE_NAME));
81     }
82 
getResourcesString(String resName)83     private String getResourcesString(String resName) {
84         return ResourcesUtils.getResourcesString(mContext, resName);
85     }
86 }
87