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.display; 18 19 import static com.android.settings.display.SmartAutoRotatePreferenceFragment.AUTO_ROTATE_SWITCH_PREFERENCE_ID; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.never; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.times; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.when; 29 30 import android.Manifest; 31 import android.content.ContentResolver; 32 import android.content.Context; 33 import android.content.pm.PackageManager; 34 import android.content.pm.ResolveInfo; 35 import android.content.pm.ServiceInfo; 36 import android.view.View; 37 38 import androidx.preference.Preference; 39 40 import com.android.settings.R; 41 import com.android.settings.SettingsActivity; 42 import com.android.settings.testutils.ResolveInfoBuilder; 43 import com.android.settings.widget.SettingsMainSwitchBar; 44 45 import org.junit.Before; 46 import org.junit.Test; 47 import org.junit.runner.RunWith; 48 import org.mockito.Mock; 49 import org.mockito.MockitoAnnotations; 50 import org.robolectric.RobolectricTestRunner; 51 import org.robolectric.RuntimeEnvironment; 52 53 @RunWith(RobolectricTestRunner.class) 54 public class SmartAutoRotatePreferenceFragmentTest { 55 56 private static final String PACKAGE_NAME = "package_name"; 57 58 private SmartAutoRotatePreferenceFragment mFragment; 59 60 private SettingsMainSwitchBar mSwitchBar; 61 62 @Mock 63 private PackageManager mPackageManager; 64 65 @Mock 66 private View mView; 67 68 @Mock 69 private SettingsActivity mActivity; 70 71 @Mock 72 private Preference mRotateSwitchPreference; 73 74 @Before setUp()75 public void setUp() { 76 MockitoAnnotations.initMocks(this); 77 78 final Context context = spy(RuntimeEnvironment.application); 79 ContentResolver mContentResolver = RuntimeEnvironment.application.getContentResolver(); 80 when(context.getPackageManager()).thenReturn(mPackageManager); 81 when(context.getContentResolver()).thenReturn(mContentResolver); 82 doReturn(PACKAGE_NAME).when(mPackageManager).getRotationResolverPackageName(); 83 doReturn(PackageManager.PERMISSION_GRANTED).when(mPackageManager).checkPermission( 84 Manifest.permission.CAMERA, PACKAGE_NAME); 85 86 final ResolveInfo resolveInfo = new ResolveInfoBuilder(PACKAGE_NAME).build(); 87 resolveInfo.serviceInfo = new ServiceInfo(); 88 when(mPackageManager.resolveService(any(), anyInt())).thenReturn(resolveInfo); 89 90 mFragment = spy(new SmartAutoRotatePreferenceFragment()); 91 when(mActivity.getPackageManager()).thenReturn(mPackageManager); 92 when(mFragment.getActivity()).thenReturn(mActivity); 93 when(mFragment.getContext()).thenReturn(context); 94 doReturn(mView).when(mFragment).getView(); 95 96 when(mFragment.findPreference(AUTO_ROTATE_SWITCH_PREFERENCE_ID)).thenReturn( 97 mRotateSwitchPreference); 98 99 mSwitchBar = spy(new SettingsMainSwitchBar(context)); 100 when(mActivity.getSwitchBar()).thenReturn(mSwitchBar); 101 doReturn(mSwitchBar).when(mView).findViewById(R.id.switch_bar); 102 } 103 104 105 @Test createHeader_faceDetectionSupported_switchBarIsEnabled()106 public void createHeader_faceDetectionSupported_switchBarIsEnabled() { 107 mFragment.createHeader(mActivity); 108 109 verify(mSwitchBar, times(1)).show(); 110 verify(mRotateSwitchPreference, times(1)).setVisible(false); 111 } 112 113 @Test createHeader_faceDetectionUnSupported_switchBarIsDisabled()114 public void createHeader_faceDetectionUnSupported_switchBarIsDisabled() { 115 doReturn(null).when(mPackageManager).getRotationResolverPackageName(); 116 117 mFragment.createHeader(mActivity); 118 119 verify(mSwitchBar, never()).show(); 120 verify(mRotateSwitchPreference, never()).setVisible(false); 121 } 122 123 } 124