OSDN Git Service

Replace LocalBluetoothAdapter with BluetoothAdapter
authorChienyuan <chienyuanhuang@google.com>
Wed, 8 Aug 2018 07:44:42 +0000 (15:44 +0800)
committerChienyuan <chienyuanhuang@google.com>
Thu, 9 Aug 2018 03:40:54 +0000 (11:40 +0800)
LocalBluetoothAdapter is obsolete, use BluetoothAdapter instead.

Bug: 111810977
Test: make -j50 RunSettingsRoboTests
Change-Id: I5109a0296c1006a3c2e346bf966ef8901c101e30

src/com/android/settings/accessibility/AccessibilityHearingAidPreferenceController.java
tests/robotests/src/com/android/settings/accessibility/AccessibilityHearingAidPreferenceControllerTest.java
tests/robotests/src/com/android/settings/testutils/shadow/ShadowBluetoothAdapter.java

index b946e3e..b9713d7 100644 (file)
@@ -39,7 +39,6 @@ import com.android.settings.bluetooth.BluetoothDeviceDetailsFragment;
 import com.android.settings.core.BasePreferenceController;
 import com.android.settings.core.SubSettingLauncher;
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
-import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
 import com.android.settingslib.core.lifecycle.LifecycleObserver;
 import com.android.settingslib.core.lifecycle.events.OnPause;
@@ -82,6 +81,7 @@ public class AccessibilityHearingAidPreferenceController extends BasePreferenceC
     };
 
     private final LocalBluetoothManager mLocalBluetoothManager;
+    private final BluetoothAdapter mBluetoothAdapter;
     //cache value of supporting hearing aid or not
     private boolean mHearingAidProfileSupported;
     private FragmentManager mFragmentManager;
@@ -89,6 +89,7 @@ public class AccessibilityHearingAidPreferenceController extends BasePreferenceC
     public AccessibilityHearingAidPreferenceController(Context context, String preferenceKey) {
         super(context, preferenceKey);
         mLocalBluetoothManager = getLocalBluetoothManager();
+        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
         mHearingAidProfileSupported = isHearingAidProfileSupported();
     }
 
@@ -151,8 +152,7 @@ public class AccessibilityHearingAidPreferenceController extends BasePreferenceC
         if (!mHearingAidProfileSupported) {
             return null;
         }
