ScanRecordTest.java revision 6d81118032b92caa0f5cfebe11af02a98f819d5e
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
31 * 'android.bluetooth.ScanRecordTest' -w
32 * 'com.android.bluetooth.tests/android.bluetooth.BluetoothTestRunner'
33 */
34public class ScanRecordTest extends TestCase {
35
36    @SmallTest
37    public void testParser() {
38        byte[] scanRecord = new byte[] {
39                0x02, 0x01, 0x1a, // advertising flags
40                0x05, 0x02, 0x0b, 0x11, 0x0a, 0x11, // 16 bit service uuids
41                0x04, 0x09, 0x50, 0x65, 0x64, // name
42                0x02, 0x0A, (byte) 0xec, // tx power level
43                0x05, 0x16, 0x0b, 0x11, 0x50, 0x64, // service data
44                0x05, (byte) 0xff, (byte) 0xe0, 0x00, 0x02, 0x15, // manufacturer specific data
45                0x03, 0x50, 0x01, 0x02, // an unknown data type won't cause trouble
46        };
47        ScanRecord data = ScanRecord.parseFromBytes(scanRecord);
48        assertEquals(0x1a, data.getAdvertiseFlags());
49        ParcelUuid uuid1 = ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
50        ParcelUuid uuid2 = ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB");
51        assertTrue(data.getServiceUuids().contains(uuid1));
52        assertTrue(data.getServiceUuids().contains(uuid2));
53
54        assertEquals("Ped", data.getLocalName());
55        assertEquals(-20, data.getTxPowerLevel());
56
57        assertEquals(224, data.getManufacturerId());
58        assertArrayEquals(new byte[] {
59                (byte) 0xe0, 0x00, 0x02, 0x15 }, data.getManufacturerSpecificData());
60
61        assertEquals(uuid2, data.getServiceDataUuid());
62        assertArrayEquals(new byte[] {
63                0x0b, 0x11, 0x50, 0x64 }, data.getServiceData());
64    }
65
66    // Assert two byte arrays are equal.
67    private static void assertArrayEquals(byte[] expected, byte[] actual) {
68        if (!Arrays.equals(expected, actual)) {
69            fail("expected:<" + Arrays.toString(expected) +
70                    "> but was:<" + Arrays.toString(actual) + ">");
71        }
72
73    }
74}
75