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.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();

View File

@@ -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<CachedBluetoothDevice> cachedDevices;
private Collection<CachedBluetoothDevice> 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<CachedBluetoothDevice>(new ArrayList<CachedBluetoothDevice>());
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();

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;
}
}