• 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 package com.android.settings.bluetooth;
17 
18 import static org.mockito.Mockito.spy;
19 import static org.mockito.Mockito.when;
20 
21 import android.bluetooth.BluetoothAdapter;
22 import android.bluetooth.BluetoothClass;
23 import android.bluetooth.BluetoothDevice;
24 import android.bluetooth.BluetoothManager;
25 import android.content.Context;
26 
27 import androidx.fragment.app.FragmentActivity;
28 import androidx.lifecycle.LifecycleOwner;
29 import androidx.preference.PreferenceManager;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.settings.R;
33 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
34 import com.android.settingslib.core.lifecycle.Lifecycle;
35 
36 import org.junit.Before;
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 
43 @RunWith(RobolectricTestRunner.class)
44 public class BluetoothDetailsControllerTestBase {
45 
46     protected Context mContext;
47     private LifecycleOwner mLifecycleOwner;
48     protected Lifecycle mLifecycle;
49     protected DeviceConfig mDeviceConfig;
50     protected BluetoothDevice mDevice;
51     protected BluetoothManager mBluetoothManager;
52     protected BluetoothAdapter mBluetoothAdapter;
53     protected PreferenceScreen mScreen;
54     protected PreferenceManager mPreferenceManager;
55 
56     @Mock
57     protected BluetoothDeviceDetailsFragment mFragment;
58     @Mock
59     protected CachedBluetoothDevice mCachedDevice;
60     @Mock
61     protected FragmentActivity mActivity;
62     @Mock
63     protected BluetoothClass mBluetoothDeviceClass;
64 
65     @Before
setUp()66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68         mContext = RuntimeEnvironment.application;
69         mPreferenceManager = new PreferenceManager(mContext);
70         mScreen = mPreferenceManager.createPreferenceScreen(mContext);
71         mDeviceConfig = makeDefaultDeviceConfig();
72         when(mFragment.getActivity()).thenReturn(mActivity);
73         when(mActivity.getApplicationContext()).thenReturn(mContext);
74         when(mFragment.getContext()).thenReturn(mContext);
75         when(mFragment.getPreferenceManager()).thenReturn(mPreferenceManager);
76         when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
77         mLifecycleOwner = () -> mLifecycle;
78         mLifecycle = spy(new Lifecycle(mLifecycleOwner));
79         mBluetoothManager = new BluetoothManager(mContext);
80         mBluetoothAdapter = mBluetoothManager.getAdapter();
81     }
82 
83     protected static class DeviceConfig {
84 
85         private String name;
86         private String address;
87         private int majorDeviceClass;
88         private boolean connected;
89         private String connectionSummary;
90 
setName(String newValue)91         DeviceConfig setName(String newValue) {
92             this.name = newValue;
93             return this;
94         }
95 
setAddress(String newValue)96         DeviceConfig setAddress(String newValue) {
97             this.address = newValue;
98             return this;
99         }
100 
setMajorDeviceClass(int newValue)101         DeviceConfig setMajorDeviceClass(int newValue) {
102             this.majorDeviceClass = newValue;
103             return this;
104         }
105 
setConnected(boolean newValue)106         DeviceConfig setConnected(boolean newValue) {
107             this.connected = newValue;
108             return this;
109         }
110 
setConnectionSummary(String connectionSummary)111         DeviceConfig setConnectionSummary(String connectionSummary) {
112             this.connectionSummary = connectionSummary;
113             return this;
114         }
115 
getName()116         String getName() {
117             return name;
118         }
119 
getAddress()120         String getAddress() {
121             return address;
122         }
123 
getMajorDeviceClass()124         int getMajorDeviceClass() {
125             return majorDeviceClass;
126         }
127 
isConnected()128         boolean isConnected() {
129             return connected;
130         }
131 
getConnectionSummary()132         String getConnectionSummary() {
133             return connectionSummary;
134         }
135     }
136 
makeDefaultDeviceConfig()137     DeviceConfig makeDefaultDeviceConfig() {
138         return new DeviceConfig()
139                 .setName("Mock Device")
140                 .setAddress("B4:B0:34:B5:3B:1B")
141                 .setMajorDeviceClass(BluetoothClass.Device.Major.AUDIO_VIDEO)
142                 .setConnected(true)
143                 .setConnectionSummary(mContext.getString(R.string.bluetooth_connected));
144     }
145 
146     /**
147      * Sets up the device mock to return various state based on a test config.
148      */
setupDevice(DeviceConfig config)149     void setupDevice(DeviceConfig config) {
150         when(mCachedDevice.getName()).thenReturn(config.getName());
151         when(mBluetoothDeviceClass.getMajorDeviceClass()).thenReturn(config.getMajorDeviceClass());
152         when(mCachedDevice.isConnected()).thenReturn(config.isConnected());
153         when(mCachedDevice.getConnectionSummary()).thenReturn(config.getConnectionSummary());
154 
155         mDevice = mBluetoothAdapter.getRemoteDevice(config.getAddress());
156         when(mCachedDevice.getDevice()).thenReturn(mDevice);
157         when(mCachedDevice.getAddress()).thenReturn(config.getAddress());
158     }
159 
160     /**
161      * Convenience method to call displayPreference and onResume.
162      */
showScreen(BluetoothDetailsController controller)163     void showScreen(BluetoothDetailsController controller) {
164         controller.displayPreference(mScreen);
165         controller.onResume();
166     }
167 }
168 
169