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.applications.appinfo; 18 19 import static android.Manifest.permission.SYSTEM_ALERT_WINDOW; 20 import static android.Manifest.permission.WRITE_SETTINGS; 21 import static com.google.common.truth.Truth.assertThat; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.spy; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.content.Context; 28 import android.content.pm.PackageInfo; 29 import android.os.UserManager; 30 import android.support.v7.preference.Preference; 31 32 import com.android.settings.core.BasePreferenceController; 33 import com.android.settings.testutils.SettingsRobolectricTestRunner; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RuntimeEnvironment; 41 42 @RunWith(SettingsRobolectricTestRunner.class) 43 public class DrawOverlayDetailPreferenceControllerTest { 44 45 @Mock 46 private UserManager mUserManager; 47 @Mock 48 private AppInfoDashboardFragment mFragment; 49 @Mock 50 private Preference mPreference; 51 52 private Context mContext; 53 private DrawOverlayDetailPreferenceController mController; 54 55 @Before 56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 mContext = spy(RuntimeEnvironment.application); 59 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 60 mController = spy(new DrawOverlayDetailPreferenceController(mContext, "key")); 61 mController.setParentFragment(mFragment); 62 final String key = mController.getPreferenceKey(); 63 when(mPreference.getKey()).thenReturn(key); 64 } 65 66 @Test 67 public void getAvailabilityStatus_managedProfile_shouldReturnDisabled() { 68 when(mUserManager.isManagedProfile()).thenReturn(true); 69 70 assertThat(mController.getAvailabilityStatus()) 71 .isEqualTo(BasePreferenceController.DISABLED_FOR_USER); 72 } 73 74 @Test 75 public void getAvailabilityStatus_noPermissionRequested_shouldReturnDisabled() { 76 when(mUserManager.isManagedProfile()).thenReturn(false); 77 when(mFragment.getPackageInfo()).thenReturn(new PackageInfo()); 78 79 assertThat(mController.getAvailabilityStatus()) 80 .isEqualTo(BasePreferenceController.DISABLED_FOR_USER); 81 } 82 83 @Test 84 public void getAvailabilityStatus_noSystemAlertWindowPermission_shouldReturnDisabled() { 85 when(mUserManager.isManagedProfile()).thenReturn(false); 86 final PackageInfo info = new PackageInfo(); 87 info.requestedPermissions = new String[] {WRITE_SETTINGS}; 88 when(mFragment.getPackageInfo()).thenReturn(info); 89 90 assertThat(mController.getAvailabilityStatus()) 91 .isEqualTo(BasePreferenceController.DISABLED_FOR_USER); 92 } 93 94 @Test 95 public void getAvailabilityStatus_hasSystemAlertWindowPermission_shouldReturnAvailable() { 96 when(mUserManager.isManagedProfile()).thenReturn(false); 97 final PackageInfo info = new PackageInfo(); 98 info.requestedPermissions = new String[] {SYSTEM_ALERT_WINDOW}; 99 when(mFragment.getPackageInfo()).thenReturn(info); 100 101 assertThat(mController.getAvailabilityStatus()) 102 .isEqualTo(BasePreferenceController.AVAILABLE); 103 } 104 105 @Test 106 public void getDetailFragmentClass_shouldReturnDrawOverlayDetails() { 107 assertThat(mController.getDetailFragmentClass()).isEqualTo(DrawOverlayDetails.class); 108 } 109 110 @Test 111 public void updateState_shouldSetSummary() { 112 final String summary = "test summary"; 113 doReturn(summary).when(mController).getSummary(); 114 115 mController.updateState(mPreference); 116 117 verify(mPreference).setSummary(summary); 118 } 119 } 120