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