From 273c6d391883ba87daf79dae56cacf1ab05fbe40 Mon Sep 17 00:00:00 2001 From: timhypeng Date: Mon, 13 Aug 2018 17:38:36 +0800 Subject: [PATCH] Remove test-purpose constructor from ConnectedBluetoothDeviceUpdater - replace mock object with ShadowCachedBluetoothDeviceManager to test CachedBluetoothDevice - rename cachedDevices to mCachedDevices Bug: 111848213 Test: make -j50 RunSettingsRoboTests Change-Id: Ib024a3e9c3af745b1ab0be36361165a547cfa756 --- .../ConnectedBluetoothDeviceUpdater.java | 11 ----- .../ConnectedBluetoothDeviceUpdaterTest.java | 33 +++++++------- .../ShadowCachedBluetoothDeviceManager.java | 44 +++++++++++++++++++ 3 files changed, 59 insertions(+), 29 deletions(-) create mode 100644 tests/robotests/src/com/android/settings/testutils/shadow/ShadowCachedBluetoothDeviceManager.java diff --git a/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java index 259a403625b..b66b286b0fd 100644 --- a/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java +++ b/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java @@ -24,9 +24,7 @@ import android.util.Log; import com.android.settings.connecteddevice.DevicePreferenceCallback; import com.android.settings.dashboard.DashboardFragment; import com.android.settingslib.bluetooth.CachedBluetoothDevice; -import com.android.settingslib.bluetooth.LocalBluetoothManager; -import androidx.annotation.VisibleForTesting; import androidx.preference.Preference; /** @@ -45,15 +43,6 @@ public class ConnectedBluetoothDeviceUpdater extends BluetoothDeviceUpdater { mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); } - @VisibleForTesting - ConnectedBluetoothDeviceUpdater(DashboardFragment fragment, - DevicePreferenceCallback devicePreferenceCallback, - LocalBluetoothManager localBluetoothManager) { - super(fragment, devicePreferenceCallback, localBluetoothManager); - mAudioManager = (AudioManager) fragment.getContext(). - getSystemService(Context.AUDIO_SERVICE); - } - @Override public void onAudioModeChanged() { forceUpdate(); diff --git a/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java index ece71d73d9c..6faea9a2334 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java @@ -34,9 +34,8 @@ import com.android.settings.dashboard.DashboardFragment; import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.shadow.ShadowAudioManager; import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; +import com.android.settings.testutils.shadow.ShadowCachedBluetoothDeviceManager; import com.android.settingslib.bluetooth.CachedBluetoothDevice; -import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; -import com.android.settingslib.bluetooth.LocalBluetoothManager; import org.junit.Before; import org.junit.Test; @@ -51,7 +50,8 @@ import java.util.ArrayList; import java.util.Collection; @RunWith(SettingsRobolectricTestRunner.class) -@Config(shadows = {ShadowAudioManager.class, ShadowBluetoothAdapter.class}) +@Config(shadows = {ShadowAudioManager.class, ShadowBluetoothAdapter.class, + ShadowCachedBluetoothDeviceManager.class}) public class ConnectedBluetoothDeviceUpdaterTest { @Mock private DashboardFragment mDashboardFragment; @@ -61,16 +61,13 @@ public class ConnectedBluetoothDeviceUpdaterTest { private CachedBluetoothDevice mCachedBluetoothDevice; @Mock private BluetoothDevice mBluetoothDevice; - @Mock - private LocalBluetoothManager mLocalManager; - @Mock - private CachedBluetoothDeviceManager mCachedDeviceManager; private Context mContext; private ConnectedBluetoothDeviceUpdater mBluetoothDeviceUpdater; - private Collection cachedDevices; + private Collection mCachedDevices; private ShadowAudioManager mShadowAudioManager; private ShadowBluetoothAdapter mShadowBluetoothAdapter; + private ShadowCachedBluetoothDeviceManager mShadowCachedBluetoothDeviceManager; @Before public void setUp() { @@ -80,16 +77,16 @@ public class ConnectedBluetoothDeviceUpdaterTest { mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); mShadowBluetoothAdapter.setEnabled(true); mContext = RuntimeEnvironment.application; + mShadowCachedBluetoothDeviceManager = Shadow.extract( + Utils.getLocalBtManager(mContext).getCachedDeviceManager()); doReturn(mContext).when(mDashboardFragment).getContext(); - cachedDevices = + mCachedDevices = new ArrayList(new ArrayList()); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); - when(mLocalManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager); - when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices); - - mBluetoothDeviceUpdater = spy(new ConnectedBluetoothDeviceUpdater(mDashboardFragment, - mDevicePreferenceCallback, mLocalManager)); + mShadowCachedBluetoothDeviceManager.setCachedDevicesCopy(mCachedDevices); + mBluetoothDeviceUpdater = spy(new ConnectedBluetoothDeviceUpdater(mContext, + mDashboardFragment, mDevicePreferenceCallback)); mBluetoothDeviceUpdater.setPrefContext(mContext); doNothing().when(mBluetoothDeviceUpdater).addPreference(any()); doNothing().when(mBluetoothDeviceUpdater).removePreference(any()); @@ -101,7 +98,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true); - cachedDevices.add(mCachedBluetoothDevice); + mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged(); @@ -114,7 +111,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true); - cachedDevices.add(mCachedBluetoothDevice); + mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged(); @@ -127,7 +124,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true); - cachedDevices.add(mCachedBluetoothDevice); + mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged(); @@ -140,7 +137,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true); - cachedDevices.add(mCachedBluetoothDevice); + mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged(); diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowCachedBluetoothDeviceManager.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowCachedBluetoothDeviceManager.java new file mode 100644 index 00000000000..ee04c4f1d38 --- /dev/null +++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowCachedBluetoothDeviceManager.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 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.settings.testutils.shadow; + +import com.android.settingslib.bluetooth.CachedBluetoothDevice; +import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; + +import java.util.Collection; + +/** + * Shadow class for {@link CachedBluetoothDeviceManager} to allow tests to manages the set of + * remote Bluetooth devices. + */ +@Implements(CachedBluetoothDeviceManager.class) +public class ShadowCachedBluetoothDeviceManager { + + private Collection mCachedDevices; + + public void setCachedDevicesCopy(Collection cachedDevices) { + mCachedDevices = cachedDevices; + } + + @Implementation + public synchronized Collection getCachedDevicesCopy() { + return mCachedDevices; + } +} \ No newline at end of file