1 /* 2 * Copyright 2022 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.server.bluetooth; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.verify; 23 24 import android.content.Context; 25 import android.content.ContextWrapper; 26 import android.provider.Settings; 27 import android.text.TextUtils; 28 29 import androidx.test.InstrumentationRegistry; 30 import androidx.test.annotation.UiThreadTest; 31 import androidx.test.filters.SmallTest; 32 import androidx.test.runner.AndroidJUnit4; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 40 @SmallTest 41 @RunWith(AndroidJUnit4.class) 42 public class BluetoothModeChangeHelperTest { 43 44 @Mock 45 BluetoothManagerService mService; 46 47 Context mContext; 48 BluetoothModeChangeHelper mHelper; 49 50 @Before setUp()51 public void setUp() throws Exception { 52 MockitoAnnotations.initMocks(this); 53 mContext = spy(new ContextWrapper(InstrumentationRegistry.getTargetContext())); 54 55 mHelper = new BluetoothModeChangeHelper(mContext); 56 } 57 58 @Test isMediaProfileConnected()59 public void isMediaProfileConnected() { 60 assertThat(mHelper.isMediaProfileConnected()).isFalse(); 61 } 62 63 @Test isBluetoothOn_doesNotCrash()64 public void isBluetoothOn_doesNotCrash() { 65 // assertThat(mHelper.isBluetoothOn()).isFalse(); 66 // TODO: Strangely, isBluetoothOn() does not call BluetoothAdapter.isEnabled(). 67 // Instead, it calls isLeEnabled(). Two results can be different. 68 // Is this a mistake, or in purpose? 69 mHelper.isBluetoothOn(); 70 } 71 72 @Test isAirplaneModeOn()73 public void isAirplaneModeOn() { 74 assertThat(mHelper.isAirplaneModeOn()).isFalse(); 75 } 76 77 @Test onAirplaneModeChanged()78 public void onAirplaneModeChanged() { 79 mHelper.onAirplaneModeChanged(mService); 80 81 verify(mService).onAirplaneModeChanged(); 82 } 83 84 @Test setSettingsInt()85 public void setSettingsInt() { 86 String testSettingsName = "BluetoothModeChangeHelperTest_test_settings_name"; 87 int value = 9876; 88 89 try { 90 mHelper.setSettingsInt(testSettingsName, value); 91 assertThat(mHelper.getSettingsInt(testSettingsName)).isEqualTo(value); 92 } finally { 93 Settings.Global.resetToDefaults(mContext.getContentResolver(), null); 94 } 95 } 96 97 @Test setSettingsSecureInt()98 public void setSettingsSecureInt() { 99 String testSettingsName = "BluetoothModeChangeHelperTest_test_settings_name"; 100 int value = 1234; 101 102 try { 103 mHelper.setSettingsSecureInt(testSettingsName, value); 104 assertThat(mHelper.getSettingsSecureInt(testSettingsName, 0)).isEqualTo(value); 105 } finally { 106 Settings.Global.resetToDefaults(mContext.getContentResolver(), null); 107 } 108 } 109 110 @Test isBluetoothOnAPM_doesNotCrash()111 public void isBluetoothOnAPM_doesNotCrash() { 112 mHelper.isBluetoothOnAPM(); 113 } 114 115 @UiThreadTest 116 @Test showToastMessage_doesNotCrash()117 public void showToastMessage_doesNotCrash() { 118 mHelper.showToastMessage(); 119 } 120 121 @Test getBluetoothPackageName()122 public void getBluetoothPackageName() { 123 // TODO: Find a good way to specify the exact name of bluetooth package. 124 // mContext.getPackageName() does not work as this is not a test for BT app. 125 String bluetoothPackageName = mHelper.getBluetoothPackageName(); 126 127 boolean packageNameFound = 128 TextUtils.equals(bluetoothPackageName, "com.android.bluetooth") 129 || TextUtils.equals(bluetoothPackageName, "com.google.android.bluetooth"); 130 131 assertThat(packageNameFound).isTrue(); 132 } 133 } 134