blob: 976445eb8c04bda2fe8c488f75bb01c952c16f08 [file] [log] [blame]
Chienyuan447fbde2018-09-05 21:28:59 +08001/*
2 * Copyright (C) 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.settingslib.bluetooth;
18
19import static com.google.common.truth.Truth.assertThat;
20
Chienyuan447fbde2018-09-05 21:28:59 +080021import static org.mockito.Mockito.verify;
James Lemieuxec3ad9e2018-11-28 17:49:14 -080022import static org.mockito.Mockito.when;
Chienyuan447fbde2018-09-05 21:28:59 +080023
James Lemieuxec3ad9e2018-11-28 17:49:14 -080024import android.bluetooth.BluetoothA2dpSink;
Chienyuan447fbde2018-09-05 21:28:59 +080025import android.bluetooth.BluetoothAdapter;
26import android.bluetooth.BluetoothDevice;
Chienyuan447fbde2018-09-05 21:28:59 +080027import android.bluetooth.BluetoothProfile;
28
Chienyuan447fbde2018-09-05 21:28:59 +080029import com.android.settingslib.testutils.shadow.ShadowBluetoothAdapter;
30
31import org.junit.Before;
32import org.junit.Test;
33import org.junit.runner.RunWith;
34import org.mockito.Mock;
35import org.mockito.MockitoAnnotations;
James Lemieuxec3ad9e2018-11-28 17:49:14 -080036import org.robolectric.RobolectricTestRunner;
Chienyuan447fbde2018-09-05 21:28:59 +080037import org.robolectric.RuntimeEnvironment;
James Lemieuxec3ad9e2018-11-28 17:49:14 -080038import org.robolectric.annotation.Config;
Chienyuan447fbde2018-09-05 21:28:59 +080039import org.robolectric.shadow.api.Shadow;
40
James Lemieuxec3ad9e2018-11-28 17:49:14 -080041@RunWith(RobolectricTestRunner.class)
Chienyuan447fbde2018-09-05 21:28:59 +080042@Config(shadows = {ShadowBluetoothAdapter.class})
43public class A2dpSinkProfileTest {
44
45 @Mock
46 private CachedBluetoothDeviceManager mDeviceManager;
47 @Mock
48 private LocalBluetoothProfileManager mProfileManager;
49 @Mock
50 private BluetoothA2dpSink mService;
51 @Mock
Chienyuan447fbde2018-09-05 21:28:59 +080052 private BluetoothDevice mBluetoothDevice;
53 private BluetoothProfile.ServiceListener mServiceListener;
54 private A2dpSinkProfile mProfile;
55 private ShadowBluetoothAdapter mShadowBluetoothAdapter;
56
57 @Before
58 public void setUp() {
59 MockitoAnnotations.initMocks(this);
60 mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
61 mProfile = new A2dpSinkProfile(RuntimeEnvironment.application,
62 mDeviceManager, mProfileManager);
63 mServiceListener = mShadowBluetoothAdapter.getServiceListener();
64 mServiceListener.onServiceConnected(BluetoothProfile.A2DP_SINK, mService);
65 }
66
67 @Test
68 public void connect_shouldConnectBluetoothA2dpSink() {
69 mProfile.connect(mBluetoothDevice);
70 verify(mService).connect(mBluetoothDevice);
71 }
72
73 @Test
74 public void disconnect_shouldDisconnectBluetoothA2dpSink() {
75 mProfile.disconnect(mBluetoothDevice);
76 verify(mService).disconnect(mBluetoothDevice);
77 }
78
79 @Test
80 public void getConnectionStatus_shouldReturnConnectionState() {
81 when(mService.getConnectionState(mBluetoothDevice)).
82 thenReturn(BluetoothProfile.STATE_CONNECTED);
83 assertThat(mProfile.getConnectionStatus(mBluetoothDevice)).
84 isEqualTo(BluetoothProfile.STATE_CONNECTED);
85 }
86}