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.uwb; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.clearInvocations; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.never; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 27 import android.content.Context; 28 import android.content.pm.PackageManager; 29 import android.uwb.UwbManager; 30 31 import com.android.settings.core.BasePreferenceController; 32 33 import org.junit.Before; 34 import org.junit.Rule; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.junit.MockitoJUnit; 39 import org.mockito.junit.MockitoRule; 40 import org.robolectric.RobolectricTestRunner; 41 import org.robolectric.RuntimeEnvironment; 42 43 /** Unit tests for UWB preference toggle. */ 44 @RunWith(RobolectricTestRunner.class) 45 public class UwbPreferenceControllerTest { 46 @Rule 47 public MockitoRule rule = MockitoJUnit.rule(); 48 49 private Context mContext; 50 private PackageManager mPackageManager; 51 private UwbPreferenceController mController; 52 53 @Mock 54 private UwbManager mUwbManager; 55 56 @Before setUp()57 public void setUp() { 58 mContext = spy(RuntimeEnvironment.application); 59 mPackageManager = spy(mContext.getPackageManager()); 60 mController = new UwbPreferenceController(mContext, "uwb_settings"); 61 mController.mUwbManager = mUwbManager; 62 } 63 64 @Test getAvailabilityStatus_uwbDisabled_shouldReturnDisabled()65 public void getAvailabilityStatus_uwbDisabled_shouldReturnDisabled() { 66 doReturn(mPackageManager).when(mContext).getPackageManager(); 67 doReturn(true).when(mPackageManager) 68 .hasSystemFeature(PackageManager.FEATURE_UWB); 69 mController.mAirplaneModeOn = true; 70 71 assertThat(mController.getAvailabilityStatus()) 72 .isEqualTo(BasePreferenceController.DISABLED_DEPENDENT_SETTING); 73 } 74 75 @Test getAvailabilityStatus_uwbShown_shouldReturnAvailable()76 public void getAvailabilityStatus_uwbShown_shouldReturnAvailable() { 77 doReturn(mPackageManager).when(mContext).getPackageManager(); 78 doReturn(true).when(mPackageManager) 79 .hasSystemFeature(PackageManager.FEATURE_UWB); 80 mController.mAirplaneModeOn = false; 81 82 assertThat(mController.getAvailabilityStatus()) 83 .isEqualTo(BasePreferenceController.AVAILABLE); 84 } 85 86 @Test getAvailabilityStatus_uwbNotShown_shouldReturnUnsupported()87 public void getAvailabilityStatus_uwbNotShown_shouldReturnUnsupported() { 88 doReturn(mPackageManager).when(mContext).getPackageManager(); 89 doReturn(false).when(mPackageManager) 90 .hasSystemFeature(PackageManager.FEATURE_UWB); 91 92 assertThat(mController.getAvailabilityStatus()) 93 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE); 94 } 95 96 @Test isChecked_uwbEnabled_shouldReturnTrue()97 public void isChecked_uwbEnabled_shouldReturnTrue() { 98 doReturn(mController.STATE_ENABLED_ACTIVE).when(mUwbManager).getAdapterState(); 99 100 assertThat(mController.isChecked()).isTrue(); 101 } 102 103 @Test isChecked_uwbDisabled_shouldReturnFalse()104 public void isChecked_uwbDisabled_shouldReturnFalse() { 105 doReturn(mController.STATE_DISABLED).when(mUwbManager).getAdapterState(); 106 107 assertThat(mController.isChecked()).isFalse(); 108 } 109 110 @Test setChecked_uwbDisabled_shouldEnableUwb()111 public void setChecked_uwbDisabled_shouldEnableUwb() { 112 clearInvocations(mUwbManager); 113 doReturn(mPackageManager).when(mContext).getPackageManager(); 114 doReturn(true).when(mPackageManager) 115 .hasSystemFeature(PackageManager.FEATURE_UWB); 116 117 mController.setChecked(true); 118 119 verify(mUwbManager).setUwbEnabled(true); 120 verify(mUwbManager, never()).setUwbEnabled(false); 121 } 122 123 @Test setChecked_uwbEnabled_shouldDisableUwb()124 public void setChecked_uwbEnabled_shouldDisableUwb() { 125 clearInvocations(mUwbManager); 126 doReturn(mPackageManager).when(mContext).getPackageManager(); 127 doReturn(true).when(mPackageManager) 128 .hasSystemFeature(PackageManager.FEATURE_UWB); 129 130 mController.setChecked(false); 131 132 verify(mUwbManager).setUwbEnabled(false); 133 verify(mUwbManager, never()).setUwbEnabled(true); 134 } 135 } 136 137