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