Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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.bluetooth.btservice; |
| 18 | |
| 19 | import static org.mockito.ArgumentMatchers.eq; |
| 20 | import static org.mockito.Mockito.*; |
| 21 | |
| 22 | import android.bluetooth.BluetoothAdapter; |
| 23 | import android.content.Intent; |
| 24 | import android.os.Looper; |
KOUSHIK PANUGANTI | 8befdc7 | 2019-03-29 10:47:47 -0700 | [diff] [blame] | 25 | |
| 26 | import androidx.test.InstrumentationRegistry; |
| 27 | import androidx.test.filters.MediumTest; |
| 28 | import androidx.test.rule.ServiceTestRule; |
| 29 | import androidx.test.runner.AndroidJUnit4; |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 30 | |
Jack He | bd90909 | 2018-02-02 17:24:00 -0800 | [diff] [blame] | 31 | import com.android.bluetooth.TestUtils; |
| 32 | |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 33 | import org.junit.After; |
| 34 | import org.junit.Assert; |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Rule; |
| 37 | import org.junit.Test; |
| 38 | import org.junit.runner.RunWith; |
Myles Watson | 35bbcab | 2018-01-29 13:30:20 -0800 | [diff] [blame] | 39 | import org.mockito.ArgumentCaptor; |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 40 | import org.mockito.Mock; |
| 41 | import org.mockito.MockitoAnnotations; |
| 42 | |
| 43 | import java.lang.reflect.InvocationTargetException; |
Myles Watson | 35bbcab | 2018-01-29 13:30:20 -0800 | [diff] [blame] | 44 | import java.util.List; |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 45 | import java.util.concurrent.TimeoutException; |
| 46 | |
| 47 | @MediumTest |
| 48 | @RunWith(AndroidJUnit4.class) |
| 49 | public class ProfileServiceTest { |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 50 | private static final int PROFILE_START_MILLIS = 1250; |
Myles Watson | 35bbcab | 2018-01-29 13:30:20 -0800 | [diff] [blame] | 51 | private static final int NUM_REPEATS = 5; |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 52 | |
| 53 | @Rule public final ServiceTestRule mServiceTestRule = new ServiceTestRule(); |
| 54 | |
| 55 | private void setProfileState(Class profile, int state) throws TimeoutException { |
| 56 | Intent startIntent = new Intent(InstrumentationRegistry.getTargetContext(), profile); |
| 57 | startIntent.putExtra(AdapterService.EXTRA_ACTION, |
| 58 | AdapterService.ACTION_SERVICE_STATE_CHANGED); |
| 59 | startIntent.putExtra(BluetoothAdapter.EXTRA_STATE, state); |
| 60 | mServiceTestRule.startService(startIntent); |
| 61 | } |
| 62 | |
| 63 | private void setAllProfilesState(int state, int invocationNumber) throws TimeoutException { |
| 64 | for (Class profile : mProfiles) { |
| 65 | setProfileState(profile, state); |
| 66 | } |
Myles Watson | 35bbcab | 2018-01-29 13:30:20 -0800 | [diff] [blame] | 67 | ArgumentCaptor<ProfileService> argument = ArgumentCaptor.forClass(ProfileService.class); |
| 68 | verify(mMockAdapterService, timeout(PROFILE_START_MILLIS).times( |
| 69 | mProfiles.length * invocationNumber)).onProfileServiceStateChanged( |
| 70 | argument.capture(), eq(state)); |
| 71 | List<ProfileService> argumentProfiles = argument.getAllValues(); |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 72 | for (Class profile : mProfiles) { |
Myles Watson | 35bbcab | 2018-01-29 13:30:20 -0800 | [diff] [blame] | 73 | int matches = 0; |
| 74 | for (ProfileService arg : argumentProfiles) { |
| 75 | if (arg.getClass().getName().equals(profile.getName())) { |
| 76 | matches += 1; |
| 77 | } |
| 78 | } |
| 79 | Assert.assertEquals(invocationNumber, matches); |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | private @Mock AdapterService mMockAdapterService; |
| 84 | |
| 85 | private Class[] mProfiles; |
| 86 | |
| 87 | @Before |
| 88 | public void setUp() |
| 89 | throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
| 90 | if (Looper.myLooper() == null) { |
| 91 | Looper.prepare(); |
| 92 | } |
| 93 | Assert.assertNotNull(Looper.myLooper()); |
| 94 | |
| 95 | MockitoAnnotations.initMocks(this); |
| 96 | |
| 97 | mProfiles = Config.getSupportedProfiles(); |
| 98 | |
weichinweng | ebc4b1e | 2020-04-24 15:22:17 +0800 | [diff] [blame] | 99 | mMockAdapterService.initNative(false /* is_restricted */, false /* is_niap_mode */, |
Rahul Sabnis | 81c1c7d | 2020-07-31 19:47:06 -0700 | [diff] [blame^] | 100 | 0 /* config_compare_result */, false); |
Myles Watson | 0a84fe8 | 2018-02-15 12:40:29 -0800 | [diff] [blame] | 101 | |
Jack He | bd90909 | 2018-02-02 17:24:00 -0800 | [diff] [blame] | 102 | TestUtils.setAdapterService(mMockAdapterService); |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 103 | |
| 104 | Assert.assertNotNull(AdapterService.getAdapterService()); |
| 105 | } |
| 106 | |
| 107 | @After |
Jack He | bd90909 | 2018-02-02 17:24:00 -0800 | [diff] [blame] | 108 | public void tearDown() |
| 109 | throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
Myles Watson | 0a84fe8 | 2018-02-15 12:40:29 -0800 | [diff] [blame] | 110 | mMockAdapterService.cleanupNative(); |
Jack He | bd90909 | 2018-02-02 17:24:00 -0800 | [diff] [blame] | 111 | TestUtils.clearAdapterService(mMockAdapterService); |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 112 | mMockAdapterService = null; |
| 113 | mProfiles = null; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Test: Start the Bluetooth services that are configured. |
| 118 | * Verify that the same services start. |
| 119 | */ |
| 120 | @Test |
| 121 | public void testEnableDisable() throws TimeoutException { |
| 122 | setAllProfilesState(BluetoothAdapter.STATE_ON, 1); |
| 123 | setAllProfilesState(BluetoothAdapter.STATE_OFF, 1); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Test: Start the Bluetooth services that are configured twice. |
| 128 | * Verify that the services start. |
| 129 | */ |
| 130 | @Test |
| 131 | public void testEnableDisableTwice() throws TimeoutException { |
| 132 | setAllProfilesState(BluetoothAdapter.STATE_ON, 1); |
| 133 | setAllProfilesState(BluetoothAdapter.STATE_OFF, 1); |
| 134 | setAllProfilesState(BluetoothAdapter.STATE_ON, 2); |
| 135 | setAllProfilesState(BluetoothAdapter.STATE_OFF, 2); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Test: Start the Bluetooth services that are configured. |
| 140 | * Verify that each profile starts and stops. |
| 141 | */ |
| 142 | @Test |
| 143 | public void testEnableDisableInterleaved() throws TimeoutException { |
| 144 | for (Class profile : mProfiles) { |
| 145 | setProfileState(profile, BluetoothAdapter.STATE_ON); |
| 146 | setProfileState(profile, BluetoothAdapter.STATE_OFF); |
| 147 | } |
Myles Watson | 35bbcab | 2018-01-29 13:30:20 -0800 | [diff] [blame] | 148 | ArgumentCaptor<ProfileService> starts = ArgumentCaptor.forClass(ProfileService.class); |
| 149 | ArgumentCaptor<ProfileService> stops = ArgumentCaptor.forClass(ProfileService.class); |
| 150 | int invocationNumber = mProfiles.length; |
| 151 | verify(mMockAdapterService, |
| 152 | timeout(PROFILE_START_MILLIS).times(invocationNumber)).onProfileServiceStateChanged( |
| 153 | starts.capture(), eq(BluetoothAdapter.STATE_ON)); |
| 154 | verify(mMockAdapterService, |
| 155 | timeout(PROFILE_START_MILLIS).times(invocationNumber)).onProfileServiceStateChanged( |
| 156 | stops.capture(), eq(BluetoothAdapter.STATE_OFF)); |
| 157 | |
| 158 | List<ProfileService> startedArguments = starts.getAllValues(); |
| 159 | List<ProfileService> stoppedArguments = stops.getAllValues(); |
| 160 | Assert.assertEquals(startedArguments.size(), stoppedArguments.size()); |
| 161 | for (ProfileService service : startedArguments) { |
| 162 | Assert.assertTrue(stoppedArguments.contains(service)); |
| 163 | stoppedArguments.remove(service); |
| 164 | Assert.assertFalse(stoppedArguments.contains(service)); |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Test: Start and stop a single profile repeatedly. |
| 170 | * Verify that the profiles start and stop. |
| 171 | */ |
| 172 | @Test |
| 173 | public void testRepeatedEnableDisableSingly() throws TimeoutException { |
Myles Watson | 35bbcab | 2018-01-29 13:30:20 -0800 | [diff] [blame] | 174 | int profileNumber = 0; |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 175 | for (Class profile : mProfiles) { |
Myles Watson | 35bbcab | 2018-01-29 13:30:20 -0800 | [diff] [blame] | 176 | for (int i = 0; i < NUM_REPEATS; i++) { |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 177 | setProfileState(profile, BluetoothAdapter.STATE_ON); |
Myles Watson | 35bbcab | 2018-01-29 13:30:20 -0800 | [diff] [blame] | 178 | ArgumentCaptor<ProfileService> start = |
| 179 | ArgumentCaptor.forClass(ProfileService.class); |
| 180 | verify(mMockAdapterService, timeout(PROFILE_START_MILLIS).times( |
| 181 | NUM_REPEATS * profileNumber + i + 1)).onProfileServiceStateChanged( |
| 182 | start.capture(), eq(BluetoothAdapter.STATE_ON)); |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 183 | setProfileState(profile, BluetoothAdapter.STATE_OFF); |
Myles Watson | 35bbcab | 2018-01-29 13:30:20 -0800 | [diff] [blame] | 184 | ArgumentCaptor<ProfileService> stop = ArgumentCaptor.forClass(ProfileService.class); |
| 185 | verify(mMockAdapterService, timeout(PROFILE_START_MILLIS).times( |
| 186 | NUM_REPEATS * profileNumber + i + 1)).onProfileServiceStateChanged( |
| 187 | stop.capture(), eq(BluetoothAdapter.STATE_OFF)); |
| 188 | Assert.assertEquals(start.getValue(), stop.getValue()); |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 189 | } |
Myles Watson | 35bbcab | 2018-01-29 13:30:20 -0800 | [diff] [blame] | 190 | profileNumber += 1; |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
Hansong Zhang | 2f37ccb | 2018-03-12 14:25:36 -0700 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * Test: Start and stop a single profile repeatedly and verify that the profile services are |
| 196 | * registered and unregistered accordingly. |
| 197 | */ |
| 198 | @Test |
| 199 | public void testProfileServiceRegisterUnregister() throws TimeoutException { |
| 200 | int profileNumber = 0; |
| 201 | for (Class profile : mProfiles) { |
| 202 | for (int i = 0; i < NUM_REPEATS; i++) { |
| 203 | setProfileState(profile, BluetoothAdapter.STATE_ON); |
| 204 | ArgumentCaptor<ProfileService> start = |
| 205 | ArgumentCaptor.forClass(ProfileService.class); |
| 206 | verify(mMockAdapterService, timeout(PROFILE_START_MILLIS).times( |
| 207 | NUM_REPEATS * profileNumber + i + 1)).addProfile( |
| 208 | start.capture()); |
| 209 | setProfileState(profile, BluetoothAdapter.STATE_OFF); |
| 210 | ArgumentCaptor<ProfileService> stop = ArgumentCaptor.forClass(ProfileService.class); |
| 211 | verify(mMockAdapterService, timeout(PROFILE_START_MILLIS).times( |
| 212 | NUM_REPEATS * profileNumber + i + 1)).removeProfile( |
| 213 | stop.capture()); |
| 214 | Assert.assertEquals(start.getValue(), stop.getValue()); |
| 215 | } |
| 216 | profileNumber += 1; |
| 217 | } |
| 218 | } |
Myles Watson | c7a95e1 | 2017-12-08 09:31:47 -0800 | [diff] [blame] | 219 | } |