1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.bluetooth; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyBoolean; 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.bluetooth.BluetoothLeBroadcastMetadata; 30 import android.content.Context; 31 import android.os.Bundle; 32 33 import androidx.fragment.app.FragmentActivity; 34 import androidx.fragment.app.FragmentManager; 35 import androidx.fragment.app.FragmentTransaction; 36 import androidx.preference.PreferenceCategory; 37 38 import com.android.settings.testutils.FakeFeatureFactory; 39 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 40 import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant; 41 import com.android.settingslib.bluetooth.LocalBluetoothManager; 42 43 import org.junit.Before; 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 import org.mockito.Answers; 47 import org.mockito.Mock; 48 import org.mockito.MockitoAnnotations; 49 import org.robolectric.Robolectric; 50 import org.robolectric.RobolectricTestRunner; 51 import org.robolectric.RuntimeEnvironment; 52 import org.robolectric.annotation.Config; 53 54 @RunWith(RobolectricTestRunner.class) 55 @Config(shadows = { 56 com.android.settings.testutils.shadow.ShadowFragment.class, 57 }) 58 public class BluetoothFindBroadcastsFragmentTest { 59 60 private static final String TEST_ADDRESS = "55:66:77:88:99:AA"; 61 62 private BluetoothFindBroadcastsFragment mFragment; 63 private FragmentActivity mActivity; 64 private Context mContext; 65 private FragmentTransaction mFragmentTransaction; 66 67 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 68 private CachedBluetoothDevice mCachedDevice; 69 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 70 private LocalBluetoothManager mLocalManager; 71 @Mock 72 private PreferenceCategory mPreferenceCategroy; 73 @Mock 74 private LocalBluetoothLeBroadcastAssistant mBroadcastAssistant; 75 @Mock 76 private BluetoothLeBroadcastMetadata mBroadcastMetadata; 77 @Mock 78 private BluetoothBroadcastSourcePreference mBroadcastSourcePreference; 79 @Mock 80 private BluetoothBroadcastSourcePreference mBroadcastSourcePreferenceUserClick; 81 82 @Before setUp()83 public void setUp() { 84 MockitoAnnotations.initMocks(this); 85 mContext = spy(RuntimeEnvironment.application); 86 FakeFeatureFactory.setupForTest(); 87 88 mFragment = spy(new BluetoothFindBroadcastsFragment()); 89 doReturn(mLocalManager).when(mFragment).getLocalBluetoothManager(any()); 90 doReturn(mCachedDevice).when(mFragment).getCachedDevice(any()); 91 doReturn(mBroadcastAssistant).when(mFragment).getLeBroadcastAssistant(); 92 doReturn(mPreferenceCategroy).when(mFragment).findPreference(any()); 93 mActivity = Robolectric.setupActivity(FragmentActivity.class); 94 when(mFragment.getActivity()).thenReturn(mActivity); 95 96 FragmentManager fragmentManager = mock(FragmentManager.class); 97 when(mFragment.getFragmentManager()).thenReturn(fragmentManager); 98 mFragmentTransaction = mock(FragmentTransaction.class); 99 when(fragmentManager.beginTransaction()).thenReturn(mFragmentTransaction); 100 101 when(mCachedDevice.getAddress()).thenReturn(TEST_ADDRESS); 102 when(mCachedDevice.getIdentityAddress()).thenReturn(TEST_ADDRESS); 103 Bundle args = new Bundle(); 104 args.putString(BluetoothFindBroadcastsFragment.KEY_DEVICE_ADDRESS, TEST_ADDRESS); 105 mFragment.setArguments(args); 106 mFragment.onAttach(mContext); 107 } 108 109 @Test verifyOnAttachResult()110 public void verifyOnAttachResult() { 111 assertThat(mFragment.mDeviceAddress).isEqualTo(TEST_ADDRESS); 112 assertThat(mFragment.mManager).isEqualTo(mLocalManager); 113 assertThat(mFragment.mCachedDevice).isEqualTo(mCachedDevice); 114 } 115 116 @Test addSource_selectedPrefIsNull_returnsNewPref()117 public void addSource_selectedPrefIsNull_returnsNewPref() { 118 mFragment.mSelectedPreference = null; 119 120 mFragment.addSource(mBroadcastSourcePreference); 121 122 assertThat(mFragment.mSelectedPreference).isEqualTo(mBroadcastSourcePreference); 123 } 124 125 @Test addSource_sourcePrefIsCreatedByReceiveState_removesOldPref()126 public void addSource_sourcePrefIsCreatedByReceiveState_removesOldPref() { 127 mFragment.mSelectedPreference = mBroadcastSourcePreference; 128 mFragment.mBroadcastSourceListCategory = mPreferenceCategroy; 129 doReturn(true).when(mFragment.mSelectedPreference).isCreatedByReceiveState(); 130 131 mFragment.addSource(mBroadcastSourcePreferenceUserClick); 132 133 verify(mFragment.mBroadcastSourceListCategory).removePreference(mBroadcastSourcePreference); 134 assertThat(mFragment.mSelectedPreference).isEqualTo(mBroadcastSourcePreferenceUserClick); 135 } 136 137 @Test addSource_sourcePrefIsCreatedByMetadata_updatesOldPref()138 public void addSource_sourcePrefIsCreatedByMetadata_updatesOldPref() { 139 when(mBroadcastSourcePreference.isCreatedByReceiveState()).thenReturn(false); 140 when(mBroadcastSourcePreference.getBluetoothLeBroadcastMetadata()) 141 .thenReturn(mBroadcastMetadata); 142 mFragment.mSelectedPreference = mBroadcastSourcePreference; 143 mFragment.mBroadcastSourceListCategory = mPreferenceCategroy; 144 145 mFragment.addSource(mBroadcastSourcePreferenceUserClick); 146 147 verify(mBroadcastSourcePreference).updateMetadataAndRefreshUi( 148 any(BluetoothLeBroadcastMetadata.class), anyBoolean()); 149 assertThat(mFragment.mSelectedPreference).isEqualTo(mBroadcastSourcePreferenceUserClick); 150 } 151 152 } 153