[go: nahoru, domu]

blob: 4648e1f9dfd342f5870aa00a381c821c3a9d9887 [file] [log] [blame]
Myles Watsonc7a95e12017-12-08 09:31:47 -08001/*
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
17package com.android.bluetooth.btservice;
18
19import static org.mockito.ArgumentMatchers.eq;
20import static org.mockito.Mockito.*;
21
22import android.bluetooth.BluetoothAdapter;
23import android.content.Intent;
24import android.os.Looper;
KOUSHIK PANUGANTI8befdc72019-03-29 10:47:47 -070025
26import androidx.test.InstrumentationRegistry;
27import androidx.test.filters.MediumTest;
28import androidx.test.rule.ServiceTestRule;
29import androidx.test.runner.AndroidJUnit4;
Myles Watsonc7a95e12017-12-08 09:31:47 -080030
Jack Hebd909092018-02-02 17:24:00 -080031import com.android.bluetooth.TestUtils;
32
Myles Watsonc7a95e12017-12-08 09:31:47 -080033import org.junit.After;
34import org.junit.Assert;
35import org.junit.Before;
36import org.junit.Rule;
37import org.junit.Test;
38import org.junit.runner.RunWith;
Myles Watson35bbcab2018-01-29 13:30:20 -080039import org.mockito.ArgumentCaptor;
Myles Watsonc7a95e12017-12-08 09:31:47 -080040import org.mockito.Mock;
41import org.mockito.MockitoAnnotations;
42
43import java.lang.reflect.InvocationTargetException;
Myles Watson35bbcab2018-01-29 13:30:20 -080044import java.util.List;
Myles Watsonc7a95e12017-12-08 09:31:47 -080045import java.util.concurrent.TimeoutException;
46
47@MediumTest
48@RunWith(AndroidJUnit4.class)
49public class ProfileServiceTest {
Myles Watsonc7a95e12017-12-08 09:31:47 -080050 private static final int PROFILE_START_MILLIS = 1250;
Myles Watson35bbcab2018-01-29 13:30:20 -080051 private static final int NUM_REPEATS = 5;
Myles Watsonc7a95e12017-12-08 09:31:47 -080052
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 Watson35bbcab2018-01-29 13:30:20 -080067 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 Watsonc7a95e12017-12-08 09:31:47 -080072 for (Class profile : mProfiles) {
Myles Watson35bbcab2018-01-29 13:30:20 -080073 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 Watsonc7a95e12017-12-08 09:31:47 -080080 }
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
weichinwengebc4b1e2020-04-24 15:22:17 +080099 mMockAdapterService.initNative(false /* is_restricted */, false /* is_niap_mode */,
Rahul Sabnis81c1c7d2020-07-31 19:47:06 -0700100 0 /* config_compare_result */, false);
Myles Watson0a84fe82018-02-15 12:40:29 -0800101
Jack Hebd909092018-02-02 17:24:00 -0800102 TestUtils.setAdapterService(mMockAdapterService);
Myles Watsonc7a95e12017-12-08 09:31:47 -0800103
104 Assert.assertNotNull(AdapterService.getAdapterService());
105 }
106
107 @After
Jack Hebd909092018-02-02 17:24:00 -0800108 public void tearDown()
109 throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Myles Watson0a84fe82018-02-15 12:40:29 -0800110 mMockAdapterService.cleanupNative();
Jack Hebd909092018-02-02 17:24:00 -0800111 TestUtils.clearAdapterService(mMockAdapterService);
Myles Watsonc7a95e12017-12-08 09:31:47 -0800112 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 Watson35bbcab2018-01-29 13:30:20 -0800148 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 Watsonc7a95e12017-12-08 09:31:47 -0800165 }
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 Watson35bbcab2018-01-29 13:30:20 -0800174 int profileNumber = 0;
Myles Watsonc7a95e12017-12-08 09:31:47 -0800175 for (Class profile : mProfiles) {
Myles Watson35bbcab2018-01-29 13:30:20 -0800176 for (int i = 0; i < NUM_REPEATS; i++) {
Myles Watsonc7a95e12017-12-08 09:31:47 -0800177 setProfileState(profile, BluetoothAdapter.STATE_ON);
Myles Watson35bbcab2018-01-29 13:30:20 -0800178 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 Watsonc7a95e12017-12-08 09:31:47 -0800183 setProfileState(profile, BluetoothAdapter.STATE_OFF);
Myles Watson35bbcab2018-01-29 13:30:20 -0800184 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 Watsonc7a95e12017-12-08 09:31:47 -0800189 }
Myles Watson35bbcab2018-01-29 13:30:20 -0800190 profileNumber += 1;
Myles Watsonc7a95e12017-12-08 09:31:47 -0800191 }
192 }
Hansong Zhang2f37ccb2018-03-12 14:25:36 -0700193
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 Watsonc7a95e12017-12-08 09:31:47 -0800219}