1 /*
2  * Copyright (C) 2017 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.Mockito.inOrder;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import android.bluetooth.BluetoothDevice;
27 import android.graphics.drawable.Drawable;
28 
29 import com.android.settings.core.SettingsUIDeviceConfig;
30 import com.android.settings.testutils.FakeFeatureFactory;
31 import com.android.settings.testutils.shadow.ShadowDeviceConfig;
32 import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
33 import com.android.settings.widget.EntityHeaderController;
34 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
35 import com.android.settingslib.bluetooth.LocalBluetoothManager;
36 import com.android.settingslib.widget.LayoutPreference;
37 
38 import org.junit.After;
39 import org.junit.Ignore;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Answers;
43 import org.mockito.InOrder;
44 import org.mockito.Mock;
45 import org.robolectric.RobolectricTestRunner;
46 import org.robolectric.annotation.Config;
47 
48 @RunWith(RobolectricTestRunner.class)
49 @Ignore
50 @Config(shadows = {ShadowEntityHeaderController.class, ShadowDeviceConfig.class})
51 public class BluetoothDetailsHeaderControllerTest extends BluetoothDetailsControllerTestBase {
52 
53     private BluetoothDetailsHeaderController mController;
54     private LayoutPreference mPreference;
55 
56     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
57     private EntityHeaderController mHeaderController;
58     @Mock
59     private LocalBluetoothManager mBluetoothManager;
60     @Mock
61     private CachedBluetoothDeviceManager mCachedDeviceManager;
62     @Mock
63     private BluetoothDevice mBluetoothDevice;
64 
65     @Override
setUp()66     public void setUp() {
67         super.setUp();
68         FakeFeatureFactory.setupForTest();
69         ShadowEntityHeaderController.setUseMock(mHeaderController);
70         when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
71         when(mCachedDeviceManager.getSubDeviceSummary(mCachedDevice)).thenReturn("abc");
72         mController =
73             new BluetoothDetailsHeaderController(mContext, mFragment, mCachedDevice, mLifecycle);
74         mPreference = new LayoutPreference(
75                 mContext, com.android.settingslib.widget.preference.layout.R.layout.settings_entity_header);
76         mPreference.setKey(mController.getPreferenceKey());
77         mScreen.addPreference(mPreference);
78         setupDevice(mDeviceConfig);
79         when(mCachedDevice.getDevice()).thenReturn(mBluetoothDevice);
80     }
81 
82     @After
tearDown()83     public void tearDown() {
84         ShadowEntityHeaderController.reset();
85     }
86 
87     /**
88      * Test to verify the current test context object works so that we are not checking null
89      * against null
90      */
91     @Test
testContextMock()92     public void testContextMock() {
93         assertThat(mContext.getString(com.android.settingslib.R.string.bluetooth_connected))
94                 .isNotNull();
95     }
96 
97     @Test
header()98     public void header() {
99         showScreen(mController);
100 
101         verify(mHeaderController).setLabel(mDeviceConfig.getName());
102         verify(mHeaderController).setIcon(any(Drawable.class));
103         verify(mHeaderController).setIconContentDescription(any(String.class));
104         verify(mHeaderController).setSummary(any(String.class));
105         verify(mHeaderController).setSecondSummary(any(String.class));
106         verify(mHeaderController).done(true);
107     }
108 
109     @Test
connectionStatusChangesWhileScreenOpen()110     public void connectionStatusChangesWhileScreenOpen() {
111         InOrder inOrder = inOrder(mHeaderController);
112         when(mCachedDevice.getConnectionSummary())
113             .thenReturn(mContext.getString(com.android.settingslib.R.string.bluetooth_connected));
114         showScreen(mController);
115         inOrder.verify(mHeaderController)
116             .setSummary(mContext.getString(com.android.settingslib.R.string.bluetooth_connected));
117 
118         when(mCachedDevice.getConnectionSummary()).thenReturn(null);
119         mController.onDeviceAttributesChanged();
120         inOrder.verify(mHeaderController).setSummary((CharSequence) null);
121 
122         when(mCachedDevice.getConnectionSummary())
123             .thenReturn(mContext.getString(com.android.settingslib.R.string.bluetooth_connecting));
124         mController.onDeviceAttributesChanged();
125         inOrder.verify(mHeaderController)
126             .setSummary(mContext.getString(com.android.settingslib.R.string.bluetooth_connecting));
127     }
128 
129     @Test
isAvailable_untetheredHeadsetWithConfigOn_returnFalse()130     public void isAvailable_untetheredHeadsetWithConfigOn_returnFalse() {
131         android.provider.DeviceConfig.setProperty(
132                 android.provider.DeviceConfig.NAMESPACE_SETTINGS_UI,
133                 SettingsUIDeviceConfig.BT_ADVANCED_HEADER_ENABLED, "true", true);
134         when(mBluetoothDevice.getMetadata(
135                 BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn("true".getBytes());
136 
137         assertThat(mController.isAvailable()).isFalse();
138     }
139 
140     @Test
isAvailable_untetheredHeadsetWithConfigOff_returnTrue()141     public void isAvailable_untetheredHeadsetWithConfigOff_returnTrue() {
142         android.provider.DeviceConfig.setProperty(
143                 android.provider.DeviceConfig.NAMESPACE_SETTINGS_UI,
144                 SettingsUIDeviceConfig.BT_ADVANCED_HEADER_ENABLED, "false", true);
145         when(mBluetoothDevice.getMetadata(
146                 BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn("true".getBytes());
147 
148         assertThat(mController.isAvailable()).isTrue();
149     }
150 }
151