• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import static org.mockito.Matchers.any;
21 import static org.mockito.Mockito.inOrder;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.graphics.drawable.Drawable;
26 
27 import com.android.settings.R;
28 import com.android.settings.applications.LayoutPreference;
29 import com.android.settings.testutils.FakeFeatureFactory;
30 import com.android.settings.testutils.SettingsRobolectricTestRunner;
31 import com.android.settings.testutils.shadow.SettingsShadowBluetoothDevice;
32 import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
33 import com.android.settings.widget.EntityHeaderController;
34 
35 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
36 import com.android.settingslib.bluetooth.LocalBluetoothManager;
37 
38 import org.junit.After;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Answers;
42 import org.mockito.InOrder;
43 import org.mockito.Mock;
44 import org.robolectric.annotation.Config;
45 
46 @RunWith(SettingsRobolectricTestRunner.class)
47 
48 @Config(shadows = {SettingsShadowBluetoothDevice.class, ShadowEntityHeaderController.class})
49 public class BluetoothDetailsHeaderControllerTest extends BluetoothDetailsControllerTestBase {
50 
51     private BluetoothDetailsHeaderController mController;
52     private LayoutPreference mPreference;
53 
54     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
55     private EntityHeaderController mHeaderController;
56     @Mock
57     private LocalBluetoothManager mBluetoothManager;
58     @Mock
59     private CachedBluetoothDeviceManager mCachedDeviceManager;
60 
61     @Override
setUp()62     public void setUp() {
63         super.setUp();
64         FakeFeatureFactory.setupForTest();
65         ShadowEntityHeaderController.setUseMock(mHeaderController);
66         when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
67         when(mCachedDeviceManager.getHearingAidPairDeviceSummary(mCachedDevice)).thenReturn("abc");
68         mController =
69             new BluetoothDetailsHeaderController(mContext, mFragment, mCachedDevice, mLifecycle,
70                 mBluetoothManager);
71         mPreference = new LayoutPreference(mContext, R.layout.settings_entity_header);
72         mPreference.setKey(mController.getPreferenceKey());
73         mScreen.addPreference(mPreference);
74         setupDevice(mDeviceConfig);
75     }
76 
77     @After
tearDown()78     public void tearDown() {
79         ShadowEntityHeaderController.reset();
80     }
81 
82     /**
83      * Test to verify the current test context object works so that we are not checking null
84      * against null
85      */
86     @Test
testContextMock()87     public void testContextMock() {
88         assertThat(mContext.getString(R.string.bluetooth_connected)).isNotNull();
89     }
90 
91     @Test
header()92     public void header() {
93         showScreen(mController);
94 
95         verify(mHeaderController).setLabel(mDeviceConfig.getName());
96         verify(mHeaderController).setIcon(any(Drawable.class));
97         verify(mHeaderController).setIconContentDescription(any(String.class));
98         verify(mHeaderController).setSummary(any(String.class));
99         verify(mHeaderController).setSecondSummary(any(String.class));
100         verify(mHeaderController).done(mActivity, true);
101     }
102 
103     @Test
connectionStatusChangesWhileScreenOpen()104     public void connectionStatusChangesWhileScreenOpen() {
105         InOrder inOrder = inOrder(mHeaderController);
106         when(mCachedDevice.getConnectionSummary())
107             .thenReturn(mContext.getString(R.string.bluetooth_connected));
108         showScreen(mController);
109         inOrder.verify(mHeaderController)
110             .setSummary(mContext.getString(R.string.bluetooth_connected));
111 
112         when(mCachedDevice.getConnectionSummary()).thenReturn(null);
113         mController.onDeviceAttributesChanged();
114         inOrder.verify(mHeaderController).setSummary((CharSequence) null);
115 
116         when(mCachedDevice.getConnectionSummary())
117             .thenReturn(mContext.getString(R.string.bluetooth_connecting));
118         mController.onDeviceAttributesChanged();
119         inOrder.verify(mHeaderController)
120             .setSummary(mContext.getString(R.string.bluetooth_connecting));
121     }
122 }
123