/*
 * Copyright (C) 2023 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.connecteddevice.audiosharing;

import static com.android.settings.connecteddevice.audiosharing.AudioSharingUtils.MetricKey.METRIC_KEY_SOURCE_PAGE_ID;
import static com.android.settings.connecteddevice.audiosharing.AudioSharingUtils.MetricKey.METRIC_KEY_USER_TRIGGERED;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
import static com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast.EXTRA_START_LE_AUDIO_SHARING;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
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;
import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;

import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothAdapter;
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;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Looper;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.util.FeatureFlagUtils;
import android.util.Pair;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.LifecycleOwner;
import androidx.test.core.app.ApplicationProvider;

import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.bluetooth.Utils;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
import com.android.settings.testutils.shadow.ShadowThreadUtils;
import com.android.settings.widget.SettingsMainSwitchBar;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.BluetoothEventManager;
import com.android.settingslib.bluetooth.BluetoothLeBroadcastMetadataExt;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LeAudioProfile;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import com.android.settingslib.bluetooth.VolumeControlProfile;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.flags.Flags;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.truth.Correspondence;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.androidx.fragment.FragmentController;

import java.util.List;
import java.util.concurrent.Executor;

@RunWith(RobolectricTestRunner.class)
@Config(
        shadows = {
            ShadowBluetoothAdapter.class,
            ShadowBluetoothUtils.class,
            ShadowThreadUtils.class,
            ShadowAlertDialogCompat.class
        })
public class AudioSharingSwitchBarControllerTest {
    private static final String EXTRA_SOURCE_METRICS = ":settings:source_metrics";
    private static final String TEST_DEVICE_NAME1 = "test1";
    private static final String TEST_DEVICE_NAME2 = "test2";
    private static final String TEST_DEVICE_ANONYMIZED_ADDR1 = "XX:XX:01";
    private static final String TEST_DEVICE_ANONYMIZED_ADDR2 = "XX:XX:02";
    private static final int TEST_DEVICE_GROUP_ID1 = 1;
    private static final int TEST_DEVICE_GROUP_ID2 = 2;
    private static final int TEST_SOURCE_METRICS = SettingsEnums.SETTINGS_CONNECTED_DEVICE_CATEGORY;
    private static final Correspondence<Fragment, String> CLAZZNAME_EQUALS =
            Correspondence.from(
                    (Fragment fragment, String clazzName) ->
                            fragment instanceof DialogFragment
                                    && ((DialogFragment) fragment).getClass().getName() != null
                                    && ((DialogFragment) fragment)
                                            .getClass()
                                            .getName()
                                            .equals(clazzName),
                    "is equal to");

    @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();
    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    @Spy Context mContext = ApplicationProvider.getApplicationContext();
    @Mock private LocalBluetoothManager mLocalBtManager;
    @Mock private CachedBluetoothDeviceManager mDeviceManager;
    @Mock private BluetoothEventManager mEventManager;
    @Mock private LocalBluetoothProfileManager mBtProfileManager;
    @Mock private LeAudioProfile mLeAudio;
    @Mock private LocalBluetoothLeBroadcast mBroadcast;
    @Mock private LocalBluetoothLeBroadcastAssistant mAssistant;
    @Mock private VolumeControlProfile mVolumeControl;
    @Mock private BluetoothLeBroadcastMetadata mMetadata;
    @Mock private BluetoothLeBroadcastReceiveState mState;
    @Mock private CompoundButton mBtnView;
    @Mock private CachedBluetoothDevice mCachedDevice1;
    @Mock private CachedBluetoothDevice mCachedDevice2;
    @Mock private BluetoothDevice mDevice1;
    @Mock private BluetoothDevice mDevice2;
    private SettingsMainSwitchBar mSwitchBar;
    private AudioSharingSwitchBarController mController;
    private FakeFeatureFactory mFeatureFactory;
    private Lifecycle mLifecycle;
    private LifecycleOwner mLifecycleOwner;
    private boolean mOnAudioSharingStateChanged;
    private boolean mOnAudioSharingServiceConnected;
    private ShadowBluetoothAdapter mShadowBluetoothAdapter;
    private Fragment mParentFragment;

    @Before
    public void setUp() {
        mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
        mShadowBluetoothAdapter.setEnabled(true);
        mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
                BluetoothStatusCodes.FEATURE_SUPPORTED);
        mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
                BluetoothStatusCodes.FEATURE_SUPPORTED);
        mLifecycleOwner = () -> mLifecycle;
        mLifecycle = new Lifecycle(mLifecycleOwner);
        ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBtManager;
        LocalBluetoothManager localBluetoothManager = Utils.getLocalBtManager(mContext);
        mFeatureFactory = FakeFeatureFactory.setupForTest();
        when(localBluetoothManager.getProfileManager()).thenReturn(mBtProfileManager);
        when(localBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager);
        when(localBluetoothManager.getEventManager()).thenReturn(mEventManager);
        when(mDevice1.getAnonymizedAddress()).thenReturn(TEST_DEVICE_ANONYMIZED_ADDR1);
        when(mDeviceManager.findDevice(mDevice1)).thenReturn(mCachedDevice1);
        when(mCachedDevice1.getDevice()).thenReturn(mDevice1);
        when(mCachedDevice1.getGroupId()).thenReturn(TEST_DEVICE_GROUP_ID1);
        when(mCachedDevice1.getName()).thenReturn(TEST_DEVICE_NAME1);
        when(mCachedDevice1.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(false);
        when(mDevice2.getAnonymizedAddress()).thenReturn(TEST_DEVICE_ANONYMIZED_ADDR2);
        when(mDeviceManager.findDevice(mDevice2)).thenReturn(mCachedDevice2);
        when(mCachedDevice2.getDevice()).thenReturn(mDevice2);
        when(mCachedDevice2.getGroupId()).thenReturn(TEST_DEVICE_GROUP_ID2);
        when(mCachedDevice2.getName()).thenReturn(TEST_DEVICE_NAME2);
        when(mCachedDevice2.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(true);
        when(mBtProfileManager.getLeAudioProfile()).thenReturn(mLeAudio);
        when(mBtProfileManager.getLeAudioBroadcastProfile()).thenReturn(mBroadcast);
        when(mBtProfileManager.getLeAudioBroadcastAssistantProfile()).thenReturn(mAssistant);
        when(mBtProfileManager.getVolumeControlProfile()).thenReturn(mVolumeControl);
        when(mLeAudio.isProfileReady()).thenReturn(true);
        when(mVolumeControl.isProfileReady()).thenReturn(true);
        when(mBroadcast.isProfileReady()).thenReturn(true);
        doNothing()
                .when(mBroadcast)
                .registerServiceCallBack(
                        any(Executor.class), any(BluetoothLeBroadcast.Callback.class));
        doNothing()
                .when(mBroadcast)
                .unregisterServiceCallBack(any(BluetoothLeBroadcast.Callback.class));
        when(mAssistant.isProfileReady()).thenReturn(true);
        doNothing()
                .when(mAssistant)
                .registerServiceCallBack(
                        any(Executor.class), any(BluetoothLeBroadcastAssistant.Callback.class));
        doNothing()
                .when(mAssistant)
                .unregisterServiceCallBack(any(BluetoothLeBroadcastAssistant.Callback.class));
        mSwitchBar = spy(new SettingsMainSwitchBar(mContext));
        mSwitchBar.setDisabledByAdmin(mock(RestrictedLockUtils.EnforcedAdmin.class));
        mOnAudioSharingStateChanged = false;
        mOnAudioSharingServiceConnected = false;
        AudioSharingSwitchBarController.OnAudioSharingStateChangedListener listener =
                new AudioSharingSwitchBarController.OnAudioSharingStateChangedListener() {
                    @Override
                    public void onAudioSharingStateChanged() {
                        mOnAudioSharingStateChanged = true;
                    }

                    @Override
                    public void onAudioSharingProfilesConnected() {
                        mOnAudioSharingServiceConnected = true;
                    }
                };
        mController = new AudioSharingSwitchBarController(mContext, mSwitchBar, listener);
        mParentFragment = new Fragment();
        FragmentController.setupFragment(
                mParentFragment,
                FragmentActivity.class,
                0 /* containerViewId */,
                null /* bundle */);
        mController.init(mParentFragment, /* needHandleIntent */ true);
    }

    @After
    public void tearDown() {
        ShadowAlertDialogCompat.reset();
        ShadowBluetoothUtils.reset();
        ShadowThreadUtils.reset();
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void bluetoothOn_profileReady_switchEnabled() {
        mSwitchBar.setEnabled(false);
        mContext.registerReceiver(
                mController.mReceiver,
                mController.mIntentFilter,
                Context.RECEIVER_EXPORTED_UNAUDITED);
        mShadowBluetoothAdapter.setEnabled(true);
        when(mBroadcast.isEnabled(null)).thenReturn(false);
        Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
        intent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_ON);
        mContext.sendBroadcast(intent);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mSwitchBar).setEnabled(true);
        assertThat(mSwitchBar.isChecked()).isFalse();
        assertThat(mOnAudioSharingStateChanged).isTrue();
        assertThat(mOnAudioSharingServiceConnected).isFalse();
        verify(mBtProfileManager, never()).addServiceListener(any());
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void bluetoothOn_profileNotReady_switchDisabled_registerProfileListener() {
        mSwitchBar.setEnabled(false);
        mContext.registerReceiver(
                mController.mReceiver,
                mController.mIntentFilter,
                Context.RECEIVER_EXPORTED_UNAUDITED);
        mShadowBluetoothAdapter.setEnabled(true);
        when(mBroadcast.isEnabled(null)).thenReturn(false);
        when(mBroadcast.isProfileReady()).thenReturn(false);
        Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
        intent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_ON);
        mContext.sendBroadcast(intent);
        shadowOf(Looper.getMainLooper()).idle();

        assertThat(mSwitchBar.isEnabled()).isFalse();
        assertThat(mSwitchBar.isChecked()).isFalse();
        assertThat(mOnAudioSharingStateChanged).isFalse();
        assertThat(mOnAudioSharingServiceConnected).isFalse();
        verify(mBtProfileManager).addServiceListener(mController);
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void bluetoothOff_switchDisabled() {
        assertThat(mSwitchBar.isEnabled()).isTrue();
        mContext.registerReceiver(
                mController.mReceiver,
                mController.mIntentFilter,
                Context.RECEIVER_EXPORTED_UNAUDITED);
        mShadowBluetoothAdapter.setEnabled(false);
        when(mBroadcast.isEnabled(null)).thenReturn(false);
        Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
        intent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
        mContext.sendBroadcast(intent);
        shadowOf(Looper.getMainLooper()).idle();

        assertThat(mSwitchBar.isEnabled()).isFalse();
        assertThat(mSwitchBar.isChecked()).isFalse();
        assertThat(mOnAudioSharingStateChanged).isTrue();
        assertThat(mOnAudioSharingServiceConnected).isFalse();
        verify(mBtProfileManager, never()).addServiceListener(any());
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void onServiceConnected_switchEnabled() {
        when(mBroadcast.isEnabled(null)).thenReturn(true);
        mController.onServiceConnected();
        shadowOf(Looper.getMainLooper()).idle();

        assertThat(mSwitchBar.isEnabled()).isTrue();
        assertThat(mSwitchBar.isChecked()).isTrue();
        assertThat(mOnAudioSharingStateChanged).isTrue();
        assertThat(mOnAudioSharingServiceConnected).isTrue();
        verify(mBtProfileManager).removeServiceListener(mController);
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void getAvailabilityStatus_flagOn() {
        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
    }

    @Test
    @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void getAvailabilityStatus_flagOff() {
        assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
    }

    @Test
    @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void onStart_flagOff_doNothing() {
        mController.onStart(mLifecycleOwner);
        verify(mContext, never())
                .registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class), anyInt());
        verify(mBroadcast, never())
                .registerServiceCallBack(
                        any(Executor.class), any(BluetoothLeBroadcast.Callback.class));
        verify(mAssistant, never())
                .registerServiceCallBack(
                        any(Executor.class), any(BluetoothLeBroadcastAssistant.Callback.class));
        verify(mBtProfileManager, never()).addServiceListener(mController);
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void onStart_flagOnProfileNotReady_registerProfileCallback() {
        when(mBroadcast.isEnabled(null)).thenReturn(false);
        when(mBroadcast.isProfileReady()).thenReturn(false);
        mController.onStart(mLifecycleOwner);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mContext)
                .registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class), anyInt());
        verify(mBroadcast)
                .registerServiceCallBack(
                        any(Executor.class), any(BluetoothLeBroadcast.Callback.class));
        verify(mAssistant)
                .registerServiceCallBack(
                        any(Executor.class), any(BluetoothLeBroadcastAssistant.Callback.class));
        verify(mEventManager).registerCallback(any(BluetoothCallback.class));
        verify(mBtProfileManager).addServiceListener(mController);
        assertThat(mSwitchBar.isChecked()).isFalse();
        assertThat(mSwitchBar.isEnabled()).isFalse();
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void onStart_flagOn_registerCallback() {
        when(mBroadcast.isEnabled(null)).thenReturn(true);
        mController.onStart(mLifecycleOwner);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mContext)
                .registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class), anyInt());
        verify(mBroadcast)
                .registerServiceCallBack(
                        any(Executor.class), any(BluetoothLeBroadcast.Callback.class));
        verify(mAssistant)
                .registerServiceCallBack(
                        any(Executor.class), any(BluetoothLeBroadcastAssistant.Callback.class));
        verify(mEventManager).registerCallback(any(BluetoothCallback.class));
        verify(mBtProfileManager, never()).addServiceListener(mController);
        assertThat(mSwitchBar.isChecked()).isTrue();
        assertThat(mSwitchBar.isEnabled()).isTrue();
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void onStart_flagOn_updateSwitch() {
        when(mBroadcast.isEnabled(null)).thenReturn(false);
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of());
        mController.onStart(mLifecycleOwner);
        shadowOf(Looper.getMainLooper()).idle();

        assertThat(mSwitchBar.isChecked()).isFalse();
        assertThat(mSwitchBar.isEnabled()).isTrue();
    }

    @Test
    @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void onStop_flagOff_doNothing() {
        mController.onStop(mLifecycleOwner);
        verify(mContext, never()).unregisterReceiver(any(BroadcastReceiver.class));
        verify(mBroadcast, never())
                .unregisterServiceCallBack(any(BluetoothLeBroadcast.Callback.class));
        verify(mAssistant, never())
                .unregisterServiceCallBack(any(BluetoothLeBroadcastAssistant.Callback.class));
        verify(mEventManager, never()).unregisterCallback(any(BluetoothCallback.class));
        verify(mBtProfileManager, never()).removeServiceListener(mController);
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void onStop_flagOn_notRegistered_doNothing() {
        mController.setCallbacksRegistered(false);
        doNothing().when(mContext).unregisterReceiver(any(BroadcastReceiver.class));
        mController.onStop(mLifecycleOwner);

        verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
        verify(mBtProfileManager).removeServiceListener(mController);
        verify(mBroadcast, never())
                .unregisterServiceCallBack(any(BluetoothLeBroadcast.Callback.class));
        verify(mAssistant, never())
                .unregisterServiceCallBack(any(BluetoothLeBroadcastAssistant.Callback.class));
        verify(mEventManager, never()).unregisterCallback(any(BluetoothCallback.class));
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void onStop_flagOn_registered_unregisterCallback() {
        mController.setCallbacksRegistered(true);
        mContext.registerReceiver(
                mController.mReceiver,
                mController.mIntentFilter,
                Context.RECEIVER_EXPORTED_UNAUDITED);
        mController.onStop(mLifecycleOwner);
        verify(mContext).unregisterReceiver(mController.mReceiver);
        verify(mBtProfileManager).removeServiceListener(mController);
        verify(mBroadcast).unregisterServiceCallBack(any(BluetoothLeBroadcast.Callback.class));
        verify(mAssistant)
                .unregisterServiceCallBack(any(BluetoothLeBroadcastAssistant.Callback.class));
        verify(mEventManager).unregisterCallback(any(BluetoothCallback.class));
    }

    @Test
    public void onCheckedChangedToChecked_sharing_doNothing() {
        when(mBtnView.isEnabled()).thenReturn(true);
        when(mBroadcast.isEnabled(null)).thenReturn(true);
        mController.onCheckedChanged(mBtnView, /* isChecked= */ true);
        verify(mBroadcast, never()).startPrivateBroadcast();
    }

    @Test
    public void onCheckedChangedToChecked_noConnectedLeaDevices_flagOn_showDialog() {
        FeatureFlagUtils.setEnabled(
                mContext, FeatureFlagUtils.SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST, true);
        when(mBtnView.isEnabled()).thenReturn(true);
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of());
        doNothing().when(mBroadcast).startPrivateBroadcast();
        mController.onCheckedChanged(mBtnView, /* isChecked= */ true);
        shadowOf(Looper.getMainLooper()).idle();

        assertThat(mSwitchBar.isChecked()).isFalse();
        verify(mBroadcast, never()).startPrivateBroadcast();
        List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
        assertThat(childFragments)
                .comparingElementsUsing(CLAZZNAME_EQUALS)
                .containsExactly(AudioSharingConfirmDialogFragment.class.getName());

        childFragments.forEach(fragment -> ((DialogFragment) fragment).dismiss());
    }

    @Test
    public void onCheckedChangedToChecked_noConnectedLeaDevices_flagOff_startAudioSharing() {
        FeatureFlagUtils.setEnabled(
                mContext, FeatureFlagUtils.SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST, false);
        when(mBtnView.isEnabled()).thenReturn(true);
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of());
        doNothing().when(mBroadcast).startPrivateBroadcast();
        mController.onCheckedChanged(mBtnView, /* isChecked= */ true);
        verify(mBroadcast).startPrivateBroadcast();
    }

    @Test
    public void onCheckedChangedToChecked_notSharing_withConnectedLeaDevices_startAudioSharing() {
        FeatureFlagUtils.setEnabled(
                mContext, FeatureFlagUtils.SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST, true);
        when(mBtnView.isEnabled()).thenReturn(true);
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1));
        doNothing().when(mBroadcast).startPrivateBroadcast();
        mController.onCheckedChanged(mBtnView, /* isChecked= */ true);
        verify(mBroadcast).startPrivateBroadcast();
    }

    @Test
    public void onCheckedChangedToUnChecked_notSharing_doNothing() {
        when(mBtnView.isEnabled()).thenReturn(true);
        when(mBroadcast.isEnabled(null)).thenReturn(false);
        when(mBroadcast.getLatestBroadcastId()).thenReturn(1);
        mController.onCheckedChanged(mBtnView, /* isChecked= */ false);
        verify(mBroadcast, never()).stopBroadcast(anyInt());
        verify(mFeatureFactory.metricsFeatureProvider, never())
                .action(mContext, SettingsEnums.ACTION_AUDIO_SHARING_MAIN_SWITCH_OFF);
    }

    @Test
    public void onCheckedChangedToUnChecked_sharing_stopAudioSharing() {
        when(mBtnView.isEnabled()).thenReturn(true);
        when(mBroadcast.isEnabled(null)).thenReturn(true);
        when(mBroadcast.getLatestBroadcastId()).thenReturn(1);
        doNothing().when(mBroadcast).stopBroadcast(anyInt());
        mController.onCheckedChanged(mBtnView, /* isChecked= */ false);
        verify(mBroadcast).stopBroadcast(1);
        verify(mFeatureFactory.metricsFeatureProvider)
                .action(mContext, SettingsEnums.ACTION_AUDIO_SHARING_MAIN_SWITCH_OFF);
    }

    @Test
    public void onBroadcastMetadataChanged_notInit_noDialog() {
        FeatureFlagUtils.setEnabled(
                mContext, FeatureFlagUtils.SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST, true);
        when(mBtnView.isEnabled()).thenReturn(true);
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice2, mDevice1));
        when(mAssistant.getAllSources(any(BluetoothDevice.class))).thenReturn(ImmutableList.of());
        when(mBroadcast.getLatestBluetoothLeBroadcastMetadata()).thenReturn(mMetadata);
        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);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mBroadcast).startPrivateBroadcast();
        List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
        // No progress dialog.
        assertThat(childFragments).isEmpty();

        mController.mBroadcastCallback.onBroadcastMetadataChanged(/* reason= */ 1, mMetadata);
        shadowOf(Looper.getMainLooper()).idle();

        ImmutableList<Pair<Integer, Object>> addEventData =
                ImmutableList.of(
                        Pair.create(
                                METRIC_KEY_SOURCE_PAGE_ID.getId(),
                                SettingsEnums.ACTION_AUTO_JOIN_AUDIO_SHARING),
                        Pair.create(METRIC_KEY_USER_TRIGGERED.getId(), false));
        verify(mFeatureFactory.metricsFeatureProvider)
                .action(
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        SettingsEnums.ACTION_AUDIO_SHARING_ADD_SOURCE,
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        addEventData.toString(),
                        /* changedPreferenceIntValue */ 0);

        childFragments = mParentFragment.getChildFragmentManager().getFragments();
        // No audio sharing dialog.
        assertThat(childFragments).isEmpty();
    }

    @Test
    public void onBroadcastMetadataChanged_notTriggeredHere_noDialog() {
        FeatureFlagUtils.setEnabled(
                mContext, FeatureFlagUtils.SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST, true);
        when(mBtnView.isEnabled()).thenReturn(true);
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice2, mDevice1));
        when(mAssistant.getAllSources(any(BluetoothDevice.class))).thenReturn(ImmutableList.of());
        when(mBroadcast.getLatestBluetoothLeBroadcastMetadata()).thenReturn(mMetadata);
        doNothing().when(mBroadcast).startPrivateBroadcast();

        mController.mBroadcastCallback.onBroadcastMetadataChanged(/* reason= */ 1, mMetadata);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mAssistant, never()).addSource(any(), any(), anyBoolean());
        verify(mFeatureFactory.metricsFeatureProvider, never())
                .action(
                        eq(SettingsEnums.AUDIO_SHARING_SETTINGS),
                        eq(SettingsEnums.ACTION_AUDIO_SHARING_ADD_SOURCE),
                        eq(SettingsEnums.AUDIO_SHARING_SETTINGS),
                        anyString(),
                        anyInt());

        List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
        // No audio sharing dialog.
        assertThat(childFragments)
                .comparingElementsUsing(CLAZZNAME_EQUALS)
                .doesNotContain(AudioSharingDialogFragment.class.getName());

        childFragments.forEach(fragment -> ((DialogFragment) fragment).dismiss());
    }

    @Test
    public void onBroadcastMetadataChanged_singleActiveDevice_showJoinAudioSharingDialog() {
        FeatureFlagUtils.setEnabled(
                mContext, FeatureFlagUtils.SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST, true);
        String metadataStr =
                "BLUETOOTH:UUID:184F;BN:VGVzdA==;AT:1;AD:00A1A1A1A1A1;BI:1E240;BC:VGVzdENvZGU=;"
                        + "MD:BgNwVGVzdA==;AS:1;PI:A0;NS:1;BS:3;NB:2;SM:BQNUZXN0BARlbmc=;;";
        // Use real metadata as the dialog will display a qr code image
        BluetoothLeBroadcastMetadata realMetadata =
                BluetoothLeBroadcastMetadataExt.INSTANCE.convertToBroadcastMetadata(metadataStr);
        when(mBtnView.isEnabled()).thenReturn(true);
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice2));
        when(mAssistant.getAllSources(any(BluetoothDevice.class))).thenReturn(ImmutableList.of());
        doNothing().when(mBroadcast).startPrivateBroadcast();
        mController.onCheckedChanged(mBtnView, /* isChecked= */ true);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mBroadcast).startPrivateBroadcast();
        ImmutableList<Pair<Integer, Object>> switchOnEventData =
                ImmutableList.of(
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_SOURCE_PAGE_ID.getId(),
                                SettingsEnums.AUDIO_SHARING_SETTINGS),
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_SOURCE_PACKAGE_NAME.getId(),
                                mContext.getPackageName()),
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_CANDIDATE_DEVICE_COUNT
                                        .getId(),
                                1));
        verify(mFeatureFactory.metricsFeatureProvider)
                .action(
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        SettingsEnums.ACTION_AUDIO_SHARING_MAIN_SWITCH_ON,
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        switchOnEventData.toString(),
                        /* changedPreferenceIntValue */ 0);
        List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
        assertThat(childFragments)
                .comparingElementsUsing(CLAZZNAME_EQUALS)
                .containsExactly(AudioSharingProgressDialogFragment.class.getName());

        when(mBroadcast.isEnabled(null)).thenReturn(true);
        when(mBroadcast.getLatestBluetoothLeBroadcastMetadata()).thenReturn(realMetadata);
        mController.mBroadcastCallback.onBroadcastMetadataChanged(/* reason= */ 1, realMetadata);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mAssistant).addSource(mDevice2, realMetadata, /* isGroupOp= */ false);
        ImmutableList<Pair<Integer, Object>> addEventData =
                ImmutableList.of(
                        Pair.create(
                                METRIC_KEY_SOURCE_PAGE_ID.getId(),
                                SettingsEnums.ACTION_AUTO_JOIN_AUDIO_SHARING),
                        Pair.create(METRIC_KEY_USER_TRIGGERED.getId(), false));
        verify(mFeatureFactory.metricsFeatureProvider)
                .action(
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        SettingsEnums.ACTION_AUDIO_SHARING_ADD_SOURCE,
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        addEventData.toString(),
                        /* changedPreferenceIntValue */ 0);

        when(mState.getBisSyncState()).thenReturn(ImmutableList.of(1L));
        mController.mBroadcastAssistantCallback.onSourceAdded(
                mDevice2, /* sourceId= */ 1, /* reason= */ 1);
        shadowOf(Looper.getMainLooper()).idle();

        childFragments = mParentFragment.getChildFragmentManager().getFragments();
        assertThat(childFragments)
                .comparingElementsUsing(CLAZZNAME_EQUALS)
                .containsExactly(AudioSharingDialogFragment.class.getName());

        ImmutableList<Pair<Integer, Object>> eventData = ImmutableList.of();
        for (Fragment fragment : childFragments) {
            if (fragment instanceof AudioSharingDialogFragment) {
                eventData = ((AudioSharingDialogFragment) fragment).getEventData();
                break;
            }
        }
        assertThat(eventData)
                .containsExactly(
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_SOURCE_PAGE_ID.getId(),
                                SettingsEnums.AUDIO_SHARING_SETTINGS),
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_PAGE_ID.getId(),
                                SettingsEnums.DIALOG_AUDIO_SHARING_MAIN),
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_USER_TRIGGERED.getId(),
                                false),
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_DEVICE_COUNT_IN_SHARING
                                        .getId(),
                                1),
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_CANDIDATE_DEVICE_COUNT
                                        .getId(),
                                0));

        childFragments.forEach(fragment -> ((DialogFragment) fragment).dismiss());
    }

    @Test
    public void onBroadcastMetadataChanged_oneActiveOneConnected_showJoinAudioSharingDialog() {
        FeatureFlagUtils.setEnabled(
                mContext, FeatureFlagUtils.SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST, true);
        when(mBtnView.isEnabled()).thenReturn(true);
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice2, mDevice1));
        when(mAssistant.getAllSources(any(BluetoothDevice.class))).thenReturn(ImmutableList.of());
        doNothing().when(mBroadcast).startPrivateBroadcast();
        mController.onCheckedChanged(mBtnView, /* isChecked= */ true);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mBroadcast).startPrivateBroadcast();
        ImmutableList<Pair<Integer, Object>> switchOnEventData =
                ImmutableList.of(
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_SOURCE_PAGE_ID.getId(),
                                SettingsEnums.AUDIO_SHARING_SETTINGS),
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_SOURCE_PACKAGE_NAME.getId(),
                                mContext.getPackageName()),
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_CANDIDATE_DEVICE_COUNT
                                        .getId(),
                                2));
        verify(mFeatureFactory.metricsFeatureProvider)
                .action(
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        SettingsEnums.ACTION_AUDIO_SHARING_MAIN_SWITCH_ON,
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        switchOnEventData.toString(),
                        /* changedPreferenceIntValue */ 0);
        List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
        assertThat(childFragments)
                .comparingElementsUsing(CLAZZNAME_EQUALS)
                .containsExactly(AudioSharingProgressDialogFragment.class.getName());
        AudioSharingProgressDialogFragment progressFragment =
                (AudioSharingProgressDialogFragment) Iterables.getOnlyElement(childFragments);
        String expectedMessage =
                mContext.getString(R.string.audio_sharing_progress_dialog_start_stream_content);
        checkProgressDialogMessage(progressFragment, expectedMessage);

        when(mBroadcast.isEnabled(null)).thenReturn(true);
        when(mBroadcast.getLatestBluetoothLeBroadcastMetadata()).thenReturn(mMetadata);
        mController.mBroadcastCallback.onBroadcastMetadataChanged(/* reason= */ 1, mMetadata);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mAssistant).addSource(mDevice2, mMetadata, /* isGroupOp= */ false);
        ImmutableList<Pair<Integer, Object>> addEventData =
                ImmutableList.of(
                        Pair.create(
                                METRIC_KEY_SOURCE_PAGE_ID.getId(),
                                SettingsEnums.ACTION_AUTO_JOIN_AUDIO_SHARING),
                        Pair.create(METRIC_KEY_USER_TRIGGERED.getId(), false));
        verify(mFeatureFactory.metricsFeatureProvider)
                .action(
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        SettingsEnums.ACTION_AUDIO_SHARING_ADD_SOURCE,
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        addEventData.toString(),
                        /* changedPreferenceIntValue */ 0);
        expectedMessage =
                mContext.getString(
                        R.string.audio_sharing_progress_dialog_add_source_content,
                        TEST_DEVICE_NAME2);
        checkProgressDialogMessage(progressFragment, expectedMessage);

        childFragments = mParentFragment.getChildFragmentManager().getFragments();
        assertThat(childFragments)
                .comparingElementsUsing(CLAZZNAME_EQUALS)
                .containsExactly(
                        AudioSharingDialogFragment.class.getName(),
                        AudioSharingProgressDialogFragment.class.getName());

        ImmutableList<Pair<Integer, Object>> eventData = ImmutableList.of();
        for (Fragment fragment : childFragments) {
            if (fragment instanceof AudioSharingDialogFragment) {
                eventData = ((AudioSharingDialogFragment) fragment).getEventData();
                break;
            }
        }
        assertThat(eventData)
                .containsExactly(
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_SOURCE_PAGE_ID.getId(),
                                SettingsEnums.AUDIO_SHARING_SETTINGS),
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_PAGE_ID.getId(),
                                SettingsEnums.DIALOG_AUDIO_SHARING_MAIN),
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_USER_TRIGGERED.getId(),
                                false),
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_DEVICE_COUNT_IN_SHARING
                                        .getId(),
                                1),
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_CANDIDATE_DEVICE_COUNT
                                        .getId(),
                                1));

        childFragments.forEach(fragment -> ((DialogFragment) fragment).dismiss());
    }

    @Test
    public void onBroadcastMetadataChanged_oneActiveOneConnected_clickShareBtnOnDialog_addSource() {
        FeatureFlagUtils.setEnabled(
                mContext, FeatureFlagUtils.SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST, true);
        when(mBtnView.isEnabled()).thenReturn(true);
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice2, mDevice1));
        when(mAssistant.getAllSources(any(BluetoothDevice.class))).thenReturn(ImmutableList.of());
        doNothing().when(mBroadcast).startPrivateBroadcast();
        mController.onCheckedChanged(mBtnView, /* isChecked= */ true);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mBroadcast).startPrivateBroadcast();
        when(mBroadcast.isEnabled(null)).thenReturn(true);
        when(mBroadcast.getLatestBluetoothLeBroadcastMetadata()).thenReturn(mMetadata);
        mController.mBroadcastCallback.onBroadcastMetadataChanged(/* reason= */ 1, mMetadata);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mAssistant).addSource(mDevice2, mMetadata, /* isGroupOp= */ false);

        AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
        assertThat(dialog).isNotNull();
        View btnView = dialog.findViewById(R.id.positive_btn);
        assertThat(btnView).isNotNull();
        btnView.performClick();
        shadowMainLooper().idle();

        verify(mAssistant).addSource(mDevice1, mMetadata, /* isGroupOp= */ false);
        ImmutableList<Pair<Integer, Object>> addEventData =
                ImmutableList.of(
                        Pair.create(
                                METRIC_KEY_SOURCE_PAGE_ID.getId(),
                                SettingsEnums.DIALOG_AUDIO_SHARING_MAIN),
                        Pair.create(METRIC_KEY_USER_TRIGGERED.getId(), true));
        verify(mFeatureFactory.metricsFeatureProvider)
                .action(
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        SettingsEnums.ACTION_AUDIO_SHARING_ADD_SOURCE,
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        addEventData.toString(),
                        /* changedPreferenceIntValue */ 0);
        assertThat(dialog.isShowing()).isFalse();
        // Progress dialog shows sharing progress for the user chosen sink.
        List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
        assertThat(childFragments)
                .comparingElementsUsing(CLAZZNAME_EQUALS)
                .containsExactly(AudioSharingProgressDialogFragment.class.getName());
        AudioSharingProgressDialogFragment progressFragment =
                (AudioSharingProgressDialogFragment) Iterables.getOnlyElement(childFragments);
        String expectedMessage =
                mContext.getString(
                        R.string.audio_sharing_progress_dialog_add_source_content,
                        TEST_DEVICE_NAME1);
        checkProgressDialogMessage(progressFragment, expectedMessage);

        childFragments.forEach(fragment -> ((DialogFragment) fragment).dismiss());
    }

    @Test
    public void onBroadcastMetadataChanged_oneActiveOneConnected_clickCancelBtnOnDlg_doNothing() {
        FeatureFlagUtils.setEnabled(
                mContext, FeatureFlagUtils.SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST, true);
        when(mBtnView.isEnabled()).thenReturn(true);
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice2, mDevice1));
        when(mAssistant.getAllSources(any(BluetoothDevice.class))).thenReturn(ImmutableList.of());
        doNothing().when(mBroadcast).startPrivateBroadcast();
        mController.onCheckedChanged(mBtnView, /* isChecked= */ true);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mBroadcast).startPrivateBroadcast();
        when(mBroadcast.isEnabled(null)).thenReturn(true);
        when(mBroadcast.getLatestBluetoothLeBroadcastMetadata()).thenReturn(mMetadata);
        mController.mBroadcastCallback.onBroadcastMetadataChanged(/* reason= */ 1, mMetadata);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mAssistant).addSource(mDevice2, mMetadata, /* isGroupOp= */ false);

        AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
        assertThat(dialog).isNotNull();
        View btnView = dialog.findViewById(R.id.negative_btn);
        assertThat(btnView).isNotNull();
        btnView.performClick();
        shadowMainLooper().idle();

        verify(mAssistant, never()).addSource(mDevice1, mMetadata, /* isGroupOp= */ false);
        ImmutableList<Pair<Integer, Object>> addEventData =
                ImmutableList.of(
                        Pair.create(
                                METRIC_KEY_SOURCE_PAGE_ID.getId(),
                                SettingsEnums.DIALOG_AUDIO_SHARING_MAIN),
                        Pair.create(METRIC_KEY_USER_TRIGGERED.getId(), true));
        verify(mFeatureFactory.metricsFeatureProvider, never())
                .action(
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        SettingsEnums.ACTION_AUDIO_SHARING_ADD_SOURCE,
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        addEventData.toString(),
                        /* changedPreferenceIntValue */ 0);
        assertThat(dialog.isShowing()).isFalse();
        // Progress dialog shows sharing progress for the auto add active sink.
        List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
        assertThat(childFragments)
                .comparingElementsUsing(CLAZZNAME_EQUALS)
                .containsExactly(AudioSharingProgressDialogFragment.class.getName());
        AudioSharingProgressDialogFragment progressFragment =
                (AudioSharingProgressDialogFragment) Iterables.getOnlyElement(childFragments);
        String expectedMessage =
                mContext.getString(
                        R.string.audio_sharing_progress_dialog_add_source_content,
                        TEST_DEVICE_NAME2);
        checkProgressDialogMessage(progressFragment, expectedMessage);

        childFragments.forEach(fragment -> ((DialogFragment) fragment).dismiss());
    }

    @Test
    public void testBroadcastCallbacks_updateSwitch() {
        mOnAudioSharingStateChanged = false;
        mSwitchBar.setChecked(false);
        when(mBroadcast.isEnabled(any())).thenReturn(false);
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1, mDevice2));
        when(mDeviceManager.getCachedDevicesCopy())
                .thenReturn(ImmutableList.of(mCachedDevice1, mCachedDevice2));
        mController.mBroadcastCallback.onBroadcastStartFailed(/* reason= */ 1);
        shadowOf(Looper.getMainLooper()).idle();
        assertThat(mSwitchBar.isChecked()).isFalse();
        assertThat(mSwitchBar.isEnabled()).isTrue();
        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);
        shadowOf(Looper.getMainLooper()).idle();
        assertThat(mSwitchBar.isChecked()).isTrue();
        assertThat(mSwitchBar.isEnabled()).isTrue();
        assertThat(mOnAudioSharingStateChanged).isTrue();

        mOnAudioSharingStateChanged = false;
        mController.mBroadcastCallback.onBroadcastStopFailed(/* reason= */ 1);
        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);
        when(mCachedDevice2.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(false);
        mController.mBroadcastCallback.onBroadcastStopped(/* reason= */ 1, /* broadcastId= */ 1);
        shadowOf(Looper.getMainLooper()).idle();
        assertThat(mSwitchBar.isChecked()).isFalse();
        assertThat(mSwitchBar.isEnabled()).isFalse();
        assertThat(mOnAudioSharingStateChanged).isTrue();
    }

    @Test
    public void testBroadcastCallbacks_doNothing() {
        mController.mBroadcastCallback.onBroadcastMetadataChanged(/* reason= */ 1, mMetadata);
        mController.mBroadcastCallback.onBroadcastUpdated(/* reason= */ 1, /* broadcastId= */ 1);
        mController.mBroadcastCallback.onPlaybackStarted(/* reason= */ 1, /* broadcastId= */ 1);
        mController.mBroadcastCallback.onPlaybackStopped(/* reason= */ 1, /* broadcastId= */ 1);
        mController.mBroadcastCallback.onBroadcastUpdateFailed(
                /* reason= */ 1, /* broadcastId= */ 1);
        verify(mSwitchBar, never()).setChecked(anyBoolean());
        assertThat(mOnAudioSharingStateChanged).isFalse();
    }

    @Test
    public void testAssistantCallbacks_onSourceAddFailed_twoDevices_showErrorAndLogAction() {
        FeatureFlagUtils.setEnabled(
                mContext, FeatureFlagUtils.SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST, true);
        when(mBtnView.isEnabled()).thenReturn(true);
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice2, mDevice1));
        when(mAssistant.getAllSources(any(BluetoothDevice.class))).thenReturn(ImmutableList.of());
        doNothing().when(mBroadcast).startPrivateBroadcast();
        mController.onCheckedChanged(mBtnView, /* isChecked= */ true);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mBroadcast).startPrivateBroadcast();

        when(mBroadcast.isEnabled(null)).thenReturn(true);
        when(mBroadcast.getLatestBluetoothLeBroadcastMetadata()).thenReturn(mMetadata);
        mController.mBroadcastCallback.onBroadcastMetadataChanged(/* reason= */ 1, mMetadata);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mAssistant).addSource(mDevice2, mMetadata, /* isGroupOp= */ false);

        AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
        assertThat(dialog).isNotNull();
        View btnView = dialog.findViewById(R.id.positive_btn);
        assertThat(btnView).isNotNull();
        btnView.performClick();
        shadowMainLooper().idle();

        verify(mAssistant).addSource(mDevice1, mMetadata, /* isGroupOp= */ false);
        assertThat(dialog.isShowing()).isFalse();

        mController.mBroadcastAssistantCallback.onSourceAddFailed(
                mDevice1, mMetadata, /* reason= */ 1);
        shadowMainLooper().idle();

        // Progress dialog shows sharing progress for the user chosen sink.
        List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
        assertThat(childFragments)
                .comparingElementsUsing(CLAZZNAME_EQUALS)
                .containsExactly(AudioSharingErrorDialogFragment.class.getName());
        verify(mFeatureFactory.metricsFeatureProvider)
                .action(
                        mContext,
                        SettingsEnums.ACTION_AUDIO_SHARING_JOIN_FAILED,
                        SettingsEnums.AUDIO_SHARING_SETTINGS);

        childFragments.forEach(fragment -> ((DialogFragment) fragment).dismiss());
    }

    @Test
    public void testAssistantCallbacks_onSourceAdded_dismissProgressDialog() {
        AudioSharingProgressDialogFragment.show(mParentFragment, TEST_DEVICE_NAME1);
        shadowOf(Looper.getMainLooper()).idle();
        List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
        assertThat(childFragments)
                .comparingElementsUsing(CLAZZNAME_EQUALS)
                .containsExactly(AudioSharingProgressDialogFragment.class.getName());

        mController.mBroadcastAssistantCallback.onSourceAdded(
                mDevice1, /* sourceId= */ 1, /* reason= */ 1);
        shadowOf(Looper.getMainLooper()).idle();
        childFragments = mParentFragment.getChildFragmentManager().getFragments();
        assertThat(childFragments).isEmpty();
    }

    @Test
    public void testAssistantCallbacks_doNothing() {
        // Do nothing
        mController.mBroadcastAssistantCallback.onReceiveStateChanged(
                mDevice1, /* sourceId= */ 1, mState);
        mController.mBroadcastAssistantCallback.onSearchStarted(/* reason= */ 1);
        mController.mBroadcastAssistantCallback.onSearchStartFailed(/* reason= */ 1);
        mController.mBroadcastAssistantCallback.onSearchStopped(/* reason= */ 1);
        mController.mBroadcastAssistantCallback.onSearchStopFailed(/* 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(mMetadata);
        mController.mBroadcastAssistantCallback.onSourceLost(/* broadcastId= */ 1);
        verifyNoMoreInteractions(mFeatureFactory.metricsFeatureProvider);
    }

    @Test
    public void onActiveDeviceChanged_leaProfile_updateSwitch() {
        mSwitchBar.setChecked(true);
        mSwitchBar.setEnabled(false);
        when(mBroadcast.isEnabled(null)).thenReturn(false);
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice2, mDevice1));
        when(mDeviceManager.getCachedDevicesCopy())
                .thenReturn(ImmutableList.of(mCachedDevice2, mCachedDevice1));
        mController.onActiveDeviceChanged(mCachedDevice2, BluetoothProfile.LE_AUDIO);
        shadowOf(Looper.getMainLooper()).idle();
        assertThat(mSwitchBar.isChecked()).isFalse();
        verify(mSwitchBar).setEnabled(true);
    }

    @Test
    public void onActiveDeviceChanged_a2dpProfile_updateSwitch() {
        mSwitchBar.setChecked(true);
        mSwitchBar.setEnabled(false);
        when(mBroadcast.isEnabled(null)).thenReturn(false);
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1, mDevice2));
        when(mCachedDevice2.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(false);
        when(mCachedDevice2.isActiveDevice(BluetoothProfile.A2DP)).thenReturn(true);
        when(mDeviceManager.getCachedDevicesCopy())
                .thenReturn(ImmutableList.of(mCachedDevice1, mCachedDevice2));
        mController.onActiveDeviceChanged(mCachedDevice2, BluetoothProfile.A2DP);
        shadowOf(Looper.getMainLooper()).idle();
        assertThat(mSwitchBar.isChecked()).isFalse();
        verify(mSwitchBar).setEnabled(true);
    }

    @Test
    public void onActiveDeviceChanged_nullActiveDevice_doNothing() {
        mController.onActiveDeviceChanged(/* activeDevice= */ null, BluetoothProfile.LE_AUDIO);
        shadowOf(Looper.getMainLooper()).idle();
        verify(mSwitchBar, never()).setEnabled(anyBoolean());
        verify(mSwitchBar, never()).setChecked(anyBoolean());
    }

    @Test
    public void testAccessibilityDelegate() {
        View view = new View(mContext);
        AccessibilityEvent event =
                new AccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
        event.setContentChangeTypes(AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
        assertThat(
                        mSwitchBar
                                .getRootView()
                                .getAccessibilityDelegate()
                                .onRequestSendAccessibilityEvent(mSwitchBar, view, event))
                .isTrue();

        event.setContentChangeTypes(AccessibilityEvent.CONTENT_CHANGE_TYPE_ENABLED);
        assertThat(
                        mSwitchBar
                                .getRootView()
                                .getAccessibilityDelegate()
                                .onRequestSendAccessibilityEvent(mSwitchBar, view, event))
                .isFalse();
    }

    @Test
    @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void handleStartAudioSharingFromIntent_flagOff_doNothing() {
        var unused = setUpFragmentWithStartSharingIntent(/* needHandleIntent */ true);
        mController.onStart(mLifecycleOwner);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mSwitchBar, never()).setChecked(true);
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void handleStartAudioSharingFromIntent_profileNotReady_doNothing() {
        when(mAssistant.isProfileReady()).thenReturn(false);
        var unused = setUpFragmentWithStartSharingIntent(/* needHandleIntent */ true);
        mController.onServiceConnected();
        shadowOf(Looper.getMainLooper()).idle();

        verify(mSwitchBar, never()).setChecked(true);
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void handleStartAudioSharingFromIntent_argFalse_doNothing() {
        mController.onStart(mLifecycleOwner);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mSwitchBar, never()).setChecked(true);
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void handleStartAudioSharingFromIntent_skipWhenScreenRotate_doNothing() {
        when(mAssistant.isProfileReady()).thenReturn(false);
        // When screen rotate, saveInstanceState is non null so that the fragment will be init with
        // needHandleIntent = false
        var unused = setUpFragmentWithStartSharingIntent(/* needHandleIntent */ false);
        mController.onServiceConnected();
        shadowOf(Looper.getMainLooper()).idle();

        verify(mSwitchBar, never()).setChecked(true);
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void handleStartAudioSharingFromIntent_handle() {
        when(mBtnView.isEnabled()).thenReturn(true);
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice2, mDevice1));
        when(mBroadcast.getLatestBluetoothLeBroadcastMetadata()).thenReturn(mMetadata);
        Fragment parentFragment = setUpFragmentWithStartSharingIntent(/* needHandleIntent */ true);
        mController.onServiceConnected();
        shadowOf(Looper.getMainLooper()).idle();

        verify(mSwitchBar).setChecked(true);
        doNothing().when(mBroadcast).startPrivateBroadcast();
        mController.onCheckedChanged(mBtnView, /* isChecked= */ true);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mBroadcast).startPrivateBroadcast();
        ImmutableList<Pair<Integer, Object>> switchOnEventData =
                ImmutableList.of(
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_SOURCE_PAGE_ID.getId(),
                                TEST_SOURCE_METRICS),
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_SOURCE_PACKAGE_NAME.getId(),
                                ""),
                        Pair.create(
                                AudioSharingUtils.MetricKey.METRIC_KEY_CANDIDATE_DEVICE_COUNT
                                        .getId(),
                                2));
        verify(mFeatureFactory.metricsFeatureProvider)
                .action(
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        SettingsEnums.ACTION_AUDIO_SHARING_MAIN_SWITCH_ON,
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        switchOnEventData.toString(),
                        /* changedPreferenceIntValue */ 0);
        mController.mBroadcastCallback.onBroadcastMetadataChanged(/* reason= */ 1, mMetadata);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mAssistant).addSource(mDevice1, mMetadata, /* isGroupOp= */ false);
        verify(mAssistant).addSource(mDevice2, mMetadata, /* isGroupOp= */ false);
        ImmutableList<Pair<Integer, Object>> addEventData1 =
                ImmutableList.of(
                        Pair.create(
                                METRIC_KEY_SOURCE_PAGE_ID.getId(),
                                SettingsEnums.ACTION_AUTO_JOIN_AUDIO_SHARING),
                        Pair.create(METRIC_KEY_USER_TRIGGERED.getId(), false));
        verify(mFeatureFactory.metricsFeatureProvider)
                .action(
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        SettingsEnums.ACTION_AUDIO_SHARING_ADD_SOURCE,
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        addEventData1.toString(),
                        /* changedPreferenceIntValue */ 0);
        ImmutableList<Pair<Integer, Object>> addEventData2 =
                ImmutableList.of(
                        Pair.create(
                                METRIC_KEY_SOURCE_PAGE_ID.getId(),
                                SettingsEnums.ACTION_AUTO_JOIN_AUDIO_SHARING),
                        Pair.create(METRIC_KEY_USER_TRIGGERED.getId(), true));
        verify(mFeatureFactory.metricsFeatureProvider)
                .action(
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        SettingsEnums.ACTION_AUDIO_SHARING_ADD_SOURCE,
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        addEventData2.toString(),
                        /* changedPreferenceIntValue */ 0);
        List<Fragment> childFragments = parentFragment.getChildFragmentManager().getFragments();
        // Skip audio sharing dialog.
        assertThat(childFragments)
                .comparingElementsUsing(CLAZZNAME_EQUALS)
                .containsExactly(AudioSharingProgressDialogFragment.class.getName());
        // Progress dialog shows sharing progress for the auto add second sink.
        AudioSharingProgressDialogFragment progressFragment =
                (AudioSharingProgressDialogFragment) Iterables.getOnlyElement(childFragments);
        String expectedMessage =
                mContext.getString(
                        R.string.audio_sharing_progress_dialog_add_source_content,
                        TEST_DEVICE_NAME1);
        checkProgressDialogMessage(progressFragment, expectedMessage);

        childFragments.forEach(fragment -> ((DialogFragment) fragment).dismiss());
    }

    @Test
    public void handleAutoAddSourceAfterPair() {
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1));
        when(mBroadcast.getLatestBluetoothLeBroadcastMetadata()).thenReturn(mMetadata);
        mController.handleAutoAddSourceAfterPair(mDevice1);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mAssistant).addSource(mDevice1, mMetadata, /* isGroupOp= */ false);
        ImmutableList<Pair<Integer, Object>> addEventData =
                ImmutableList.of(
                        Pair.create(
                                METRIC_KEY_SOURCE_PAGE_ID.getId(), SettingsEnums.BLUETOOTH_PAIRING),
                        Pair.create(METRIC_KEY_USER_TRIGGERED.getId(), true));
        verify(mFeatureFactory.metricsFeatureProvider)
                .action(
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        SettingsEnums.ACTION_AUDIO_SHARING_ADD_SOURCE,
                        SettingsEnums.AUDIO_SHARING_SETTINGS,
                        addEventData.toString(),
                        /* changedPreferenceIntValue */ 0);
        List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
        assertThat(childFragments)
                .comparingElementsUsing(CLAZZNAME_EQUALS)
                .containsExactly(AudioSharingProgressDialogFragment.class.getName());
    }

    private Fragment setUpFragmentWithStartSharingIntent(boolean needHandleIntent) {
        Bundle args = new Bundle();
        args.putBoolean(EXTRA_START_LE_AUDIO_SHARING, true);
        Intent intent = new Intent();
        intent.putExtra(EXTRA_SOURCE_METRICS, TEST_SOURCE_METRICS);
        intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
        Fragment fragment = new Fragment();
        FragmentController.of(fragment, intent)
                .create(/* containerViewId= */ 0, /* bundle= */ null)
                .start()
                .resume()
                .visible()
                .get();
        shadowOf(Looper.getMainLooper()).idle();
        mController.init(fragment, needHandleIntent);
        return fragment;
    }

    private void checkProgressDialogMessage(
            @NonNull AudioSharingProgressDialogFragment fragment, @NonNull String expectedMessage) {
        TextView progressMessage =
                fragment.getDialog() == null
                        ? null
                        : fragment.getDialog().findViewById(R.id.message);
        assertThat(progressMessage).isNotNull();
        assertThat(progressMessage.getText().toString()).isEqualTo(expectedMessage);
    }
}
