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