| /* |
| * Copyright 2018 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.android.car.settings.bluetooth; |
| |
| import static android.content.pm.PackageManager.FEATURE_BLUETOOTH; |
| import static android.os.UserManager.DISALLOW_BLUETOOTH; |
| |
| import static com.android.car.settings.common.PreferenceController.AVAILABLE; |
| import static com.android.car.settings.common.PreferenceController.CONDITIONALLY_UNAVAILABLE; |
| import static com.android.car.settings.common.PreferenceController.DISABLED_FOR_USER; |
| import static com.android.car.settings.common.PreferenceController.UNSUPPORTED_ON_DEVICE; |
| |
| import static com.google.common.truth.Truth.assertThat; |
| |
| import static org.mockito.Mockito.verify; |
| import static org.mockito.Mockito.when; |
| |
| import android.bluetooth.BluetoothAdapter; |
| import android.car.drivingstate.CarUxRestrictions; |
| import android.car.userlib.CarUserManagerHelper; |
| import android.content.Context; |
| |
| import androidx.lifecycle.Lifecycle; |
| import androidx.preference.Preference; |
| |
| import com.android.car.settings.CarSettingsRobolectricTestRunner; |
| import com.android.car.settings.common.FragmentController; |
| import com.android.car.settings.common.PreferenceControllerTestHelper; |
| import com.android.car.settings.testutils.ShadowBluetoothAdapter; |
| import com.android.car.settings.testutils.ShadowBluetoothPan; |
| import com.android.car.settings.testutils.ShadowCarUserManagerHelper; |
| import com.android.settingslib.bluetooth.BluetoothEventManager; |
| import com.android.settingslib.bluetooth.LocalBluetoothManager; |
| |
| import org.junit.After; |
| import org.junit.Before; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| import org.mockito.Mock; |
| import org.mockito.MockitoAnnotations; |
| import org.robolectric.RuntimeEnvironment; |
| import org.robolectric.Shadows; |
| import org.robolectric.annotation.Config; |
| import org.robolectric.shadow.api.Shadow; |
| import org.robolectric.util.ReflectionHelpers; |
| |
| /** Unit test for {@link BluetoothPreferenceController}. */ |
| @RunWith(CarSettingsRobolectricTestRunner.class) |
| @Config(shadows = {ShadowCarUserManagerHelper.class, ShadowBluetoothAdapter.class, |
| ShadowBluetoothPan.class}) |
| public class BluetoothPreferenceControllerTest { |
| |
| @Mock |
| private CarUserManagerHelper mCarUserManagerHelper; |
| @Mock |
| private BluetoothEventManager mEventManager; |
| private BluetoothEventManager mSaveRealEventManager; |
| private LocalBluetoothManager mLocalBluetoothManager; |
| private PreferenceControllerTestHelper<TestBluetoothPreferenceController> mControllerHelper; |
| private TestBluetoothPreferenceController mController; |
| |
| @Before |
| public void setUp() { |
| MockitoAnnotations.initMocks(this); |
| ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper); |
| Context context = RuntimeEnvironment.application; |
| mLocalBluetoothManager = LocalBluetoothManager.getInstance(context, /* onInitCallback= */ |
| null); |
| mSaveRealEventManager = mLocalBluetoothManager.getEventManager(); |
| ReflectionHelpers.setField(mLocalBluetoothManager, "mEventManager", mEventManager); |
| mControllerHelper = new PreferenceControllerTestHelper<>(context, |
| TestBluetoothPreferenceController.class, new Preference(context)); |
| mController = mControllerHelper.getController(); |
| } |
| |
| @After |
| public void tearDown() { |
| ShadowCarUserManagerHelper.reset(); |
| ShadowBluetoothAdapter.reset(); |
| ReflectionHelpers.setField(mLocalBluetoothManager, "mEventManager", mSaveRealEventManager); |
| } |
| |
| @Test |
| public void getAvailabilityStatus_bluetoothNotAvailable_unsupportedOnDevice() { |
| Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager()).setSystemFeature( |
| FEATURE_BLUETOOTH, /* supported= */ false); |
| |
| assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); |
| } |
| |
| @Test |
| public void getAvailabilityStatus_disallowBluetoothUserRestriction_disabledForUser() { |
| Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager()).setSystemFeature( |
| FEATURE_BLUETOOTH, /* supported= */ true); |
| when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction( |
| DISALLOW_BLUETOOTH)).thenReturn(true); |
| |
| assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER); |
| } |
| |
| @Test |
| public void getAvailabilityStatus_adapterDisabled_conditionallyUnavailable() { |
| Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager()).setSystemFeature( |
| FEATURE_BLUETOOTH, /* supported= */ true); |
| BluetoothAdapter.getDefaultAdapter().disable(); |
| getShadowBluetoothAdapter().setState(BluetoothAdapter.STATE_OFF); |
| |
| assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); |
| } |
| |
| @Test |
| public void getAvailabilityStatus_available() { |
| Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager()).setSystemFeature( |
| FEATURE_BLUETOOTH, /* supported= */ true); |
| BluetoothAdapter.getDefaultAdapter().enable(); |
| getShadowBluetoothAdapter().setState(BluetoothAdapter.STATE_ON); |
| // No user restrictions. |
| |
| assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); |
| } |
| |
| @Test |
| public void onStart_registersEventListener() { |
| Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager()).setSystemFeature( |
| FEATURE_BLUETOOTH, /* supported= */ true); |
| BluetoothAdapter.getDefaultAdapter().enable(); |
| getShadowBluetoothAdapter().setState(BluetoothAdapter.STATE_ON); |
| mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_START); |
| |
| verify(mEventManager).registerCallback(mController); |
| } |
| |
| @Test |
| public void onStop_unregistersEventListener() { |
| Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager()).setSystemFeature( |
| FEATURE_BLUETOOTH, /* supported= */ true); |
| BluetoothAdapter.getDefaultAdapter().enable(); |
| getShadowBluetoothAdapter().setState(BluetoothAdapter.STATE_ON); |
| mControllerHelper.markState(Lifecycle.State.STARTED); |
| mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_STOP); |
| |
| verify(mEventManager).unregisterCallback(mController); |
| } |
| |
| private ShadowBluetoothAdapter getShadowBluetoothAdapter() { |
| return (ShadowBluetoothAdapter) Shadow.extract(BluetoothAdapter.getDefaultAdapter()); |
| } |
| |
| /** Concrete impl of {@link BluetoothPreferenceController} for testing. */ |
| private static class TestBluetoothPreferenceController extends |
| BluetoothPreferenceController<Preference> { |
| |
| TestBluetoothPreferenceController(Context context, String preferenceKey, |
| FragmentController fragmentController, CarUxRestrictions uxRestrictions) { |
| super(context, preferenceKey, fragmentController, uxRestrictions); |
| } |
| |
| @Override |
| protected Class<Preference> getPreferenceType() { |
| return Preference.class; |
| } |
| } |
| } |