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
This commit is contained in:
timhypeng
2018-08-13 17:38:36 +08:00
committed by tim peng
parent 1aabc2cc4e
commit 273c6d3918
3 changed files with 59 additions and 29 deletions

View File

@@ -24,9 +24,7 @@ import android.util.Log;
import com.android.settings.connecteddevice.DevicePreferenceCallback; import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference; import androidx.preference.Preference;
/** /**
@@ -45,15 +43,6 @@ public class ConnectedBluetoothDeviceUpdater extends BluetoothDeviceUpdater {
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 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 @Override
public void onAudioModeChanged() { public void onAudioModeChanged() {
forceUpdate(); forceUpdate();

View File

@@ -34,9 +34,8 @@ import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowAudioManager; import com.android.settings.testutils.shadow.ShadowAudioManager;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; 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.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -51,7 +50,8 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = {ShadowAudioManager.class, ShadowBluetoothAdapter.class}) @Config(shadows = {ShadowAudioManager.class, ShadowBluetoothAdapter.class,
ShadowCachedBluetoothDeviceManager.class})
public class ConnectedBluetoothDeviceUpdaterTest { public class ConnectedBluetoothDeviceUpdaterTest {
@Mock @Mock
private DashboardFragment mDashboardFragment; private DashboardFragment mDashboardFragment;
@@ -61,16 +61,13 @@ public class ConnectedBluetoothDeviceUpdaterTest {
private CachedBluetoothDevice mCachedBluetoothDevice; private CachedBluetoothDevice mCachedBluetoothDevice;
@Mock @Mock
private BluetoothDevice mBluetoothDevice; private BluetoothDevice mBluetoothDevice;
@Mock
private LocalBluetoothManager mLocalManager;
@Mock
private CachedBluetoothDeviceManager mCachedDeviceManager;
private Context mContext; private Context mContext;
private ConnectedBluetoothDeviceUpdater mBluetoothDeviceUpdater; private ConnectedBluetoothDeviceUpdater mBluetoothDeviceUpdater;
private Collection<CachedBluetoothDevice> cachedDevices; private Collection<CachedBluetoothDevice> mCachedDevices;
private ShadowAudioManager mShadowAudioManager; private ShadowAudioManager mShadowAudioManager;
private ShadowBluetoothAdapter mShadowBluetoothAdapter; private ShadowBluetoothAdapter mShadowBluetoothAdapter;
private ShadowCachedBluetoothDeviceManager mShadowCachedBluetoothDeviceManager;
@Before @Before
public void setUp() { public void setUp() {
@@ -80,16 +77,16 @@ public class ConnectedBluetoothDeviceUpdaterTest {
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setEnabled(true); mShadowBluetoothAdapter.setEnabled(true);
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mShadowCachedBluetoothDeviceManager = Shadow.extract(
Utils.getLocalBtManager(mContext).getCachedDeviceManager());
doReturn(mContext).when(mDashboardFragment).getContext(); doReturn(mContext).when(mDashboardFragment).getContext();
cachedDevices = mCachedDevices =
new ArrayList<CachedBluetoothDevice>(new ArrayList<CachedBluetoothDevice>()); new ArrayList<CachedBluetoothDevice>(new ArrayList<CachedBluetoothDevice>());
when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
when(mLocalManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager); mShadowCachedBluetoothDeviceManager.setCachedDevicesCopy(mCachedDevices);
when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices); mBluetoothDeviceUpdater = spy(new ConnectedBluetoothDeviceUpdater(mContext,
mDashboardFragment, mDevicePreferenceCallback));
mBluetoothDeviceUpdater = spy(new ConnectedBluetoothDeviceUpdater(mDashboardFragment,
mDevicePreferenceCallback, mLocalManager));
mBluetoothDeviceUpdater.setPrefContext(mContext); mBluetoothDeviceUpdater.setPrefContext(mContext);
doNothing().when(mBluetoothDeviceUpdater).addPreference(any()); doNothing().when(mBluetoothDeviceUpdater).addPreference(any());
doNothing().when(mBluetoothDeviceUpdater).removePreference(any()); doNothing().when(mBluetoothDeviceUpdater).removePreference(any());
@@ -101,7 +98,7 @@ public class ConnectedBluetoothDeviceUpdaterTest {
when(mBluetoothDeviceUpdater. when(mBluetoothDeviceUpdater.
isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true); when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true);
cachedDevices.add(mCachedBluetoothDevice); mCachedDevices.add(mCachedBluetoothDevice);
mBluetoothDeviceUpdater.onAudioModeChanged(); mBluetoothDeviceUpdater.onAudioModeChanged();
@@ -114,7 +111,7 @@ public class ConnectedBluetoothDeviceUpdaterTest {
when(mBluetoothDeviceUpdater. when(mBluetoothDeviceUpdater.
isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true); when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true);
cachedDevices.add(mCachedBluetoothDevice); mCachedDevices.add(mCachedBluetoothDevice);
mBluetoothDeviceUpdater.onAudioModeChanged(); mBluetoothDeviceUpdater.onAudioModeChanged();
@@ -127,7 +124,7 @@ public class ConnectedBluetoothDeviceUpdaterTest {
when(mBluetoothDeviceUpdater. when(mBluetoothDeviceUpdater.
isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true); when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true);
cachedDevices.add(mCachedBluetoothDevice); mCachedDevices.add(mCachedBluetoothDevice);
mBluetoothDeviceUpdater.onAudioModeChanged(); mBluetoothDeviceUpdater.onAudioModeChanged();
@@ -140,7 +137,7 @@ public class ConnectedBluetoothDeviceUpdaterTest {
when(mBluetoothDeviceUpdater. when(mBluetoothDeviceUpdater.
isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true); when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true);
cachedDevices.add(mCachedBluetoothDevice); mCachedDevices.add(mCachedBluetoothDevice);
mBluetoothDeviceUpdater.onAudioModeChanged(); mBluetoothDeviceUpdater.onAudioModeChanged();

View File

@@ -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<CachedBluetoothDevice> mCachedDevices;
public void setCachedDevicesCopy(Collection<CachedBluetoothDevice> cachedDevices) {
mCachedDevices = cachedDevices;
}
@Implementation
public synchronized Collection<CachedBluetoothDevice> getCachedDevicesCopy() {
return mCachedDevices;
}
}