1/*
2 * Copyright (C) 2014 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 android.bluetooth.le;
18
19import android.bluetooth.le.ScanRecord;
20import android.os.ParcelUuid;
21import android.test.suitebuilder.annotation.SmallTest;
22
23import junit.framework.TestCase;
24
25import java.util.Arrays;
26
27/**
28 * Unit test cases for {@link ScanRecord}.
29 * <p>
30 * To run this test, use adb shell am instrument -e class 'android.bluetooth.ScanRecordTest' -w
31 * 'com.android.bluetooth.tests/android.bluetooth.BluetoothTestRunner'
32 */
33public class ScanRecordTest extends TestCase {
34
35    @SmallTest
36    public void testParser() {
37        byte[] scanRecord = new byte[] {
38                0x02, 0x01, 0x1a, // advertising flags
39                0x05, 0x02, 0x0b, 0x11, 0x0a, 0x11, // 16 bit service uuids
40                0x04, 0x09, 0x50, 0x65, 0x64, // name
41                0x02, 0x0A, (byte) 0xec, // tx power level
42                0x05, 0x16, 0x0b, 0x11, 0x50, 0x64, // service data
43                0x05, (byte) 0xff, (byte) 0xe0, 0x00, 0x02, 0x15, // manufacturer specific data
44                0x03, 0x50, 0x01, 0x02, // an unknown data type won't cause trouble
45        };
46        ScanRecord data = ScanRecord.parseFromBytes(scanRecord);
47        assertEquals(0x1a, data.getAdvertiseFlags());
48        ParcelUuid uuid1 = ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
49        ParcelUuid uuid2 = ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB");
50        assertTrue(data.getServiceUuids().contains(uuid1));
51        assertTrue(data.getServiceUuids().contains(uuid2));
52
53        assertEquals("Ped", data.getDeviceName());
54        assertEquals(-20, data.getTxPowerLevel());
55
56        assertTrue(data.getManufacturerSpecificData().get(0x00E0) != null);
57        assertArrayEquals(new byte[] {
58                0x02, 0x15 }, data.getManufacturerSpecificData().get(0x00E0));
59
60        assertTrue(data.getServiceData().containsKey(uuid2));
61        assertArrayEquals(new byte[] {
62                0x50, 0x64 }, data.getServiceData().get(uuid2));
63    }
64
65    // Assert two byte arrays are equal.
66    private static void assertArrayEquals(byte[] expected, byte[] actual) {
67        if (!Arrays.equals(expected, actual)) {
68            fail("expected:<" + Arrays.toString(expected) +
69                    "> but was:<" + Arrays.toString(actual) + ">");
70        }
71
72    }
73}
74