• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.car;
18 
19 import static org.mockito.Mockito.any;
20 import static org.mockito.Mockito.never;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 
24 import android.bluetooth.BluetoothAdapter;
25 import android.bluetooth.BluetoothManager;
26 import android.bluetooth.le.AdvertiseData;
27 import android.bluetooth.le.BluetoothLeAdvertiser;
28 import android.content.Context;
29 import android.content.res.Resources;
30 import android.os.ParcelUuid;
31 import android.support.test.filters.SmallTest;
32 import android.support.test.runner.AndroidJUnit4;
33 
34 import org.junit.Assert;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 
42 import java.util.Arrays;
43 
44 @SmallTest
45 @RunWith(AndroidJUnit4.class)
46 public class FastPairProviderTest {
47     // Service ID assigned for FastPair.
48     private static final ParcelUuid FastPairServiceUuid = ParcelUuid
49             .fromString("0000FE2C-0000-1000-8000-00805f9b34fb");
50 
51     @Mock
52     private BluetoothLeAdvertiser mBluetoothLeAdvertiser;
53     @Mock
54     private BluetoothManager mMockBluetoothManager;
55     @Mock
56     private BluetoothAdapter mMockBluetoothAdapter;
57     @Mock
58     private Context mMockContext;
59     @Mock
60     Resources mMockResources;
61 
62     @Before
setUp()63     public void setUp() {
64         MockitoAnnotations.initMocks(this);
65         when(mMockContext.getSystemService(Context.BLUETOOTH_SERVICE)).thenReturn(
66                 mMockBluetoothManager);
67         when(mMockBluetoothManager.getAdapter()).thenReturn(mMockBluetoothAdapter);
68         when(mMockContext.getResources()).thenReturn(mMockResources);
69         when(mMockResources.getInteger(R.integer.fastPairModelId)).thenReturn(0);
70         when(mMockBluetoothAdapter.getBluetoothLeAdvertiser()).thenReturn(mBluetoothLeAdvertiser);
71     }
72 
73     /**
74      * Verify that when a model id is set it gets serialized correctly.
75      */
76     @Test
enabledViaModelIdTest()77     public void enabledViaModelIdTest() {
78         int modelId = 0xABCDEF;
79         byte[] modelIdBytes = new byte[]{(byte) 0xEF, (byte) 0xCD, (byte) 0xAB};
80         when(mMockResources.getInteger(R.integer.fastPairModelId)).thenReturn(modelId);
81         ArgumentCaptor<AdvertiseData> advertiseDataCaptor = ArgumentCaptor.forClass(
82                 AdvertiseData.class);
83 
84         FastPairProvider fastPairProvider = new FastPairProvider(mMockContext);
85         fastPairProvider.startAdvertising();
86 
87         verify(mBluetoothLeAdvertiser).startAdvertising(any(), advertiseDataCaptor.capture(),
88                 any());
89         Assert.assertTrue(Arrays.equals(modelIdBytes,
90                 advertiseDataCaptor.getValue().getServiceData().get(FastPairServiceUuid)));
91     }
92 
93     /**
94      * Verify that when the model id is 0 Fast Pair is disabled.
95      */
96     @Test
disabledViaModelIdTest()97     public void disabledViaModelIdTest() {
98         FastPairProvider fastPairProvider = new FastPairProvider(mMockContext);
99         fastPairProvider.startAdvertising();
100         verify(mBluetoothLeAdvertiser, never()).startAdvertising(any(), any(), any());
101     }
102 }
103