-        final LocalBluetoothAdapter localAdapter = mLocalBluetoothManager.getBluetoothAdapter();
-        if (!localAdapter.isEnabled()) {
+        if (!mBluetoothAdapter.isEnabled()) {
             return null;
         }
         final List<BluetoothDevice> deviceList = mLocalBluetoothManager.getProfileManager()
@@ -166,8 +166,7 @@ public class AccessibilityHearingAidPreferenceController extends BasePreferenceC
     }
 
     private boolean isHearingAidProfileSupported() {
-        final LocalBluetoothAdapter localAdapter = mLocalBluetoothManager.getBluetoothAdapter();
-        final List<Integer> supportedList = localAdapter.getSupportedProfiles();
+        final List<Integer> supportedList = mBluetoothAdapter.getSupportedProfiles();
         if (supportedList.contains(BluetoothProfile.HEARING_AID)) {
             return true;
         }
index 8957f85..9fdcef5 100644 (file)
@@ -40,10 +40,10 @@ import androidx.preference.Preference;
 import com.android.settings.R;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
+import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
 import com.android.settingslib.bluetooth.HearingAidProfile;
-import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
 
@@ -55,23 +55,24 @@ import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
+import org.robolectric.shadow.api.Shadow;
 
 import java.util.ArrayList;
 import java.util.List;
 
 @RunWith(SettingsRobolectricTestRunner.class)
-@Config(shadows = {ShadowBluetoothUtils.class})
+@Config(shadows = {ShadowBluetoothAdapter.class, ShadowBluetoothUtils.class})
 public class AccessibilityHearingAidPreferenceControllerTest {
     private static final String TEST_DEVICE_ADDRESS = "00:A1:A1:A1:A1:A1";
     private static final String TEST_DEVICE_NAME = "TEST_HEARING_AID_BT_DEVICE_NAME";
     private static final String HEARING_AID_PREFERENCE = "hearing_aid_preference";
 
     private BluetoothAdapter mBluetoothAdapter;
+    private ShadowBluetoothAdapter mShadowBluetoothAdapter;
     private BluetoothManager mBluetoothManager;
     private BluetoothDevice mBluetoothDevice;
     private Context mContext;
     private Preference mHearingAidPreference;
-    private List<Integer> mProfileSupportedList;
     private AccessibilityHearingAidPreferenceController mPreferenceController;
 
     @Mock
@@ -79,8 +80,6 @@ public class AccessibilityHearingAidPreferenceControllerTest {
     @Mock
     private CachedBluetoothDeviceManager mCachedDeviceManager;
     @Mock
-    private LocalBluetoothAdapter mLocalBluetoothAdapter;
-    @Mock
     private LocalBluetoothManager mLocalBluetoothManager;
     @Mock
     private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
@@ -161,7 +160,7 @@ public class AccessibilityHearingAidPreferenceControllerTest {
     @Test
     public void onNotSupportHearingAidProfile_doNotDoReceiverOperation() {
         //clear bluetooth supported profile
-        mProfileSupportedList.clear();
+        mShadowBluetoothAdapter.clearSupportedProfiles();
         mPreferenceController = new AccessibilityHearingAidPreferenceController(mContext, HEARING_AID_PREFERENCE);
         mPreferenceController.setPreference(mHearingAidPreference);
         //not call registerReceiver()
@@ -178,18 +177,17 @@ public class AccessibilityHearingAidPreferenceControllerTest {
         mLocalBluetoothManager = ShadowBluetoothUtils.getLocalBtManager(mContext);
         mBluetoothManager = new BluetoothManager(mContext);
         mBluetoothAdapter = mBluetoothManager.getAdapter();
-        when(mLocalBluetoothManager.getBluetoothAdapter()).thenReturn(mLocalBluetoothAdapter);
-        when(mLocalBluetoothAdapter.isEnabled()).thenReturn(true);
         when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
         when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
         when(mLocalBluetoothProfileManager.getHearingAidProfile()).thenReturn(mHearingAidProfile);
     }
 
     private void setupHearingAidEnvironment() {
+        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
+        mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
         mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS);
-        mProfileSupportedList = new ArrayList<Integer>();
-        mProfileSupportedList.add(BluetoothProfile.HEARING_AID);
-        when(mLocalBluetoothAdapter.getSupportedProfiles()).thenReturn(mProfileSupportedList);
+        mShadowBluetoothAdapter.enable();
+        mShadowBluetoothAdapter.addSupportedProfiles(BluetoothProfile.HEARING_AID);
         when(mCachedDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice);
         when(mCachedBluetoothDevice.getName()).thenReturn(TEST_DEVICE_NAME);
         when(mCachedBluetoothDevice.isConnectedHearingAidDevice()).thenReturn(true);
index 91afb87..30837f4 100644 (file)
@@ -30,13 +30,19 @@ public class ShadowBluetoothAdapter extends org.robolectric.shadows.ShadowBlueto
     private String mName;
     private int mScanMode;
     private int mState;
+    private List<Integer> mSupportedProfiles = new ArrayList<Integer>();
 
-    /**
-     * Do nothing, implement it to avoid null pointer error inside BluetoothAdapter
-     */
     @Implementation
     public List<Integer> getSupportedProfiles() {
-        return new ArrayList<Integer>();
+        return mSupportedProfiles;
+    }
+
+    public void addSupportedProfiles(int profile) {
+        mSupportedProfiles.add(profile);
+    }
+
+    public void clearSupportedProfiles() {
+        mSupportedProfiles.clear();
     }
 
     public void setName(String name) {