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.R; 30 import com.android.settings.core.SettingsUIDeviceConfig; 31 import com.android.settings.testutils.FakeFeatureFactory; 32 import com.android.settings.testutils.shadow.ShadowDeviceConfig; 33 import com.android.settings.testutils.shadow.ShadowEntityHeaderController; 34 import com.android.settings.widget.EntityHeaderController; 35 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; 36 import com.android.settingslib.bluetooth.LocalBluetoothManager; 37 import com.android.settingslib.widget.LayoutPreference; 38 39 import org.junit.After; 40 import org.junit.Ignore; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Answers; 44 import org.mockito.InOrder; 45 import org.mockito.Mock; 46 import org.robolectric.RobolectricTestRunner; 47 import org.robolectric.annotation.Config; 48 49 @RunWith(RobolectricTestRunner.class) 50 @Ignore 51 @Config(shadows = {ShadowEntityHeaderController.class, ShadowDeviceConfig.class}) 52 public class BluetoothDetailsHeaderControllerTest extends BluetoothDetailsControllerTestBase { 53 54 private BluetoothDetailsHeaderController mController; 55 private LayoutPreference mPreference; 56 57 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 58 private EntityHeaderController mHeaderController; 59 @Mock 60 private LocalBluetoothManager mBluetoothManager; 61 @Mock 62 private CachedBluetoothDeviceManager mCachedDeviceManager; 63 @Mock 64 private BluetoothDevice mBluetoothDevice; 65 66 @Override setUp()67 public void setUp() { 68 super.setUp(); 69 FakeFeatureFactory.setupForTest(); 70 ShadowEntityHeaderController.setUseMock(mHeaderController); 71 when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager); 72 when(mCachedDeviceManager.getSubDeviceSummary(mCachedDevice)).thenReturn("abc"); 73 mController = 74 new BluetoothDetailsHeaderController(mContext, mFragment, mCachedDevice, mLifecycle, 75 mBluetoothManager); 76 mPreference = new LayoutPreference(mContext, R.layout.settings_entity_header); 77 mPreference.setKey(mController.getPreferenceKey()); 78 mScreen.addPreference(mPreference); 79 setupDevice(mDeviceConfig); 80 when(mCachedDevice.getDevice()).thenReturn(mBluetoothDevice); 81 } 82 83 @After tearDown()84 public void tearDown() { 85 ShadowEntityHeaderController.reset(); 86 } 87 88 /** 89 * Test to verify the current test context object works so that we are not checking null 90 * against null 91 */ 92 @Test testContextMock()93 public void testContextMock() { 94 assertThat(mContext.getString(R.string.bluetooth_connected)).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(mActivity, true); 107 } 108 109 @Test connectionStatusChangesWhileScreenOpen()110 public void connectionStatusChangesWhileScreenOpen() { 111 InOrder inOrder = inOrder(mHeaderController); 112 when(mCachedDevice.getConnectionSummary()) 113 .thenReturn(mContext.getString(R.string.bluetooth_connected)); 114 showScreen(mController); 115 inOrder.verify(mHeaderController) 116 .setSummary(mContext.getString(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(R.string.bluetooth_connecting)); 124 mController.onDeviceAttributesChanged(); 125 inOrder.verify(mHeaderController) 126 .setSummary(mContext.getString(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