OSDN Git Service

Remove test-purpose constructor from ConnectedBluetoothDeviceUpdater
authortimhypeng <timhypeng@google.com>
Mon, 13 Aug 2018 09:38:36 +0000 (17:38 +0800)
committertim peng <timhypeng@google.com>
Thu, 16 Aug 2018 01:24:40 +0000 (01:24 +0000)
- replace mock object with ShadowCachedBluetoothDeviceManager to test CachedBluetoothDevice
- rename cachedDevices to mCachedDevices

Bug: 111848213
Test: make -j50 RunSettingsRoboTests
Change-Id: Ib024a3e9c3af745b1ab0be36361165a547cfa756

src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java
tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java
tests/robotests/src/com/android/settings/testutils/shadow/ShadowCachedBluetoothDeviceManager.java [new file with mode: 0644]

index 259a403..b66b286 100644 (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();
index ece71d7..6faea9a 100644 (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();
 
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 (file)
index 0000000..ee04c4f
--- /dev/null
@@ -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;
+    }
+}
\ No newline at end of file