1 /*
2  * Copyright (C) 2024 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 org.mockito.Mockito.spy;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22 
23 import android.bluetooth.BluetoothDevice;
24 
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceCategory;
27 import androidx.preference.PreferenceScreen;
28 import androidx.preference.SwitchPreferenceCompat;
29 
30 import com.android.settings.testutils.FakeFeatureFactory;
31 import com.android.settingslib.core.lifecycle.Lifecycle;
32 
33 import com.google.common.collect.ImmutableList;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.RuntimeEnvironment;
42 import org.robolectric.shadows.ShadowLooper;
43 
44 @RunWith(RobolectricTestRunner.class)
45 public class BluetoothDetailsExtraOptionsControllerTest extends BluetoothDetailsControllerTestBase {
46 
47     private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C";
48     @Mock private BluetoothDevice mBluetoothDevice;
49     @Mock private Lifecycle mExtraOptionsLifecycle;
50     @Mock private PreferenceCategory mOptionsContainer;
51     @Mock private PreferenceScreen mPreferenceScreen;
52 
53     private BluetoothDetailsExtraOptionsController mController;
54     private BluetoothFeatureProvider mFeatureProvider;
55 
56     @Before
setUp()57     public void setUp() {
58         MockitoAnnotations.initMocks(this);
59 
60         mContext = spy(RuntimeEnvironment.application);
61         when(mCachedDevice.getAddress()).thenReturn(MAC_ADDRESS);
62         when(mCachedDevice.getDevice()).thenReturn(mBluetoothDevice);
63         when(mBluetoothDevice.getAnonymizedAddress()).thenReturn(MAC_ADDRESS);
64 
65         final FakeFeatureFactory fakeFeatureFactory = FakeFeatureFactory.setupForTest();
66         mFeatureProvider = fakeFeatureFactory.getBluetoothFeatureProvider();
67 
68         mController =
69                 new BluetoothDetailsExtraOptionsController(
70                         mContext, mFragment, mCachedDevice, mExtraOptionsLifecycle);
71     }
72 
73     @Test
displayPreference_removeAndAddPreferences()74     public void displayPreference_removeAndAddPreferences() {
75         Preference preference1 = new SwitchPreferenceCompat(mContext);
76         Preference preference2 = new SwitchPreferenceCompat(mContext);
77         when(mFeatureProvider.getBluetoothExtraOptions(mContext, mCachedDevice))
78                 .thenReturn(ImmutableList.of(preference1, preference2));
79         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
80                 .thenReturn(mOptionsContainer);
81 
82         mController.displayPreference(mPreferenceScreen);
83         ShadowLooper.idleMainLooper();
84 
85         verify(mOptionsContainer).removeAll();
86         verify(mOptionsContainer).addPreference(preference1);
87         verify(mOptionsContainer).addPreference(preference2);
88     }
89 }
90