From 47a6dbcd9403be105d00ba17b37a1fe9a1a48438 Mon Sep 17 00:00:00 2001 From: timhypeng Date: Tue, 14 Aug 2018 10:30:50 +0800 Subject: [PATCH] Remove test-purpose constructor from AvailableMediaBluetoothDeviceUpdater - replace mock object with ShadowCachedBluetoothDeviceManager to test CachedBluetoothDevice - rename cachedDevices to mCachedDevices Bug: 111848213 Test: make -j50 RunSettingsRoboTests Change-Id: I3028a6fe06c39c48e7cee33976bdfcab2c8b73c8 --- .../AvailableMediaBluetoothDeviceUpdater.java | 11 ----- ...ilableMediaBluetoothDeviceUpdaterTest.java | 40 ++++++++----------- 2 files changed, 16 insertions(+), 35 deletions(-) diff --git a/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java index 2b9c2cb5565..c2e6f2f8d89 100644 --- a/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java +++ b/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java @@ -23,9 +23,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 AvailableMediaBluetoothDeviceUpdater extends BluetoothDeviceUpdater mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); } - @VisibleForTesting - AvailableMediaBluetoothDeviceUpdater(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/AvailableMediaBluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdaterTest.java index e676cf4b041..14d9ca79d01 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdaterTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdaterTest.java @@ -33,10 +33,9 @@ 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.HeadsetProfile; -import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; import org.junit.Before; @@ -52,7 +51,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 AvailableMediaBluetoothDeviceUpdaterTest { @Mock private DashboardFragment mDashboardFragment; @@ -62,21 +62,14 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { private CachedBluetoothDevice mCachedBluetoothDevice; @Mock private BluetoothDevice mBluetoothDevice; - @Mock - private LocalBluetoothManager mLocalManager; - @Mock - private LocalBluetoothProfileManager mLocalBluetoothProfileManager; - @Mock - private HeadsetProfile mHeadsetProfile; - @Mock - private CachedBluetoothDeviceManager mCachedDeviceManager; private Context mContext; private AvailableMediaBluetoothDeviceUpdater mBluetoothDeviceUpdater; - private Collection cachedDevices; + private Collection mCachedDevices; private ShadowAudioManager mShadowAudioManager; private BluetoothDevicePreference mPreference; private ShadowBluetoothAdapter mShadowBluetoothAdapter; + private ShadowCachedBluetoothDeviceManager mShadowCachedBluetoothDeviceManager; @Before public void setUp() { @@ -86,18 +79,17 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); mShadowBluetoothAdapter.setEnabled(true); mContext = RuntimeEnvironment.application; - doReturn(mContext).when(mDashboardFragment).getContext(); - cachedDevices = + mShadowCachedBluetoothDeviceManager = Shadow.extract( + Utils.getLocalBtManager(mContext).getCachedDeviceManager()); + mCachedDevices = new ArrayList(new ArrayList()); + mShadowCachedBluetoothDeviceManager.setCachedDevicesCopy(mCachedDevices); + doReturn(mContext).when(mDashboardFragment).getContext(); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); - when(mLocalManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager); - when(mLocalBluetoothProfileManager.getHeadsetProfile()).thenReturn(mHeadsetProfile); - when(mLocalManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager); - when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices); - mBluetoothDeviceUpdater = spy(new AvailableMediaBluetoothDeviceUpdater(mDashboardFragment, - mDevicePreferenceCallback, mLocalManager)); + mBluetoothDeviceUpdater = spy(new AvailableMediaBluetoothDeviceUpdater(mContext, + mDashboardFragment, mDevicePreferenceCallback)); mBluetoothDeviceUpdater.setPrefContext(mContext); mPreference = new BluetoothDevicePreference(mContext, mCachedBluetoothDevice, false); doNothing().when(mBluetoothDeviceUpdater).addPreference(any()); @@ -110,7 +102,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true); - cachedDevices.add(mCachedBluetoothDevice); + mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged(); @@ -123,7 +115,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true); - cachedDevices.add(mCachedBluetoothDevice); + mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged(); @@ -136,7 +128,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true); - cachedDevices.add(mCachedBluetoothDevice); + mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged(); @@ -149,7 +141,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true); - cachedDevices.add(mCachedBluetoothDevice); + mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged();