From 5b017f7b055d2d501e7aec537e5ad01d28c4fb0b Mon Sep 17 00:00:00 2001 From: Jack He Date: Wed, 30 Aug 2017 19:18:40 -0700 Subject: [PATCH] Bluetooth: add metrics for pairing with devices without names * Also caches context in onClick() method Bug: 34685932 Test: make, unit test Change-Id: I99beab2c85b8e48c4bc41f69146759d4b7c62428 --- .../bluetooth/BluetoothDevicePreference.java | 13 +++++++++---- .../BluetoothDevicePreferenceTest.java | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/com/android/settings/bluetooth/BluetoothDevicePreference.java b/src/com/android/settings/bluetooth/BluetoothDevicePreference.java index 084b50ebb96..94ba478c5b3 100644 --- a/src/com/android/settings/bluetooth/BluetoothDevicePreference.java +++ b/src/com/android/settings/bluetooth/BluetoothDevicePreference.java @@ -188,22 +188,27 @@ public final class BluetoothDevicePreference extends GearPreference implements } void onClicked() { + Context context = getContext(); int bondState = mCachedDevice.getBondState(); final MetricsFeatureProvider metricsFeatureProvider = - FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider(); + FeatureFactory.getFactory(context).getMetricsFeatureProvider(); if (mCachedDevice.isConnected()) { - metricsFeatureProvider.action(getContext(), + metricsFeatureProvider.action(context, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_DISCONNECT); askDisconnect(); } else if (bondState == BluetoothDevice.BOND_BONDED) { - metricsFeatureProvider.action(getContext(), + metricsFeatureProvider.action(context, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_CONNECT); mCachedDevice.connect(true); } else if (bondState == BluetoothDevice.BOND_NONE) { - metricsFeatureProvider.action(getContext(), + metricsFeatureProvider.action(context, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR); + if (!mCachedDevice.hasHumanReadableName()) { + metricsFeatureProvider.action(context, + MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES); + } pair(); } } diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePreferenceTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePreferenceTest.java index a1db5de77e8..ac0720a246f 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePreferenceTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePreferenceTest.java @@ -42,6 +42,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -98,11 +99,29 @@ public class BluetoothDevicePreferenceTest { when(mCachedBluetoothDevice.isConnected()).thenReturn(false); when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE); when(mCachedBluetoothDevice.startPairing()).thenReturn(true); + when(mCachedBluetoothDevice.hasHumanReadableName()).thenReturn(true); mPreference.onClicked(); verify(mMetricsFeatureProvider).action( mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR); + verify(mMetricsFeatureProvider, never()).action(mContext, + MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES); + } + + @Test + public void onClicked_deviceNotBonded_shouldLogBluetoothPairEventAndPairWithoutNameEvent() { + when(mCachedBluetoothDevice.isConnected()).thenReturn(false); + when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE); + when(mCachedBluetoothDevice.startPairing()).thenReturn(true); + when(mCachedBluetoothDevice.hasHumanReadableName()).thenReturn(false); + + mPreference.onClicked(); + + verify(mMetricsFeatureProvider).action( + mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR); + verify(mMetricsFeatureProvider).action(mContext, + MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES); } @Test