Merge "[Audiosharing] Add audio sharing loggings (P4)" into main

This commit is contained in:
Yiyi Shen
2024-06-18 07:47:24 +00:00
committed by Android (Google) Code Review
7 changed files with 287 additions and 43 deletions

View File

@@ -30,9 +30,11 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastAssistant;
@@ -57,6 +59,7 @@ import androidx.test.core.app.ApplicationProvider;
import com.android.settings.SettingsActivity;
import com.android.settings.bluetooth.Utils;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
import com.android.settings.testutils.shadow.ShadowFragment;
@@ -136,6 +139,7 @@ public class AudioSharingDevicePreferenceControllerTest {
private LifecycleOwner mLifecycleOwner;
private PreferenceCategory mPreferenceGroup;
private Preference mAudioSharingPreference;
private FakeFeatureFactory mFeatureFactory;
@Before
public void setUp() {
@@ -148,6 +152,7 @@ public class AudioSharingDevicePreferenceControllerTest {
BluetoothStatusCodes.FEATURE_SUPPORTED);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
mFeatureFactory = FakeFeatureFactory.setupForTest();
ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBtManager;
mLocalBtManager = Utils.getLocalBtManager(mContext);
when(mLocalBtManager.getEventManager()).thenReturn(mEventManager);
@@ -175,6 +180,7 @@ public class AudioSharingDevicePreferenceControllerTest {
.thenReturn(mAudioSharingPreference);
when(mScreen.findPreference(KEY)).thenReturn(mPreferenceGroup);
mController = new AudioSharingDevicePreferenceController(mContext);
mController.init(mFragment);
mController.setBluetoothDeviceUpdater(mBluetoothDeviceUpdater);
mController.setDialogHandler(mDialogHandler);
doReturn(mActivity).when(mFragment).getActivity();
@@ -526,6 +532,25 @@ public class AudioSharingDevicePreferenceControllerTest {
verify(mBluetoothDeviceUpdater, times(2)).forceUpdate();
}
@Test
public void testBluetoothLeBroadcastAssistantCallbacks_logAction() {
mController.mBroadcastAssistantCallback.onSourceAddFailed(
mDevice, mSource, /* reason= */ 1);
verify(mFeatureFactory.metricsFeatureProvider)
.action(
mContext,
SettingsEnums.ACTION_AUDIO_SHARING_JOIN_FAILED,
SettingsEnums.SETTINGS_CONNECTED_DEVICE_CATEGORY);
mController.mBroadcastAssistantCallback.onSourceRemoveFailed(
mDevice, /* sourceId= */ 1, /* reason= */ 1);
verify(mFeatureFactory.metricsFeatureProvider)
.action(
mContext,
SettingsEnums.ACTION_AUDIO_SHARING_LEAVE_FAILED,
SettingsEnums.SETTINGS_CONNECTED_DEVICE_CATEGORY);
}
@Test
public void testBluetoothLeBroadcastAssistantCallbacks_doNothing() {
mController.mBroadcastAssistantCallback.onSearchStarted(/* reason= */ 1);
@@ -534,10 +559,6 @@ public class AudioSharingDevicePreferenceControllerTest {
mController.mBroadcastAssistantCallback.onSearchStopFailed(/* reason= */ 1);
mController.mBroadcastAssistantCallback.onSourceAdded(
mDevice, /* sourceId= */ 1, /* reason= */ 1);
mController.mBroadcastAssistantCallback.onSourceAddFailed(
mDevice, mSource, /* reason= */ 1);
mController.mBroadcastAssistantCallback.onSourceRemoveFailed(
mDevice, /* sourceId= */ 1, /* reason= */ 1);
mController.mBroadcastAssistantCallback.onSourceModified(
mDevice, /* sourceId= */ 1, /* reason= */ 1);
mController.mBroadcastAssistantCallback.onSourceModifyFailed(
@@ -546,7 +567,8 @@ public class AudioSharingDevicePreferenceControllerTest {
mController.mBroadcastAssistantCallback.onSourceLost(/* broadcastId= */ 1);
shadowOf(Looper.getMainLooper()).idle();
// Above callbacks won't update group preference
// Above callbacks won't update group preference and log actions
verify(mBluetoothDeviceUpdater, never()).forceUpdate();
verifyNoMoreInteractions(mFeatureFactory.metricsFeatureProvider);
}
}

View File

@@ -25,6 +25,7 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
@@ -288,6 +289,8 @@ public class AudioSharingDialogHandlerTest {
assertThat(listener).isNotNull();
listener.onShareClick();
verify(mBroadcast).startPrivateBroadcast();
listener.onCancelClick();
verify(mCachedDevice1).setActive();
}
@Test
@@ -330,6 +333,8 @@ public class AudioSharingDialogHandlerTest {
1));
AudioSharingJoinDialogFragment.DialogEventListener listener = fragment.getListener();
assertThat(listener).isNotNull();
listener.onCancelClick();
verify(mAssistant, never()).addSource(mDevice1, mMetadata, /* isGroupOp= */ false);
listener.onShareClick();
verify(mAssistant).addSource(mDevice1, mMetadata, /* isGroupOp= */ false);
}
@@ -397,7 +402,7 @@ public class AudioSharingDialogHandlerTest {
@Test
public void handleNonLeaDeviceConnected_sharing_showStopDialog() {
setUpBroadcast(true);
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice2);
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1);
when(mAssistant.getDevicesMatchingConnectionStates(
new int[] {BluetoothProfile.STATE_CONNECTED}))
.thenReturn(deviceList);
@@ -487,6 +492,8 @@ public class AudioSharingDialogHandlerTest {
assertThat(listener).isNotNull();
listener.onShareClick();
verify(mBroadcast).startPrivateBroadcast();
listener.onCancelClick();
verify(mCachedDevice1, never()).setActive();
}
@Test
@@ -529,6 +536,8 @@ public class AudioSharingDialogHandlerTest {
1));
AudioSharingJoinDialogFragment.DialogEventListener listener = fragment.getListener();
assertThat(listener).isNotNull();
listener.onCancelClick();
verify(mAssistant, never()).addSource(mDevice1, mMetadata, /* isGroupOp= */ false);
listener.onShareClick();
verify(mAssistant).addSource(mDevice1, mMetadata, /* isGroupOp= */ false);
}
@@ -604,11 +613,38 @@ public class AudioSharingDialogHandlerTest {
SettingsEnums.DIALOG_START_AUDIO_SHARING);
}
@Test
public void closeOpeningDialogsForLeaDevice_closeDisconnectDialog() {
// Show disconnect dialog
setUpBroadcast(true);
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1, mDevice3, mDevice4);
when(mAssistant.getDevicesMatchingConnectionStates(
new int[] {BluetoothProfile.STATE_CONNECTED}))
.thenReturn(deviceList);
when(mAssistant.getAllSources(mDevice1)).thenReturn(ImmutableList.of());
when(mAssistant.getAllSources(mDevice3)).thenReturn(ImmutableList.of(mState));
when(mAssistant.getAllSources(mDevice4)).thenReturn(ImmutableList.of(mState));
mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */ false);
shadowOf(Looper.getMainLooper()).idle();
assertThat(mParentFragment.getChildFragmentManager().getFragments())
.comparingElementsUsing(TAG_EQUALS)
.containsExactly(AudioSharingDisconnectDialogFragment.tag());
// Close opening dialogs
mHandler.closeOpeningDialogsForLeaDevice(mCachedDevice1);
shadowOf(Looper.getMainLooper()).idle();
assertThat(mParentFragment.getChildFragmentManager().getFragments()).isEmpty();
verify(mFeatureFactory.metricsFeatureProvider)
.action(
mContext,
SettingsEnums.ACTION_AUDIO_SHARING_DIALOG_AUTO_DISMISS,
SettingsEnums.DIALOG_AUDIO_SHARING_SWITCH_DEVICE);
}
@Test
public void closeOpeningDialogsForNonLeaDevice_closeStopDialog() {
// Show stop dialog
setUpBroadcast(true);
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice2);
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1);
when(mAssistant.getDevicesMatchingConnectionStates(
new int[] {BluetoothProfile.STATE_CONNECTED}))
.thenReturn(deviceList);
@@ -677,6 +713,34 @@ public class AudioSharingDialogHandlerTest {
verify(mBroadcast).unregisterServiceCallBack(any(BluetoothLeBroadcast.Callback.class));
}
@Test
public void onBroadcastStartFailed_logAction() {
setUpBroadcast(false);
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1, mDevice3);
when(mAssistant.getDevicesMatchingConnectionStates(
new int[] {BluetoothProfile.STATE_CONNECTED}))
.thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */ false);
shadowOf(Looper.getMainLooper()).idle();
List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
assertThat(childFragments)
.comparingElementsUsing(TAG_EQUALS)
.containsExactly(AudioSharingJoinDialogFragment.tag());
AudioSharingJoinDialogFragment fragment =
(AudioSharingJoinDialogFragment) Iterables.getOnlyElement(childFragments);
AudioSharingJoinDialogFragment.DialogEventListener listener = fragment.getListener();
assertThat(listener).isNotNull();
listener.onShareClick();
mHandler.mBroadcastCallback.onBroadcastStartFailed(/* reason= */ 1);
verify(mFeatureFactory.metricsFeatureProvider)
.action(
mContext,
SettingsEnums.ACTION_AUDIO_SHARING_START_FAILED,
SettingsEnums.SETTINGS_CONNECTED_DEVICE_CATEGORY);
}
@Test
public void onPlaybackStarted_addSource() {
setUpBroadcast(false);
@@ -705,16 +769,42 @@ public class AudioSharingDialogHandlerTest {
verify(mAssistant).addSource(mDevice3, mMetadata, /* isGroupOp= */ false);
}
@Test
public void onBroadcastStopFailed_logAction() {
setUpBroadcast(true);
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1);
when(mAssistant.getDevicesMatchingConnectionStates(
new int[] {BluetoothProfile.STATE_CONNECTED}))
.thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState));
mHandler.handleDeviceConnected(mCachedDevice2, /* userTriggered= */ false);
shadowOf(Looper.getMainLooper()).idle();
List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
assertThat(childFragments)
.comparingElementsUsing(TAG_EQUALS)
.containsExactly(AudioSharingStopDialogFragment.tag());
AudioSharingStopDialogFragment fragment =
(AudioSharingStopDialogFragment) Iterables.getOnlyElement(childFragments);
AudioSharingStopDialogFragment.DialogEventListener listener = fragment.getListener();
assertThat(listener).isNotNull();
listener.onStopSharingClick();
mHandler.mBroadcastCallback.onBroadcastStopFailed(/* reason= */ 1);
verify(mFeatureFactory.metricsFeatureProvider)
.action(
mContext,
SettingsEnums.ACTION_AUDIO_SHARING_STOP_FAILED,
SettingsEnums.SETTINGS_CONNECTED_DEVICE_CATEGORY);
}
@Test
public void testBluetoothLeBroadcastCallbacks_doNothing() {
mHandler.mBroadcastCallback.onBroadcastStarted(/* reason= */ 1, /* broadcastId= */ 1);
mHandler.mBroadcastCallback.onBroadcastStopped(/* reason= */ 1, /* broadcastId= */ 1);
mHandler.mBroadcastCallback.onBroadcastMetadataChanged(/* reason= */ 1, mMetadata);
mHandler.mBroadcastCallback.onBroadcastUpdated(/* reason= */ 1, /* broadcastId= */ 1);
mHandler.mBroadcastCallback.onPlaybackStarted(/* reason= */ 1, /* broadcastId= */ 1);
mHandler.mBroadcastCallback.onPlaybackStopped(/* reason= */ 1, /* broadcastId= */ 1);
mHandler.mBroadcastCallback.onBroadcastStartFailed(/* reason= */ 1);
mHandler.mBroadcastCallback.onBroadcastStopFailed(/* reason= */ 1);
mHandler.mBroadcastCallback.onBroadcastUpdateFailed(/* reason= */ 1, /* broadcastId= */ 1);
verify(mAssistant, never())
@@ -723,6 +813,7 @@ public class AudioSharingDialogHandlerTest {
any(BluetoothLeBroadcastMetadata.class),
anyBoolean());
verify(mAssistant, never()).removeSource(any(BluetoothDevice.class), anyInt());
verifyNoMoreInteractions(mFeatureFactory.metricsFeatureProvider);
}
private void setUpBroadcast(boolean isBroadcasting) {

View File

@@ -30,6 +30,7 @@ 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.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
@@ -39,6 +40,7 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcast;
import android.bluetooth.BluetoothLeBroadcastAssistant;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothStatusCodes;
import android.content.BroadcastReceiver;
@@ -430,6 +432,38 @@ public class AudioSharingSwitchBarControllerTest {
verify(mBroadcast).stopBroadcast(1);
}
@Test
public void onPlaybackStarted_notInit_noDialog() {
FeatureFlagUtils.setEnabled(
mContext, FeatureFlagUtils.SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST, true);
when(mBtnView.isEnabled()).thenReturn(true);
when(mAssistant.getDevicesMatchingConnectionStates(
new int[] {BluetoothProfile.STATE_CONNECTED}))
.thenReturn(ImmutableList.of(mDevice2, mDevice1));
doNothing().when(mBroadcast).startPrivateBroadcast();
mController =
new AudioSharingSwitchBarController(
mContext,
mSwitchBar,
new AudioSharingSwitchBarController.OnAudioSharingStateChangedListener() {
@Override
public void onAudioSharingStateChanged() {}
@Override
public void onAudioSharingProfilesConnected() {}
});
mController.onCheckedChanged(mBtnView, /* isChecked= */ true);
verify(mBroadcast).startPrivateBroadcast();
mController.mBroadcastCallback.onPlaybackStarted(0, 0);
shadowOf(Looper.getMainLooper()).idle();
verify(mFeatureFactory.metricsFeatureProvider)
.action(any(Context.class), eq(SettingsEnums.ACTION_AUTO_JOIN_AUDIO_SHARING));
List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
assertThat(childFragments).isEmpty();
}
@Test
public void onPlaybackStarted_showJoinAudioSharingDialog() {
FeatureFlagUtils.setEnabled(
@@ -485,6 +519,11 @@ public class AudioSharingSwitchBarControllerTest {
shadowOf(Looper.getMainLooper()).idle();
assertThat(mSwitchBar.isChecked()).isFalse();
assertThat(mOnAudioSharingStateChanged).isFalse();
verify(mFeatureFactory.metricsFeatureProvider)
.action(
mContext,
SettingsEnums.ACTION_AUDIO_SHARING_START_FAILED,
SettingsEnums.AUDIO_SHARING_SETTINGS);
when(mBroadcast.isEnabled(any())).thenReturn(true);
mController.mBroadcastCallback.onBroadcastStarted(/* reason= */ 1, /* broadcastId= */ 1);
@@ -497,6 +536,11 @@ public class AudioSharingSwitchBarControllerTest {
shadowOf(Looper.getMainLooper()).idle();
assertThat(mSwitchBar.isChecked()).isTrue();
assertThat(mOnAudioSharingStateChanged).isFalse();
verify(mFeatureFactory.metricsFeatureProvider)
.action(
mContext,
SettingsEnums.ACTION_AUDIO_SHARING_STOP_FAILED,
SettingsEnums.AUDIO_SHARING_SETTINGS);
when(mBroadcast.isEnabled(any())).thenReturn(false);
mController.mBroadcastCallback.onBroadcastStopped(/* reason= */ 1, /* broadcastId= */ 1);
@@ -517,4 +561,43 @@ public class AudioSharingSwitchBarControllerTest {
verify(mSwitchBar, never()).setChecked(anyBoolean());
assertThat(mOnAudioSharingStateChanged).isFalse();
}
@Test
public void testBluetoothLeBroadcastAssistantCallbacks_logAction() {
BluetoothLeBroadcastMetadata metadata = mock(BluetoothLeBroadcastMetadata.class);
mController.mBroadcastAssistantCallback.onSourceAddFailed(
mDevice1, metadata, /* reason= */ 1);
verify(mFeatureFactory.metricsFeatureProvider)
.action(
mContext,
SettingsEnums.ACTION_AUDIO_SHARING_JOIN_FAILED,
SettingsEnums.AUDIO_SHARING_SETTINGS);
}
@Test
public void testBluetoothLeBroadcastAssistantCallbacks_doNothing() {
BluetoothLeBroadcastReceiveState state = mock(BluetoothLeBroadcastReceiveState.class);
BluetoothLeBroadcastMetadata metadata = mock(BluetoothLeBroadcastMetadata.class);
// Do nothing
mController.mBroadcastAssistantCallback.onReceiveStateChanged(
mDevice1, /* sourceId= */ 1, state);
mController.mBroadcastAssistantCallback.onSearchStarted(/* reason= */ 1);
mController.mBroadcastAssistantCallback.onSearchStartFailed(/* reason= */ 1);
mController.mBroadcastAssistantCallback.onSearchStopped(/* reason= */ 1);
mController.mBroadcastAssistantCallback.onSearchStopFailed(/* reason= */ 1);
mController.mBroadcastAssistantCallback.onSourceAdded(
mDevice1, /* sourceId= */ 1, /* reason= */ 1);
mController.mBroadcastAssistantCallback.onSourceRemoved(
mDevice1, /* sourceId= */ 1, /* reason= */ 1);
mController.mBroadcastAssistantCallback.onSourceRemoveFailed(
mDevice1, /* sourceId= */ 1, /* reason= */ 1);
mController.mBroadcastAssistantCallback.onSourceModified(
mDevice1, /* sourceId= */ 1, /* reason= */ 1);
mController.mBroadcastAssistantCallback.onSourceModifyFailed(
mDevice1, /* sourceId= */ 1, /* reason= */ 1);
mController.mBroadcastAssistantCallback.onSourceFound(metadata);
mController.mBroadcastAssistantCallback.onSourceLost(/* broadcastId= */ 1);
verifyNoMoreInteractions(mFeatureFactory.metricsFeatureProvider);
}
}