Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settingslib.bluetooth; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 21 | import static org.mockito.Mockito.verify; |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 22 | import static org.mockito.Mockito.when; |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 23 | |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 24 | import android.bluetooth.BluetoothA2dpSink; |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 25 | import android.bluetooth.BluetoothAdapter; |
| 26 | import android.bluetooth.BluetoothDevice; |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 27 | import android.bluetooth.BluetoothProfile; |
| 28 | |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 29 | import com.android.settingslib.testutils.shadow.ShadowBluetoothAdapter; |
| 30 | |
| 31 | import org.junit.Before; |
| 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
| 34 | import org.mockito.Mock; |
| 35 | import org.mockito.MockitoAnnotations; |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 36 | import org.robolectric.RobolectricTestRunner; |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 37 | import org.robolectric.RuntimeEnvironment; |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 38 | import org.robolectric.annotation.Config; |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 39 | import org.robolectric.shadow.api.Shadow; |
| 40 | |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 41 | @RunWith(RobolectricTestRunner.class) |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 42 | @Config(shadows = {ShadowBluetoothAdapter.class}) |
| 43 | public class A2dpSinkProfileTest { |
| 44 | |
| 45 | @Mock |
| 46 | private CachedBluetoothDeviceManager mDeviceManager; |
| 47 | @Mock |
| 48 | private LocalBluetoothProfileManager mProfileManager; |
| 49 | @Mock |
| 50 | private BluetoothA2dpSink mService; |
| 51 | @Mock |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 52 | 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 | } |