1 /* 2 * Copyright (C) 2017 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.enterprise; 18 19 import static com.android.settings.testutils.ApplicationTestUtils.buildInfo; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 24 import static org.mockito.Mockito.when; 25 26 import android.content.Context; 27 import android.content.pm.UserInfo; 28 29 import androidx.preference.PreferenceManager; 30 import androidx.preference.PreferenceScreen; 31 32 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 33 import com.android.settings.R; 34 import com.android.settings.applications.ApplicationFeatureProvider; 35 import com.android.settings.applications.UserAppInfo; 36 import com.android.settingslib.core.AbstractPreferenceController; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.RobolectricTestRunner; 44 import org.robolectric.RuntimeEnvironment; 45 46 import java.util.ArrayList; 47 import java.util.List; 48 49 @RunWith(RobolectricTestRunner.class) 50 public class ApplicationListFragmentTest { 51 52 private static final int USER_ID = 0; 53 private static final int USER_APP_UID = 0; 54 55 private static final String APP = "APP"; 56 57 @Mock(answer = RETURNS_DEEP_STUBS) 58 private PreferenceScreen mScreen; 59 @Mock(answer = RETURNS_DEEP_STUBS) 60 private PreferenceManager mPreferenceManager; 61 62 private ApplicationListFragment mFragment; 63 private Context mContext; 64 65 @Before setUp()66 public void setUp() { 67 MockitoAnnotations.initMocks(this); 68 mContext = RuntimeEnvironment.application; 69 when(mPreferenceManager.getContext()).thenReturn(mContext); 70 71 mFragment = new ApplicationListFragmentTestable(mPreferenceManager, mScreen); 72 } 73 74 @Test getLogTag()75 public void getLogTag() { 76 assertThat(mFragment.getLogTag()).isEqualTo("EnterprisePrivacySettings"); 77 } 78 79 @Test getScreenResource()80 public void getScreenResource() { 81 assertThat(mFragment.getPreferenceScreenResId()) 82 .isEqualTo(R.xml.app_list_disclosure_settings); 83 } 84 85 @Test getPreferenceControllers()86 public void getPreferenceControllers() { 87 final List<AbstractPreferenceController> controllers = 88 mFragment.createPreferenceControllers(mContext); 89 assertThat(controllers).isNotNull(); 90 assertThat(controllers.size()).isEqualTo(1); 91 assertThat(controllers.get(0)).isInstanceOf(ApplicationListPreferenceController.class); 92 } 93 94 @Test getCategories()95 public void getCategories() { 96 assertThat(new ApplicationListFragment.AdminGrantedPermissionCamera().getMetricsCategory()) 97 .isEqualTo(MetricsEvent.ENTERPRISE_PRIVACY_PERMISSIONS); 98 assertThat(new ApplicationListFragment.AdminGrantedPermissionLocation(). 99 getMetricsCategory()).isEqualTo(MetricsEvent.ENTERPRISE_PRIVACY_PERMISSIONS); 100 assertThat(new ApplicationListFragment.AdminGrantedPermissionMicrophone(). 101 getMetricsCategory()).isEqualTo(MetricsEvent.ENTERPRISE_PRIVACY_PERMISSIONS); 102 assertThat(new ApplicationListFragment.EnterpriseInstalledPackages().getMetricsCategory()) 103 .isEqualTo(MetricsEvent.ENTERPRISE_PRIVACY_INSTALLED_APPS); 104 } 105 106 private static class ApplicationListFragmentTestable extends ApplicationListFragment { 107 108 private final PreferenceManager mPreferenceManager; 109 private final PreferenceScreen mPreferenceScreen; 110 ApplicationListFragmentTestable(PreferenceManager preferenceManager, PreferenceScreen screen)111 private ApplicationListFragmentTestable(PreferenceManager preferenceManager, 112 PreferenceScreen screen) { 113 this.mPreferenceManager = preferenceManager; 114 this.mPreferenceScreen = screen; 115 } 116 117 @Override buildApplicationList(Context context, ApplicationFeatureProvider.ListOfAppsCallback callback)118 public void buildApplicationList(Context context, 119 ApplicationFeatureProvider.ListOfAppsCallback callback) { 120 final UserInfo user = new UserInfo(USER_ID, "main", UserInfo.FLAG_ADMIN); 121 final List<UserAppInfo> apps = new ArrayList<>(); 122 apps.add(new UserAppInfo(user, buildInfo(USER_APP_UID, APP, 0, 0))); 123 callback.onListOfAppsResult(apps); 124 } 125 126 @Override getPreferenceManager()127 public PreferenceManager getPreferenceManager() { 128 return mPreferenceManager; 129 } 130 131 @Override getPreferenceScreen()132 public PreferenceScreen getPreferenceScreen() { 133 return mPreferenceScreen; 134 } 135 136 @Override getMetricsCategory()137 public int getMetricsCategory() { 138 return MetricsEvent.VIEW_UNKNOWN; 139 } 140 } 141 } 142