1 /* 2 * Copyright (C) 2018 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 package com.android.settings.connecteddevice; 17 18 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 19 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.junit.Assert.assertTrue; 24 import static org.mockito.Mockito.when; 25 26 import android.bluetooth.BluetoothAdapter; 27 import android.content.BroadcastReceiver; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.content.pm.PackageManager; 31 import android.text.TextUtils; 32 33 import androidx.preference.PreferenceScreen; 34 35 import com.android.settings.R; 36 import com.android.settingslib.RestrictedPreference; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.RobolectricTestRunner; 44 import org.robolectric.RuntimeEnvironment; 45 import org.robolectric.Shadows; 46 import org.robolectric.annotation.Config; 47 import org.robolectric.shadows.ShadowApplicationPackageManager; 48 import org.robolectric.util.ReflectionHelpers; 49 50 @RunWith(RobolectricTestRunner.class) 51 @Config(shadows = ShadowApplicationPackageManager.class) 52 public class AddDevicePreferenceControllerTest { 53 54 @Mock 55 private PreferenceScreen mScreen; 56 @Mock 57 private BluetoothAdapter mBluetoothAdapter; 58 59 private Context mContext; 60 private AddDevicePreferenceController mAddDevicePreferenceController; 61 private RestrictedPreference mAddDevicePreference; 62 private ShadowApplicationPackageManager mPackageManager; 63 64 65 @Before setUp()66 public void setUp() { 67 MockitoAnnotations.initMocks(this); 68 69 mContext = RuntimeEnvironment.application; 70 mPackageManager = (ShadowApplicationPackageManager) Shadows.shadowOf( 71 mContext.getPackageManager()); 72 mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, true); 73 74 mAddDevicePreferenceController = new AddDevicePreferenceController(mContext, 75 "add_bt_devices"); 76 ReflectionHelpers.setField(mAddDevicePreferenceController, 77 "mBluetoothAdapter", mBluetoothAdapter); 78 79 String key = mAddDevicePreferenceController.getPreferenceKey(); 80 mAddDevicePreference = new RestrictedPreference(mContext); 81 mAddDevicePreference.setKey(key); 82 when(mBluetoothAdapter.isEnabled()).thenReturn(true); 83 when(mScreen.findPreference(key)).thenReturn(mAddDevicePreference); 84 mAddDevicePreferenceController.displayPreference(mScreen); 85 } 86 87 @Test addDevice_bt_resume_on_then_off()88 public void addDevice_bt_resume_on_then_off() { 89 when(mBluetoothAdapter.isEnabled()).thenReturn(true); 90 mAddDevicePreferenceController.updateState(mAddDevicePreference); 91 assertTrue(TextUtils.isEmpty(mAddDevicePreference.getSummary())); 92 93 Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED); 94 intent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF); 95 BroadcastReceiver receiver = ReflectionHelpers.getField( 96 mAddDevicePreferenceController, "mReceiver"); 97 when(mBluetoothAdapter.isEnabled()).thenReturn(false); 98 receiver.onReceive(mContext, intent); 99 assertThat(mAddDevicePreference.getSummary()).isEqualTo( 100 mContext.getString(R.string.connected_device_add_device_summary)); 101 } 102 103 @Test addDevice_bt_resume_off_then_on()104 public void addDevice_bt_resume_off_then_on() { 105 when(mBluetoothAdapter.isEnabled()).thenReturn(false); 106 mAddDevicePreferenceController.updateState(mAddDevicePreference); 107 assertThat(mAddDevicePreference.getSummary()).isEqualTo( 108 mContext.getString(R.string.connected_device_add_device_summary)); 109 110 Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED); 111 intent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_ON); 112 BroadcastReceiver receiver = ReflectionHelpers.getField( 113 mAddDevicePreferenceController, "mReceiver"); 114 when(mBluetoothAdapter.isEnabled()).thenReturn(true); 115 receiver.onReceive(mContext, intent); 116 assertTrue(TextUtils.isEmpty(mAddDevicePreference.getSummary())); 117 } 118 119 @Test addDevice_Availability_UnSupported()120 public void addDevice_Availability_UnSupported() { 121 mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, false); 122 assertThat(mAddDevicePreferenceController.getAvailabilityStatus()) 123 .isEqualTo(UNSUPPORTED_ON_DEVICE); 124 } 125 126 @Test addDevice_Availability_Supported()127 public void addDevice_Availability_Supported() { 128 mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, true); 129 assertThat(mAddDevicePreferenceController.getAvailabilityStatus()) 130 .isEqualTo(AVAILABLE); 131 } 132 133 @Test getAvailabilityStatus_noBluetoothFeature_unSupported()134 public void getAvailabilityStatus_noBluetoothFeature_unSupported() { 135 mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, false); 136 137 assertThat(mAddDevicePreferenceController.getAvailabilityStatus()) 138 .isEqualTo(UNSUPPORTED_ON_DEVICE); 139 } 140 } 141