1 /*
2  * Copyright (C) 2015 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 android.bluetooth.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 
23 import android.bluetooth.le.AdvertiseData;
24 import android.bluetooth.le.TransportBlock;
25 import android.bluetooth.le.TransportDiscoveryData;
26 import android.os.Parcel;
27 import android.os.ParcelUuid;
28 
29 import androidx.test.ext.junit.runners.AndroidJUnit4;
30 import androidx.test.filters.SmallTest;
31 import androidx.test.platform.app.InstrumentationRegistry;
32 
33 import com.android.compatibility.common.util.CddTest;
34 
35 import org.junit.Assume;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 
40 import java.util.ArrayList;
41 import java.util.List;
42 
43 /**
44  * Unit test cases for {@link AdvertiseData}.
45  * <p>
46  * To run the test, use adb shell am instrument -e class 'android.bluetooth.le.AdvertiseDataTest' -w
47  * 'com.android.bluetooth.tests/android.bluetooth.BluetoothTestRunner'
48  */
49 @RunWith(AndroidJUnit4.class)
50 public class AdvertiseDataTest {
51 
52     private AdvertiseData.Builder mAdvertiseDataBuilder;
53 
54     @Before
setUp()55     public void setUp() {
56         Assume.assumeTrue(TestUtils.isBleSupported(
57                 InstrumentationRegistry.getInstrumentation().getTargetContext()));
58         mAdvertiseDataBuilder = new AdvertiseData.Builder();
59     }
60 
61     @CddTest(requirements = {"7.4.3/C-2-1"})
62     @SmallTest
63     @Test
emptyData()64     public void emptyData() {
65         Parcel parcel = Parcel.obtain();
66         AdvertiseData data = mAdvertiseDataBuilder.build();
67         data.writeToParcel(parcel, 0);
68         parcel.setDataPosition(0);
69         AdvertiseData dataFromParcel =
70                 AdvertiseData.CREATOR.createFromParcel(parcel);
71         assertEquals(data, dataFromParcel);
72         assertFalse(dataFromParcel.getIncludeDeviceName());
73         assertFalse(dataFromParcel.getIncludeTxPowerLevel());
74         assertEquals(0, dataFromParcel.getManufacturerSpecificData().size());
75         assertTrue(dataFromParcel.getServiceData().isEmpty());
76         assertTrue(dataFromParcel.getServiceUuids().isEmpty());
77     }
78 
79     @CddTest(requirements = {"7.4.3/C-2-1"})
80     @SmallTest
81     @Test
emptyServiceUuid()82     public void emptyServiceUuid() {
83         Parcel parcel = Parcel.obtain();
84         AdvertiseData data = mAdvertiseDataBuilder.setIncludeDeviceName(true).build();
85         data.writeToParcel(parcel, 0);
86         parcel.setDataPosition(0);
87         AdvertiseData dataFromParcel =
88                 AdvertiseData.CREATOR.createFromParcel(parcel);
89         assertEquals(data, dataFromParcel);
90         assertTrue(dataFromParcel.getIncludeDeviceName());
91         assertTrue(dataFromParcel.getServiceUuids().isEmpty());
92     }
93 
94     @CddTest(requirements = {"7.4.3/C-2-1"})
95     @SmallTest
96     @Test
emptyManufacturerData()97     public void emptyManufacturerData() {
98         Parcel parcel = Parcel.obtain();
99         int manufacturerId = 50;
100         byte[] manufacturerData = new byte[0];
101         AdvertiseData data =
102                 mAdvertiseDataBuilder.setIncludeDeviceName(true)
103                         .addManufacturerData(manufacturerId, manufacturerData).build();
104         data.writeToParcel(parcel, 0);
105         parcel.setDataPosition(0);
106         AdvertiseData dataFromParcel =
107                 AdvertiseData.CREATOR.createFromParcel(parcel);
108         assertEquals(data, dataFromParcel);
109         TestUtils.assertArrayEquals(new byte[0], dataFromParcel.getManufacturerSpecificData().get(manufacturerId));
110     }
111 
112     @CddTest(requirements = {"7.4.3/C-2-1"})
113     @SmallTest
114     @Test
emptyServiceData()115     public void emptyServiceData() {
116         Parcel parcel = Parcel.obtain();
117         ParcelUuid uuid = ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
118         byte[] serviceData = new byte[0];
119         AdvertiseData data =
120                 mAdvertiseDataBuilder.setIncludeDeviceName(true)
121                         .addServiceData(uuid, serviceData).build();
122         data.writeToParcel(parcel, 0);
123         parcel.setDataPosition(0);
124         AdvertiseData dataFromParcel =
125                 AdvertiseData.CREATOR.createFromParcel(parcel);
126         assertEquals(data, dataFromParcel);
127         TestUtils.assertArrayEquals(new byte[0], dataFromParcel.getServiceData().get(uuid));
128     }
129 
130     @CddTest(requirements = {"7.4.3/C-2-1"})
131     @SmallTest
132     @Test
serviceUuid()133     public void serviceUuid() {
134         Parcel parcel = Parcel.obtain();
135         ParcelUuid uuid = ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
136         ParcelUuid uuid2 = ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB");
137 
138         AdvertiseData data =
139                 mAdvertiseDataBuilder.setIncludeDeviceName(true)
140                         .addServiceUuid(uuid).addServiceUuid(uuid2).build();
141         data.writeToParcel(parcel, 0);
142         parcel.setDataPosition(0);
143         AdvertiseData dataFromParcel =
144                 AdvertiseData.CREATOR.createFromParcel(parcel);
145         assertEquals(data, dataFromParcel);
146         assertTrue(dataFromParcel.getServiceUuids().contains(uuid));
147         assertTrue(dataFromParcel.getServiceUuids().contains(uuid2));
148     }
149 
150     @CddTest(requirements = {"7.4.3/C-2-1"})
151     @SmallTest
152     @Test
serviceSolicitationUuids()153     public void serviceSolicitationUuids() {
154         AdvertiseData emptyData = mAdvertiseDataBuilder.build();
155         assertEquals(0, emptyData.getServiceSolicitationUuids().size());
156 
157         Parcel parcel = Parcel.obtain();
158         ParcelUuid uuid = ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
159         ParcelUuid uuid2 = ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB");
160 
161         AdvertiseData data =
162                 mAdvertiseDataBuilder.setIncludeDeviceName(true)
163                         .addServiceSolicitationUuid(uuid).addServiceSolicitationUuid(uuid2).build();
164         data.writeToParcel(parcel, 0);
165         parcel.setDataPosition(0);
166         AdvertiseData dataFromParcel =
167                 AdvertiseData.CREATOR.createFromParcel(parcel);
168         assertEquals(data, dataFromParcel);
169         assertTrue(dataFromParcel.getServiceSolicitationUuids().contains(uuid));
170         assertTrue(dataFromParcel.getServiceSolicitationUuids().contains(uuid2));
171     }
172 
173     @CddTest(requirements = {"7.4.3/C-2-1"})
174     @SmallTest
175     @Test
manufacturerData()176     public void manufacturerData() {
177         Parcel parcel = Parcel.obtain();
178         ParcelUuid uuid = ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
179         ParcelUuid uuid2 = ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB");
180 
181         int manufacturerId = 50;
182         byte[] manufacturerData = new byte[] {
183                 (byte) 0xF0, 0x00, 0x02, 0x15 };
184         AdvertiseData data =
185                 mAdvertiseDataBuilder.setIncludeDeviceName(true)
186                         .addServiceUuid(uuid).addServiceUuid(uuid2)
187                         .addManufacturerData(manufacturerId, manufacturerData).build();
188 
189         data.writeToParcel(parcel, 0);
190         parcel.setDataPosition(0);
191         AdvertiseData dataFromParcel =
192                 AdvertiseData.CREATOR.createFromParcel(parcel);
193         assertEquals(data, dataFromParcel);
194         TestUtils.assertArrayEquals(manufacturerData,
195                 dataFromParcel.getManufacturerSpecificData().get(manufacturerId));
196     }
197 
198     @CddTest(requirements = {"7.4.3/C-2-1"})
199     @SmallTest
200     @Test
serviceData()201     public void serviceData() {
202         Parcel parcel = Parcel.obtain();
203         ParcelUuid uuid = ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
204         byte[] serviceData = new byte[] {
205                 (byte) 0xF0, 0x00, 0x02, 0x15 };
206         AdvertiseData data =
207                 mAdvertiseDataBuilder.setIncludeDeviceName(true)
208                         .addServiceData(uuid, serviceData).build();
209         data.writeToParcel(parcel, 0);
210         parcel.setDataPosition(0);
211         AdvertiseData dataFromParcel =
212                 AdvertiseData.CREATOR.createFromParcel(parcel);
213         assertEquals(data, dataFromParcel);
214         TestUtils.assertArrayEquals(serviceData, dataFromParcel.getServiceData().get(uuid));
215     }
216 
217     @CddTest(requirements = {"7.4.3/C-2-1"})
218     @SmallTest
219     @Test
transportDiscoveryData()220     public void transportDiscoveryData() {
221         Parcel parcel = Parcel.obtain();
222         ParcelUuid uuid = ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
223         List<TransportBlock> transportBlocks = new ArrayList();
224         transportBlocks.add(new TransportBlock(1, 0, 4, new byte[] {
225                 (byte) 0xF0, 0x00, 0x02, 0x15 }));
226         TransportDiscoveryData discoveryData = new TransportDiscoveryData(0, transportBlocks);
227         AdvertiseData data =
228                 mAdvertiseDataBuilder.setIncludeDeviceName(true)
229                         .addTransportDiscoveryData(discoveryData).build();
230         data.writeToParcel(parcel, 0);
231         parcel.setDataPosition(0);
232         AdvertiseData dataFromParcel =
233                 AdvertiseData.CREATOR.createFromParcel(parcel);
234 
235         assertEquals(discoveryData.getTransportDataType(),
236                 dataFromParcel.getTransportDiscoveryData().get(0).getTransportDataType());
237 
238         assertEquals(discoveryData.getTransportBlocks().get(0).getOrgId(),
239                 dataFromParcel.getTransportDiscoveryData().get(0).getTransportBlocks().get(0).getOrgId());
240 
241         assertEquals(discoveryData.getTransportBlocks().get(0).getTdsFlags(),
242                 dataFromParcel.getTransportDiscoveryData().get(0).getTransportBlocks().get(0).getTdsFlags());
243 
244         assertEquals(discoveryData.getTransportBlocks().get(0).totalBytes(),
245                 dataFromParcel.getTransportDiscoveryData().get(0).getTransportBlocks().get(0).totalBytes());
246 
247         TestUtils.assertArrayEquals(discoveryData.toByteArray(),
248                 dataFromParcel.getTransportDiscoveryData().get(0).toByteArray());
249 
250         assertEquals(data, dataFromParcel);
251     }
252 
253     @CddTest(requirements = {"7.4.3/C-2-1"})
254     @SmallTest
255     @Test
includeTxPower()256     public void includeTxPower() {
257         Parcel parcel = Parcel.obtain();
258         AdvertiseData data = mAdvertiseDataBuilder.setIncludeTxPowerLevel(true).build();
259         data.writeToParcel(parcel, 0);
260         parcel.setDataPosition(0);
261         AdvertiseData dataFromParcel =
262                 AdvertiseData.CREATOR.createFromParcel(parcel);
263         assertEquals(dataFromParcel.getIncludeTxPowerLevel(), true);
264     }
265 
266     @CddTest(requirements = {"7.4.3/C-2-1"})
267     @SmallTest
268     @Test
describeContents()269     public void describeContents() {
270         AdvertiseData data = new AdvertiseData.Builder().build();
271         assertEquals(0, data.describeContents());
272     }
273 }
274